Browse code

Add coverage on pkg/fileutils

Should fix #11598

Signed-off-by: Vincent Demeester <vincent@sbr.pm>

Vincent Demeester authored on 2015/04/29 23:27:12
Showing 2 changed files
... ...
@@ -118,18 +118,20 @@ func OptimizedMatches(file string, patterns []string, patDirs [][]string) (bool,
118 118
 }
119 119
 
120 120
 func CopyFile(src, dst string) (int64, error) {
121
-	if src == dst {
121
+	cleanSrc := filepath.Clean(src)
122
+	cleanDst := filepath.Clean(dst)
123
+	if cleanSrc == cleanDst {
122 124
 		return 0, nil
123 125
 	}
124
-	sf, err := os.Open(src)
126
+	sf, err := os.Open(cleanSrc)
125 127
 	if err != nil {
126 128
 		return 0, err
127 129
 	}
128 130
 	defer sf.Close()
129
-	if err := os.Remove(dst); err != nil && !os.IsNotExist(err) {
131
+	if err := os.Remove(cleanDst); err != nil && !os.IsNotExist(err) {
130 132
 		return 0, err
131 133
 	}
132
-	df, err := os.Create(dst)
134
+	df, err := os.Create(cleanDst)
133 135
 	if err != nil {
134 136
 		return 0, err
135 137
 	}
... ...
@@ -1,10 +1,125 @@
1 1
 package fileutils
2 2
 
3 3
 import (
4
+	"io/ioutil"
4 5
 	"os"
6
+	"path"
5 7
 	"testing"
6 8
 )
7 9
 
10
+// CopyFile with invalid src
11
+func TestCopyFileWithInvalidSrc(t *testing.T) {
12
+	tempFolder, err := ioutil.TempDir("", "docker-fileutils-test")
13
+	defer os.RemoveAll(tempFolder)
14
+	if err != nil {
15
+		t.Fatal(err)
16
+	}
17
+	bytes, err := CopyFile("/invalid/file/path", path.Join(tempFolder, "dest"))
18
+	if err == nil {
19
+		t.Fatal("Should have fail to copy an invalid src file")
20
+	}
21
+	if bytes != 0 {
22
+		t.Fatal("Should have written 0 bytes")
23
+	}
24
+
25
+}
26
+
27
+// CopyFile with invalid dest
28
+func TestCopyFileWithInvalidDest(t *testing.T) {
29
+	tempFolder, err := ioutil.TempDir("", "docker-fileutils-test")
30
+	defer os.RemoveAll(tempFolder)
31
+	if err != nil {
32
+		t.Fatal(err)
33
+	}
34
+	src := path.Join(tempFolder, "file")
35
+	err = ioutil.WriteFile(src, []byte("content"), 0740)
36
+	if err != nil {
37
+		t.Fatal(err)
38
+	}
39
+	bytes, err := CopyFile(src, path.Join(tempFolder, "/invalid/dest/path"))
40
+	if err == nil {
41
+		t.Fatal("Should have fail to copy an invalid src file")
42
+	}
43
+	if bytes != 0 {
44
+		t.Fatal("Should have written 0 bytes")
45
+	}
46
+
47
+}
48
+
49
+// CopyFile with same src and dest
50
+func TestCopyFileWithSameSrcAndDest(t *testing.T) {
51
+	tempFolder, err := ioutil.TempDir("", "docker-fileutils-test")
52
+	defer os.RemoveAll(tempFolder)
53
+	if err != nil {
54
+		t.Fatal(err)
55
+	}
56
+	file := path.Join(tempFolder, "file")
57
+	err = ioutil.WriteFile(file, []byte("content"), 0740)
58
+	if err != nil {
59
+		t.Fatal(err)
60
+	}
61
+	bytes, err := CopyFile(file, file)
62
+	if err != nil {
63
+		t.Fatal(err)
64
+	}
65
+	if bytes != 0 {
66
+		t.Fatal("Should have written 0 bytes as it is the same file.")
67
+	}
68
+}
69
+
70
+// CopyFile with same src and dest but path is different and not clean
71
+func TestCopyFileWithSameSrcAndDestWithPathNameDifferent(t *testing.T) {
72
+	tempFolder, err := ioutil.TempDir("", "docker-fileutils-test")
73
+	defer os.RemoveAll(tempFolder)
74
+	if err != nil {
75
+		t.Fatal(err)
76
+	}
77
+	testFolder := path.Join(tempFolder, "test")
78
+	err = os.MkdirAll(testFolder, 0740)
79
+	if err != nil {
80
+		t.Fatal(err)
81
+	}
82
+	file := path.Join(testFolder, "file")
83
+	sameFile := testFolder + "/../test/file"
84
+	err = ioutil.WriteFile(file, []byte("content"), 0740)
85
+	if err != nil {
86
+		t.Fatal(err)
87
+	}
88
+	bytes, err := CopyFile(file, sameFile)
89
+	if err != nil {
90
+		t.Fatal(err)
91
+	}
92
+	if bytes != 0 {
93
+		t.Fatal("Should have written 0 bytes as it is the same file.")
94
+	}
95
+}
96
+
97
+func TestCopyFile(t *testing.T) {
98
+	tempFolder, err := ioutil.TempDir("", "docker-fileutils-test")
99
+	defer os.RemoveAll(tempFolder)
100
+	if err != nil {
101
+		t.Fatal(err)
102
+	}
103
+	src := path.Join(tempFolder, "src")
104
+	dest := path.Join(tempFolder, "dest")
105
+	ioutil.WriteFile(src, []byte("content"), 0777)
106
+	ioutil.WriteFile(dest, []byte("destContent"), 0777)
107
+	bytes, err := CopyFile(src, dest)
108
+	if err != nil {
109
+		t.Fatal(err)
110
+	}
111
+	if bytes != 7 {
112
+		t.Fatalf("Should have written %d bytes but wrote %d", 7, bytes)
113
+	}
114
+	actual, err := ioutil.ReadFile(dest)
115
+	if err != nil {
116
+		t.Fatal(err)
117
+	}
118
+	if string(actual) != "content" {
119
+		t.Fatalf("Dest content was '%s', expected '%s'", string(actual), "content")
120
+	}
121
+}
122
+
8 123
 // Reading a symlink to a directory must return the directory
9 124
 func TestReadSymlinkedDirectoryExistingDirectory(t *testing.T) {
10 125
 	var err error
... ...
@@ -159,6 +274,28 @@ func TestExclusion(t *testing.T) {
159 159
 	}
160 160
 }
161 161
 
162
+// Matches with no patterns
163
+func TestMatchesWithNoPatterns(t *testing.T) {
164
+	matches, err := Matches("/any/path/there", []string{})
165
+	if err != nil {
166
+		t.Fatal(err)
167
+	}
168
+	if matches {
169
+		t.Fatalf("Should not have match anything")
170
+	}
171
+}
172
+
173
+// Matches with malformed patterns
174
+func TestMatchesWithMalformedPatterns(t *testing.T) {
175
+	matches, err := Matches("/any/path/there", []string{"["})
176
+	if err == nil {
177
+		t.Fatal("Should have failed because of a malformed syntax in the pattern")
178
+	}
179
+	if matches {
180
+		t.Fatalf("Should not have match anything")
181
+	}
182
+}
183
+
162 184
 // An empty string should return true from Empty.
163 185
 func TestEmpty(t *testing.T) {
164 186
 	empty := Empty("")