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

     1  # This test checks basic "go build -cover" functionality.
     2  
     3  [short] skip
     4  
     5  # Build for coverage.
     6  go build -gcflags=-m -o example.exe -cover example/main &
     7  [race] go build -o examplewithrace.exe -race -cover example/main &
     8  wait
     9  
    10  # First execute without GOCOVERDIR set...
    11  env GOCOVERDIR=
    12  exec ./example.exe normal
    13  stderr '^warning: GOCOVERDIR not set, no coverage data emitted'
    14  
    15  # ... then with GOCOVERDIR set.
    16  env GOCOVERDIR=data/normal
    17  exec ./example.exe normal
    18  ! stderr '^warning: GOCOVERDIR not set, no coverage data emitted'
    19  go tool covdata percent -i=data/normal
    20  stdout  'coverage:.*[1-9][0-9.]+%'
    21  
    22  # Program makes a direct call to os.Exit(0).
    23  env GOCOVERDIR=data/goodexit
    24  exec ./example.exe goodexit
    25  ! stderr '^warning: GOCOVERDIR not set, no coverage data emitted'
    26  go tool covdata percent -i=data/goodexit
    27  stdout  'coverage:.*[1-9][0-9.]+%'
    28  
    29  # Program makes a direct call to os.Exit(1).
    30  env GOCOVERDIR=data/badexit
    31  ! exec ./example.exe badexit
    32  ! stderr '^warning: GOCOVERDIR not set, no coverage data emitted'
    33  go tool covdata percent -i=data/badexit
    34  stdout  'coverage:.*[1-9][0-9.]+%'
    35  
    36  # Program invokes panic.
    37  env GOCOVERDIR=data/panic
    38  ! exec ./example.exe panic
    39  ! stderr '^warning: GOCOVERDIR not set, no coverage data emitted'
    40  go tool covdata percent -i=data/panic
    41  stdout  'coverage:.*[0-9.]+%'
    42  
    43  # Skip remainder if no race detector support.
    44  [!race] skip
    45  
    46  env GOCOVERDIR=data2/normal
    47  exec ./examplewithrace.exe normal
    48  ! stderr '^warning: GOCOVERDIR not set, no coverage data emitted'
    49  go tool covdata percent -i=data2/normal
    50  stdout  'coverage:.*[1-9][0-9.]+%'
    51  
    52  # Program makes a direct call to os.Exit(0).
    53  env GOCOVERDIR=data2/goodexit
    54  exec ./examplewithrace.exe goodexit
    55  ! stderr '^warning: GOCOVERDIR not set, no coverage data emitted'
    56  go tool covdata percent -i=data2/goodexit
    57  stdout  'coverage:.*[1-9][0-9.]+%'
    58  
    59  # Program makes a direct call to os.Exit(1).
    60  env GOCOVERDIR=data2/badexit
    61  ! exec ./examplewithrace.exe badexit
    62  ! stderr '^warning: GOCOVERDIR not set, no coverage data emitted'
    63  go tool covdata percent -i=data2/badexit
    64  stdout  'coverage:.*[1-9][0-9.]+%'
    65  
    66  # Program invokes panic.
    67  env GOCOVERDIR=data2/panic
    68  ! exec ./examplewithrace.exe panic
    69  ! stderr '^warning: GOCOVERDIR not set, no coverage data emitted'
    70  go tool covdata percent -i=data2/panic
    71  stdout  'coverage:.*[0-9.]+%'
    72  
    73  # end of test cmds, start of harness and related files.
    74  
    75  -- go.mod --
    76  module example
    77  
    78  go 1.18
    79  
    80  -- main/example.go --
    81  package main
    82  
    83  import "example/sub"
    84  
    85  func main() {
    86  	sub.S()
    87  }
    88  
    89  -- sub/sub.go --
    90  
    91  package sub
    92  
    93  import "os"
    94  
    95  func S() {
    96  	switch os.Args[1] {
    97  	case "normal":
    98  		println("hi")
    99  	case "goodexit":
   100  		os.Exit(0)
   101  	case "badexit":
   102  		os.Exit(1)
   103  	case "panic":
   104  		panic("something bad happened")
   105  	}
   106  }
   107  
   108  -- data/README.txt --
   109  
   110  Just a location where we can write coverage profiles.
   111  
   112  -- data/normal/f.txt --
   113  
   114  X
   115  
   116  -- data/goodexit/f.txt --
   117  
   118  X
   119  
   120  -- data/badexit/f.txt --
   121  
   122  X
   123  
   124  -- data/panic/f.txt --
   125  
   126  X
   127  
   128  -- data2/README.txt --
   129  
   130  Just a location where we can write coverage profiles.
   131  
   132  -- data2/normal/f.txt --
   133  
   134  X
   135  
   136  -- data2/goodexit/f.txt --
   137  
   138  X
   139  
   140  -- data2/badexit/f.txt --
   141  
   142  X
   143  
   144  -- data2/panic/f.txt --
   145  
   146  X
   147  

View as plain text