...

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

Documentation: cmd/go/testdata/script

     1# This test checks more of the "go build -cover" functionality,
     2# specifically which packages get selected when building.
     3
     4[short] skip
     5
     6# Skip if new coverage is not enabled.
     7[!GOEXPERIMENT:coverageredesign] skip
     8
     9#-------------------------------------------
    10
    11# Build for coverage.
    12go build -mod=mod -o $WORK/modex.exe -cover mod.example/main
    13
    14# Save off old GOCOVERDIR setting
    15env SAVEGOCOVERDIR=$GOCOVERDIR
    16
    17# Execute.
    18mkdir $WORK/covdata
    19env GOCOVERDIR=$WORK/covdata
    20exec $WORK/modex.exe
    21
    22# Restore previous GOCOVERDIR setting
    23env GOCOVERDIR=$SAVEGOCOVERDIR
    24
    25# Examine the result.
    26go tool covdata percent -i=$WORK/covdata
    27stdout 'coverage: 100.0% of statements'
    28
    29# By default we want to see packages resident in the module covered,
    30# but not dependencies.
    31go tool covdata textfmt -i=$WORK/covdata -o=$WORK/covdata/out.txt
    32grep 'mode: set' $WORK/covdata/out.txt
    33grep 'mod.example/main/main.go:' $WORK/covdata/out.txt
    34grep 'mod.example/sub/sub.go:' $WORK/covdata/out.txt
    35! grep 'rsc.io' $WORK/covdata/out.txt
    36
    37rm $WORK/covdata
    38rm $WORK/modex.exe
    39
    40#-------------------------------------------
    41
    42# Repeat the build but with -coverpkg=all
    43
    44go build -mod=mod -coverpkg=all -o $WORK/modex.exe -cover mod.example/main
    45
    46# Execute.
    47mkdir $WORK/covdata
    48env GOCOVERDIR=$WORK/covdata
    49exec $WORK/modex.exe
    50
    51# Restore previous GOCOVERDIR setting
    52env GOCOVERDIR=$SAVEGOCOVERDIR
    53
    54# Examine the result.
    55go tool covdata percent -i=$WORK/covdata
    56stdout  'coverage:.*[1-9][0-9.]+%'
    57
    58# The whole enchilada.
    59go tool covdata textfmt -i=$WORK/covdata -o=$WORK/covdata/out.txt
    60grep 'mode: set' $WORK/covdata/out.txt
    61grep 'mod.example/main/main.go:' $WORK/covdata/out.txt
    62grep 'mod.example/sub/sub.go:' $WORK/covdata/out.txt
    63grep 'rsc.io' $WORK/covdata/out.txt
    64grep 'bufio/bufio.go:' $WORK/covdata/out.txt
    65
    66# Use the covdata tool to select a specific set of module paths
    67mkdir $WORK/covdata2
    68go tool covdata merge -pkg=rsc.io/quote -i=$WORK/covdata -o=$WORK/covdata2
    69
    70# Examine the result.
    71go tool covdata percent -i=$WORK/covdata2
    72stdout  'coverage:.*[1-9][0-9.]+%'
    73
    74# Check for expected packages + check that we don't see things from stdlib.
    75go tool covdata textfmt -i=$WORK/covdata2 -o=$WORK/covdata2/out.txt
    76grep 'mode: set' $WORK/covdata2/out.txt
    77! grep 'mod.example/main/main.go:' $WORK/covdata2/out.txt
    78! grep 'mod.example/sub/sub.go:' $WORK/covdata2/out.txt
    79grep 'rsc.io' $WORK/covdata2/out.txt
    80! grep 'bufio/bufio.go:' $WORK/covdata2/out.txt
    81
    82#-------------------------------------------
    83# end of test cmds, start of harness and related files.
    84
    85-- go.mod --
    86module mod.example
    87
    88go 1.20
    89
    90require rsc.io/quote/v3 v3.0.0
    91
    92-- main/main.go --
    93package main
    94
    95import (
    96	"fmt"
    97	"mod.example/sub"
    98
    99	"rsc.io/quote"
   100)
   101
   102func main() {
   103	fmt.Println(quote.Go(), sub.F())
   104}
   105
   106-- sub/sub.go --
   107
   108package sub
   109
   110func F() int {
   111	return 42
   112}
   113
   114

View as plain text