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

     1  # https://go.dev/issue/51723: 'go test' should not stamp VCS metadata
     2  # in the build settings. (It isn't worth the latency hit, given that
     3  # test binaries are almost never distributed to users.)
     4  
     5  [short] skip
     6  [!exec:git] skip
     7  
     8  env GOFLAGS=-buildvcs  # override default -buildvcs=auto in GOFLAGS, as a user might
     9  
    10  exec git init
    11  
    12  # The test binaries should not have VCS settings stamped.
    13  # (The test itself verifies that.)
    14  go test . ./testonly
    15  
    16  
    17  # Remove 'git' from $PATH. The test should still build.
    18  # This ensures that we aren't loading VCS metadata that
    19  # we subsequently throw away.
    20  env PATH=''
    21  env path=''
    22  
    23  # Compiling the test should not require the VCS tool.
    24  go test -c -o $devnull .
    25  
    26  
    27  # When listing a main package, in general we need its VCS metadata to determine
    28  # the .Stale and .StaleReason fields.
    29  ! go list .
    30  stderr '^go: missing Git command\. See https://golang\.org/s/gogetcmd\nerror obtaining VCS status: .*\n\tUse -buildvcs=false to disable VCS stamping.'
    31  
    32  # Adding the -test flag should be strictly additive — it should not suppress the error.
    33  ! go list -test .
    34  stderr '^go: missing Git command\. See https://golang\.org/s/gogetcmd\nerror obtaining VCS status: .*\n\tUse -buildvcs=false to disable VCS stamping.'
    35  
    36  # Adding the suggested flag should suppress the error.
    37  go list -test -buildvcs=false .
    38  ! stderr .
    39  
    40  
    41  # Since the ./testonly package can't produce an actual binary, we shouldn't
    42  # invoke a VCS tool to compute a build stamp when listing it.
    43  go list ./testonly
    44  ! stderr .
    45  go list -test ./testonly
    46  ! stderr .
    47  
    48  
    49  -- go.mod --
    50  module example
    51  
    52  go 1.18
    53  -- example.go --
    54  package main
    55  -- example_test.go --
    56  package main
    57  
    58  import (
    59  	"runtime/debug"
    60  	"strings"
    61  	"testing"
    62  )
    63  
    64  func TestDetail(t *testing.T) {
    65  	bi, ok := debug.ReadBuildInfo()
    66  	if !ok {
    67  		t.Fatal("BuildInfo not present")
    68  	}
    69  	for _, s := range bi.Settings {
    70  		if strings.HasPrefix(s.Key, "vcs.") {
    71  			t.Fatalf("unexpected VCS setting: %s=%s", s.Key, s.Value)
    72  		}
    73  	}
    74  }
    75  -- testonly/main_test.go --
    76  package main
    77  
    78  import (
    79  	"runtime/debug"
    80  	"strings"
    81  	"testing"
    82  )
    83  
    84  func TestDetail(t *testing.T) {
    85  	bi, ok := debug.ReadBuildInfo()
    86  	if !ok {
    87  		t.Fatal("BuildInfo not present")
    88  	}
    89  	for _, s := range bi.Settings {
    90  		if strings.HasPrefix(s.Key, "vcs.") {
    91  			t.Fatalf("unexpected VCS setting: %s=%s", s.Key, s.Value)
    92  		}
    93  	}
    94  }
    95  

View as plain text