-
Notifications
You must be signed in to change notification settings - Fork 1
/
pop_test.go
224 lines (187 loc) · 4.88 KB
/
pop_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package pop
import (
"bytes"
"io/ioutil"
"os"
"path"
"strings"
"testing"
)
func TestCreateEmpty(t *testing.T) {
root, err := Generate(nil)
defer os.RemoveAll(root)
if err != nil {
t.Fatalf("TestCreateNothing: unexpected error: %s", err)
}
if !doesDirExist(root) {
t.Fatalf("TestCreateNothing: directory has not been created")
}
if countFiles(t, root) > 0 || countDirectories(t, root) > 0 {
t.Fatalf("TestCreateNothing: directory is not empty")
}
}
func TestCreateEmptyFromNonExistingRoot(t *testing.T) {
root := "path/does/not/exist/"
err := GenerateFromRoot(root, nil)
defer os.RemoveAll("path")
if err != nil {
t.Fatalf("TestCreateEmptyFromNonExistingRoot: unexpected error: %s", err)
}
if !doesDirExist(root) {
t.Fatalf("TestCreateEmptyFromNonExistingRoot: directory has not been created")
}
if countFiles(t, root) > 0 || countDirectories(t, root) > 0 {
t.Fatalf("TestCreateEmptyFromNonExistingRoot: directory is not empty")
}
}
func TestCreateEmptyFromExistingRoot(t *testing.T) {
root, err := ioutil.TempDir("", "go_test_")
defer os.RemoveAll(root)
if err != nil {
t.Fatalf("TestCreateEmptyFromExistingRoot: cannot create root directory: %s", err)
}
if err = GenerateFromRoot(root, nil); err != nil {
t.Fatalf("TestCreateEmptyFromExistingRoot: unexpected error: %s", err)
}
if !doesDirExist(root) {
t.Fatalf("TestCreateEmptyFromExistingRoot: directory has not been created")
}
if countFiles(t, root) > 0 || countDirectories(t, root) > 0 {
t.Fatalf("TestCreateEmptyFromExistingRoot: directory is not empty")
}
}
func TestCreateOneDir(t *testing.T) {
files := Corn{
"README.md": "# This is the title",
"json/": Corn{
"test1.json": bytes.NewBufferString(`{"key1":"value1","key2":"value2"}`),
"test2.json": `{"key3":"value3","key4":"value4"}`,
},
"vendor/": nil,
"src/": Corn{
"one.cc": "int main() {}",
"two.cc": "#include <iostream>",
"empty.txt": nil,
},
"test/": Corn{
".gitkeep": nil,
},
}
root, err := Generate(files)
defer os.RemoveAll(root)
if err != nil {
t.Fatalf("TestCreateComplex: cannot generate tree: %s", err)
}
for name, content := range files {
checkTree(t, root, name, content)
}
}
func checkTree(t *testing.T, root string, name string, content interface{}) {
if strings.HasSuffix(name, "/") {
checkDir(t, root, name, content)
} else {
checkFile(t, root, name, content)
}
}
func checkDir(t *testing.T, root string, name string, content interface{}) {
dirPath := path.Join(root, name)
if !doesDirExist(dirPath) {
t.Fatalf("checkDir: %s directory should have been generated", dirPath)
}
if content == nil {
if countFiles(t, dirPath) > 0 || countDirectories(t, dirPath) > 0 {
t.Fatalf("checkDir: %s directory should be empty", dirPath)
}
return
}
switch content := content.(type) {
case Corn:
for subName, subContent := range content {
checkTree(t, dirPath, subName, subContent)
}
default:
t.Fatalf("checkDir: %s directory content should be of type `pop.Corn` but got %T instead", dirPath, content)
}
}
func contentToString(content interface{}) (string, error) {
r, err := contentToReader(content)
if err != nil {
return "", err
}
data, err := ioutil.ReadAll(r)
if err != nil {
return "", err
}
return string(data), nil
}
func checkFile(t *testing.T, root string, name string, content interface{}) {
filePath := path.Join(root, name)
if !doesFileExist(filePath) {
t.Fatalf("checkFile: %s file should have been generated", filePath)
}
if content == nil {
return
}
text, err := contentToString(content)
if err != nil {
t.Fatalf("checkFile: %s file %s", filePath, err)
}
if text == "" {
return
}
checkFileContent(t, filePath, text)
}
func checkFileContent(t *testing.T, path string, expected string) {
f, err := os.Open(path)
if err != nil {
t.Fatalf("checkFileContent: cannot open file %s: %s", path, err)
}
data, err := ioutil.ReadAll(f)
if err != nil {
t.Fatalf("checkFileContent: cannot read file %s: %s", path, err)
}
content := string(data)
if content != expected {
t.Fatalf("checkFileContent:\n expected: %s\n got: %s", expected, content)
}
}
func doesDirExist(path string) bool {
file, err := os.Stat(path)
if err != nil {
return false
}
return file.IsDir()
}
func doesFileExist(path string) bool {
file, err := os.Stat(path)
if err != nil {
return false
}
return file.Mode().IsRegular()
}
func count(t *testing.T, path string, directories bool) int {
files, err := ioutil.ReadDir(path)
if err != nil {
t.Fatalf("countFiles: cannot read directory: %s", err)
}
dirCount := 0
fileCount := 0
for _, file := range files {
if file.IsDir() {
dirCount++
} else {
fileCount++
}
}
if directories {
return dirCount
} else {
return fileCount
}
}
func countDirectories(t *testing.T, path string) int {
return count(t, path, true)
}
func countFiles(t *testing.T, path string) int {
return count(t, path, false)
}