Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0
1
1 change: 1 addition & 0 deletions approvals/kir_test.TestCLI.MissingFile.stderr.approved.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
error: no such file or match for "does-not-exist.yaml"
9 changes: 9 additions & 0 deletions fileutil/fileutil.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fileutil

import (
"fmt"
"os"
"path/filepath"
)
Expand All @@ -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 {
Expand Down
9 changes: 9 additions & 0 deletions fileutil/fileutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}