From 755ce86c7629f15bd3128fa8dbfa6a619a9a6c7a Mon Sep 17 00:00:00 2001 From: Heschi Kreinick Date: Fri, 7 Jun 2019 18:37:28 -0400 Subject: [PATCH] internal/imports: fix TestNoMainModule https://golang.org/cl/179998 made it an error to run go mod download without args and without a go.mod. Don't do that. Fixes golang/go#32488 Change-Id: Icd845feb8fecae69b6a8363438bb9ee546a310dc Reviewed-on: https://go-review.googlesource.com/c/tools/+/181298 Run-TryBot: Heschi Kreinick Reviewed-by: Jay Conrod TryBot-Result: Gobot Gobot --- internal/imports/mod_test.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/internal/imports/mod_test.go b/internal/imports/mod_test.go index 590b1c89d53..97ef715cd9d 100644 --- a/internal/imports/mod_test.go +++ b/internal/imports/mod_test.go @@ -524,10 +524,15 @@ func setup(t *testing.T, main, wd string) *modTest { WorkingDir: filepath.Join(mainDir, wd), } - // go mod tidy instead of download because tidy will notice dependencies - // in code, not just in go.mod files. - if _, err := env.invokeGo("mod", "download"); err != nil { - t.Fatal(err) + // go mod download gets mad if we don't have a go.mod, so make sure we do. + _, err = os.Stat(filepath.Join(mainDir, "go.mod")) + if err != nil && !os.IsNotExist(err) { + t.Fatalf("checking if go.mod exists: %v", err) + } + if err == nil { + if _, err := env.invokeGo("mod", "download"); err != nil { + t.Fatal(err) + } } return &modTest{