Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

storage/fs: change order of precedence for URL expansion #322

Merged
merged 4 commits into from
Jul 26, 2021
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
56 changes: 56 additions & 0 deletions e2e/cp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1109,6 +1109,62 @@ func TestCopyMultipleFilesToS3WithPrefixWithoutSlash(t *testing.T) {
assert.Assert(t, fs.Equal(workdir.Path(), expected))
}

// cp prefix* s3://bucket/
func TestCopyDirectoryWithGlobCharactersToS3Bucket(t *testing.T) {
t.Parallel()

if runtime.GOOS == "windows" {
t.Skip("Files in Windows cannot contain glob(*) characters")
}

bucket := s3BucketFromTestName(t)

s3client, s5cmd, cleanup := setup(t)
defer cleanup()

createBucket(t, s3client, bucket)

folderLayout := []fs.PathOp{
fs.WithDir(
"abc*",
fs.WithFile("file1.txt", "this is the first test file"),
fs.WithFile("file2.txt", "this is the second test file"),
),
fs.WithDir(
"abcd",
fs.WithFile("file1.txt", "this is the first test file"),
),
fs.WithDir(
"abcde",
fs.WithFile("file1.txt", "this is the first test file"),
fs.WithFile("file2.txt", "this is the second test file"),
),
}

workdir := fs.NewDir(t, "somedir", folderLayout...)
defer workdir.Remove()

src := fmt.Sprintf("%v/abc*", workdir.Path())
dst := fmt.Sprintf("s3://%v", bucket)

cmd := s5cmd("cp", src, dst)
result := icmd.RunCmd(cmd)

result.Assert(t, icmd.Success)

assertLines(t, result.Stdout(), map[int]compareFunc{
0: equals(`cp %v/abc*/file1.txt %v/abc*/file1.txt`, workdir.Path(), dst),
1: equals(`cp %v/abc*/file2.txt %v/abc*/file2.txt`, workdir.Path(), dst),
2: equals(`cp %v/abcd/file1.txt %v/abcd/file1.txt`, workdir.Path(), dst),
3: equals(`cp %v/abcde/file1.txt %v/abcde/file1.txt`, workdir.Path(), dst),
4: equals(`cp %v/abcde/file2.txt %v/abcde/file2.txt`, workdir.Path(), dst),
}, sortInput(true))

// assert local filesystem
expected := fs.Expected(t, folderLayout...)
assert.Assert(t, fs.Equal(workdir.Path(), expected))
}

// cp dir/* s3://bucket/prefix/
func TestCopyMultipleFilesToS3WithPrefixWithSlash(t *testing.T) {
t.Parallel()
Expand Down
57 changes: 57 additions & 0 deletions e2e/rm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package e2e

import (
"fmt"
"runtime"
"testing"

"gotest.tools/v3/assert"
Expand Down Expand Up @@ -383,6 +384,62 @@ func TestRemoveLocalDirectory(t *testing.T) {
assert.Assert(t, fs.Equal(workdir.Path(), expected))
}

// rm prefix*
func TestRemoveLocalDirectoryWithGlob(t *testing.T) {
t.Parallel()

if runtime.GOOS == "windows" {
t.Skip("Files in Windows cannot contain glob(*) characters")
}

_, s5cmd, cleanup := setup(t)
defer cleanup()

folderLayout := []fs.PathOp{
fs.WithDir(
"abc*",
fs.WithFile("file1.txt", "this is the first test file"),
fs.WithFile("file2.txt", "this is the second test file"),
),
fs.WithDir(
"abcd",
fs.WithFile("file1.txt", "this is the first test file"),
),
fs.WithDir(
"abcde",
fs.WithFile("file1.txt", "this is the first test file"),
fs.WithFile("file2.txt", "this is the second test file"),
),
}

workdir := fs.NewDir(t, t.Name(), folderLayout...)
defer workdir.Remove()

cmd := s5cmd("rm", "abc*")
result := icmd.RunCmd(cmd, withWorkingDir(workdir))

result.Assert(t, icmd.Success)

assertLines(t, result.Stdout(), map[int]compareFunc{
0: equals("rm abc*/file1.txt"),
1: equals("rm abc*/file2.txt"),
2: equals("rm abcd/file1.txt"),
3: equals("rm abcde/file1.txt"),
4: equals("rm abcde/file2.txt"),
}, sortInput(true))

assertLines(t, result.Stderr(), map[int]compareFunc{})

// expected 3 empty dirs
expected := fs.Expected(
t,
fs.WithDir("abc*"),
fs.WithDir("abcd"),
fs.WithDir("abcde"),
)
assert.Assert(t, fs.Equal(workdir.Path(), expected))
}

// rm dir/ file file2
func TestVariadicMultipleLocalFilesWithDirectory(t *testing.T) {
t.Parallel()
Expand Down
8 changes: 4 additions & 4 deletions storage/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,17 @@ func (f *Filesystem) Stat(ctx context.Context, url *url.URL) (*Object, error) {

// List returns the objects and directories reside in given src.
func (f *Filesystem) List(ctx context.Context, src *url.URL, followSymlinks bool) <-chan *Object {
if src.HasGlob() {
return f.expandGlob(ctx, src, followSymlinks)
}

obj, err := f.Stat(ctx, src)
isDir := err == nil && obj.Type.IsDir()

if isDir {
return f.walkDir(ctx, src, followSymlinks)
}

if src.HasGlob() {
return f.expandGlob(ctx, src, followSymlinks)
}

return f.listSingleObject(ctx, src)
}

Expand Down