From: Bryan C. Mills Date: Wed, 29 Jan 2020 14:14:50 +0000 (-0500) Subject: go/build: update TestImportDirNotExist to accept more detailed error strings X-Git-Tag: go1.14rc1~41 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=67fee6005d48f8e4554c3e933763aec7cd503f8f;p=gostls13.git go/build: update TestImportDirNotExist to accept more detailed error strings In CL 203820, we switched go/build to use the caller's working directory for the main module (rather than srcDir), so that go/build resolution now respects the requirements and replacements of the main module. When the passed-in srcDir is empty, as of that CL we use "go list" instead of falling back to in-process (GOPATH-mode) path lookup. Unfortunately, that broke go/build.TestImportDirNotExist when GO111MODULE=on: the test was looking for the specific error message produced by the in-process lookup. This change relaxes the test to accept the error message produced by "go list" when srcDir is empty. Updates #34769 Updates #34860 Updates #35734 Fixes #36867 Change-Id: Id0f7814a4b7dabe8917216eb013bb4eaee283648 Reviewed-on: https://go-review.googlesource.com/c/go/+/216817 Run-TryBot: Bryan C. Mills TryBot-Result: Gobot Gobot Reviewed-by: Jay Conrod --- diff --git a/src/go/build/build_test.go b/src/go/build/build_test.go index 9e850fab94..05ddb49920 100644 --- a/src/go/build/build_test.go +++ b/src/go/build/build_test.go @@ -340,20 +340,38 @@ func TestImportDirNotExist(t *testing.T) { {"Import(full, FindOnly)", "go/build/doesnotexist", "", FindOnly}, {"Import(local, FindOnly)", "./doesnotexist", filepath.Join(ctxt.GOROOT, "src/go/build"), FindOnly}, } - for _, test := range tests { - p, err := ctxt.Import(test.path, test.srcDir, test.mode) - if err == nil || !strings.HasPrefix(err.Error(), "cannot find package") { - t.Errorf(`%s got error: %q, want "cannot find package" error`, test.label, err) - } - // If an error occurs, build.Import is documented to return - // a non-nil *Package containing partial information. - if p == nil { - t.Fatalf(`%s got nil p, want non-nil *Package`, test.label) - } - // Verify partial information in p. - if p.ImportPath != "go/build/doesnotexist" { - t.Errorf(`%s got p.ImportPath: %q, want "go/build/doesnotexist"`, test.label, p.ImportPath) - } + + defer os.Setenv("GO111MODULE", os.Getenv("GO111MODULE")) + + for _, GO111MODULE := range []string{"off", "on"} { + t.Run("GO111MODULE="+GO111MODULE, func(t *testing.T) { + os.Setenv("GO111MODULE", GO111MODULE) + + for _, test := range tests { + p, err := ctxt.Import(test.path, test.srcDir, test.mode) + + errOk := (err != nil && strings.HasPrefix(err.Error(), "cannot find package")) + wantErr := `"cannot find package" error` + if test.srcDir == "" { + if err != nil && strings.Contains(err.Error(), "is not in GOROOT") { + errOk = true + } + wantErr = `"cannot find package" or "is not in GOROOT" error` + } + if !errOk { + t.Errorf("%s got error: %q, want %s", test.label, err, wantErr) + } + // If an error occurs, build.Import is documented to return + // a non-nil *Package containing partial information. + if p == nil { + t.Fatalf(`%s got nil p, want non-nil *Package`, test.label) + } + // Verify partial information in p. + if p.ImportPath != "go/build/doesnotexist" { + t.Errorf(`%s got p.ImportPath: %q, want "go/build/doesnotexist"`, test.label, p.ImportPath) + } + } + }) } }