Skip to content

Commit 6aacbe1

Browse files
committed
Fix file copy mode
Adding some missing tests, I found a bug in that the file permissions were not copied over in CopyFile.
1 parent f2487e6 commit 6aacbe1

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

‎filehelpers/filehelpers.go‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@ func CopyFile(from, to string) error {
2424
return err
2525
}
2626
si, err := os.Stat(from)
27-
if err != nil {
27+
if err == nil {
2828
err = os.Chmod(to, si.Mode())
29-
3029
if err != nil {
3130
return err
3231
}

‎filehelpers/filehelpers_test.go‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,18 @@ func TestCopyFile(t *testing.T) {
2222
}
2323

2424
c := qt.New(t)
25-
_, err := os.Create(abs("f1.txt"))
25+
_, err := os.OpenFile(abs("f1.txt"), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0700)
2626
c.Assert(err, qt.IsNil)
2727
c.Assert(CopyFile(abs("f1.txt"), abs("f2.txt")), qt.IsNil)
2828
fi1, err := os.Stat(abs("f1.txt"))
2929
c.Assert(err, qt.IsNil)
3030
fi2, err := os.Stat(abs("f2.txt"))
3131
c.Assert(err, qt.IsNil)
3232
c.Assert(fi1.Mode(), qt.Equals, fi2.Mode())
33+
34+
// Error cases.
35+
c.Assert(CopyFile(abs("doesnotexist.txt"), abs("f2.txt")), qt.IsNotNil)
36+
c.Assert(CopyFile(abs("f1.txt"), abs("doesnotexist/f2.txt")), qt.IsNotNil)
3337
}
3438

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

69+
c.Assert(CopyDir(abs("a"), abs("b"), nil), qt.IsNil)
70+
rdir2, _ = os.ReadDir(abs("b/b/c"))
71+
c.Assert(len(rdir2), qt.Equals, 3)
72+
73+
// Error cases.
74+
c.Assert(CopyDir(abs("doesnotexist"), abs("b"), nil), qt.IsNotNil)
75+
c.Assert(CopyDir(abs("a/b/c/f3.txt"), abs("b"), nil), qt.IsNotNil)
6576
}

0 commit comments

Comments
 (0)