From cc85d27a9c07a92e9d013d8972771e77e7855933 Mon Sep 17 00:00:00 2001 From: Maciej Szulik Date: Thu, 14 Apr 2022 19:00:56 +0200 Subject: [PATCH] Ensure that not-exist and pattern error return different results --- .../cli-runtime/pkg/resource/builder.go | 9 ++--- .../cli-runtime/pkg/resource/builder_test.go | 35 +++++++++++++++++-- 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/staging/src/k8s.io/cli-runtime/pkg/resource/builder.go b/staging/src/k8s.io/cli-runtime/pkg/resource/builder.go index 153030e9b7f83..4f4b3143abf55 100644 --- a/staging/src/k8s.io/cli-runtime/pkg/resource/builder.go +++ b/staging/src/k8s.io/cli-runtime/pkg/resource/builder.go @@ -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,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 } diff --git a/staging/src/k8s.io/cli-runtime/pkg/resource/builder_test.go b/staging/src/k8s.io/cli-runtime/pkg/resource/builder_test.go index 69f3b726cdab3..f44ecb3195911 100644 --- a/staging/src/k8s.io/cli-runtime/pkg/resource/builder_test.go +++ b/staging/src/k8s.io/cli-runtime/pkg/resource/builder_test.go @@ -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 {