Skip to content

Commit 9aa6f7e

Browse files
committed
Sync cp to u-root/pkg/cp
Signed-off-by: Chris Koch <[email protected]>
1 parent f708bf3 commit 9aa6f7e

File tree

1 file changed

+115
-38
lines changed

1 file changed

+115
-38
lines changed

cp/cp_test.go

+115-38
Original file line numberDiff line numberDiff line change
@@ -2,59 +2,136 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
package cp_test
5+
package cp
66

77
import (
8-
"io/ioutil"
8+
"errors"
9+
"fmt"
10+
"io/fs"
911
"os"
1012
"path/filepath"
13+
"strings"
1114
"testing"
1215

13-
"github.com/u-root/uio/cp"
14-
"github.com/u-root/uio/cp/cmp"
16+
"golang.org/x/sys/unix"
1517
)
1618

17-
func copyAndTest(t *testing.T, o cp.Options, src, dst string) {
18-
if err := o.Copy(src, dst); err != nil {
19-
t.Fatalf("Copy(%q -> %q) = %v, want %v", src, dst, err, nil)
20-
}
21-
if err := cmp.IsEqualTree(o, src, dst); err != nil {
22-
t.Fatalf("Expected %q and %q to be same, got %v", src, dst, err)
23-
}
24-
}
19+
var (
20+
testdata = []byte("This is a test string")
21+
)
2522

