github.com/gagliardetto/golang-go@v0.0.0-20201020153340-53909ea70814/cmd/go/testdata/script/test_cache_inputs.txt (about)

     1  env GO111MODULE=off
     2  
     3  # Test that cached test results are invalidated in response to
     4  # changes to the external inputs to the test.
     5  
     6  [short] skip
     7  [GODEBUG:gocacheverify=1] skip
     8  
     9  # We're testing cache behavior, so start with a clean GOCACHE.
    10  env GOCACHE=$WORK/cache
    11  
    12  # Build a helper binary to invoke os.Chtimes.
    13  go build -o mkold$GOEXE mkold.go
    14  
    15  # Make test input files appear to be a minute old.
    16  exec ./mkold$GOEXE 1m testcache/file.txt
    17  exec ./mkold$GOEXE 1m testcache/script.sh
    18  
    19  # If the test reads an environment variable, changes to that variable
    20  # should invalidate cached test results.
    21  env TESTKEY=x
    22  go test testcache -run=TestLookupEnv
    23  go test testcache -run=TestLookupEnv
    24  stdout '\(cached\)'
    25  
    26  env TESTKEY=y
    27  go test testcache -run=TestLookupEnv
    28  ! stdout '\(cached\)'
    29  go test testcache -run=TestLookupEnv
    30  stdout '\(cached\)'
    31  
    32  # If the test stats a file, changes to the file should invalidate the cache.
    33  go test testcache -run=FileSize
    34  go test testcache -run=FileSize
    35  stdout '\(cached\)'
    36  
    37  cp 4x.txt testcache/file.txt
    38  go test testcache -run=FileSize
    39  ! stdout '\(cached\)'
    40  go test testcache -run=FileSize
    41  stdout '\(cached\)'
    42  
    43  # Files should be tracked even if the test changes its working directory.
    44  go test testcache -run=Chdir
    45  go test testcache -run=Chdir
    46  stdout '\(cached\)'
    47  cp 6x.txt testcache/file.txt
    48  go test testcache -run=Chdir
    49  ! stdout '\(cached\)'
    50  go test testcache -run=Chdir
    51  stdout '\(cached\)'
    52  
    53  # The content of files should affect caching, provided that the mtime also changes.
    54  exec ./mkold$GOEXE 1m testcache/file.txt
    55  go test testcache -run=FileContent
    56  go test testcache -run=FileContent
    57  stdout '\(cached\)'
    58  cp 2y.txt testcache/file.txt
    59  exec ./mkold$GOEXE 50s testcache/file.txt
    60  go test testcache -run=FileContent
    61  ! stdout '\(cached\)'
    62  go test testcache -run=FileContent
    63  stdout '\(cached\)'
    64  
    65  # Directory contents read via os.ReadDirNames should affect caching.
    66  go test testcache -run=DirList
    67  go test testcache -run=DirList
    68  stdout '\(cached\)'
    69  rm testcache/file.txt
    70  go test testcache -run=DirList
    71  ! stdout '\(cached\)'
    72  go test testcache -run=DirList
    73  stdout '\(cached\)'
    74  
    75  # Files outside GOROOT and GOPATH should not affect caching.
    76  env TEST_EXTERNAL_FILE=$WORK/external.txt
    77  go test testcache -run=ExternalFile
    78  go test testcache -run=ExternalFile
    79  stdout '\(cached\)'
    80  
    81  rm $WORK/external.txt
    82  go test testcache -run=ExternalFile
    83  stdout '\(cached\)'
    84  
    85  # Executables within GOROOT and GOPATH should affect caching,
    86  # even if the test does not stat them explicitly.
    87  
    88  [!exec:/bin/sh] skip
    89  chmod 0755 ./testcache/script.sh
    90  
    91  exec ./mkold$GOEXEC 1m testcache/script.sh
    92  go test testcache -run=Exec
    93  go test testcache -run=Exec
    94  stdout '\(cached\)'
    95  
    96  exec ./mkold$GOEXE 50s testcache/script.sh
    97  go test testcache -run=Exec
    98  ! stdout '\(cached\)'
    99  go test testcache -run=Exec
   100  stdout '\(cached\)'
   101  
   102  -- testcache/file.txt --
   103  xx
   104  -- 4x.txt --
   105  xxxx
   106  -- 6x.txt --
   107  xxxxxx
   108  -- 2y.txt --
   109  yy
   110  -- $WORK/external.txt --
   111  This file is outside of GOPATH.
   112  -- testcache/script.sh --
   113  #!/bin/sh
   114  exit 0
   115  -- testcache/testcache_test.go --
   116  // Copyright 2017 The Go Authors. All rights reserved.
   117  // Use of this source code is governed by a BSD-style
   118  // license that can be found in the LICENSE file.
   119  
   120  package testcache
   121  
   122  import (
   123  	"io/ioutil"
   124  	"os"
   125  	"testing"
   126  )
   127  
   128  func TestChdir(t *testing.T) {
   129  	os.Chdir("..")
   130  	defer os.Chdir("testcache")
   131  	info, err := os.Stat("testcache/file.txt")
   132  	if err != nil {
   133  		t.Fatal(err)
   134  	}
   135  	if info.Size()%2 != 1 {
   136  		t.Fatal("even file")
   137  	}
   138  }
   139  
   140  func TestOddFileContent(t *testing.T) {
   141  	f, err := os.Open("file.txt")
   142  	if err != nil {
   143  		t.Fatal(err)
   144  	}
   145  	data, err := ioutil.ReadAll(f)
   146  	f.Close()
   147  	if err != nil {
   148  		t.Fatal(err)
   149  	}
   150  	if len(data)%2 != 1 {
   151  		t.Fatal("even file")
   152  	}
   153  }
   154  
   155  func TestOddFileSize(t *testing.T) {
   156  	info, err := os.Stat("file.txt")
   157  	if err != nil {
   158  		t.Fatal(err)
   159  	}
   160  	if info.Size()%2 != 1 {
   161  		t.Fatal("even file")
   162  	}
   163  }
   164  
   165  func TestOddGetenv(t *testing.T) {
   166  	val := os.Getenv("TESTKEY")
   167  	if len(val)%2 != 1 {
   168  		t.Fatal("even env value")
   169  	}
   170  }
   171  
   172  func TestLookupEnv(t *testing.T) {
   173  	_, ok := os.LookupEnv("TESTKEY")
   174  	if !ok {
   175  		t.Fatal("env missing")
   176  	}
   177  }
   178  
   179  func TestDirList(t *testing.T) {
   180  	f, err := os.Open(".")
   181  	if err != nil {
   182  		t.Fatal(err)
   183  	}
   184  	f.Readdirnames(-1)
   185  	f.Close()
   186  }
   187  
   188  func TestExec(t *testing.T) {
   189  	// Note: not using os/exec to make sure there is no unexpected stat.
   190  	p, err := os.StartProcess("./script.sh", []string{"script"}, new(os.ProcAttr))
   191  	if err != nil {
   192  		t.Fatal(err)
   193  	}
   194  	ps, err := p.Wait()
   195  	if err != nil {
   196  		t.Fatal(err)
   197  	}
   198  	if !ps.Success() {
   199  		t.Fatalf("script failed: %v", err)
   200  	}
   201  }
   202  
   203  func TestExternalFile(t *testing.T) {
   204  	os.Open(os.Getenv("TEST_EXTERNAL_FILE"))
   205  	_, err := os.Stat(os.Getenv("TEST_EXTERNAL_FILE"))
   206  	if err != nil {
   207  		t.Fatal(err)
   208  	}
   209  }
   210  -- mkold.go --
   211  package main
   212  
   213  import (
   214  	"log"
   215  	"os"
   216  	"time"
   217  )
   218  
   219  func main() {
   220  	d, err := time.ParseDuration(os.Args[1])
   221  	if err != nil {
   222  		log.Fatal(err)
   223  	}
   224  	path := os.Args[2]
   225  	old := time.Now().Add(-d)
   226  	err = os.Chtimes(path, old, old)
   227  	if err != nil {
   228  		log.Fatal(err)
   229  	}
   230  }