summaryrefslogtreecommitdiffstats
path: root/src/cmd/go/testdata/script/test_buildvcs.txt
blob: db844f88b35341114653a36032e2a2efe7efafd2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# https://go.dev/issue/51723: 'go test' should not stamp VCS metadata
# in the build settings. (It isn't worth the latency hit, given that
# test binaries are almost never distributed to users.)

[short] skip
[!git] skip

exec git init

# The test binaries should not have VCS settings stamped by default.
# (The test itself verifies that.)
go test . ./testonly

# However, setting -buildvcs explicitly should override that and
# stamp anyway (https://go.dev/issue/52648).
go test -buildvcs -c -o ./testonly.exe ./testonly
! exec ./testonly.exe
stdout 'unexpected VCS setting: vcs\.modified=true'


# Remove 'git' from $PATH. The test should still build.
# This ensures that we aren't loading VCS metadata that
# we subsequently throw away.
env PATH=''
env path=''

# Compiling the test should not require the VCS tool.
go test -c -o $devnull .


# When listing a main package, in general we need its VCS metadata to determine
# the .Stale and .StaleReason fields.
! go list -buildvcs=true .
stderr '^go: missing Git command\. See https://golang\.org/s/gogetcmd\nerror obtaining VCS status: .*\n\tUse -buildvcs=false to disable VCS stamping.'

# Adding the -test flag should be strictly additive — it should not suppress the error.
! go list -buildvcs=true -test .
stderr '^go: missing Git command\. See https://golang\.org/s/gogetcmd\nerror obtaining VCS status: .*\n\tUse -buildvcs=false to disable VCS stamping.'

# Adding the suggested flag should suppress the error.
go list -test -buildvcs=false .
! stderr .


# Since the ./testonly package doesn't itself produce an actual binary, we shouldn't
# invoke a VCS tool to compute a build stamp by default when listing it.
go list ./testonly
! stderr .
go list -test ./testonly
! stderr .

# Again, setting -buildvcs explicitly should force the use of the VCS tool.
! go list -buildvcs ./testonly
stderr '^go: missing Git command\. See https://golang\.org/s/gogetcmd\nerror obtaining VCS status: .*\n\tUse -buildvcs=false to disable VCS stamping.'
! go list -buildvcs -test ./testonly
stderr '^go: missing Git command\. See https://golang\.org/s/gogetcmd\nerror obtaining VCS status: .*\n\tUse -buildvcs=false to disable VCS stamping.'


-- go.mod --
module example

go 1.18
-- example.go --
package main
-- example_test.go --
package main

import (
	"runtime/debug"
	"strings"
	"testing"
)

func TestDetail(t *testing.T) {
	bi, ok := debug.ReadBuildInfo()
	if !ok {
		t.Fatal("BuildInfo not present")
	}
	for _, s := range bi.Settings {
		if strings.HasPrefix(s.Key, "vcs.") {
			t.Fatalf("unexpected VCS setting: %s=%s", s.Key, s.Value)
		}
	}
}
-- testonly/main_test.go --
package main

import (
	"runtime/debug"
	"strings"
	"testing"
)

func TestDetail(t *testing.T) {
	bi, ok := debug.ReadBuildInfo()
	if !ok {
		t.Fatal("BuildInfo not present")
	}
	for _, s := range bi.Settings {
		if strings.HasPrefix(s.Key, "vcs.") {
			t.Fatalf("unexpected VCS setting: %s=%s", s.Key, s.Value)
		}
	}
}