26-
func TestSimpleCopy(t *testing.T) {
27-
tmpDir, err := ioutil.TempDir("", "u-root-pkg-cp-")
28-
if err != nil {
29-
t.Fatal(err)
23+
func TestCopySimple(t *testing.T) {
24+
var err error
25+
tmpdirDst := t.TempDir()
26+
tmpdirSrc := t.TempDir()
27+
28+
srcfiles := make([]*os.File, 2)
29+
dstfiles := make([]*os.File, 2)
30+
for iterator := range srcfiles {
31+
srcfiles[iterator], err = os.CreateTemp(tmpdirSrc, "file-to-copy"+fmt.Sprintf("%d", iterator))
32+
if err != nil {
33+
t.Errorf("failed to create temp file: %q", err)
34+
}
35+
if _, err = srcfiles[iterator].Write(testdata); err != nil {
36+
t.Errorf("failed to write testdata to file")
37+
}
38+
dstfiles[iterator], err = os.CreateTemp(tmpdirDst, "file-to-copy"+fmt.Sprintf("%d", iterator))
39+
if err != nil {
40+
t.Errorf("failed to create temp file: %q", err)
41+
}
3042
}
31-
defer os.RemoveAll(tmpDir)
3243

33-
// Copy a directory.
34-
origd := filepath.Join(tmpDir, "directory")
35-
if err := os.Mkdir(origd, 0744); err != nil {
36-
t.Fatal(err)
44+
sl := filepath.Join(tmpdirDst, "test-symlink")
45+
if err := os.Symlink(srcfiles[1].Name(), sl); err != nil {
46+
t.Errorf("creating symlink failed")
3747
}
3848

39-
copyAndTest(t, cp.Default, origd, filepath.Join(tmpDir, "directory-copied"))
40-
copyAndTest(t, cp.NoFollowSymlinks, origd, filepath.Join(tmpDir, "directory-copied-2"))
49+
for _, tt := range []struct {
50+
name string
51+
srcfile string
52+
dstfile string
53+
opt Options
54+
wantErr error
55+
}{
56+
{
57+
name: "Success",
58+
srcfile: srcfiles[0].Name(),
59+
dstfile: dstfiles[0].Name(),
60+
opt: Default,
61+
},
62+
{
63+
name: "SrcDstDirctoriesSuccess",
64+
srcfile: tmpdirSrc,
65+
dstfile: tmpdirDst,
66+
},
67+
{
68+
name: "SrcNotExist",
69+
srcfile: "file-does-not-exist",
70+
dstfile: dstfiles[0].Name(),
71+
wantErr: fs.ErrNotExist,
72+
},
73+
{
74+
name: "DstIsDirectory",
75+
srcfile: srcfiles[0].Name(),
76+
dstfile: tmpdirDst,
77+
wantErr: unix.EISDIR,
78+
},
79+
{
80+
name: "CopySymlink",
81+
srcfile: sl,
82+
dstfile: dstfiles[1].Name(),
83+
opt: Options{
84+
NoFollowSymlinks: false,
85+
},
86+
},
87+
{
88+
name: "CopySymlinkFollow",
89+
srcfile: sl,
90+
dstfile: filepath.Join(tmpdirDst, "followed-symlink"),
91+
opt: Options{
92+
NoFollowSymlinks: true,
93+
},
94+
},
95+
} {
96+
t.Run(tt.name, func(t *testing.T) {
97+
err := Copy(tt.srcfile, tt.dstfile)
98+
if !errors.Is(err, tt.wantErr) {
99+
t.Errorf("Test %q failed. Want: %q, Got: %q", tt.name, tt.wantErr, err)
100+
}
101+
})
102+
//After every test with NoFollowSymlink we have to delete the created symlink
103+
if strings.Contains(tt.dstfile, "symlink") {
104+
os.Remove(tt.dstfile)
105+
}
41106

42-
// Copy a file.
43-
origf := filepath.Join(tmpDir, "normal-file")
44-
if err := ioutil.WriteFile(origf, []byte("F is for fire that burns down the whole town"), 0766); err != nil {
45-
t.Fatal(err)
46-
}
107+
t.Run(tt.name, func(t *testing.T) {
108+
if err := tt.opt.Copy(tt.srcfile, tt.dstfile); !errors.Is(err, tt.wantErr) {
109+
t.Errorf("%q failed. Want: %q, Got: %q", tt.name, tt.wantErr, err)
110+
}
111+
})
112+
//After every test with NoFollowSymlink we have to delete the created symlink
113+
if strings.Contains(tt.dstfile, "symlink") {
114+
os.Remove(tt.dstfile)
115+
}
47116

48-
copyAndTest(t, cp.Default, origf, filepath.Join(tmpDir, "normal-file-copied"))
49-
copyAndTest(t, cp.NoFollowSymlinks, origf, filepath.Join(tmpDir, "normal-file-copied-2"))
117+
t.Run(tt.name, func(t *testing.T) {
118+
if err := tt.opt.CopyTree(tt.srcfile, tt.dstfile); !errors.Is(err, tt.wantErr) {
119+
t.Errorf("Test %q failed. Want: %q, Got: %q", tt.name, tt.wantErr, err)
120+
}
121+
})
122+
//After every test with NoFollowSymlink we have to delete the created symlink
123+
if strings.Contains(tt.dstfile, "symlink") {
124+
os.Remove(tt.dstfile)
125+
}
50126

51-
// Copy a symlink.
52-
origs := filepath.Join(tmpDir, "foobar")
53-
// foobar -> normal-file
54-
if err := os.Symlink(origf, origs); err != nil {
55-
t.Fatal(err)
127+
t.Run(tt.name, func(t *testing.T) {
128+
if err := CopyTree(tt.srcfile, tt.dstfile); !errors.Is(err, tt.wantErr) {
129+
t.Errorf("Test %q failed. Want: %q, Got: %q", tt.name, tt.wantErr, err)
130+
}
131+
})
132+
//After every test with NoFollowSymlink we have to delete the created symlink
133+
if strings.Contains(tt.dstfile, "symlink") {
134+
os.Remove(tt.dstfile)
135+
}
56136
}
57-
58-
copyAndTest(t, cp.Default, origf, filepath.Join(tmpDir, "foobar-copied"))
59-
copyAndTest(t, cp.NoFollowSymlinks, origf, filepath.Join(tmpDir, "foobar-copied-just-symlink"))
60137
}

0 commit comments

Comments
 (0)