Ensure that not-exist and pattern error return different results

This commit is contained in:
Maciej Szulik 2022-04-14 19:00:56 +02:00
parent f33ca23065
commit cc85d27a9c
No known key found for this signature in database
GPG Key ID: F15E55D276FA84C4
2 changed files with 38 additions and 6 deletions

View File

@ -259,7 +259,7 @@ func (b *Builder) FilenameParam(enforceNamespace bool, filenameOptions *Filename
default:
matches, err := expandIfFilePattern(s)
if err != nil {
b.errs = append(b.errs, fmt.Errorf("pattern %q is not valid: %v", s, err))
b.errs = append(b.errs, err)
continue
}
if !recursive && len(matches) == 1 {
@ -1213,12 +1213,13 @@ func expandIfFilePattern(pattern string) ([]string, error) {
if _, err := os.Stat(pattern); os.IsNotExist(err) {
matches, err := filepath.Glob(pattern)
if err == nil && len(matches) == 0 {
return nil, fmt.Errorf("no match")
return nil, fmt.Errorf("the path %q does not exist", pattern)
}
if err == filepath.ErrBadPattern {
return nil, fmt.Errorf("pattern %q is not valid: %v", pattern, err)
}
if err == nil || err != filepath.ErrBadPattern {
return matches, err
}
}
return []string{pattern}, nil
}

View File

@ -638,7 +638,7 @@ func TestFilePatternBuilderWhenPatternYieldsNoResult(t *testing.T) {
if err == nil {
t.Fatalf("unexpected response: error is nil")
}
const expectedErrorMsg = "no match"
const expectedErrorMsg = "does not exist"
if !strings.Contains(err.Error(), expectedErrorMsg) {
t.Fatalf("expected %s but got %s", expectedErrorMsg, err.Error())
}
@ -678,7 +678,7 @@ func TestFilePatternBuilderWhenBadPatternUsesRawInput(t *testing.T) {
if err == nil {
t.Fatalf("unexpected response: error is nil")
}
const expectedErrorMsg = "does not exist"
const expectedErrorMsg = "syntax error in pattern"
if !strings.Contains(err.Error(), expectedErrorMsg) {
t.Fatalf("expected %s but got %s", expectedErrorMsg, err.Error())
}
@ -707,6 +707,37 @@ func TestFilePatternBuilder(t *testing.T) {
}
}
func TestErrorFilePatternBuilder(t *testing.T) {
testCases := map[string]struct {
input []string
expectedErr string
}{
"invalid pattern": {
input: []string{"[a-z*.yaml"},
expectedErr: "syntax error in pattern",
},
"file does not exist": {
input: []string{"../../artifacts/guestbook/notexist.yaml"},
expectedErr: "does not exist",
},
}
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
b := newDefaultBuilder().
FilenameParam(false, &FilenameOptions{Recursive: false, Filenames: tc.input}).
NamespaceParam("test").DefaultNamespace()
test := &testVisitor{}
singleItemImplied := false
err := b.Do().IntoSingleItemImplied(&singleItemImplied).Visit(test.Handle)
if err == nil || len(test.Infos) != 0 || !strings.Contains(err.Error(), tc.expectedErr) {
t.Fatalf("unexpected response: %v %#v", err, test.Infos)
}
})
}
}
func setupKustomizeDirectory() (string, error) {
path, err := ioutil.TempDir("/tmp", "")
if err != nil {