mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-21 10:51:29 +00:00
Ensure that not-exist and pattern error return different results
This commit is contained in:
parent
f33ca23065
commit
cc85d27a9c
@ -259,7 +259,7 @@ func (b *Builder) FilenameParam(enforceNamespace bool, filenameOptions *Filename
|
|||||||
default:
|
default:
|
||||||
matches, err := expandIfFilePattern(s)
|
matches, err := expandIfFilePattern(s)
|
||||||
if err != nil {
|
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
|
continue
|
||||||
}
|
}
|
||||||
if !recursive && len(matches) == 1 {
|
if !recursive && len(matches) == 1 {
|
||||||
@ -1213,11 +1213,12 @@ func expandIfFilePattern(pattern string) ([]string, error) {
|
|||||||
if _, err := os.Stat(pattern); os.IsNotExist(err) {
|
if _, err := os.Stat(pattern); os.IsNotExist(err) {
|
||||||
matches, err := filepath.Glob(pattern)
|
matches, err := filepath.Glob(pattern)
|
||||||
if err == nil && len(matches) == 0 {
|
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 {
|
if err == filepath.ErrBadPattern {
|
||||||
return matches, err
|
return nil, fmt.Errorf("pattern %q is not valid: %v", pattern, err)
|
||||||
}
|
}
|
||||||
|
return matches, err
|
||||||
}
|
}
|
||||||
return []string{pattern}, nil
|
return []string{pattern}, nil
|
||||||
}
|
}
|
||||||
|
@ -638,7 +638,7 @@ func TestFilePatternBuilderWhenPatternYieldsNoResult(t *testing.T) {
|
|||||||
if err == nil {
|
if err == nil {
|
||||||
t.Fatalf("unexpected response: error is nil")
|
t.Fatalf("unexpected response: error is nil")
|
||||||
}
|
}
|
||||||
const expectedErrorMsg = "no match"
|
const expectedErrorMsg = "does not exist"
|
||||||
if !strings.Contains(err.Error(), expectedErrorMsg) {
|
if !strings.Contains(err.Error(), expectedErrorMsg) {
|
||||||
t.Fatalf("expected %s but got %s", expectedErrorMsg, err.Error())
|
t.Fatalf("expected %s but got %s", expectedErrorMsg, err.Error())
|
||||||
}
|
}
|
||||||
@ -678,7 +678,7 @@ func TestFilePatternBuilderWhenBadPatternUsesRawInput(t *testing.T) {
|
|||||||
if err == nil {
|
if err == nil {
|
||||||
t.Fatalf("unexpected response: error is 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) {
|
if !strings.Contains(err.Error(), expectedErrorMsg) {
|
||||||
t.Fatalf("expected %s but got %s", expectedErrorMsg, err.Error())
|
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) {
|
func setupKustomizeDirectory() (string, error) {
|
||||||
path, err := ioutil.TempDir("/tmp", "")
|
path, err := ioutil.TempDir("/tmp", "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user