-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfilebuilder_test.go
150 lines (147 loc) · 3.1 KB
/
filebuilder_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
package filebuilder
import (
"os"
"testing"
"github.com/spf13/afero"
)
func TestBuild(t *testing.T) {
// TODO(jackmordaunt) test for correct content of files, not just existence.
tests := []struct {
desc string
fs afero.Fs
root string
entries []Entry
want []string
wantErr bool
}{
{
desc: "unspecified root",
fs: afero.NewMemMapFs(),
root: "",
entries: []Entry{
Directory{Path: "foo", Entries: []Entry{
File{Path: "bar.exe"},
File{Path: "baz.exe"},
}},
File{Path: "bar.exe"},
File{Path: "baz.exe"},
},
want: []string{
"/foo/bar.exe",
"/foo/baz.exe",
"/bar.exe",
"/baz.exe",
},
wantErr: false,
},
{
desc: "specified root",
fs: afero.NewMemMapFs(),
root: "root/path/here",
entries: []Entry{
Directory{Path: "foo", Entries: []Entry{
File{Path: "bar.exe"},
File{Path: "baz.exe"},
}},
File{Path: "bar.exe"},
File{Path: "baz.exe"},
},
want: []string{
"root/path/here/foo/bar.exe",
"root/path/here/foo/baz.exe",
"root/path/here/bar.exe",
"root/path/here/baz.exe",
},
wantErr: false,
},
{
desc: "flat list",
fs: afero.NewMemMapFs(),
root: "",
entries: []Entry{
File{Path: "bar.exe"},
File{Path: "baz.exe"},
File{Path: "foo/bar/baz.exe"},
File{Path: "foo/baz/baz.exe"},
File{Path: "foo/baz.exe"},
Directory{Path: "bar"},
},
want: []string{
"/bar.exe",
"/baz.exe",
"/foo/bar/baz.exe",
"/foo/baz/baz.exe",
"/foo/baz.exe",
"/foo/bar",
"/foo/baz",
"/bar",
},
wantErr: false,
},
{
desc: "unspecfied root and filesystem",
fs: afero.NewOsFs(),
root: "",
entries: []Entry{
File{Path: "bar.exe"},
File{Path: "baz.exe"},
File{Path: "foo/bar/baz.exe"},
File{Path: "foo/baz/baz.exe"},
File{Path: "foo/baz.exe"},
Directory{Path: "bar"},
},
want: nil,
wantErr: true,
},
{
desc: "zip file",
fs: afero.NewMemMapFs(),
root: "",
entries: []Entry{
Zip{
Path: "bar.zip",
Files: []File{
File{Path: "baz.exe"},
File{Path: "foo/bar/baz.exe"},
File{Path: "foo/baz/baz.exe"},
File{Path: "foo/baz.exe"},
},
},
},
want: []string{"/bar.zip"},
wantErr: false,
},
}
for _, tt := range tests {
cleanup, err := Build(tt.fs, tt.root, tt.entries...)
if tt.wantErr {
if err == nil {
t.Errorf("[%s] wanted error, got nil", tt.desc)
}
continue
}
if err != nil {
t.Errorf("[%s] unexpected error while creating files: %v",
tt.desc, err)
}
for _, path := range tt.want {
_, err := tt.fs.Stat(path)
if err != nil {
t.Errorf("[%s] entry '%s' does not exist",
tt.desc, path)
} else if err != nil && !os.IsNotExist(err) {
t.Errorf("[%s] unexpected error while reading entry: %v",
tt.desc, path)
}
}
if err := cleanup(); err != nil {
t.Fatalf("[%s] unexpected error during cleanup: %v", tt.desc, err)
}
for _, path := range tt.want {
_, err := tt.fs.Stat(path)
if err == nil {
t.Errorf("[%s] file '%s' was not cleaned up", tt.desc, path)
}
}
}
}