Skip to content

Commit 186a3bb

Browse files
author
Bryan C. Mills
committed
cmd/go/internal/modfetch/codehost: skip hg tests if no hg binary is present
Change-Id: I5cf57bf1153eb662bcab71e3d2c04848212559a6 Reviewed-on: https://go-review.googlesource.com/c/go/+/330989 Trust: Bryan C. Mills <bcmills@google.com> Run-TryBot: Bryan C. Mills <bcmills@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Jay Conrod <jayconrod@google.com>
1 parent 00c0055 commit 186a3bb

File tree

1 file changed

+23
-26
lines changed

1 file changed

+23
-26
lines changed

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

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"archive/zip"
99
"bytes"
1010
"flag"
11-
"fmt"
1211
"internal/testenv"
1312
"io"
1413
"io/fs"
@@ -47,36 +46,32 @@ var altRepos = []string{
4746
var localGitRepo string
4847

4948
func testMain(m *testing.M) int {
50-
if _, err := exec.LookPath("git"); err != nil {
51-
fmt.Fprintln(os.Stderr, "skipping because git binary not found")
52-
fmt.Println("PASS")
53-
return 0
54-
}
55-
5649
dir, err := os.MkdirTemp("", "gitrepo-test-")
5750
if err != nil {
5851
log.Fatal(err)
5952
}
6053
defer os.RemoveAll(dir)
6154

6255
if testenv.HasExternalNetwork() && testenv.HasExec() {
63-
// Clone gitrepo1 into a local directory.
64-
// If we use a file:// URL to access the local directory,
65-
// then git starts up all the usual protocol machinery,
66-
// which will let us test remote git archive invocations.
67-
localGitRepo = filepath.Join(dir, "gitrepo2")
68-
if _, err := Run("", "git", "clone", "--mirror", gitrepo1, localGitRepo); err != nil {
69-
log.Fatal(err)
70-
}
71-
if _, err := Run(localGitRepo, "git", "config", "daemon.uploadarch", "true"); err != nil {
72-
log.Fatal(err)
56+
if _, err := exec.LookPath("git"); err == nil {
57+
// Clone gitrepo1 into a local directory.
58+
// If we use a file:// URL to access the local directory,
59+
// then git starts up all the usual protocol machinery,
60+
// which will let us test remote git archive invocations.
61+
localGitRepo = filepath.Join(dir, "gitrepo2")
62+
if _, err := Run("", "git", "clone", "--mirror", gitrepo1, localGitRepo); err != nil {
63+
log.Fatal(err)
64+
}
65+
if _, err := Run(localGitRepo, "git", "config", "daemon.uploadarch", "true"); err != nil {
66+
log.Fatal(err)
67+
}
7368
}
7469
}
7570

7671
return m.Run()
7772
}
7873

79-
func testRepo(remote string) (Repo, error) {
74+
func testRepo(t *testing.T, remote string) (Repo, error) {
8075
if remote == "localGitRepo" {
8176
// Convert absolute path to file URL. LocalGitRepo will not accept
8277
// Windows absolute paths because they look like a host:path remote.
@@ -87,15 +82,17 @@ func testRepo(remote string) (Repo, error) {
8782
} else {
8883
url = "file:///" + filepath.ToSlash(localGitRepo)
8984
}
85+
testenv.MustHaveExecPath(t, "git")
9086
return LocalGitRepo(url)
9187
}
92-
kind := "git"
88+
vcs := "git"
9389
for _, k := range []string{"hg"} {
9490
if strings.Contains(remote, "/"+k+"/") {
95-
kind = k
91+
vcs = k
9692
}
9793
}
98-
return NewRepo(kind, remote)
94+
testenv.MustHaveExecPath(t, vcs)
95+
return NewRepo(vcs, remote)
9996
}
10097

10198
var tagsTests = []struct {
@@ -116,7 +113,7 @@ func TestTags(t *testing.T) {
116113

117114
for _, tt := range tagsTests {
118115
f := func(t *testing.T) {
119-
r, err := testRepo(tt.repo)
116+
r, err := testRepo(t, tt.repo)
120117
if err != nil {
121118
t.Fatal(err)
122119
}
@@ -168,7 +165,7 @@ func TestLatest(t *testing.T) {
168165

169166
for _, tt := range latestTests {
170167
f := func(t *testing.T) {
171-
r, err := testRepo(tt.repo)
168+
r, err := testRepo(t, tt.repo)
172169
if err != nil {
173170
t.Fatal(err)
174171
}
@@ -221,7 +218,7 @@ func TestReadFile(t *testing.T) {
221218

222219
for _, tt := range readFileTests {
223220
f := func(t *testing.T) {
224-
r, err := testRepo(tt.repo)
221+
r, err := testRepo(t, tt.repo)
225222
if err != nil {
226223
t.Fatal(err)
227224
}
@@ -412,7 +409,7 @@ func TestReadZip(t *testing.T) {
412409

413410
for _, tt := range readZipTests {
414411
f := func(t *testing.T) {
415-
r, err := testRepo(tt.repo)
412+
r, err := testRepo(t, tt.repo)
416413
if err != nil {
417414
t.Fatal(err)
418415
}
@@ -581,7 +578,7 @@ func TestStat(t *testing.T) {
581578

582579
for _, tt := range statTests {
583580
f := func(t *testing.T) {
584-
r, err := testRepo(tt.repo)
581+
r, err := testRepo(t, tt.repo)
585582
if err != nil {
586583
t.Fatal(err)
587584
}

0 commit comments

Comments
 (0)