github.com/MangoDowner/go-gm@v0.0.0-20180818020936-8baa2bd4408c/src/compress/gzip/issue14937_test.go (about)

     1  package gzip
     2  
     3  import (
     4  	"internal/testenv"
     5  	"os"
     6  	"path/filepath"
     7  	"runtime"
     8  	"strings"
     9  	"testing"
    10  )
    11  
    12  // TestGZIPFilesHaveZeroMTimes checks that every .gz file in the tree
    13  // has a zero MTIME. This is a requirement for the Debian maintainers
    14  // to be able to have deterministic packages.
    15  //
    16  // See https://golang.org/issue/14937.
    17  func TestGZIPFilesHaveZeroMTimes(t *testing.T) {
    18  	// To avoid spurious false positives due to untracked GZIP files that
    19  	// may be in the user's GOROOT (Issue 18604), we only run this test on
    20  	// the builders, which should have a clean checkout of the tree.
    21  	if testenv.Builder() == "" {
    22  		t.Skip("skipping test on non-builder")
    23  	}
    24  	goroot, err := filepath.EvalSymlinks(runtime.GOROOT())
    25  	if err != nil {
    26  		t.Fatal("error evaluating GOROOT: ", err)
    27  	}
    28  	var files []string
    29  	err = filepath.Walk(goroot, func(path string, info os.FileInfo, err error) error {
    30  		if err != nil {
    31  			return err
    32  		}
    33  		if !info.IsDir() && strings.HasSuffix(path, ".gz") {
    34  			files = append(files, path)
    35  		}
    36  		return nil
    37  	})
    38  	if err != nil {
    39  		if os.IsNotExist(err) {
    40  			t.Skipf("skipping: GOROOT directory not found: %s", runtime.GOROOT())
    41  		}
    42  		t.Fatal("error collecting list of .gz files in GOROOT: ", err)
    43  	}
    44  	if len(files) == 0 {
    45  		t.Fatal("expected to find some .gz files under GOROOT")
    46  	}
    47  	for _, path := range files {
    48  		checkZeroMTime(t, path)
    49  	}
    50  }
    51  
    52  func checkZeroMTime(t *testing.T, path string) {
    53  	f, err := os.Open(path)
    54  	if err != nil {
    55  		t.Error(err)
    56  		return
    57  	}
    58  	defer f.Close()
    59  	gz, err := NewReader(f)
    60  	if err != nil {
    61  		t.Errorf("cannot read gzip file %s: %s", path, err)
    62  		return
    63  	}
    64  	defer gz.Close()
    65  	if !gz.ModTime.IsZero() {
    66  		t.Errorf("gzip file %s has non-zero mtime (%s)", path, gz.ModTime)
    67  	}
    68  }