]> Cypherpunks repositories - gostls13.git/commitdiff
go/build: update TestImportDirNotExist to accept more detailed error strings
authorBryan C. Mills <bcmills@google.com>
Wed, 29 Jan 2020 14:14:50 +0000 (09:14 -0500)
committerBryan C. Mills <bcmills@google.com>
Wed, 29 Jan 2020 19:54:12 +0000 (19:54 +0000)
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 <bcmills@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Jay Conrod <jayconrod@google.com>
src/go/build/build_test.go

index 9e850fab9412d04b9cd87c57469fa40c42687704..05ddb499203bc85a3e217ad1859a91bf6af0ab52 100644 (file)
@@ -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)
+                               }
+                       }
+               })
        }
 }