From ecb7392444713606ce34821041d78c35271e9241 Mon Sep 17 00:00:00 2001 From: Jay Conrod Date: Wed, 5 May 2021 17:42:38 -0400 Subject: [PATCH] go/packages: in overlay_test, allow test variant in second_file case When CL 241577 lands, 'go list -e' will include unparseable files (like a file without a package declaration) in GoFiles in addition to InvalidGoFiles. This makes go/packages more tolerant of empty test files. With CL 241577, if a file=src.go query is given when an empty _test.go is present, packages.Load will return two matching packages: the library under test, and the internal test package. This CL broadens overlay_test.go so the new behavior doesn't break the test. For golang/go#39986 Change-Id: I14d019129465e928a3f60a2230edd2e2d1d74687 Reviewed-on: https://go-review.googlesource.com/c/tools/+/317409 Trust: Jay Conrod Run-TryBot: Jay Conrod TryBot-Result: Go Bot gopls-CI: kokoro Reviewed-by: Bryan C. Mills --- go/packages/overlay_test.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/go/packages/overlay_test.go b/go/packages/overlay_test.go index 97193cfd59d..5850a7f41a6 100644 --- a/go/packages/overlay_test.go +++ b/go/packages/overlay_test.go @@ -818,8 +818,9 @@ func testInvalidFilesBeforeOverlayContains(t *testing.T, exporter packagestest.E if err != nil { t.Fatal(err) } - if len(initial) != 1 { - t.Fatalf("expected 1 packages, got %v", len(initial)) + if len(initial) != 1 && + (len(initial) != 2 || !isTestVariant(initial[0].ID, initial[1].ID)) { + t.Fatalf("expected 1 package (perhaps with test variant), got %v", len(initial)) } pkg := initial[0] if pkg.ID != tt.wantID { @@ -849,6 +850,11 @@ func testInvalidFilesBeforeOverlayContains(t *testing.T, exporter packagestest.E } } +func isTestVariant(libID, testID string) bool { + variantID := fmt.Sprintf("%[1]s [%[1]s.test]", libID) + return variantID == testID +} + func TestInvalidXTestInGOPATH(t *testing.T) { packagestest.TestAll(t, testInvalidXTestInGOPATH) }