Text file src/cmd/go/testdata/script/test_cache_inputs.txt

     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  # Changes in arguments forwarded to the test should invalidate cached test
    33  # results.
    34  go test testcache -run=TestOSArgs -v hello
    35  ! stdout '\(cached\)'
    36  stdout 'hello'
    37  go test testcache -run=TestOSArgs -v goodbye
    38  ! stdout '\(cached\)'
    39  stdout 'goodbye'
    40  
    41  # golang.org/issue/36134: that includes the `-timeout` argument.
    42  go test testcache -run=TestOSArgs -timeout=20m -v
    43  ! stdout '\(cached\)'
    44  stdout '-test\.timeout[= ]20m'
    45  go test testcache -run=TestOSArgs -timeout=5s -v
    46  ! stdout '\(cached\)'
    47  stdout '-test\.timeout[= ]5s'
    48  
    49  # If the test stats a file, changes to the file should invalidate the cache.
    50  go test testcache -run=FileSize
    51  go test testcache -run=FileSize
    52  stdout '\(cached\)'
    53  
    54  cp 4x.txt testcache/file.txt
    55  go test testcache -run=FileSize
    56  ! stdout '\(cached\)'
    57  go test testcache -run=FileSize
    58  stdout '\(cached\)'
    59  
    60  # Files should be tracked even if the test changes its working directory.
    61  go test testcache -run=Chdir
    62  go test testcache -run=Chdir
    63  stdout '\(cached\)'
    64  cp 6x.txt testcache/file.txt
    65  go test testcache -run=Chdir
    66  ! stdout '\(cached\)'
    67  go test testcache -run=Chdir
    68  stdout '\(cached\)'
    69  
    70  # The content of files should affect caching, provided that the mtime also changes.
    71  exec ./mkold$GOEXE 1m testcache/file.txt
    72  go test testcache -run=FileContent
    73  go test testcache -run=FileContent
    74  stdout '\(cached\)'
    75  cp 2y.txt testcache/file.txt
    76  exec ./mkold$GOEXE 50s testcache/file.txt
    77  go test testcache -run=FileContent
    78  ! stdout '\(cached\)'
    79  go test testcache -run=FileContent
    80  stdout '\(cached\)'
    81  
    82  # Directory contents read via os.ReadDirNames should affect caching.
    83  go test testcache -run=DirList
    84  go test testcache -run=DirList
    85  stdout '\(cached\)'
    86  rm testcache/file.txt
    87  go test testcache -run=DirList
    88  ! stdout '\(cached\)'
    89  go test testcache -run=DirList
    90  stdout '\(cached\)'
    91  
    92  # Files outside GOROOT and GOPATH should not affect caching.
    93  env TEST_EXTERNAL_FILE=$WORK/external.txt
    94  go test testcache -run=ExternalFile
    95  go test testcache -run=ExternalFile
    96  stdout '\(cached\)'
    97  
    98  rm $WORK/external.txt
    99  go test testcache -run=ExternalFile
   100  stdout '\(cached\)'
   101  
   102  # The -benchtime flag without -bench should not affect caching.
   103  go test testcache -run=Benchtime -benchtime=1x
   104  go test testcache -run=Benchtime -benchtime=1x
   105  stdout '\(cached\)'
   106  
   107  go test testcache -run=Benchtime -bench=Benchtime -benchtime=1x
   108  go test testcache -run=Benchtime -bench=Benchtime -benchtime=1x
   109  ! stdout '\(cached\)'
   110  
   111  # golang.org/issue/47355: that includes the `-failfast` argument.
   112  go test testcache -run=TestOSArgs -failfast
   113  ! stdout '\(cached\)'
   114  go test testcache -run=TestOSArgs -failfast
   115  stdout '\(cached\)'
   116  
   117  # Executables within GOROOT and GOPATH should affect caching,
   118  # even if the test does not stat them explicitly.
   119  
   120  [!exec:/bin/sh] skip
   121  chmod 0755 ./testcache/script.sh
   122  
   123  exec ./mkold$GOEXEC 1m testcache/script.sh
   124  go test testcache -run=Exec
   125  go test testcache -run=Exec
   126  stdout '\(cached\)'
   127  
   128  exec ./mkold$GOEXE 50s testcache/script.sh
   129  go test testcache -run=Exec
   130  ! stdout '\(cached\)'
   131  go test testcache -run=Exec
   132  stdout '\(cached\)'
   133  
   134  -- testcache/file.txt --
   135  xx
   136  -- 4x.txt --
   137  xxxx
   138  -- 6x.txt --
   139  xxxxxx
   140  -- 2y.txt --
   141  yy
   142  -- $WORK/external.txt --
   143  This file is outside of GOPATH.
   144  -- testcache/script.sh --
   145  #!/bin/sh
   146  exit 0
   147  -- testcache/testcache_test.go --
   148  // Copyright 2017 The Go Authors. All rights reserved.
   149  // Use of this source code is governed by a BSD-style
   150  // license that can be found in the LICENSE file.
   151  
   152  package testcache
   153  
   154  import (
   155  	"io"
   156  	"os"
   157  	"testing"
   158  )
   159  
   160  func TestChdir(t *testing.T) {
   161  	os.Chdir("..")
   162  	defer os.Chdir("testcache")
   163  	info, err := os.Stat("testcache/file.txt")
   164  	if err != nil {
   165  		t.Fatal(err)
   166  	}
   167  	if info.Size()%2 != 1 {
   168  		t.Fatal("even file")
   169  	}
   170  }
   171  
   172  func TestOddFileContent(t *testing.T) {
   173  	f, err := os.Open("file.txt")
   174  	if err != nil {
   175  		t.Fatal(err)
   176  	}
   177  	data, err := io.ReadAll(f)
   178  	f.Close()
   179  	if err != nil {
   180  		t.Fatal(err)
   181  	}
   182  	if len(data)%2 != 1 {
   183  		t.Fatal("even file")
   184  	}
   185  }
   186  
   187  func TestOddFileSize(t *testing.T) {
   188  	info, err := os.Stat("file.txt")
   189  	if err != nil {
   190  		t.Fatal(err)
   191  	}
   192  	if info.Size()%2 != 1 {
   193  		t.Fatal("even file")
   194  	}
   195  }
   196  
   197  func TestOddGetenv(t *testing.T) {
   198  	val := os.Getenv("TESTKEY")
   199  	if len(val)%2 != 1 {
   200  		t.Fatal("even env value")
   201  	}
   202  }
   203  
   204  func TestLookupEnv(t *testing.T) {
   205  	_, ok := os.LookupEnv("TESTKEY")
   206  	if !ok {
   207  		t.Fatal("env missing")
   208  	}
   209  }
   210  
   211  func TestDirList(t *testing.T) {
   212  	f, err := os.Open(".")
   213  	if err != nil {
   214  		t.Fatal(err)
   215  	}
   216  	f.Readdirnames(-1)
   217  	f.Close()
   218  }
   219  
   220  func TestExec(t *testing.T) {
   221  	// Note: not using os/exec to make sure there is no unexpected stat.
   222  	p, err := os.StartProcess("./script.sh", []string{"script"}, new(os.ProcAttr))
   223  	if err != nil {
   224  		t.Fatal(err)
   225  	}
   226  	ps, err := p.Wait()
   227  	if err != nil {
   228  		t.Fatal(err)
   229  	}
   230  	if !ps.Success() {
   231  		t.Fatalf("script failed: %v", err)
   232  	}
   233  }
   234  
   235  func TestExternalFile(t *testing.T) {
   236  	os.Open(os.Getenv("TEST_EXTERNAL_FILE"))
   237  	_, err := os.Stat(os.Getenv("TEST_EXTERNAL_FILE"))
   238  	if err != nil {
   239  		t.Fatal(err)
   240  	}
   241  }
   242  
   243  func TestOSArgs(t *testing.T) {
   244  	t.Log(os.Args)
   245  }
   246  
   247  func TestBenchtime(t *testing.T) {
   248  }
   249  
   250  -- mkold.go --
   251  package main
   252  
   253  import (
   254  	"log"
   255  	"os"
   256  	"time"
   257  )
   258  
   259  func main() {
   260  	d, err := time.ParseDuration(os.Args[1])
   261  	if err != nil {
   262  		log.Fatal(err)
   263  	}
   264  	path := os.Args[2]
   265  	old := time.Now().Add(-d)
   266  	err = os.Chtimes(path, old, old)
   267  	if err != nil {
   268  		log.Fatal(err)
   269  	}
   270  }
   271  

View as plain text