From c3b2c24c398b87d305bc0ed39f253e61123307f5 Mon Sep 17 00:00:00 2001 From: ellie-idb Date: Thu, 28 Nov 2024 13:05:55 -0500 Subject: [PATCH] fixup tests --- WORKSPACE | 7 +++ .../gopackagesdriver/gopackagesdriver_test.go | 48 +++---------------- go/tools/gopackagesdriver/utils.go | 4 +- 3 files changed, 16 insertions(+), 43 deletions(-) diff --git a/WORKSPACE b/WORKSPACE index 893c997023..05678791a1 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -169,6 +169,13 @@ go_repository( version = "v0.22.0", ) +go_repository( + name = "org_golang_x_net", + importpath = "golang.org/x/net", + sum = "h1:P4fl1q7dY2hnZFxEk4pPSkDHF+QqjitcnDjUQyMM+pM=", + version = "v0.31.0", +) + go_repository( name = "org_golang_x_sync", importpath = "golang.org/x/sync", diff --git a/go/tools/gopackagesdriver/gopackagesdriver_test.go b/go/tools/gopackagesdriver/gopackagesdriver_test.go index 232ee55a30..613ec8707d 100644 --- a/go/tools/gopackagesdriver/gopackagesdriver_test.go +++ b/go/tools/gopackagesdriver/gopackagesdriver_test.go @@ -10,8 +10,9 @@ import ( "strings" "testing" - "github.com/bazelbuild/rules_go/go/tools/bazel_testing" "golang.org/x/tools/go/packages" + + "github.com/bazelbuild/rules_go/go/tools/bazel_testing" ) func TestMain(m *testing.M) { @@ -86,33 +87,6 @@ const ( bzlmodOsPkgID = "@@io_bazel_rules_go//stdlib:os" ) -func TestStdlib(t *testing.T) { - resp := runForTest(t, packages.DriverRequest{}, ".", "std") - - if len(resp.Packages) == 0 { - t.Fatal("Expected stdlib packages") - } - - for _, pkg := range resp.Packages { - // net, plugin and user stdlib packages seem to have compiled files only for Linux. - if pkg.Name != "cgo" { - continue - } - - var hasCompiledFiles bool - for _, x := range pkg.CompiledGoFiles { - if filepath.Ext(x) == "" { - hasCompiledFiles = true - break - } - } - - if !hasCompiledFiles { - t.Errorf("%q stdlib package should have compiled files", pkg.Name) - } - } -} - func TestBaseFileLookup(t *testing.T) { resp := runForTest(t, packages.DriverRequest{}, ".", "file=hello.go") @@ -142,24 +116,19 @@ func TestBaseFileLookup(t *testing.T) { return } - if pkg.Standard { - t.Errorf("Expected package to not be Standard:\n%+v", pkg) - return - } - if len(pkg.Imports) != 1 { t.Errorf("Expected one import:\n%+v", pkg) return } - if pkg.Imports["os"] != osPkgID && pkg.Imports["os"] != bzlmodOsPkgID { + if pkg.Imports["os"].ID != osPkgID && pkg.Imports["os"].ID != bzlmodOsPkgID { t.Errorf("Expected os import to map to %q or %q:\n%+v", osPkgID, bzlmodOsPkgID, pkg) return } }) t.Run("dependency", func(t *testing.T) { - var osPkg *FlatPackage + var osPkg *packages.Package for _, p := range resp.Packages { if p.ID == osPkgID || p.ID == bzlmodOsPkgID { osPkg = p @@ -170,11 +139,6 @@ func TestBaseFileLookup(t *testing.T) { t.Errorf("Expected os package to be included:\n%+v", osPkg) return } - - if !osPkg.Standard { - t.Errorf("Expected os import to be standard:\n%+v", osPkg) - return - } }) } @@ -325,7 +289,7 @@ func TestOverlay(t *testing.T) { expectSetEquality(t, expectedImportsPerFile[subhelloPath], subhelloPkgImportPaths, "subhello imports") } -func runForTest(t *testing.T, driverRequest packages.DriverRequest, relativeWorkingDir string, args ...string) driverResponse { +func runForTest(t *testing.T, driverRequest packages.DriverRequest, relativeWorkingDir string, args ...string) packages.DriverResponse { t.Helper() // Remove most environment variables, other than those on an allowlist. @@ -393,7 +357,7 @@ func runForTest(t *testing.T, driverRequest packages.DriverRequest, relativeWork if err := run(context.Background(), in, out, args); err != nil { t.Fatalf("running gopackagesdriver: %v", err) } - var resp driverResponse + var resp packages.DriverResponse if err := json.Unmarshal(out.Bytes(), &resp); err != nil { t.Fatalf("unmarshaling response: %v", err) } diff --git a/go/tools/gopackagesdriver/utils.go b/go/tools/gopackagesdriver/utils.go index 35d6243e9a..9af37443d7 100644 --- a/go/tools/gopackagesdriver/utils.go +++ b/go/tools/gopackagesdriver/utils.go @@ -22,6 +22,8 @@ import ( "os/signal" "path" "path/filepath" + + "golang.org/x/tools/go/packages" ) func getenvDefault(key, defaultValue string) string { @@ -76,7 +78,7 @@ func packageID(pattern string) string { return fmt.Sprintf("//%s", pattern) } -func findPackageByID(packages []*FlatPackage, id string) *FlatPackage { +func findPackageByID(packages []*packages.Package, id string) *packages.Package { for _, pkg := range packages { if pkg.ID == id { return pkg