...

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

Documentation: cmd/go/testdata/script

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

View as plain text