Skip to content

Commit 388c41c

Browse files
dfinkelgopherbot
authored andcommitted
cmd/go: skip git sha256 tests if git < 2.29
Fix test building on older Ubuntu LTS releases (that are still supported). Git SHA256 support was only included in 2.29, which came out in 2021. Check the output of `git version` and skip these tests if the version is older than that introduction. Thanks to @ianlancetaylor for flagging this. Updates: golang#73704 Change-Id: I9d413a63fa43f34f94c274bba7f7b883c80433b6 Reviewed-on: https://go-review.googlesource.com/c/go/+/698835 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Michael Matloob <matloob@golang.org> Reviewed-by: Michael Matloob <matloob@google.com> Auto-Submit: Michael Matloob <matloob@google.com> Reviewed-by: Ian Alexander <jitsu@google.com>
1 parent 385dc33 commit 388c41c

File tree

9 files changed

+112
-1
lines changed

9 files changed

+112
-1
lines changed

src/cmd/go/internal/modfetch/codehost/git_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,18 @@ import (
1616
"io/fs"
1717
"log"
1818
"os"
19+
"os/exec"
1920
"path"
2021
"path/filepath"
2122
"reflect"
23+
"regexp"
2224
"runtime"
2325
"strings"
2426
"sync"
2527
"testing"
2628
"time"
29+
30+
"golang.org/x/mod/semver"
2731
)
2832

