Skip to content

Commit

Permalink
Merge pull request #160 from moov-io/clarify-case-insensitive-compare
Browse files Browse the repository at this point in the history
fix: clarify ListFiles returns case intensive matches but returns server case
  • Loading branch information
adamdecaf authored Aug 23, 2023
2 parents 73dd027 + b501c62 commit 6168c43
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 5 deletions.
25 changes: 22 additions & 3 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,10 +370,16 @@ func (c *client) UploadFile(path string, contents io.ReadCloser) error {
return nil
}

// ListFiles will return the filepaths of files within dir
// ListFiles will return the paths of files within dir. Paths are returned as locations from dir,
// so if dir is an absolute path the returned paths will be.
//
// Paths are matched in case-insensitive comparisons, but results are returned exactly as they
// appear on the server.
func (c *client) ListFiles(dir string) ([]string, error) {
pattern := filepath.Clean(strings.TrimPrefix(dir, string(os.PathSeparator)))
switch {
case dir == "/":
pattern = "*"
case pattern == ".":
if dir == "" {
pattern = "*"
Expand All @@ -392,9 +398,22 @@ func (c *client) ListFiles(dir string) ([]string, error) {
if d.IsDir() {
return nil
}
matches, err := filepath.Match(pattern, path)

// Check if the server's path matches what we're searching for in a case-insensitive comparison.
matches, err := filepath.Match(strings.ToLower(pattern), strings.ToLower(path))
if matches && err == nil {
filenames = append(filenames, filepath.Join(dir, filepath.Base(path)))
// Return the path with exactly the case on the server.
idx := strings.Index(strings.ToLower(path), strings.ToLower(strings.TrimSuffix(pattern, "*")))
if idx > -1 {
path = path[idx:]
if strings.HasPrefix(dir, "/") && !strings.HasPrefix(path, "/") {
path = "/" + path
}
filenames = append(filenames, path)
} else {
// Fallback to Go logic of presenting the path
filenames = append(filenames, filepath.Join(dir, filepath.Base(path)))
}
}
return err
})
Expand Down
21 changes: 19 additions & 2 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,13 @@ func TestClient(t *testing.T) {
})

t.Run("ListFiles", func(t *testing.T) {
files, err := client.ListFiles("/")
files, err := client.ListFiles(".")
require.NoError(t, err)
require.Len(t, files, 0)
require.ElementsMatch(t, files, []string{"root.txt"})

files, err = client.ListFiles("/")
require.NoError(t, err)
require.ElementsMatch(t, files, []string{"/root.txt"})

files, err = client.ListFiles("/outbox")
require.NoError(t, err)
Expand Down Expand Up @@ -147,6 +151,16 @@ func TestClient(t *testing.T) {
require.ElementsMatch(t, contents, []string{"", "", "also data\n", "has data\n"})
})

t.Run("ListFiles case testing", func(t *testing.T) {
files, err := client.ListFiles("/outbox/upper")
require.NoError(t, err)
require.ElementsMatch(t, files, []string{"/outbox/Upper/names.txt"})

files, err = client.ListFiles("outbox/ARCHIVE")
require.NoError(t, err)
require.ElementsMatch(t, files, []string{"outbox/archive/empty2.txt", "outbox/archive/three.txt"})
})

t.Run("Walk", func(t *testing.T) {
var walkedFiles []string
err = client.Walk(".", func(path string, info fs.DirEntry, err error) error {
Expand All @@ -158,6 +172,8 @@ func TestClient(t *testing.T) {
})
require.NoError(t, err)
require.ElementsMatch(t, walkedFiles, []string{
"root.txt",
"outbox/Upper/names.txt",
"outbox/one.txt", "outbox/two.txt", "outbox/empty.txt",
"outbox/archive/empty2.txt", "outbox/archive/three.txt",
"outbox/with-empty/EMPTY1.txt", "outbox/with-empty/empty_file2.txt",
Expand All @@ -176,6 +192,7 @@ func TestClient(t *testing.T) {
})
require.NoError(t, err)
require.ElementsMatch(t, walkedFiles, []string{
"/outbox/Upper/names.txt",
"/outbox/one.txt", "/outbox/two.txt", "/outbox/empty.txt",
"/outbox/archive/empty2.txt", "/outbox/archive/three.txt",
"/outbox/with-empty/EMPTY1.txt", "/outbox/with-empty/empty_file2.txt",
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ services:
- "2222:22"
volumes:
- "./testdata/outbox:/home/demo/outbox"
- "./testdata/root.txt:/home/demo/root.txt"
command:
- "demo:password:::outbox,upload"
1 change: 1 addition & 0 deletions testdata/outbox/Upper/names.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
adam
1 change: 1 addition & 0 deletions testdata/root.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
file in root dir

0 comments on commit 6168c43

Please sign in to comment.