Skip to content

Commit

Permalink
go/build: update TestImportDirNotExist to accept more detailed error …
Browse files Browse the repository at this point in the history
…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 <[email protected]>
TryBot-Result: Gobot Gobot <[email protected]>
Reviewed-by: Jay Conrod <[email protected]>
  • Loading branch information
Bryan C. Mills committed Jan 29, 2020
1 parent a50c3ff commit 67fee60
Showing 1 changed file with 32 additions and 14 deletions.
46 changes: 32 additions & 14 deletions src/go/build/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
})
}
}

Expand Down

0 comments on commit 67fee60

Please sign in to comment.