Source file src/compress/gzip/issue14937_test.go

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

View as plain text