Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Have label properly resolve directives starting with "@//" #1086

Merged
merged 1 commit into from
Jul 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions label/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func New(repo, pkg, name string) Label {
var NoLabel = Label{}

var (
labelRepoRegexp = regexp.MustCompile(`^$|^[A-Za-z][A-Za-z0-9_]*$`)
labelRepoRegexp = regexp.MustCompile(`^@$|^[A-Za-z][A-Za-z0-9_]*$`)
labelPkgRegexp = regexp.MustCompile(`^[A-Za-z0-9/._-]*$`)
labelNameRegexp = regexp.MustCompile(`^[A-Za-z0-9_/.+=,@~-]*$`)
)
Expand All @@ -77,9 +77,14 @@ func Parse(s string) (Label, error) {
if strings.HasPrefix(s, "@") {
relative = false
endRepo := strings.Index(s, "//")
if endRepo > 0 {
if endRepo > len("@") {
repo = s[len("@"):endRepo]
s = s[endRepo:]
// If the label begins with "@//...", set repo = "@"
// to remain distinct from "//...", where repo = ""
} else if endRepo == len("@") {
tyler-french marked this conversation as resolved.
Show resolved Hide resolved
repo = s[:len("@")]
s = s[len("@"):]
} else {
repo = s[len("@"):]
s = "//:" + repo
Expand Down Expand Up @@ -134,8 +139,12 @@ func (l Label) String() string {
}

var repo string
if l.Repo != "" {
if l.Repo != "" && l.Repo != "@" {
repo = fmt.Sprintf("@%s", l.Repo)
} else {
// if l.Repo == "", the label string will begin with "//"
// if l.Repo == "@", the label string will begin with "@//"
repo = l.Repo
tyler-french marked this conversation as resolved.
Show resolved Hide resolved
}

if path.Base(l.Pkg) == l.Name {
Expand Down
7 changes: 5 additions & 2 deletions label/label_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ func TestLabelString(t *testing.T) {
}, {
l: Label{Relative: true, Name: "foo"},
want: ":foo",
}, {
l: Label{Repo: "@", Pkg: "foo/bar", Name: "baz"},
want: "@//foo/bar:baz",
},
} {
if got, want := spec.l.String(), spec.want; got != want {
Expand All @@ -61,8 +64,8 @@ func TestParse(t *testing.T) {
{str: "@//:", wantErr: true},
{str: "@a:b", wantErr: true},
{str: "@a//", wantErr: true},
{str: "@//:a", want: Label{Name: "a", Relative: false}},
{str: "@//a:b", want: Label{Repo: "", Pkg: "a", Name: "b"}},
{str: "@//:a", want: Label{Repo: "@", Name: "a", Relative: false}},
{str: "@//a:b", want: Label{Repo: "@", Pkg: "a", Name: "b"}},
{str: ":a", want: Label{Name: "a", Relative: true}},
{str: "a", want: Label{Name: "a", Relative: true}},
{str: "//:a", want: Label{Name: "a", Relative: false}},
Expand Down