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

* fixing bug when looking up plugins on path with slash in argument

* test for dropping slash substrings

* use path separator

* more tests, using pathseparator in tests
This commit is contained in:
Paul Schweigert 2021-08-06 12:00:20 -04:00 committed by GitHub
parent 9d1bfc6197
commit 60c740f1e3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 53 additions and 0 deletions

View File

@ -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))

View File

@ -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