Fix issue with Windows pathes not being escaped when applying a regexp. (#1395)

This commit is contained in:
Roland Huß 2021-07-21 18:22:39 +02:00 committed by GitHub
parent 271d25fce7
commit 5a4496900b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 1 deletions

View File

@ -193,7 +193,7 @@ func printError(err error) {
// by checking a pattern like 'kn service' in the error message. If not found, return the
// base command name.
func extractCommandPathFromErrorMessage(errorMsg string, arg0 string) string {
extractPattern := regexp.MustCompile(fmt.Sprintf("'(%s\\s.+?)'", arg0))
extractPattern := regexp.MustCompile(fmt.Sprintf("'(%s\\s.+?)'", regexp.QuoteMeta(arg0)))
command := extractPattern.FindSubmatch([]byte(errorMsg))
if command != nil {
return string(command[1])

View File

@ -335,6 +335,15 @@ func TestRunWithExit(t *testing.T) {
}
}
func TestExtractCommandPathFromErrorMessage(t *testing.T) {
for _, d := range []struct{ arg0, errMsg, expected string }{
{"kn", "Invalid argument for 'kn service'", "kn service"},
{"C:\\Users\\hudson.DESKTOP-T61GB27\\Documents\\foo-with-revisions", "Invalid argument for 'C:\\Users\\hudson.DESKTOP-T61GB27\\Documents\\foo-with-revisions test'", "C:\\Users\\hudson.DESKTOP-T61GB27\\Documents\\foo-with-revisions test"},
} {
assert.Equal(t, extractCommandPathFromErrorMessage(d.errMsg, d.arg0), d.expected)
}
}
type internalPlugin struct {
executeError func() error
commandParts []string