Skip to content

Commit

Permalink
Fix file copy mode
Browse files Browse the repository at this point in the history
Adding some missing tests, I found a bug in that the file permissions were not copied over in CopyFile.
  • Loading branch information
bep committed Aug 26, 2022
1 parent f2487e6 commit 6aacbe1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
3 changes: 1 addition & 2 deletions filehelpers/filehelpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ func CopyFile(from, to string) error {
return err
}
si, err := os.Stat(from)
if err != nil {
if err == nil {
err = os.Chmod(to, si.Mode())

if err != nil {
return err
}
Expand Down
13 changes: 12 additions & 1 deletion filehelpers/filehelpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,18 @@ func TestCopyFile(t *testing.T) {
}

c := qt.New(t)
_, err := os.Create(abs("f1.txt"))
_, err := os.OpenFile(abs("f1.txt"), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0700)
c.Assert(err, qt.IsNil)
c.Assert(CopyFile(abs("f1.txt"), abs("f2.txt")), qt.IsNil)
fi1, err := os.Stat(abs("f1.txt"))
c.Assert(err, qt.IsNil)
fi2, err := os.Stat(abs("f2.txt"))
c.Assert(err, qt.IsNil)
c.Assert(fi1.Mode(), qt.Equals, fi2.Mode())

// Error cases.
c.Assert(CopyFile(abs("doesnotexist.txt"), abs("f2.txt")), qt.IsNotNil)
c.Assert(CopyFile(abs("f1.txt"), abs("doesnotexist/f2.txt")), qt.IsNotNil)
}

func TestCopyDir(t *testing.T) {
Expand Down Expand Up @@ -62,4 +66,11 @@ func TestCopyDir(t *testing.T) {
rdir2, _ := os.ReadDir(abs("b/b/c"))
c.Assert(len(rdir2), qt.Equals, 2)

c.Assert(CopyDir(abs("a"), abs("b"), nil), qt.IsNil)
rdir2, _ = os.ReadDir(abs("b/b/c"))
c.Assert(len(rdir2), qt.Equals, 3)

// Error cases.
c.Assert(CopyDir(abs("doesnotexist"), abs("b"), nil), qt.IsNotNil)
c.Assert(CopyDir(abs("a/b/c/f3.txt"), abs("b"), nil), qt.IsNotNil)
}

0 comments on commit 6aacbe1

Please sign in to comment.