1 # This test checks that VCS information is stamped into Go binaries by default,
2 # controlled with -buildvcs. This test focuses on Mercurial specifics.
3 # The Git test covers common functionality.
4
5 [!exec:hg] skip
6 [short] skip
7 env GOBIN=$WORK/gopath/bin
8 env oldpath=$PATH
9 env TZ=GMT
10 env HGRCPATH=$WORK/hgrc
11 cd repo/a
12
13 # If there's no local repository, there's no VCS info.
14 go install
15 go version -m $GOBIN/a$GOEXE
16 ! stdout hgrevision
17 stdout '\s+mod\s+example.com/a\s+\(devel\)'
18 rm $GOBIN/a$GOEXE
19
20 # If there is a repository, but it can't be used for some reason,
21 # there should be an error. It should hint about -buildvcs=false.
22 cd ..
23 mkdir .hg
24 env PATH=$WORK${/}fakebin${:}$oldpath
25 chmod 0755 $WORK/fakebin/hg
26 ! exec hg help
27 cd a
28 ! go install
29 stderr '^error obtaining VCS status: exit status 1\n\tUse -buildvcs=false to disable VCS stamping.$'
30 rm $GOBIN/a$GOEXE
31 cd ..
32 env PATH=$oldpath
33 rm .hg
34
35 # An empty repository or one explicitly updated to null uses the null cset ID,
36 # and the time is hard set to 1/1/70, regardless of the current time.
37 exec hg init
38 cd a
39 go install
40 go version -m $GOBIN/a$GOEXE
41 stdout '^\tbuild\tvcs.revision=0000000000000000000000000000000000000000$'
42 stdout '^\tbuild\tvcs.time=1970-01-01T00:00:00Z$'
43 stdout '^\tbuild\tvcs.modified=true$'
44 stdout '\s+mod\s+example.com/a\s+v0.0.0-19700101000000-000000000000\+dirty'
45 cd ..
46
47 # Revision and commit time are tagged for repositories with commits.
48 exec hg add a README
49 exec hg commit -m 'initial commit' --user test-user --date '2024-07-31T01:21:27+00:00'
50 exec hg tag v1.2.3
51 # Switch back to the tagged branch.
52 # Tagging a commit causes a new commit to be created. (See https://repo.mercurial-scm.org/hg/help/revsets)
53 exec hg update '.~1'
54 cd a
55 go install
56 go version -m $GOBIN/a$GOEXE
57 stdout '^\tbuild\tvcs.revision=71eaed52daeaafea83cb604f75b0a0336ef2c345$'
58 stdout '^\tbuild\tvcs.time=2024-07-31T01:21:27Z$'
59 stdout '^\tbuild\tvcs.modified=false$'
60 stdout '\s+mod\s+example.com/a\s+v1.2.3\s+'
61 rm $GOBIN/a$GOEXE
62
63 # Add an extra commit and then back off of it to show that the hash is
64 # from the checked out revision, not the tip revision.
65 cp ../../outside/empty.txt .
66 exec hg ci -Am 'another commit' --user test-user --date '2024-08-01T19:24:38+00:00'
67 exec hg update --clean -r '.^'
68
69 # Modified state is not thrown off by extra status output
70 exec hg bisect -v -g .
71 exec hg bisect -v -b '.^^'
72 exec hg status
73 stdout '^.+'
74 go install
75 go version -m $GOBIN/a$GOEXE
76 stdout '^\tbuild\tvcs.revision=71eaed52daeaafea83cb604f75b0a0336ef2c345$'
77 stdout '^\tbuild\tvcs.time=2024-07-31T01:21:27Z$'
78 stdout '^\tbuild\tvcs.modified=false$'
79 stdout '\s+mod\s+example.com/a\s+v1.2.3\s+'
80 rm $GOBIN/a$GOEXE
81
82 # Building with -buildvcs=false suppresses the info.
83 go install -buildvcs=false
84 go version -m $GOBIN/a$GOEXE
85 ! stdout hgrevision
86 stdout '\s+mod\s+example.com/a\s+\(devel\)'
87 rm $GOBIN/a$GOEXE
88
89 # An untracked file is shown as uncommitted, even if it isn't part of the build.
90 cp ../../outside/empty.txt .
91 go install
92 go version -m $GOBIN/a$GOEXE
93 stdout '^\tbuild\tvcs.modified=true$'
94 stdout '\s+mod\s+example.com/a\s+v1.2.3\+dirty\s+'
95 rm empty.txt
96 rm $GOBIN/a$GOEXE
97
98 # An edited file is shown as uncommitted, even if it isn't part of the build.
99 cp ../../outside/empty.txt ../README
100 go install
101 go version -m $GOBIN/a$GOEXE
102 stdout '^\tbuild\tvcs.modified=true$'
103 stdout '\s+mod\s+example.com/a\s+v1.2.3\+dirty\s+'
104 exec hg revert ../README
105 rm $GOBIN/a$GOEXE
106
107 -- $WORK/fakebin/hg --
108 #!/bin/sh
109 exit 1
110 -- $WORK/fakebin/hg.bat --
111 exit 1
112 -- repo/README --
113 Far out in the uncharted backwaters of the unfashionable end of the western
114 spiral arm of the Galaxy lies a small, unregarded yellow sun.
115 -- repo/a/go.mod --
116 module example.com/a
117
118 go 1.18
119 -- repo/a/a.go --
120 package main
121
122 func main() {}
123 -- $WORK/hgrc --
124 [ui]
125 # tweakdefaults is an opt-in that may print extra output in commands like
126 # status. That can be disabled by setting HGPLAIN=1.
127 tweakdefaults = 1
128
129 -- outside/empty.txt --
130
View as plain text