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

     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  #-------------------------------------------
     7  
     8  # Build for coverage.
     9  go build -mod=mod -o $WORK/modex.exe -cover mod.example/main
    10  
    11  # Save off old GOCOVERDIR setting
    12  env SAVEGOCOVERDIR=$GOCOVERDIR
    13  
    14  # Execute.
    15  mkdir $WORK/covdata
    16  env GOCOVERDIR=$WORK/covdata
    17  exec $WORK/modex.exe
    18  
    19  # Restore previous GOCOVERDIR setting
    20  env GOCOVERDIR=$SAVEGOCOVERDIR
    21  
    22  # Examine the result.
    23  go tool covdata percent -i=$WORK/covdata
    24  stdout 'coverage: 100.0% of statements'
    25  
    26  # By default we want to see packages resident in the module covered,
    27  # but not dependencies.
    28  go tool covdata textfmt -i=$WORK/covdata -o=$WORK/covdata/out.txt
    29  grep 'mode: set' $WORK/covdata/out.txt
    30  grep 'mod.example/main/main.go:' $WORK/covdata/out.txt
    31  grep 'mod.example/sub/sub.go:' $WORK/covdata/out.txt
    32  ! grep 'rsc.io' $WORK/covdata/out.txt
    33  
    34  rm $WORK/covdata
    35  rm $WORK/modex.exe
    36  
    37  #-------------------------------------------
    38  
    39  # Build with coverage limited to a dependency. The main package still needs
    40  # registration hooks so that the dependency can emit coverage data.
    41  go build -mod=mod -coverpkg=rsc.io/quote -o $WORK/modex.exe mod.example/main
    42  
    43  # Execute.
    44  mkdir $WORK/covdata
    45  env GOCOVERDIR=$WORK/covdata
    46  exec $WORK/modex.exe
    47  
    48  # Restore previous GOCOVERDIR setting
    49  env GOCOVERDIR=$SAVEGOCOVERDIR
    50  
    51  # Only the selected dependency should be covered.
    52  go tool covdata textfmt -i=$WORK/covdata -o=$WORK/covdata/out.txt
    53  ! stderr 'warning: no applicable files found'
    54  grep 'mode: set' $WORK/covdata/out.txt
    55  grep 'rsc.io/quote' $WORK/covdata/out.txt
    56  ! grep 'mod.example/main/main.go:' $WORK/covdata/out.txt
    57  ! grep 'mod.example/sub/sub.go:' $WORK/covdata/out.txt
    58  
    59  rm $WORK/covdata
    60  rm $WORK/modex.exe
    61  
    62  #-------------------------------------------
    63  
    64  # Repeat the build but with -coverpkg=all
    65  
    66  go build -mod=mod -coverpkg=all -o $WORK/modex.exe -cover mod.example/main
    67  
    68  # Execute.
    69  mkdir $WORK/covdata
    70  env GOCOVERDIR=$WORK/covdata
    71  exec $WORK/modex.exe
    72  
    73  # Restore previous GOCOVERDIR setting
    74  env GOCOVERDIR=$SAVEGOCOVERDIR
    75  
    76  # Examine the result.
    77  go tool covdata percent -i=$WORK/covdata
    78  stdout  'coverage:.*[1-9][0-9.]+%'
    79  
    80  # The whole enchilada.
    81  go tool covdata textfmt -i=$WORK/covdata -o=$WORK/covdata/out.txt
    82  grep 'mode: set' $WORK/covdata/out.txt
    83  grep 'mod.example/main/main.go:' $WORK/covdata/out.txt
    84  grep 'mod.example/sub/sub.go:' $WORK/covdata/out.txt
    85  grep 'rsc.io' $WORK/covdata/out.txt
    86  grep 'bufio/bufio.go:' $WORK/covdata/out.txt
    87  
    88  # Use the covdata tool to select a specific set of module paths
    89  mkdir $WORK/covdata2
    90  go tool covdata merge -pkg=rsc.io/quote -i=$WORK/covdata -o=$WORK/covdata2
    91  
    92  # Examine the result.
    93  go tool covdata percent -i=$WORK/covdata2
    94  stdout  'coverage:.*[1-9][0-9.]+%'
    95  
    96  # Check for expected packages + check that we don't see things from stdlib.
    97  go tool covdata textfmt -i=$WORK/covdata2 -o=$WORK/covdata2/out.txt
    98  grep 'mode: set' $WORK/covdata2/out.txt
    99  ! grep 'mod.example/main/main.go:' $WORK/covdata2/out.txt
   100  ! grep 'mod.example/sub/sub.go:' $WORK/covdata2/out.txt
   101  grep 'rsc.io' $WORK/covdata2/out.txt
   102  ! grep 'bufio/bufio.go:' $WORK/covdata2/out.txt
   103  
   104  #-------------------------------------------
   105  # end of test cmds, start of harness and related files.
   106  
   107  -- go.mod --
   108  module mod.example
   109  
   110  go 1.20
   111  
   112  require rsc.io/quote v1.5.2
   113  
   114  -- main/main.go --
   115  package main
   116  
   117  import (
   118  	"fmt"
   119  	"mod.example/sub"
   120  
   121  	"rsc.io/quote"
   122  )
   123  
   124  func main() {
   125  	fmt.Println(quote.Go(), sub.F())
   126  }
   127  
   128  -- sub/sub.go --
   129  
   130  package sub
   131  
   132  func F() int {
   133  	return 42
   134  }
   135  

View as plain text