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

     1  # Test that the test cache is invalidated when -coverpkg dependencies change,
     2  # even when -coverprofile is not specified. This exercises the else-if branch
     3  # in tryCacheWithID that checks covMeta without a cover profile file.
     4  
     5  [short] skip
     6  [GODEBUG:gocacheverify=1] skip
     7  
     8  # Start with a clean GOCACHE.
     9  env GOCACHE=$WORK/cache
    10  
    11  cd proj
    12  
    13  # Run tests with -cover and -coverpkg but without -coverprofile.
    14  go test -cover -coverpkg=proj/... ./...
    15  stdout 'coverage:'
    16  
    17  # Run again — should be served from cache.
    18  go test -cover -coverpkg=proj/... ./...
    19  stdout '\(cached\)'
    20  stdout 'coverage:'
    21  
    22  # Modify a covered package that is not directly under test.
    23  cp ../sub_modified.go sub/sub.go
    24  
    25  # Run again — the cache must be invalidated because a covered package changed.
    26  go test -cover -coverpkg=proj/... ./...
    27  ! stdout '\(cached\)'
    28  stdout 'coverage:'
    29  
    30  -- proj/go.mod --
    31  module proj
    32  
    33  go 1.21
    34  
    35  -- proj/main.go --
    36  package proj
    37  
    38  import "proj/sub"
    39  
    40  func Add(a, b int) int {
    41  	return sub.Sub(a, -b)
    42  }
    43  
    44  -- proj/main_test.go --
    45  package proj
    46  
    47  import "testing"
    48  
    49  func TestAdd(t *testing.T) {
    50  	if Add(1, 2) != 3 {
    51  		t.Fatal("expected 3")
    52  	}
    53  }
    54  
    55  -- proj/sub/sub.go --
    56  package sub
    57  
    58  func Sub(a, b int) int {
    59  	return a - b
    60  }
    61  
    62  -- proj/sub/sub_test.go --
    63  package sub
    64  
    65  import "testing"
    66  
    67  func TestSub(t *testing.T) {
    68  	if Sub(3, 1) != 2 {
    69  		t.Fatal("expected 2")
    70  	}
    71  }
    72  
    73  -- sub_modified.go --
    74  package sub
    75  
    76  // Added a comment to change the source.
    77  
    78  func Sub(a, b int) int {
    79  	return a - b
    80  }
    81  

View as plain text