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

Fixing bug when looking up plugins on path with slash in argument #1415

Merged
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
5 changes: 5 additions & 0 deletions pkg/kn/plugin/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,11 @@ func findMostSpecificPluginInPath(dir string, parts []string, lookupInPath bool)
var nameParts []string
var commandParts []string
for _, p := range parts[0:i] {
// for arguments that contain the path separator,
// stop the loop once the separator appears
if strings.Contains(p, string(os.PathSeparator)) {
break
}
// Subcommands with "-" are translated to "_"
// (e.g. a command "kn log-all" is translated to a plugin "kn-log_all")
nameParts = append(nameParts, convertDashToUnderscore(p))
Expand Down
48 changes: 48 additions & 0 deletions pkg/kn/plugin/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,54 @@ func TestPluginList(t *testing.T) {
}
}

func TestNoSlashInPlugin(t *testing.T) {
ctx := setup(t)
defer cleanup(t, ctx)

// Prepare PATH
tmpPathDir, cleanupFunc := preparePathDirectory(t)
defer cleanupFunc()

var middleSlashPlugin string
var middleSlashArg string
ctx.pluginManager.lookupInPath = true

// Windows does not allow slashes in filenames, so testing for a slash
// in the middle of an argument will have different results depending on OS
if os.PathSeparator == '/' {
middleSlashPlugin = "kn-slash-test-with\\slash"
middleSlashArg = "with\\slash"
} else {
middleSlashPlugin = "kn-slash-test"
middleSlashArg = "with/slash"
}

createTestPluginInDirectory(t, "kn-slash-test", tmpPathDir)
createTestPluginInDirectory(t, middleSlashPlugin, tmpPathDir)

data := []struct {
parts []string
name string
}{
{[]string{"slash", "test", string(os.PathSeparator) + "withslash"}, "kn-slash-test"},
{[]string{"slash", "test", string(os.PathSeparator) + "withslash", "extraarg"}, "kn-slash-test"},
{[]string{"slash", "test", "with" + string(os.PathSeparator) + "slash", "extraarg"}, "kn-slash-test"},
{[]string{"slash", "test", middleSlashArg}, middleSlashPlugin},
}

for _, d := range data {
plugin, err := ctx.pluginManager.FindPlugin(d.parts)
assert.NilError(t, err)
assert.Assert(t, plugin != nil)
desc, err := plugin.Description()
name := plugin.Name()
assert.NilError(t, err)
assert.Assert(t, desc != "")
assert.Equal(t, plugin.Path(), filepath.Join(tmpPathDir, d.name))
assert.Assert(t, !strings.Contains(name, string(os.PathSeparator)))
}
}

// ====================================================================
// Private

Expand Down