1
|
|
|
package files |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"io/ioutil" |
5
|
|
|
"os" |
6
|
|
|
"testing" |
7
|
|
|
) |
8
|
|
|
|
9
|
|
|
func TestDirectoryExists(t *testing.T) { |
10
|
|
|
dir, _ := ioutil.TempDir("", "*") |
11
|
|
|
|
12
|
|
|
if exists, err := FileExists(dir); err != nil { |
13
|
|
|
t.Errorf("Should not be an error: %v", err.Error()) |
14
|
|
|
} else { |
15
|
|
|
if !exists { |
16
|
|
|
t.Errorf("Directory should exist: %v", dir) |
17
|
|
|
} |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
os.RemoveAll(dir) |
21
|
|
|
|
22
|
|
|
if exists, err := FileExists(dir); err != nil { |
23
|
|
|
t.Errorf("Should not be an error: %v", err.Error()) |
24
|
|
|
} else { |
25
|
|
|
if exists { |
26
|
|
|
t.Errorf("Directory should not exist: %v", dir) |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
func TestFileExists(t *testing.T) { |
32
|
|
|
file, _ := ioutil.TempFile("", "test*") |
33
|
|
|
file.Close() |
34
|
|
|
|
35
|
|
|
if exists, err := FileExists(file.Name()); err != nil { |
36
|
|
|
t.Errorf("Should not be an error: %v", err.Error()) |
37
|
|
|
} else { |
38
|
|
|
if !exists { |
39
|
|
|
t.Errorf("File should exist: %v", file.Name()) |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
os.Remove(file.Name()) |
44
|
|
|
|
45
|
|
|
if exists, err := FileExists(file.Name()); err != nil { |
46
|
|
|
t.Errorf("Should not be an error: %v", err.Error()) |
47
|
|
|
} else { |
48
|
|
|
if exists { |
49
|
|
|
t.Errorf("File should not exist: %v", file.Name()) |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
func TestGetOrCreateFile(t *testing.T) { |
55
|
|
|
file, _ := ioutil.TempFile("", "test*") |
56
|
|
|
file.WriteString("This is a line of text") |
57
|
|
|
file.Close() |
58
|
|
|
|
59
|
|
|
validateContent(t, file.Name(), "This is a line of text") |
60
|
|
|
|
61
|
|
|
removeFile(t, file.Name()) |
62
|
|
|
validateEmptyFile(t, file.Name()) |
63
|
|
|
os.Remove(file.Name()) |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
func TestJoinAndParent(t *testing.T) { |
67
|
|
|
path := "/" |
68
|
|
|
if Join(path, "file") != "/file" { |
69
|
|
|
t.Errorf("Join failed %v", Join(path, "file")) |
70
|
|
|
t.Fail() |
71
|
|
|
} |
72
|
|
|
if Join(Join(path, "dir"), "file") != "/dir/file" { |
73
|
|
|
t.Errorf("Join failed %v", Join(Join(path, "dir"), "file")) |
74
|
|
|
t.Fail() |
75
|
|
|
} |
76
|
|
|
if Parent(Join(Join(path, "dir"), "file")) != "/dir" { |
77
|
|
|
t.Errorf("Join failed %v", Parent(Join(Join(path, "dir"), "file"))) |
78
|
|
|
t.Fail() |
79
|
|
|
} |
80
|
|
|
if Parent(path) != "/" { |
81
|
|
|
t.Errorf("Join failed %v", Parent(path)) |
82
|
|
|
t.Fail() |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
func TestFormatBytes(t *testing.T) { |
87
|
|
|
if "100 B" != FormatBytes(100) { |
88
|
|
|
t.Errorf("Failed format %d to %v", 100, FormatBytes(100)) |
89
|
|
|
t.Fail() |
90
|
|
|
} |
91
|
|
|
if "1.0 KB" != FormatBytes(1024) { |
92
|
|
|
t.Errorf("Failed format %d to %v", 1024, FormatBytes(1024)) |
93
|
|
|
t.Fail() |
94
|
|
|
} |
95
|
|
|
if "2.0 MB" != FormatBytes(1024*1024*2) { |
96
|
|
|
t.Errorf("Failed format %d to %v", 1024*1024*2, FormatBytes(1024*1024*2)) |
97
|
|
|
t.Fail() |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
func validateContent(t *testing.T, path, content string) { |
102
|
|
|
f, err := GetOrCreateFile(path) |
103
|
|
|
defer f.Close() |
104
|
|
|
|
105
|
|
|
if err != nil { |
106
|
|
|
t.Error(err) |
107
|
|
|
t.Fail() |
108
|
|
|
} else if data, _ := ioutil.ReadFile(f.Name()); string(data) != content { |
109
|
|
|
t.Errorf("File should have the content %v but was %v", content, string(data)) |
110
|
|
|
t.Fail() |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
func validateEmptyFile(t *testing.T, path string) { |
115
|
|
|
f, err := GetOrCreateFile(path) |
116
|
|
|
defer f.Close() |
117
|
|
|
|
118
|
|
|
if err != nil { |
119
|
|
|
t.Error(err) |
120
|
|
|
t.Fail() |
121
|
|
|
} else if data, _ := ioutil.ReadFile(f.Name()); len(data) != 0 { |
122
|
|
|
t.Errorf("File should be empty but was %d bytes", len(data)) |
123
|
|
|
t.Fail() |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
func removeFile(t *testing.T, path string) { |
128
|
|
|
if err := os.Remove(path); err != nil { |
129
|
|
|
t.Errorf("Unable to remove file: %v", err.Error()) |
130
|
|
|
t.Fail() |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|