...

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

Documentation: cmd/go/testdata/script

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

View as plain text