1
15
16 package label
17
18 import (
19 "reflect"
20 "testing"
21 )
22
23 func TestLabelString(t *testing.T) {
24 for _, spec := range []struct {
25 l Label
26 want string
27 }{
28 {
29 l: Label{Name: "foo"},
30 want: "//:foo",
31 }, {
32 l: Label{Pkg: "foo/bar", Name: "baz"},
33 want: "//foo/bar:baz",
34 }, {
35 l: Label{Pkg: "foo/bar", Name: "bar"},
36 want: "//foo/bar",
37 }, {
38 l: Label{Repo: "com_example_repo", Pkg: "foo/bar", Name: "baz"},
39 want: "@com_example_repo//foo/bar:baz",
40 }, {
41 l: Label{Repo: "com_example_repo", Pkg: "foo/bar", Name: "bar"},
42 want: "@com_example_repo//foo/bar",
43 }, {
44 l: Label{Relative: true, Name: "foo"},
45 want: ":foo",
46 }, {
47 l: Label{Repo: "@", Pkg: "foo/bar", Name: "baz"},
48 want: "@//foo/bar:baz",
49 },
50 } {
51 if got, want := spec.l.String(), spec.want; got != want {
52 t.Errorf("%#v.String() = %q; want %q", spec.l, got, want)
53 }
54 }
55 }
56
57 func TestParse(t *testing.T) {
58 for _, tc := range []struct {
59 str string
60 want Label
61 wantErr bool
62 }{
63 {str: "", wantErr: true},
64 {str: "@//:", wantErr: true},
65 {str: "@a:b", wantErr: true},
66 {str: "@a//", wantErr: true},
67 {str: "@//:a", want: Label{Repo: "@", Name: "a", Relative: false}},
68 {str: "@//a:b", want: Label{Repo: "@", Pkg: "a", Name: "b"}},
69 {str: ":a", want: Label{Name: "a", Relative: true}},
70 {str: "a", want: Label{Name: "a", Relative: true}},
71 {str: "//:a", want: Label{Name: "a", Relative: false}},
72 {str: "//a", want: Label{Pkg: "a", Name: "a"}},
73 {str: "//a/b", want: Label{Pkg: "a/b", Name: "b"}},
74 {str: "//a:b", want: Label{Pkg: "a", Name: "b"}},
75 {str: "@a", want: Label{Repo: "a", Pkg: "", Name: "a"}},
76 {str: "@a//b", want: Label{Repo: "a", Pkg: "b", Name: "b"}},
77 {str: "@a//b:c", want: Label{Repo: "a", Pkg: "b", Name: "c"}},
78 {str: "@a//@b:c", want: Label{Repo: "a", Pkg: "@b", Name: "c"}},
79 {str: "@..//b:c", want: Label{Repo: "..", Pkg: "b", Name: "c"}},
80 {str: "@--//b:c", want: Label{Repo: "--", Pkg: "b", Name: "c"}},
81 {str: "//api_proto:api.gen.pb.go_checkshtest", want: Label{Pkg: "api_proto", Name: "api.gen.pb.go_checkshtest"}},
82 {str: "@go_sdk//:src/cmd/go/testdata/mod/rsc.io_!q!u!o!t!e_v1.5.2.txt", want: Label{Repo: "go_sdk", Name: "src/cmd/go/testdata/mod/rsc.io_!q!u!o!t!e_v1.5.2.txt"}},
83 {str: "//:a][b", want: Label{Name: "a][b"}},
84 {str: "//:a b", want: Label{Name: "a b"}},
85 {str: "//some/pkg/[someId]:someId", want: Label{Pkg: "some/pkg/[someId]", Name: "someId"}},
86 {str: "//some/pkg/[someId]:[someId]", want: Label{Pkg: "some/pkg/[someId]", Name: "[someId]"}},
87 {str: "@a//some/pkg/[someId]:[someId]", want: Label{Repo: "a", Pkg: "some/pkg/[someId]", Name: "[someId]"}},
88 {str: "@rules_python~0.0.0~pip~name_dep//:_pkg", want: Label{Repo: "rules_python~0.0.0~pip~name_dep", Name: "_pkg"}},
89 {str: "@rules_python~0.0.0~pip~name//:dep_pkg", want: Label{Repo: "rules_python~0.0.0~pip~name", Name: "dep_pkg"}},
90 {str: "@@rules_python~0.26.0~python~python_3_10_x86_64-unknown-linux-gnu//:python_runtimes", want: Label{Repo: "rules_python~0.26.0~python~python_3_10_x86_64-unknown-linux-gnu", Name: "python_runtimes"}},
91 } {
92 got, err := Parse(tc.str)
93 if err != nil && !tc.wantErr {
94 t.Errorf("for string %q: got error %s ; want success", tc.str, err)
95 continue
96 }
97 if err == nil && tc.wantErr {
98 t.Errorf("for string %q: got label %s ; want error", tc.str, got)
99 continue
100 }
101 if !reflect.DeepEqual(got, tc.want) {
102 t.Errorf("for string %q: got %s ; want %s", tc.str, got, tc.want)
103 }
104 }
105 }
106
107 func TestImportPathToBazelRepoName(t *testing.T) {
108 for path, want := range map[string]string{
109 "git.sr.ht/~urandom/errors": "ht_sr_git_urandom_errors",
110 "golang.org/x/mod": "org_golang_x_mod",
111 } {
112 if got := ImportPathToBazelRepoName(path); got != want {
113 t.Errorf(`ImportPathToBazelRepoName(%q) = %q; want %q`, path, got, want)
114 }
115 }
116 }
117
View as plain text