diff --git a/approvals/kir_test.TestCLI.MissingFile.exitcode.approved.txt b/approvals/kir_test.TestCLI.MissingFile.exitcode.approved.txt index c227083..56a6051 100644 --- a/approvals/kir_test.TestCLI.MissingFile.exitcode.approved.txt +++ b/approvals/kir_test.TestCLI.MissingFile.exitcode.approved.txt @@ -1 +1 @@ -0 \ No newline at end of file +1 \ No newline at end of file diff --git a/approvals/kir_test.TestCLI.MissingFile.stderr.approved.txt b/approvals/kir_test.TestCLI.MissingFile.stderr.approved.txt index e69de29..79b27b1 100644 --- a/approvals/kir_test.TestCLI.MissingFile.stderr.approved.txt +++ b/approvals/kir_test.TestCLI.MissingFile.stderr.approved.txt @@ -0,0 +1 @@ +error: no such file or match for "does-not-exist.yaml" diff --git a/fileutil/fileutil.go b/fileutil/fileutil.go index 1a590b1..b26325f 100644 --- a/fileutil/fileutil.go +++ b/fileutil/fileutil.go @@ -1,6 +1,7 @@ package fileutil import ( + "fmt" "os" "path/filepath" ) @@ -14,6 +15,14 @@ func FindFiles(args []string) ([]string, error) { return nil, err } + // filepath.Glob returns no matches and no error both for a glob + // pattern that matches nothing and for a literal path that does not + // exist. Either way the argument named nothing, which is almost + // always a typo; surface it instead of silently skipping it. + if len(matchedFiles) == 0 { + return nil, fmt.Errorf("no such file or match for %q", filePath) + } + for _, matchedFile := range matchedFiles { fileInfo, err := os.Stat(matchedFile) if err != nil { diff --git a/fileutil/fileutil_test.go b/fileutil/fileutil_test.go index 828ace3..4b97dc0 100644 --- a/fileutil/fileutil_test.go +++ b/fileutil/fileutil_test.go @@ -67,3 +67,12 @@ func TestFindFiles(t *testing.T) { }) } } + +// A path that names nothing (a typo'd or missing file) must be reported as an +// error rather than silently ignored. +func TestFindFilesMissing(t *testing.T) { + missing := filepath.Join(t.TempDir(), "does-not-exist.yaml") + if _, err := FindFiles([]string{missing}); err == nil { + t.Fatalf("FindFiles(%q) returned nil error for a missing path", missing) + } +}