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

     1  [short] skip
     2  
     3  # Workaround for issue 64014 -- for the portion of this test that
     4  # verifies that caching works correctly, the cache should theoretically
     5  # always behave reliably/deterministically, however if other tests are
     6  # concurrently accessing the cache while this test is running, it can
     7  # lead to cache lookup failures, which manifest as test failures here.
     8  # To avoid such flakes, use a separate isolated GOCACHE for this test.
     9  env GOCACHE=$WORK/cache
    10  
    11  # Initial run with simple coverage.
    12  go test -cover ./pkg1 ./pkg2 ./pkg3 ./pkg4
    13  stdout 'pkg1		coverage: 0.0% of statements'
    14  stdout 'pkg2	\S+	coverage: 0.0% of statements \[no tests to run\]'
    15  stdout 'pkg3	\S+	coverage: 100.0% of statements'
    16  stdout 'pkg4	\S+	coverage: \[no statements\]'
    17  
    18  # Second run to make sure that caching works properly.
    19  go test -x -cover ./pkg1 ./pkg2 ./pkg3 ./pkg4
    20  stdout 'pkg1		coverage: 0.0% of statements'
    21  stdout 'pkg2	\S+	coverage: 0.0% of statements \[no tests to run\]'
    22  stdout 'pkg3	\S+	coverage: 100.0% of statements'
    23  stdout 'pkg4	\S+	coverage: \[no statements\]'
    24  ! stderr 'link(\.exe"?)? -'
    25  ! stderr 'compile(\.exe"?)? -'
    26  ! stderr 'cover(\.exe"?)? -'
    27  stderr 'covdata(\.exe"?)? percent'
    28  
    29  # Now add in -coverprofile.
    30  go test -cover -coverprofile=cov.dat ./pkg1 ./pkg2 ./pkg3 ./pkg4
    31  stdout 'pkg1		coverage: 0.0% of statements'
    32  stdout 'pkg2	\S+	coverage: 0.0% of statements \[no tests to run\]'
    33  stdout 'pkg3	\S+	coverage: 100.0% of statements'
    34  stdout 'pkg4	\S+	coverage: \[no statements\]'
    35  
    36  # Validate
    37  go tool cover -func=cov.dat
    38  stdout 'pkg1/a.go:5:\s+F\s+0.0%'
    39  
    40  -- go.mod --
    41  module m
    42  
    43  go 1.16
    44  -- pkg1/a.go --
    45  package pkg1
    46  
    47  import "fmt"
    48  
    49  func F() {
    50  	fmt.Println("pkg1")
    51  }
    52  -- pkg2/a.go --
    53  package pkg2
    54  
    55  import "fmt"
    56  
    57  func F() {
    58  	fmt.Println("pkg2")
    59  }
    60  -- pkg2/a_test.go --
    61  package pkg2
    62  -- pkg3/a.go --
    63  package pkg3
    64  
    65  import "fmt"
    66  
    67  func F() {
    68  	fmt.Println("pkg3")
    69  }
    70  -- pkg3/a_test.go --
    71  package pkg3
    72  
    73  import "testing"
    74  
    75  func TestF(t *testing.T) {
    76  	F()
    77  }
    78  -- pkg4/a.go --
    79  package pkg4
    80  
    81  type T struct {
    82  	X bool
    83  }
    84  -- pkg4/a_test.go --
    85  package pkg4
    86  
    87  import (
    88  	"testing"
    89  )
    90  
    91  func TestT(t *testing.T) {
    92  	_ = T{}
    93  }
    94  

View as plain text