vulncheck: improve loading error handling in tests

Change-Id: Ic6c37c92026aaad3ba5e4ca7e02180a162ab53a8
Reviewed-on: https://go-review.googlesource.com/c/vuln/+/415220
Run-TryBot: Zvonimir Pavlinovic <zpavlinovic@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Jonathan Amsterdam <jba@google.com>
diff --git a/vulncheck/helpers_test.go b/vulncheck/helpers_test.go
index c7f0805..89de424 100644
--- a/vulncheck/helpers_test.go
+++ b/vulncheck/helpers_test.go
@@ -175,9 +175,23 @@
 	}
 }
 
+// loadPackages loads packages for patterns. Returns error if the loading failed
+// or some of the specified packages have issues. In the latter case, the error
+// message will contain information only for the first observed package with issues.
 func loadPackages(e *packagestest.Exported, patterns ...string) ([]*packages.Package, error) {
 	e.Config.Mode |= packages.NeedModule | packages.NeedName | packages.NeedFiles |
 		packages.NeedCompiledGoFiles | packages.NeedImports | packages.NeedTypes |
 		packages.NeedTypesSizes | packages.NeedSyntax | packages.NeedTypesInfo | packages.NeedDeps
-	return packages.Load(e.Config, patterns...)
+	pkgs, err := packages.Load(e.Config, patterns...)
+	if err != nil {
+		return pkgs, err
+	}
+
+	for _, p := range pkgs {
+		if len(p.Errors) > 0 {
+			return pkgs, fmt.Errorf("%v", p.Errors)
+		}
+	}
+
+	return pkgs, nil
 }
diff --git a/vulncheck/source_test.go b/vulncheck/source_test.go
index 60bd3e9..1ca96d6 100644
--- a/vulncheck/source_test.go
+++ b/vulncheck/source_test.go
@@ -104,7 +104,6 @@
 	if err != nil {
 		t.Fatal(err)
 	}
-
 	if len(pkgs) != 2 {
 		t.Fatal("failed to load x and y test packages")
 	}
@@ -362,7 +361,6 @@
 	if err != nil {
 		t.Fatal(err)
 	}
-
 	if len(pkgs) != 2 {
 		t.Fatal("failed to load x and y test packages")
 	}
diff --git a/vulncheck/vulncheck_test.go b/vulncheck/vulncheck_test.go
index 7c7874c..b5079c9 100644
--- a/vulncheck/vulncheck_test.go
+++ b/vulncheck/vulncheck_test.go
@@ -226,7 +226,7 @@
 				"x/x.go": `
 			package x
 
-			import "golang.org/amod/avuln"
+			import _ "golang.org/amod/avuln"
 		`}},
 		{
 			Name: "golang.org/zmod@v0.0.0",
@@ -239,7 +239,7 @@
 			Files: map[string]interface{}{"avuln/avuln.go": `
 			package avuln
 
-			import "golang.org/wmod/w"
+			import _ "golang.org/wmod/w"
 			`},
 		},
 		{
@@ -253,14 +253,14 @@
 			Files: map[string]interface{}{"w/w.go": `
 			package w
 
-			import "golang.org/bmod/bvuln"
+			import _ "golang.org/bmod/bvuln"
 			`},
 		},
 	})
 	defer e.Cleanup()
 
-	// Load x and y as entry packages.
-	pkgs, err := loadPackages(e, path.Join(e.Temp(), "entry/x"), path.Join(e.Temp(), "entry/y"))
+	// Load x as entry package.
+	pkgs, err := loadPackages(e, path.Join(e.Temp(), "entry/x"))
 	if err != nil {
 		t.Fatal(err)
 	}
@@ -271,7 +271,6 @@
 		"golang.org/amod/avuln": {"golang.org/wmod/w"},
 		"golang.org/bmod/bvuln": nil,
 		"golang.org/entry/x":    {"golang.org/amod/avuln"},
-		"golang.org/entry/y":    nil,
 		"golang.org/wmod/w":     {"golang.org/bmod/bvuln"},
 	}
 	if got := pkgPathToImports(vpkgs); !reflect.DeepEqual(got, wantPkgs) {