diff --git a/plugin.go b/plugin.go index 70b808f..499a235 100644 --- a/plugin.go +++ b/plugin.go @@ -21,6 +21,8 @@ import ( log "github.com/sirupsen/logrus" ) +var errSkip = fmt.Errorf("skip") + // Plugin defines the S3 plugin parameters. type Plugin struct { Endpoint string @@ -167,9 +169,16 @@ func (p *Plugin) Exec() error { anyMatched := false for _, match := range matches { - // skip directories - if isDir(match, matches) { - continue + // check directories and fail if directory without glob pattern + if err := isDir(match, matches); err != nil { + if err == errSkip { + continue + } + log.WithFields(log.Fields{ + "error": err, + "match": match, + }).Error("Directory specified without glob pattern") + return err } // Preview stripping (wildcard for absolute patterns, literal for relative patterns) @@ -411,11 +420,11 @@ func resolveSource(sourceDir, source, stripPrefix string) string { return stripPrefix + path } -// checks if the source path is a dir -func isDir(source string, matches []string) bool { +// checks if the source path is a dir and returns error if directory found without glob patterns +func isDir(source string, matches []string) error { stat, err := os.Stat(source) if err != nil { - return true // should never happen + return errSkip // skip non-existent files } if stat.IsDir() { count := 0 @@ -425,11 +434,11 @@ func isDir(source string, matches []string) bool { } } if count <= 1 { - log.Warnf("Skipping '%s' since it is a directory. Please use correct glob expression if this is unexpected.", source) + return fmt.Errorf("directory '%s' specified without glob pattern. Use a pattern like '%s/*' or '%s/**' to upload directory contents", source, source, source) } - return true + return errSkip // skip directory but allow its children to be processed } - return false + return nil } // normalizePath converts the path to a forward slash format and trims the prefix. diff --git a/plugin_test.go b/plugin_test.go new file mode 100644 index 0000000..7f900a9 --- /dev/null +++ b/plugin_test.go @@ -0,0 +1,91 @@ +package main + +import ( + "os" + "path/filepath" + "strings" + "testing" +) + +func TestIsDir(t *testing.T) { + // Create temporary directory for testing + tmpDir := t.TempDir() + testDir := filepath.Join(tmpDir, "testdir") + testFile := filepath.Join(tmpDir, "testfile.txt") + + // Create a test directory + err := os.Mkdir(testDir, 0755) + if err != nil { + t.Fatalf("Failed to create test directory: %v", err) + } + + // Create a test file + file, err := os.Create(testFile) + if err != nil { + t.Fatalf("Failed to create test file: %v", err) + } + file.Close() + + tests := []struct { + name string + source string + matches []string + expectError bool + expectSkip bool + errorContains string + }{ + { + name: "file should not error", + source: testFile, + matches: []string{testFile}, + expectError: false, + expectSkip: false, + }, + { + name: "directory without glob should error", + source: testDir, + matches: []string{testDir}, + expectError: true, + expectSkip: false, + errorContains: "specified without glob pattern", + }, + { + name: "directory with glob pattern should skip", + source: testDir, + matches: []string{testDir + "/file1.txt", testDir + "/file2.txt"}, + expectError: false, + expectSkip: true, + }, + { + name: "non-existent path should skip", + source: "/non/existent/path", + matches: []string{}, + expectError: false, + expectSkip: true, + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + err := isDir(tc.source, tc.matches) + + if tc.expectError { + if err == nil { + t.Errorf("Expected error but got none") + } else if err == errSkip { + t.Errorf("Expected fatal error but got skip error") + } else if tc.errorContains != "" && !strings.Contains(err.Error(), tc.errorContains) { + t.Errorf("Expected error to contain '%s', but got: %v", tc.errorContains, err) + } + } else if tc.expectSkip { + if err != errSkip { + t.Errorf("Expected skip error but got: %v", err) + } + } else { + if err != nil { + t.Errorf("Expected no error but got: %v", err) + } + } + }) + } +} \ No newline at end of file diff --git a/plugin_unix_test.go b/plugin_unix_test.go index 336080d..cdf8415 100644 --- a/plugin_unix_test.go +++ b/plugin_unix_test.go @@ -128,3 +128,4 @@ func TestResolveSource(t *testing.T) { } } } +