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

core: fix relative copy regression #71

Merged
merged 1 commit into from
Feb 27, 2020
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
5 changes: 3 additions & 2 deletions core/job_batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@ func S3BatchDownload(command *Command, src *objurl.ObjectURL) *Job {

var joinPath string
if command.opts.Has(opt.Parents) {
joinPath = src.Path
joinPath = src.Relative()
} else {
joinPath = src.Base()
}

dst := cmdDst.Join(joinPath)
dir := filepath.Dir(dst.Absolute())

os.MkdirAll(dir, os.ModePerm)
return command.makeJob(cmd, op.Download, src, dst)
}
Expand All @@ -49,7 +50,7 @@ func S3BatchCopy(command *Command, src *objurl.ObjectURL) *Job {

var dstFilename string
if command.opts.Has(opt.Parents) {
dstFilename = src.Path
dstFilename = src.Relative()
} else {
dstFilename = src.Base()
}
Expand Down
99 changes: 99 additions & 0 deletions e2e/cp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,49 @@ func TestCopyMultipleS3ObjectsToS3(t *testing.T) {
}
}

func TestCopyMultipleS3ObjectsToS3_Issue70(t *testing.T) {
t.Parallel()

bucket := s3BucketFromTestName(t)

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

createBucket(t, s3client, bucket)

filesToContent := map[string]string{
"config/.local/folder1/file1.txt": "this is a test file 1",
"config/.local/folder2/file2.txt": "this is a test file 2",
}

for filename, content := range filesToContent {
putFile(t, s3client, bucket, filename, content)
}

src := fmt.Sprintf("s3://%v/config/.local/*", bucket)
dst := fmt.Sprintf("s3://%v/.local/", bucket)

cmd := s5cmd("cp", "-u", "-s", "--parents", src, dst)
result := icmd.RunCmd(cmd)

result.Assert(t, icmd.Success)

assertLines(t, result.Stdout(), map[int]compareFunc{
0: contains(""),
1: suffix(`# Copying file1.txt...`),
2: suffix(`# Copying file2.txt...`),
}, sortInput(true))

// assert s3 source objects
for filename, content := range filesToContent {
assert.Assert(t, ensureS3Object(s3client, bucket, filename, content))
}

// assert s3 destination objects
assert.Assert(t, ensureS3Object(s3client, bucket, ".local/folder1/file1.txt", "this is a test file 1"))
assert.Assert(t, ensureS3Object(s3client, bucket, ".local/folder2/file2.txt", "this is a test file 2"))
}

func TestCopySingleLocalFileToLocal(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -924,6 +967,62 @@ func TestCopyS3ToLocalWithSameFilenameDontOverrideIfS3ObjectIsOlder(t *testing.T
assert.Assert(t, fs.Equal(workdir.Path(), expected))
}

func TestCopyS3ToLocal_Issue70(t *testing.T) {
t.Parallel()

bucket := s3BucketFromTestName(t)

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

createBucket(t, s3client, bucket)

filesToContent := map[string]string{
"config/.local/folder1/file1.txt": "this is a test file 1",
"config/.local/folder2/file2.txt": "this is a test file 2",
}

for filename, content := range filesToContent {
putFile(t, s3client, bucket, filename, content)
}

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

srcpath := fmt.Sprintf("s3://%v/config/.local/*", bucket)
dstpath := filepath.Join(workdir.Path(), ".local")

cmd := s5cmd("cp", "-u", "-s", "--parents", srcpath, dstpath)

result := icmd.RunCmd(cmd, withWorkingDir(workdir))

result.Assert(t, icmd.Success)

assertLines(t, result.Stdout(), map[int]compareFunc{
0: equals(""),
1: suffix(`# Downloading file1.txt...`),
2: suffix(`# Downloading file2.txt...`),
}, sortInput(true))

// assert local filesystem
expectedFiles := []fs.PathOp{
fs.WithDir(
".local",
fs.WithMode(0755),
fs.WithDir("folder1", fs.WithMode(0755), fs.WithFile("file1.txt", "this is a test file 1")),
fs.WithDir("folder2", fs.WithMode(0755), fs.WithFile("file2.txt", "this is a test file 2")),
),
}

expectedResult := fs.Expected(t, expectedFiles...)
assert.Assert(t, fs.Equal(workdir.Path(), expectedResult))

// assert s3 objects
for filename, content := range filesToContent {
assert.Assert(t, ensureS3Object(s3client, bucket, filename, content))
}
}

func TestCopyLocalFileToS3WithTheSameFilename(t *testing.T) {
t.Parallel()

Expand Down