mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 19:56:01 +00:00
Move path error to const and squash tests
This commit is contained in:
parent
e207aabf04
commit
920fb984ef
@ -45,7 +45,8 @@ import (
|
|||||||
var FileExtensions = []string{".json", ".yaml", ".yml"}
|
var FileExtensions = []string{".json", ".yaml", ".yml"}
|
||||||
var InputExtensions = append(FileExtensions, "stdin")
|
var InputExtensions = append(FileExtensions, "stdin")
|
||||||
|
|
||||||
const defaultHttpGetAttempts int = 3
|
const defaultHttpGetAttempts = 3
|
||||||
|
const pathNotExistError = "the path %q does not exist"
|
||||||
|
|
||||||
// Builder provides convenience functions for taking arguments and parameters
|
// Builder provides convenience functions for taking arguments and parameters
|
||||||
// from the command line and converting them to a list of resources to iterate
|
// from the command line and converting them to a list of resources to iterate
|
||||||
@ -416,7 +417,7 @@ func (b *Builder) Path(recursive bool, paths ...string) *Builder {
|
|||||||
for _, p := range paths {
|
for _, p := range paths {
|
||||||
_, err := os.Stat(p)
|
_, err := os.Stat(p)
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
b.errs = append(b.errs, fmt.Errorf("the path %q does not exist", p))
|
b.errs = append(b.errs, fmt.Errorf(pathNotExistError, p))
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -1213,7 +1214,7 @@ 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("the path %q does not exist", pattern)
|
return nil, fmt.Errorf(pathNotExistError, pattern)
|
||||||
}
|
}
|
||||||
if err == filepath.ErrBadPattern {
|
if err == filepath.ErrBadPattern {
|
||||||
return nil, fmt.Errorf("pattern %q is not valid: %v", pattern, err)
|
return nil, fmt.Errorf("pattern %q is not valid: %v", pattern, err)
|
||||||
|
@ -625,28 +625,6 @@ func TestDirectoryBuilder(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFilePatternBuilderWhenPatternYieldsNoResult(t *testing.T) {
|
|
||||||
const pathPattern = "../../artifacts/_does_not_exist_/*.yaml"
|
|
||||||
b := newDefaultBuilder().
|
|
||||||
FilenameParam(false, &FilenameOptions{Recursive: false, Filenames: []string{pathPattern}}).
|
|
||||||
NamespaceParam("test").DefaultNamespace()
|
|
||||||
|
|
||||||
test := &testVisitor{}
|
|
||||||
singleItemImplied := false
|
|
||||||
|
|
||||||
err := b.Do().IntoSingleItemImplied(&singleItemImplied).Visit(test.Handle)
|
|
||||||
if err == nil {
|
|
||||||
t.Fatalf("unexpected response: error is nil")
|
|
||||||
}
|
|
||||||
const expectedErrorMsg = "does not exist"
|
|
||||||
if !strings.Contains(err.Error(), expectedErrorMsg) {
|
|
||||||
t.Fatalf("expected %s but got %s", expectedErrorMsg, err.Error())
|
|
||||||
}
|
|
||||||
if !strings.Contains(err.Error(), pathPattern) {
|
|
||||||
t.Fatalf("expected %s but got %s", pathPattern, err.Error())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestFilePatternBuilderWhenFileLiteralExists(t *testing.T) {
|
func TestFilePatternBuilderWhenFileLiteralExists(t *testing.T) {
|
||||||
const pathPattern = "../../artifacts/oddly-named-file[x].yaml"
|
const pathPattern = "../../artifacts/oddly-named-file[x].yaml"
|
||||||
b := newDefaultBuilder().
|
b := newDefaultBuilder().
|
||||||
@ -665,28 +643,6 @@ func TestFilePatternBuilderWhenFileLiteralExists(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFilePatternBuilderWhenBadPatternUsesRawInput(t *testing.T) {
|
|
||||||
const pathPattern = "../../artifacts/[a-z*.yaml" // missing closing bracket, "[a-z]*.yaml"
|
|
||||||
b := newDefaultBuilder().
|
|
||||||
FilenameParam(false, &FilenameOptions{Recursive: false, Filenames: []string{pathPattern}}).
|
|
||||||
NamespaceParam("test").DefaultNamespace()
|
|
||||||
|
|
||||||
test := &testVisitor{}
|
|
||||||
singleItemImplied := false
|
|
||||||
|
|
||||||
err := b.Do().IntoSingleItemImplied(&singleItemImplied).Visit(test.Handle)
|
|
||||||
if err == nil {
|
|
||||||
t.Fatalf("unexpected response: error is nil")
|
|
||||||
}
|
|
||||||
const expectedErrorMsg = "syntax error in pattern"
|
|
||||||
if !strings.Contains(err.Error(), expectedErrorMsg) {
|
|
||||||
t.Fatalf("expected %s but got %s", expectedErrorMsg, err.Error())
|
|
||||||
}
|
|
||||||
if !strings.Contains(err.Error(), pathPattern) {
|
|
||||||
t.Fatalf("expected %s but got %s", pathPattern, err.Error())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestFilePatternBuilder(t *testing.T) {
|
func TestFilePatternBuilder(t *testing.T) {
|
||||||
b := newDefaultBuilder().
|
b := newDefaultBuilder().
|
||||||
FilenameParam(false, &FilenameOptions{Recursive: false, Filenames: []string{"../../artifacts/guestbook/redis-*.yaml"}}).
|
FilenameParam(false, &FilenameOptions{Recursive: false, Filenames: []string{"../../artifacts/guestbook/redis-*.yaml"}}).
|
||||||
@ -709,30 +665,39 @@ func TestFilePatternBuilder(t *testing.T) {
|
|||||||
|
|
||||||
func TestErrorFilePatternBuilder(t *testing.T) {
|
func TestErrorFilePatternBuilder(t *testing.T) {
|
||||||
testCases := map[string]struct {
|
testCases := map[string]struct {
|
||||||
input []string
|
input string
|
||||||
expectedErr string
|
expectedErr string
|
||||||
|
inputInError bool
|
||||||
}{
|
}{
|
||||||
"invalid pattern": {
|
"invalid pattern": {
|
||||||
input: []string{"[a-z*.yaml"},
|
input: "[a-z*.yaml",
|
||||||
expectedErr: "syntax error in pattern",
|
expectedErr: "syntax error in pattern",
|
||||||
|
inputInError: true,
|
||||||
},
|
},
|
||||||
"file does not exist": {
|
"file does not exist": {
|
||||||
input: []string{"../../artifacts/guestbook/notexist.yaml"},
|
input: "../../artifacts/guestbook/notexist.yaml",
|
||||||
expectedErr: "does not exist",
|
expectedErr: "does not exist",
|
||||||
|
inputInError: true,
|
||||||
|
},
|
||||||
|
"directory does not exist and valid glob": {
|
||||||
|
input: "../../artifacts/_does_not_exist_/*.yaml",
|
||||||
|
expectedErr: "does not exist",
|
||||||
|
inputInError: true,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
for name, tc := range testCases {
|
for name, tc := range testCases {
|
||||||
t.Run(name, func(t *testing.T) {
|
t.Run(name, func(t *testing.T) {
|
||||||
b := newDefaultBuilder().
|
b := newDefaultBuilder().
|
||||||
FilenameParam(false, &FilenameOptions{Recursive: false, Filenames: tc.input}).
|
FilenameParam(false, &FilenameOptions{Recursive: false, Filenames: []string{tc.input}}).
|
||||||
NamespaceParam("test").DefaultNamespace()
|
NamespaceParam("test").DefaultNamespace()
|
||||||
|
|
||||||
test := &testVisitor{}
|
test := &testVisitor{}
|
||||||
singleItemImplied := false
|
singleItemImplied := false
|
||||||
|
|
||||||
err := b.Do().IntoSingleItemImplied(&singleItemImplied).Visit(test.Handle)
|
err := b.Do().IntoSingleItemImplied(&singleItemImplied).Visit(test.Handle)
|
||||||
if err == nil || len(test.Infos) != 0 || !strings.Contains(err.Error(), tc.expectedErr) {
|
if err == nil || len(test.Infos) != 0 || !strings.Contains(err.Error(), tc.expectedErr) ||
|
||||||
t.Fatalf("unexpected response: %v %#v", err, test.Infos)
|
(tc.inputInError && !strings.Contains(err.Error(), tc.input)) {
|
||||||
|
t.Errorf("unexpected response: %v %#v", err, test.Infos)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user