Skip to content

Commit

Permalink
Fix relative path bug
Browse files Browse the repository at this point in the history
  • Loading branch information
cespare committed Dec 20, 2018
1 parent 67d95eb commit db1407d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
8 changes: 6 additions & 2 deletions cp.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"io"
"os"
"path/filepath"
"strings"
)

var errCopyFileWithDir = errors.New("dir argument to CopyFile")
Expand Down Expand Up @@ -83,7 +82,12 @@ func makeWalkFn(dst, src string, flag int) filepath.WalkFunc {
if err != nil {
return err
}
dstPath := filepath.Join(dst, strings.TrimPrefix(path, src))
rel, err := filepath.Rel(src, path)
if err != nil {
// Given the Walk contract, Rel must succeed.
panic("shouldn't happen")
}
dstPath := filepath.Join(dst, rel)
if info.IsDir() {
err := os.Mkdir(dstPath, info.Mode())
// In overwrite mode, allow the directory to already exist
Expand Down
10 changes: 10 additions & 0 deletions cp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,16 @@ func TestCopyAllOverwrite(t *testing.T) {
)
}

func TestCopyDotSlash(t *testing.T) {
td := newTestDir(t)
defer td.remove()

if err := CopyAllOverwrite(td.path("testdata"), "./testdata"); err != nil {
t.Fatal(err)
}
td.checkAll("testdata", "a.txt", "a\n")
}

type testDir struct {
t *testing.T
dir string
Expand Down
1 change: 1 addition & 0 deletions testdata/a.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a

0 comments on commit db1407d

Please sign in to comment.