2933
func TestMain(m *testing.M) {
@@ -192,9 +196,29 @@ func testRepo(ctx context.Context, t *testing.T, remote string) (Repo, error) {
192196
return NewRepo(ctx, vcsName, remote, false)
193197
}
194198

199+
var gitVersLineExtract = regexp.MustCompile(`git version\s+([\d.]+)`)
200+
201+
func gitVersion(t testing.TB) string {
202+
gitOut, runErr := exec.Command("git", "version").CombinedOutput()
203+
if runErr != nil {
204+
t.Logf("failed to execute git version: %s", runErr)
205+
return "v0"
206+
}
207+
matches := gitVersLineExtract.FindSubmatch(gitOut)
208+
if len(matches) < 2 {
209+
t.Logf("git version extraction regexp did not match version line: %q", gitOut)
210+
return "v0"
211+
}
212+
return "v" + string(matches[1])
213+
}
214+
215+
const minGitSHA256Vers = "v2.29"
216+
195217
func TestTags(t *testing.T) {
196218
t.Parallel()
197219

220+
gitVers := gitVersion(t)
221+
198222
type tagsTest struct {
199223
repo string
200224
prefix string
@@ -204,6 +228,9 @@ func TestTags(t *testing.T) {
204228
runTest := func(tt tagsTest) func(*testing.T) {
205229
return func(t *testing.T) {
206230
t.Parallel()
231+
if tt.repo == gitsha256repo && semver.Compare(gitVers, minGitSHA256Vers) < 0 {
232+
t.Skipf("git version is too old (%+v); skipping git sha256 test", gitVers)
233+
}
207234
ctx := testContext(t)
208235

209236
r, err := testRepo(ctx, t, tt.repo)
@@ -288,6 +315,8 @@ func TestTags(t *testing.T) {
288315
func TestLatest(t *testing.T) {
289316
t.Parallel()
290317

318+
gitVers := gitVersion(t)
319+
291320
type latestTest struct {
292321
repo string
293322
info *RevInfo
@@ -297,6 +326,10 @@ func TestLatest(t *testing.T) {
297326
t.Parallel()
298327
ctx := testContext(t)
299328

329+
if tt.repo == gitsha256repo && semver.Compare(gitVers, minGitSHA256Vers) < 0 {
330+
t.Skipf("git version is too old (%+v); skipping git sha256 test", gitVers)
331+
}
332+
300333
r, err := testRepo(ctx, t, tt.repo)
301334
if err != nil {
302335
t.Fatal(err)
@@ -375,6 +408,8 @@ func TestLatest(t *testing.T) {
375408
func TestReadFile(t *testing.T) {
376409
t.Parallel()
377410

411+
gitVers := gitVersion(t)
412+
378413
type readFileTest struct {
379414
repo string
380415
rev string
@@ -387,6 +422,10 @@ func TestReadFile(t *testing.T) {
387422
t.Parallel()
388423
ctx := testContext(t)
389424

425+
if tt.repo == gitsha256repo && semver.Compare(gitVers, minGitSHA256Vers) < 0 {
426+
t.Skipf("git version is too old (%+v); skipping git sha256 test", gitVers)
427+
}
428+
390429
r, err := testRepo(ctx, t, tt.repo)
391430
if err != nil {
392431
t.Fatal(err)
@@ -468,6 +507,8 @@ type zipFile struct {
468507
func TestReadZip(t *testing.T) {
469508
t.Parallel()
470509

510+
gitVers := gitVersion(t)
511+
471512
type readZipTest struct {
472513
repo string
473514
rev string
@@ -480,6 +521,10 @@ func TestReadZip(t *testing.T) {
480521
t.Parallel()
481522
ctx := testContext(t)
482523

524+
if tt.repo == gitsha256repo && semver.Compare(gitVers, minGitSHA256Vers) < 0 {
525+
t.Skipf("git version is too old (%+v); skipping git sha256 test", gitVers)
526+
}
527+
483528
r, err := testRepo(ctx, t, tt.repo)
484529
if err != nil {
485530
t.Fatal(err)
@@ -753,6 +798,8 @@ var hgmap = map[string]string{
753798
func TestStat(t *testing.T) {
754799
t.Parallel()
755800

801+
gitVers := gitVersion(t)
802+
756803
type statTest struct {
757804
repo string
758805
rev string
@@ -764,6 +811,10 @@ func TestStat(t *testing.T) {
764811
t.Parallel()
765812
ctx := testContext(t)
766813

814+
if tt.repo == gitsha256repo && semver.Compare(gitVers, minGitSHA256Vers) < 0 {
815+
t.Skipf("git version is too old (%+v); skipping git sha256 test", gitVers)
816+
}
817+
767818
r, err := testRepo(ctx, t, tt.repo)
768819
if err != nil {
769820
t.Fatal(err)

src/cmd/go/internal/vcweb/script.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ import (
1818
"os"
1919
"os/exec"
2020
"path/filepath"
21+
"regexp"
2122
"runtime"
2223
"strconv"
2324
"strings"
2425
"time"
2526

2627
"golang.org/x/mod/module"
28+
"golang.org/x/mod/semver"
2729
"golang.org/x/mod/zip"
2830
)
2931

@@ -42,6 +44,7 @@ func newScriptEngine() *script.Engine {
4244
return script.OnceCondition(summary, func() (bool, error) { return f(), nil })
4345
}
4446
add("bzr", lazyBool("the 'bzr' executable exists and provides the standard CLI", hasWorkingBzr))
47+
add("git-min-vers", script.PrefixCondition("<suffix> indicates a minimum git version", hasAtLeastGitVersion))
4548

4649
interrupt := func(cmd *exec.Cmd) error { return cmd.Process.Signal(os.Interrupt) }
4750
gracePeriod := 30 * time.Second // arbitrary
@@ -394,3 +397,25 @@ func hasWorkingBzr() bool {
394397
err = exec.Command(bzr, "help").Run()
395398
return err == nil
396399
}
400+
401+
var gitVersLineExtract = regexp.MustCompile(`git version\s+([\d.]+)`)
402+
403+
func gitVersion() (string, error) {
404+
gitOut, runErr := exec.Command("git", "version").CombinedOutput()
405+
if runErr != nil {
406+
return "v0", fmt.Errorf("failed to execute git version: %w", runErr)
407+
}
408+
matches := gitVersLineExtract.FindSubmatch(gitOut)
409+
if len(matches) < 2 {
410+
return "v0", fmt.Errorf("git version extraction regexp did not match version line: %q", gitOut)
411+
}
412+
return "v" + string(matches[1]), nil
413+
}
414+
415+
func hasAtLeastGitVersion(s *script.State, minVers string) (bool, error) {
416+
gitVers, gitVersErr := gitVersion()
417+
if gitVersErr != nil {
418+
return false, gitVersErr
419+
}
420+
return semver.Compare(minVers, gitVers) <= 0, nil
421+
}

src/cmd/go/scriptconds_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,13 @@ import (
1414
"os"
1515
"os/exec"
1616
"path/filepath"
17+
"regexp"
1718
"runtime"
1819
"runtime/debug"
1920
"sync"
2021
"testing"
22+
23+
"golang.org/x/mod/semver"
2124
)
2225

2326
func scriptConditions(t *testing.T) map[string]script.Cond {
@@ -41,6 +44,7 @@ func scriptConditions(t *testing.T) map[string]script.Cond {
4144
add("case-sensitive", script.OnceCondition("$WORK filesystem is case-sensitive", isCaseSensitive))
4245
add("cc", script.PrefixCondition("go env CC = <suffix> (ignoring the go/env file)", ccIs))
4346
add("git", lazyBool("the 'git' executable exists and provides the standard CLI", hasWorkingGit))
47+
add("git-min-vers", script.PrefixCondition("<suffix> indicates a minimum git version", hasAtLeastGitVersion))
4448
add("net", script.PrefixCondition("can connect to external network host <suffix>", hasNet))
4549
add("trimpath", script.OnceCondition("test binary was built with -trimpath", isTrimpath))
4650

@@ -153,6 +157,28 @@ func hasWorkingGit() bool {
153157
return err == nil
154158
}
155159

160+
var gitVersLineExtract = regexp.MustCompile(`git version\s+([\d.]+)`)
161+
162+
func gitVersion() (string, error) {
163+
gitOut, runErr := exec.Command("git", "version").CombinedOutput()
164+
if runErr != nil {
165+
return "v0", fmt.Errorf("failed to execute git version: %w", runErr)
166+
}
167+
matches := gitVersLineExtract.FindSubmatch(gitOut)
168+
if len(matches) < 2 {
169+
return "v0", fmt.Errorf("git version extraction regexp did not match version line: %q", gitOut)
170+
}
171+
return "v" + string(matches[1]), nil
172+
}
173+
174+
func hasAtLeastGitVersion(s *script.State, minVers string) (bool, error) {
175+
gitVers, gitVersErr := gitVersion()
176+
if gitVersErr != nil {
177+
return false, gitVersErr
178+
}
179+
return semver.Compare(minVers, gitVers) <= 0, nil
180+
}
181+
156182
func hasWorkingBzr() bool {
157183
bzr, err := exec.LookPath("bzr")
158184
if err != nil {

src/cmd/go/testdata/script/README

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,8 @@ The available conditions are:
399399
GOOS/GOARCH supports -fuzz with instrumentation
400400
[git]
401401
the 'git' executable exists and provides the standard CLI
402+
[git-min-vers:*]
403+
<suffix> indicates a minimum git version
402404
[go-builder]
403405
GO_BUILDER_NAME is non-empty
404406
[link]

src/cmd/go/testdata/script/build_git_sha256_go_get_branch.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
[short] skip
22
[!git] skip
3+
[!git-min-vers:v2.29] skip
34

45
env GOPRIVATE=vcs-test.golang.org
56

src/cmd/go/testdata/script/build_git_sha256_moddep.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
[short] skip
22
[!git] skip
3+
[!git-min-vers:v2.29] skip
34

45
env GOPRIVATE=vcs-test.golang.org
56

src/cmd/go/testdata/script/mod_download_git_bareRepository_sha256.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
[short] skip
22
[!git] skip
3+
[!git-min-vers:v2.29] skip
34

45
# This is a git sha256-mode copy of mod_download_git_bareRepository
56

src/cmd/go/testdata/vcstest/git/gitrepo-sha256.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
[!git-min-vers:v2.29] skip
2+
13
handle git
24

35
# This is a sha256 version of gitrepo1.txt (which uses sha1 hashes)

src/cmd/internal/script/engine.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ import (
5555
"errors"
5656
"fmt"
5757
"io"
58+
"maps"
59+
"slices"
5860
"sort"
5961
"strings"
6062
"time"
@@ -518,7 +520,7 @@ func (e *Engine) conditionsActive(s *State, conds []condition) (bool, error) {
518520
if ok {
519521
impl = e.Conds[prefix]
520522
if impl == nil {
521-
return false, fmt.Errorf("unknown condition prefix %q", prefix)
523+
return false, fmt.Errorf("unknown condition prefix %q; known: %v", prefix, slices.Collect(maps.Keys(e.Conds)))
522524
}
523525
if !impl.Usage().Prefix {
524526
return false, fmt.Errorf("condition %q cannot be used with a suffix", prefix)

0 commit comments

Comments
 (0)