Merge pull request #109504 from soltysh/clean_builder

Move path error to const and squash tests
This commit is contained in:
Kubernetes Prow Robot 2022-05-04 01:28:25 -07:00 committed by GitHub
commit 1480fc3945
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 56 deletions

View File

@ -45,7 +45,8 @@ import (
var FileExtensions = []string{".json", ".yaml", ".yml"}
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
// 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 {
_, err := os.Stat(p)
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
}
if err != nil {
@ -1213,7 +1214,7 @@ 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("the path %q does not exist", pattern)
return nil, fmt.Errorf(pathNotExistError, pattern)
}
if err == filepath.ErrBadPattern {
return nil, fmt.Errorf("pattern %q is not valid: %v", pattern, err)

View File

@ -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) {
const pathPattern = "../../artifacts/oddly-named-file[x].yaml"
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) {
b := newDefaultBuilder().
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) {
testCases := map[string]struct {
input []string
expectedErr string
input string
expectedErr string
inputInError bool
}{
"invalid pattern": {
input: []string{"[a-z*.yaml"},
expectedErr: "syntax error in pattern",
input: "[a-z*.yaml",
expectedErr: "syntax error in pattern",
inputInError: true,
},
"file does not exist": {
input: []string{"../../artifacts/guestbook/notexist.yaml"},
expectedErr: "does not exist",
input: "../../artifacts/guestbook/notexist.yaml",
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 {
t.Run(name, func(t *testing.T) {
b := newDefaultBuilder().
FilenameParam(false, &FilenameOptions{Recursive: false, Filenames: tc.input}).
FilenameParam(false, &FilenameOptions{Recursive: false, Filenames: []string{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)
if err == nil || len(test.Infos) != 0 || !strings.Contains(err.Error(), tc.expectedErr) ||
(tc.inputInError && !strings.Contains(err.Error(), tc.input)) {
t.Errorf("unexpected response: %v %#v", err, test.Infos)
}
})
}