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

add helper for inferring index name from input #506

Merged
merged 1 commit into from
Feb 21, 2020
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
12 changes: 12 additions & 0 deletions internal/pathutil/pathutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"strings"

"github.com/pkg/errors"

"sigs.k8s.io/krew/pkg/constants"
)

// IsSubPath checks if the extending path is an extension of the basePath, it will return the extending path
Expand All @@ -42,3 +44,13 @@ func ReplaceBase(path, old, replacement string) (string, error) {
}
return filepath.Join(replacement, extendingPath), nil
}

// CanonicalPluginName resolves a plugin's index and name from input string.
// If an index is not specified, the default index name is assumed.
func CanonicalPluginName(in string) (string, string) {
if strings.Count(in, "/") == 0 {
return constants.DefaultIndexName, in
}
p := strings.SplitN(in, "/", 2)
return p[0], p[1]
}
24 changes: 24 additions & 0 deletions internal/pathutil/pathutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,27 @@ func TestReplaceBase(t *testing.T) {
})
}
}

func TestCanonicalPluginName(t *testing.T) {
tests := []struct {
in string
wantIndex string
wantName string
}{
{"foo", "default", "foo"},
{"", "default", ""}, // despite unsupported
{"a/b", "a", "b"},
{"a/b/c", "a", "b/c"}, // despite unsupported
}
for _, tt := range tests {
t.Run(tt.in, func(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are not entirely consistent about how test cases and the inner *testing.T is called. In some places, the inner is tt *testing.T in others just t.

I personally would prefer to have consistency here an only use tt for the inner *testing.T. However, we can do that on the whole repo in a follow-up PR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opened #508 to clarify this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is mostly because we've used different IDEs over time (vscode-go and Goland).

Similarly you might notice I added a = after the ...%q, want = here. Most other unit-tests don't have the =. :)

gotIndex, gotName := CanonicalPluginName(tt.in)
if gotIndex != tt.wantIndex {
t.Errorf("CanonicalPluginName(%q) gotIndex = %q, want = %q", tt.in, gotIndex, tt.wantIndex)
}
if gotName != tt.wantName {
t.Errorf("CanonicalPluginName(%q) gotName = %q, want = %q", tt.in, gotName, tt.wantName)
}
})
}
}
2 changes: 2 additions & 0 deletions pkg/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ const (

// IndexURI points to the upstream index.
IndexURI = "https://github.com/kubernetes-sigs/krew-index.git"
// DefaultIndexName is a magic string that's used for a plugin name specified without an index.
DefaultIndexName = "default"
)