Skip to content

Commit

Permalink
Ensure that not-exist and pattern error return different results
Browse files Browse the repository at this point in the history
  • Loading branch information
soltysh committed Apr 14, 2022
1 parent f33ca23 commit cc85d27
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
9 changes: 5 additions & 4 deletions staging/src/k8s.io/cli-runtime/pkg/resource/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -1213,11 +1213,12 @@ 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 == nil || err != filepath.ErrBadPattern {
return matches, err
if err == filepath.ErrBadPattern {
return nil, fmt.Errorf("pattern %q is not valid: %v", pattern, err)
}
return matches, err
}
return []string{pattern}, nil
}
Expand Down
35 changes: 33 additions & 2 deletions staging/src/k8s.io/cli-runtime/pkg/resource/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down Expand Up @@ -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())
}
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit cc85d27

Please sign in to comment.