Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
jayconrod committed Feb 11, 2024
1 parent 0373b5f commit f359b9a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 27 deletions.
18 changes: 13 additions & 5 deletions go/tools/gopackagesdriver/bazel.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,20 @@ func (b *Bazel) Query(ctx context.Context, args ...string) ([]string, error) {
return nil, fmt.Errorf("bazel query failed: %w", err)
}

trimmedOutput := strings.TrimSpace(output)
if len(trimmedOutput) == 0 {
return nil, nil
// We need to trim each line. Bazel prints '\r\n' newlines on Windows but
// does not accept them with --target_pattern_file.
lines := strings.Split(output, "\n")
r, w := 0, 0
for ; r < len(lines); r++ {
line := strings.TrimSpace(lines[r])
if line == "" {
continue
}
lines[w] = line
w++
}

return strings.Split(trimmedOutput, "\n"), nil
lines = lines[:w]
return lines, nil
}

func (b *Bazel) WorkspaceRoot() string {
Expand Down
27 changes: 5 additions & 22 deletions go/tools/gopackagesdriver/gopackagesdriver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,35 +181,18 @@ func TestExternalTests(t *testing.T) {
// TestIncompatible checks that a target that can be queried but not analyzed
// does not appear in .Roots.
func TestIncompatible(t *testing.T) {
reader := strings.NewReader("{}")
out, _, err := bazel_testing.BazelOutputWithInput(reader, "run", "@io_bazel_rules_go//go/tools/gopackagesdriver", "--", "./...")
if err != nil {
t.Fatalf("error running bazel: %v", err)
}
var resp response
if err := json.Unmarshal(out, &resp); err != nil {
t.Fatalf("unmarshaling response: %v", err)
}

resp := runForTest(t, "./...")
rootLabels := make(map[string]bool)
for _, root := range resp.Roots {
root = strings.ReplaceAll(root, "@", "")
rootLabels[root] = true
}

// Verify //:hello is in .Roots and check whether its label starts with
// "@@" (bzlmod) or "@" (not bzlmod).
var incompatibleLabel string
if rootLabels["@@//:hello"] {
incompatibleLabel = "@@//:incompatible"
} else if rootLabels["@//:hello"] {
incompatibleLabel = "@//:incompatible"
} else {
if !rootLabels["//:hello"] {
t.Fatalf("response does not contain //:hello; roots were %s", strings.Join(resp.Roots, ", "))
}

// Verify //:incompatible is NOT in .Roots.
if rootLabels[incompatibleLabel] {
t.Fatalf("response contains root %s", incompatibleLabel)
if rootLabels["//:incompatible"] {
t.Fatalf("response contains //:incompatible")
}
}

Expand Down
3 changes: 3 additions & 0 deletions go/tools/gopackagesdriver/packageregistry.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ func NewPackageRegistry(bazelVersion bazelVersion, pkgs ...*FlatPackage) *Packag

func (pr *PackageRegistry) Add(pkgs ...*FlatPackage) *PackageRegistry {
for _, pkg := range pkgs {
if !strings.Contains(pkg.ID, "stdlib") {
fmt.Fprintf(os.Stderr, "!! pr.Add: pkg.ID %s\n", pkg.ID)
}
pr.packagesByID[pkg.ID] = pkg

if pkg.IsStdlib() {
Expand Down

0 comments on commit f359b9a

Please sign in to comment.