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

     1  
     2  # Testcase related to #58770 and #24570. This is intended to ensure
     3  # that coverage collection works in situations where we're testing a
     4  # collection of packages and supplying a -coverpkg pattern that
     5  # matches some but not all of the collection. In addition, some of the
     6  # packages have Go code but no tests, and other packages have tests
     7  # but no Go code. Package breakdown:
     8  #
     9  # Package         Code?           Tests?          Stmts           Imports
    10  # a               yes             yes             2               f
    11  # b               yes             yes             1               a, d
    12  # c               yes             yes             3               ---
    13  # d               yes             no              1               ---
    14  # e               no              yes             0               a, b
    15  # f               yes             no              3               ---
    16  #
    17  
    18  [short] skip
    19  [!GOEXPERIMENT:coverageredesign] skip
    20  
    21  # Test all packages with -coverpkg=./...
    22  go test -coverprofile=cov.p -coverpkg=./... ./...
    23  stdout '^ok\s+M/a\s+\S+\s+coverage: 50.0% of statements in ./...'
    24  stdout '^ok\s+M/b\s+\S+\s+coverage: 60.0% of statements in ./...'
    25  stdout '^ok\s+M/c\s+\S+\s+coverage: 30.0% of statements in ./...'
    26  stdout '^\s*M/d\s+coverage: 0.0% of statements'
    27  stdout '^\s*M/f\s+coverage: 0.0% of statements'
    28  
    29  # Test just the test-only package ./e but with -coverpkg=./...
    30  # Total number of statements should be 7 (e.g. a/b/d/f but not c)
    31  # and covered percent should be 6/7 (we hit everything in the
    32  # coverpkg pattern except the func in "d").
    33  go test -coverprofile=bar.p -coverpkg=./... ./e
    34  stdout '^ok\s+M/e\s+\S+\s+coverage: 85.7% of statements in ./...'
    35  
    36  # Test b and f with -coverpkg set to a/d/f. Total of 6 statements
    37  # in a/d/f, again we hit everything except DFunc.
    38  go test -coverprofile=baz.p -coverpkg=./a,./d,./f ./b ./f
    39  stdout '^ok\s+M/b\s+\S+\s+coverage: 83.3% of statements in ./a, ./d, ./f'
    40  stdout '^\s*M/f\s+coverage: 0.0% of statements'
    41  
    42  # This sub-test inspired by issue 65653: if package P is is matched
    43  # via the package pattern supplied as the argument to "go test -cover"
    44  # but P is not part of "-coverpkg", then we don't want coverage for P
    45  # (including the specific case where P has no test files).
    46  go test -coverpkg=./a ./...
    47  stdout '^ok\s+M/a\s+\S+\s+coverage: 100.0% of statements in ./a'
    48  stdout '^\s*\?\s+M/f\s+\[no test files\]'
    49  
    50  -- a/a.go --
    51  package a
    52  
    53  import "M/f"
    54  
    55  var G int
    56  
    57  func AFunc() int {
    58  	G = 1
    59  	return f.Id()
    60  }
    61  -- a/a_test.go --
    62  package a
    63  
    64  import "testing"
    65  
    66  func TestA(t *testing.T) {
    67  	if AFunc() != 42 {
    68  		t.Fatalf("bad!")
    69  	}
    70  }
    71  -- b/b.go --
    72  package b
    73  
    74  import (
    75  	"M/a"
    76  	"M/d"
    77  )
    78  
    79  func BFunc() int {
    80  	return -d.FortyTwo + a.AFunc()
    81  }
    82  -- b/b_test.go --
    83  package b
    84  
    85  import "testing"
    86  
    87  func TestB(t *testing.T) {
    88  	if BFunc() == 1010101 {
    89  		t.Fatalf("bad!")
    90  	}
    91  }
    92  -- c/c.go --
    93  package c
    94  
    95  var G int
    96  
    97  func CFunc(x, y int) int {
    98  	G += x
    99  	G -= y
   100  	return x + y
   101  }
   102  -- c/c_test.go --
   103  package c
   104  
   105  import "testing"
   106  
   107  func TestC(t *testing.T) {
   108  	if CFunc(10, 10) == 1010101 {
   109  		t.Fatalf("bad!")
   110  	}
   111  }
   112  -- d/d.go --
   113  package d
   114  
   115  const FortyTwo = 42
   116  
   117  func DFunc() int {
   118    return FortyTwo
   119  }
   120  
   121  -- e/e_test.go --
   122  package e
   123  
   124  import (
   125  	"M/a"
   126  	"M/b"
   127  	"testing"
   128  )
   129  
   130  func TestBlah(t *testing.T) {
   131  	if b.BFunc() == 1010101 {
   132  		t.Fatalf("bad")
   133  	}
   134  	a.AFunc()
   135  }
   136  -- f/f.go --
   137  package f
   138  
   139  var F int
   140  
   141  func Id() int {
   142  	F += 9
   143  	F *= 2
   144  	return 42
   145  }
   146  -- go.mod --
   147  module M
   148  
   149  go 1.21
   150  

View as plain text