Skip to content

Commit

Permalink
fixup tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ellie-idb committed Nov 28, 2024
1 parent b568367 commit c3b2c24
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 43 deletions.
7 changes: 7 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
48 changes: 6 additions & 42 deletions go/tools/gopackagesdriver/gopackagesdriver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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")

Expand Down Expand Up @@ -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
Expand All @@ -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
}
})
}

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)
}
Expand Down
4 changes: 3 additions & 1 deletion go/tools/gopackagesdriver/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"os/signal"
"path"
"path/filepath"

"golang.org/x/tools/go/packages"
)

func getenvDefault(key, defaultValue string) string {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit c3b2c24

Please sign in to comment.