...

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

Documentation: cmd/go/testdata/script

     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=./...
    22go test -coverprofile=cov.p -coverpkg=./... ./...
    23stdout '^ok\s+M/a\s+\S+\s+coverage: 50.0% of statements in ./...'
    24stdout '^ok\s+M/b\s+\S+\s+coverage: 60.0% of statements in ./...'
    25stdout '^ok\s+M/c\s+\S+\s+coverage: 30.0% of statements in ./...'
    26stdout '^\s*M/d\s+coverage: 0.0% of statements'
    27stdout '^\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").
    33go test -coverprofile=bar.p -coverpkg=./... ./e
    34stdout '^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.
    38go test -coverprofile=baz.p -coverpkg=./a,./d,./f ./b ./f
    39stdout '^ok\s+M/b\s+\S+\s+coverage: 83.3% of statements in ./a, ./d, ./f'
    40stdout '^\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).
    46go test -coverpkg=./a ./...
    47stdout '^ok\s+M/a\s+\S+\s+coverage: 100.0% of statements in ./a'
    48stdout '^\s*\?\s+M/f\s+\[no test files\]'
    49
    50-- a/a.go --
    51package a
    52
    53import "M/f"
    54
    55var G int
    56
    57func AFunc() int {
    58	G = 1
    59	return f.Id()
    60}
    61-- a/a_test.go --
    62package a
    63
    64import "testing"
    65
    66func TestA(t *testing.T) {
    67	if AFunc() != 42 {
    68		t.Fatalf("bad!")
    69	}
    70}
    71-- b/b.go --
    72package b
    73
    74import (
    75	"M/a"
    76	"M/d"
    77)
    78
    79func BFunc() int {
    80	return -d.FortyTwo + a.AFunc()
    81}
    82-- b/b_test.go --
    83package b
    84
    85import "testing"
    86
    87func TestB(t *testing.T) {
    88	if BFunc() == 1010101 {
    89		t.Fatalf("bad!")
    90	}
    91}
    92-- c/c.go --
    93package c
    94
    95var G int
    96
    97func CFunc(x, y int) int {
    98	G += x
    99	G -= y
   100	return x + y
   101}
   102-- c/c_test.go --
   103package c
   104
   105import "testing"
   106
   107func TestC(t *testing.T) {
   108	if CFunc(10, 10) == 1010101 {
   109		t.Fatalf("bad!")
   110	}
   111}
   112-- d/d.go --
   113package d
   114
   115const FortyTwo = 42
   116
   117func DFunc() int {
   118  return FortyTwo
   119}
   120
   121-- e/e_test.go --
   122package e
   123
   124import (
   125	"M/a"
   126	"M/b"
   127	"testing"
   128)
   129
   130func TestBlah(t *testing.T) {
   131	if b.BFunc() == 1010101 {
   132		t.Fatalf("bad")
   133	}
   134	a.AFunc()
   135}
   136-- f/f.go --
   137package f
   138
   139var F int
   140
   141func Id() int {
   142	F += 9
   143	F *= 2
   144	return 42
   145}
   146-- go.mod --
   147module M
   148
   149go 1.21

View as plain text