Black Lives Matter. Support the Equal Justice Initiative.

Source file src/compress/gzip/issue14937_test.go

Documentation: compress/gzip

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

View as plain text