Skip to content

Commit

Permalink
Add test and fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
fmeum committed May 28, 2024
1 parent 540c4b2 commit 1594b11
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 4 deletions.
2 changes: 2 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ load("//:def.bzl", "gazelle", "gazelle_binary")
# gazelle:exclude .bazelci
# gazelle:exclude .bcr
# gazelle:exclude .idea
# gazelle:exclude .ijwb
# gazelle:exclude .github
# gazelle:exclude .vscode
# gazelle:exclude internal/module/testdata
# gazelle:go_naming_convention import_alias
gazelle(
name = "gazelle",
Expand Down
14 changes: 13 additions & 1 deletion internal/module/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")

go_library(
name = "module",
Expand All @@ -17,6 +17,7 @@ filegroup(
srcs = [
"BUILD.bazel",
"module.go",
"module_test.go",
],
visibility = ["//visibility:public"],
)
Expand All @@ -26,3 +27,14 @@ alias(
actual = ":module",
visibility = ["//:__subpackages__"],
)

go_test(
name = "module_test",
srcs = ["module_test.go"],
data = glob(["testdata/**"]),
embed = [":module"],
deps = [
"@com_github_google_go_cmp//cmp",
"@io_bazel_rules_go//go/runfiles:go_default_library",
],
)
5 changes: 2 additions & 3 deletions internal/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ func collectApparentNames(repoRoot, relPath string) (map[string]string, error) {
apparentNames[name] = apparentName
}
for _, includeLabel := range includeLabels {
var l label.Label
l, err = label.Parse(includeLabel)
l, err := label.Parse(includeLabel)
if err != nil {
return nil, fmt.Errorf("failed to parse include label %q: %v", includeLabel, err)
}
Expand All @@ -97,7 +96,7 @@ func collectApparentNamesAndIncludes(f *build.File) (map[string]string, []string
var includeLabels []string

for _, dep := range f.Rules("") {
if dep.Name() == "" {
if dep.ExplicitName() == "" {
if ident, ok := dep.Call.X.(*build.Ident); !ok || ident.Name != "include" {
continue
}
Expand Down
44 changes: 44 additions & 0 deletions internal/module/module_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package module

import (
"path/filepath"
"testing"

"github.com/bazelbuild/rules_go/go/runfiles"
"github.com/google/go-cmp/cmp"
)

func TestCollectApparent(t *testing.T) {
moduleFile, err := runfiles.Rlocation("bazel_gazelle/internal/module/testdata/MODULE.bazel")
if err != nil {
t.Fatal(err)
}

apparentNames, err := collectApparentNames(filepath.Dir(moduleFile), "MODULE.bazel")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}

expected := map[string]string{
"rules_bar": "rules_bar",
"rules_baz": "rules_baz",
"rules_foo": "my_rules_foo",
"rules_lang": "my_rules_lang",
"rules_quz": "rules_quz",
"test_module": "my_test_module",
}
if diff := cmp.Diff(expected, apparentNames); diff != "" {
t.Errorf("unexpected apparent names (-want +got):\n%s", diff)
}
}

func TestCollectApparent_fileDoesNotExist(t *testing.T) {
_, err := collectApparentNames(t.TempDir(), "MODULE.bazel")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
_, err = collectApparentNames(t.TempDir(), "segment.MODULE.bazel")
if err == nil {
t.Fatalf("expected error, got nil")
}
}
10 changes: 10 additions & 0 deletions internal/module/testdata/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module(
name = "test_module",
repo_name = "my_test_module",
)

bazel_dep(name = "rules_bar", version = "1.2.3")

include("//bazel:lang.MODULE.bazel")

bazel_dep(name = "rules_foo", version = "1.2.3", repo_name = "my_rules_foo")
1 change: 1 addition & 0 deletions internal/module/testdata/bazel/dir/deps.MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bazel_dep(name = "rules_quz", version = "0.0.1")
4 changes: 4 additions & 0 deletions internal/module/testdata/bazel/lang.MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
bazel_dep(name = "rules_lang", version = "1.2.3", repo_name = "my_rules_lang")

include("//:deps.MODULE.bazel")
include("//bazel:dir/deps.MODULE.bazel")
1 change: 1 addition & 0 deletions internal/module/testdata/deps.MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bazel_dep(name = "rules_baz", version = "0.0.1")

0 comments on commit 1594b11

Please sign in to comment.