-
Notifications
You must be signed in to change notification settings - Fork 6
/
main_test.go
271 lines (239 loc) · 20.1 KB
/
main_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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package main
import (
"archive/zip"
"os"
"reflect"
"regexp"
"sort"
"testing"
log "github.com/sirupsen/logrus"
)
func TestBefore(t *testing.T) {
// check if the `./test-output` folder exists (otherwise, create it)
if _, err := os.Stat("." + string(os.PathSeparator) + "test-output"); err != nil {
if os.IsNotExist(err) {
if err := os.Mkdir("."+string(os.PathSeparator)+"test-output", os.ModePerm); err != nil {
log.Fatal(err)
}
}
}
// change the log level to avoid too much logging when running tests
log.SetLevel(log.FatalLevel)
}
// Integration test for `zipSource()` with `./sample-projects/sample-node-project`
func TestZipSourceWithNodeSample(t *testing.T) {
sourcePath := "." + string(os.PathSeparator) + "sample-projects" + string(os.PathSeparator) + "sample-node-project"
targetPath := "." + string(os.PathSeparator) + "test-output" + string(os.PathSeparator) + "test-output.zip"
// generate the zip file and return a list of all its file names
zipFileContents := generateZipAndReturnItsFiles(sourcePath, targetPath, "")
// check if the output conforms with what we expected. To do this, we sort both the expected output and the actual output
// and then compare them.
expectedFilesInOutputZip := []string{
"app.js", "package.json", "package-lock.json", "testimonials-no-tests" + string(os.PathSeparator) + "should-be-included.js",
"distance" + string(os.PathSeparator) + "should-be-included.js", "building" + string(os.PathSeparator) + "something.js",
"bower_components" + string(os.PathSeparator) + "bower.json", "bower_components" + string(os.PathSeparator) + "some-thing.js",
"styles" + string(os.PathSeparator) + "blub.css2",
}
sort.Strings(expectedFilesInOutputZip)
sort.Strings(zipFileContents)
if !reflect.DeepEqual(zipFileContents, expectedFilesInOutputZip) {
t.Error("Test failed!")
t.Errorf("Got: %v", zipFileContents)
t.Errorf("Expected: %v", expectedFilesInOutputZip)
}
}
// Integration test for `zipSource()` with `./sample-projects/sample-node-project/` (note the trailing slash!). The reason
// for this test is that a trailing slash in the `-source` had lead to a bug that gave me quite some headache to figure out.
func TestZipSourceWithNodeSampleAndTrailingSlash(t *testing.T) {
sourcePath := "." + string(os.PathSeparator) + "sample-projects" + string(os.PathSeparator) + "sample-node-project/"
targetPath := "." + string(os.PathSeparator) + "test-output" + string(os.PathSeparator) + "test-output.zip"
// generate the zip file and return a list of all its file names
zipFileContents := generateZipAndReturnItsFiles(sourcePath, targetPath, "")
// check if the output conforms with what we expected. To do this, we sort both the expected output and the actual output
// and then compare them.
expectedFilesInOutputZip := []string{
"app.js", "package.json", "package-lock.json", "testimonials-no-tests" + string(os.PathSeparator) + "should-be-included.js",
"distance" + string(os.PathSeparator) + "should-be-included.js", "building" + string(os.PathSeparator) + "something.js",
"bower_components" + string(os.PathSeparator) + "bower.json", "bower_components" + string(os.PathSeparator) + "some-thing.js",
"styles" + string(os.PathSeparator) + "blub.css2",
}
sort.Strings(expectedFilesInOutputZip)
sort.Strings(zipFileContents)
if !reflect.DeepEqual(zipFileContents, expectedFilesInOutputZip) {
t.Error("Test failed!")
t.Errorf("Got: %v", zipFileContents)
t.Errorf("Expected: %v", expectedFilesInOutputZip)
}
}
// Integration test for `zipSource()` with `./sample-projects/sample-node-project` and `-tests` provided
func TestZipSourceWithNodeSampleWithTestsFlag(t *testing.T) {
sourcePath := "." + string(os.PathSeparator) + "sample-projects" + string(os.PathSeparator) + "sample-node-project"
targetPath := "." + string(os.PathSeparator) + "test-output" + string(os.PathSeparator) + "test-output.zip"
testsPath := "test"
// generate the zip file and return a list of all its file names
zipFileContents := generateZipAndReturnItsFiles(sourcePath, targetPath, testsPath)
// check if the output conforms with what we expected. To do this, we sort both the expected output and the actual output
// and then compare them.
expectedFilesInOutputZip := []string{
"app.js", "package.json", "package-lock.json", "testimonials-no-tests" + string(os.PathSeparator) + "should-be-included.js",
"distance" + string(os.PathSeparator) + "should-be-included.js", "building" + string(os.PathSeparator) + "something.js",
"bower_components" + string(os.PathSeparator) + "bower.json", "bower_components" + string(os.PathSeparator) + "some-thing.js",
"styles" + string(os.PathSeparator) + "blub.css2", "e2e" + string(os.PathSeparator) + "some-more-test.js",
}
sort.Strings(expectedFilesInOutputZip)
sort.Strings(zipFileContents)
if !reflect.DeepEqual(zipFileContents, expectedFilesInOutputZip) {
t.Error("Test failed!")
t.Errorf("Got: %v", zipFileContents)
t.Errorf("Expected: %v", expectedFilesInOutputZip)
}
}
// Integration test for `zipSource()` with `./sample-projects/sample-angular-project`
func TestZipSourceWithAngularSample(t *testing.T) {
log.Info("---------- Running Test: TestZipSourceWithAngularSample ----------")
sourcePath := "." + string(os.PathSeparator) + "sample-projects" + string(os.PathSeparator) + "sample-angular-project"
targetPath := "." + string(os.PathSeparator) + "test-output" + string(os.PathSeparator) + "test-output.zip"
// generate the zip file and return a list of all its file names
zipFileContents := generateZipAndReturnItsFiles(sourcePath, targetPath, "")
// check if the output conforms with what we expected. To do this, we sort both the expected output and the actual output
// and then compare them.
expectedFilesInOutputZip := []string{
"package.json", "package-lock.json", "src" + string(os.PathSeparator) + "main.ts",
"src" + string(os.PathSeparator) + "test.ts",
"src" + string(os.PathSeparator) + "index.html",
"src" + string(os.PathSeparator) + "environments" + string(os.PathSeparator) + "environment.prod.ts",
"src" + string(os.PathSeparator) + "environments" + string(os.PathSeparator) + "environment.ts",
"src" + string(os.PathSeparator) + "app" + string(os.PathSeparator) + "app.component.html",
"src" + string(os.PathSeparator) + "app" + string(os.PathSeparator) + "app-routing.module.ts",
"src" + string(os.PathSeparator) + "app" + string(os.PathSeparator) + "settings" + string(os.PathSeparator) + "settings-routing.module.ts",
"src" + string(os.PathSeparator) + "app" + string(os.PathSeparator) + "settings" + string(os.PathSeparator) + "settings.component.ts",
"src" + string(os.PathSeparator) + "app" + string(os.PathSeparator) + "settings" + string(os.PathSeparator) + "settings.module.ts",
"src" + string(os.PathSeparator) + "app" + string(os.PathSeparator) + "settings" + string(os.PathSeparator) + "settings.component.html",
"src" + string(os.PathSeparator) + "app" + string(os.PathSeparator) + "home" + string(os.PathSeparator) + "home-auth-resolver.service.ts",
"src" + string(os.PathSeparator) + "app" + string(os.PathSeparator) + "home" + string(os.PathSeparator) + "home.component.ts",
"src" + string(os.PathSeparator) + "app" + string(os.PathSeparator) + "home" + string(os.PathSeparator) + "home.module.ts",
"src" + string(os.PathSeparator) + "app" + string(os.PathSeparator) + "home" + string(os.PathSeparator) + "home-routing.module.ts",
"src" + string(os.PathSeparator) + "app" + string(os.PathSeparator) + "home" + string(os.PathSeparator) + "home.component.html",
"src" + string(os.PathSeparator) + "app" + string(os.PathSeparator) + "core" + string(os.PathSeparator) + "interceptors" + string(os.PathSeparator) + "http.token.interceptor.ts",
"src" + string(os.PathSeparator) + "app" + string(os.PathSeparator) + "core" + string(os.PathSeparator) + "interceptors" + string(os.PathSeparator) + "index.ts",
"src" + string(os.PathSeparator) + "app" + string(os.PathSeparator) + "core" + string(os.PathSeparator) + "models" + string(os.PathSeparator) + "user.model.ts",
"src" + string(os.PathSeparator) + "app" + string(os.PathSeparator) + "core" + string(os.PathSeparator) + "models" + string(os.PathSeparator) + "comment.model.ts",
"src" + string(os.PathSeparator) + "app" + string(os.PathSeparator) + "core" + string(os.PathSeparator) + "models" + string(os.PathSeparator) + "article-list-config.model.ts",
"src" + string(os.PathSeparator) + "app" + string(os.PathSeparator) + "core" + string(os.PathSeparator) + "models" + string(os.PathSeparator) + "profile.model.ts",
"src" + string(os.PathSeparator) + "app" + string(os.PathSeparator) + "core" + string(os.PathSeparator) + "models" + string(os.PathSeparator) + "index.ts",
"src" + string(os.PathSeparator) + "app" + string(os.PathSeparator) + "core" + string(os.PathSeparator) + "models" + string(os.PathSeparator) + "errors.model.ts",
"src" + string(os.PathSeparator) + "app" + string(os.PathSeparator) + "core" + string(os.PathSeparator) + "models" + string(os.PathSeparator) + "article.model.ts",
"src" + string(os.PathSeparator) + "app" + string(os.PathSeparator) + "core" + string(os.PathSeparator) + "core.module.ts",
"src" + string(os.PathSeparator) + "app" + string(os.PathSeparator) + "core" + string(os.PathSeparator) + "index.ts",
"src" + string(os.PathSeparator) + "app" + string(os.PathSeparator) + "core" + string(os.PathSeparator) + "services" + string(os.PathSeparator) + "api.service.ts",
"src" + string(os.PathSeparator) + "app" + string(os.PathSeparator) + "core" + string(os.PathSeparator) + "services" + string(os.PathSeparator) + "comments.service.ts",
"src" + string(os.PathSeparator) + "app" + string(os.PathSeparator) + "core" + string(os.PathSeparator) + "services" + string(os.PathSeparator) + "profiles.service.ts",
"src" + string(os.PathSeparator) + "app" + string(os.PathSeparator) + "core" + string(os.PathSeparator) + "services" + string(os.PathSeparator) + "tags.service.ts",
"src" + string(os.PathSeparator) + "app" + string(os.PathSeparator) + "core" + string(os.PathSeparator) + "services" + string(os.PathSeparator) + "jwt.service.ts",
"src" + string(os.PathSeparator) + "app" + string(os.PathSeparator) + "core" + string(os.PathSeparator) + "services" + string(os.PathSeparator) + "auth-guard.service.ts",
"src" + string(os.PathSeparator) + "app" + string(os.PathSeparator) + "core" + string(os.PathSeparator) + "services" + string(os.PathSeparator) + "user.service.ts",
"src" + string(os.PathSeparator) + "app" + string(os.PathSeparator) + "core" + string(os.PathSeparator) + "services" + string(os.PathSeparator) + "index.ts",
"src" + string(os.PathSeparator) + "app" + string(os.PathSeparator) + "core" + string(os.PathSeparator) + "services" + string(os.PathSeparator) + "articles.service.ts",
"src" + string(os.PathSeparator) + "app" + string(os.PathSeparator) + "auth" + string(os.PathSeparator) + "auth.component.ts",
"src" + string(os.PathSeparator) + "app" + string(os.PathSeparator) + "auth" + string(os.PathSeparator) + "no-auth-guard.service.ts",
"src" + string(os.PathSeparator) + "app" + string(os.PathSeparator) + "auth" + string(os.PathSeparator) + "auth-routing.module.ts",
"src" + string(os.PathSeparator) + "app" + string(os.PathSeparator) + "auth" + string(os.PathSeparator) + "auth.module.ts",
"src" + string(os.PathSeparator) + "app" + string(os.PathSeparator) + "auth" + string(os.PathSeparator) + "auth.component.html",
"src" + string(os.PathSeparator) + "app" + string(os.PathSeparator) + "shared" + string(os.PathSeparator) + "list-errors.component.html",
"src" + string(os.PathSeparator) + "app" + string(os.PathSeparator) + "shared" + string(os.PathSeparator) + "buttons" + string(os.PathSeparator) + "follow-button.component.ts",
"src" + string(os.PathSeparator) + "app" + string(os.PathSeparator) + "shared" + string(os.PathSeparator) + "buttons" + string(os.PathSeparator) + "follow-button.component.html",
"src" + string(os.PathSeparator) + "app" + string(os.PathSeparator) + "shared" + string(os.PathSeparator) + "buttons" + string(os.PathSeparator) + "favorite-button.component.html",
"src" + string(os.PathSeparator) + "app" + string(os.PathSeparator) + "shared" + string(os.PathSeparator) + "buttons" + string(os.PathSeparator) + "index.ts",
"src" + string(os.PathSeparator) + "app" + string(os.PathSeparator) + "shared" + string(os.PathSeparator) + "buttons" + string(os.PathSeparator) + "favorite-button.component.ts",
"src" + string(os.PathSeparator) + "app" + string(os.PathSeparator) + "shared" + string(os.PathSeparator) + "layout" + string(os.PathSeparator) + "header.component.html",
"src" + string(os.PathSeparator) + "app" + string(os.PathSeparator) + "shared" + string(os.PathSeparator) + "layout" + string(os.PathSeparator) + "header.component.ts",
"src" + string(os.PathSeparator) + "app" + string(os.PathSeparator) + "shared" + string(os.PathSeparator) + "layout" + string(os.PathSeparator) + "footer.component.ts",
"src" + string(os.PathSeparator) + "app" + string(os.PathSeparator) + "shared" + string(os.PathSeparator) + "layout" + string(os.PathSeparator) + "index.ts",
"src" + string(os.PathSeparator) + "app" + string(os.PathSeparator) + "shared" + string(os.PathSeparator) + "layout" + string(os.PathSeparator) + "footer.component.html",
"src" + string(os.PathSeparator) + "app" + string(os.PathSeparator) + "shared" + string(os.PathSeparator) + "article-helpers" + string(os.PathSeparator) + "article-list.component.ts",
"src" + string(os.PathSeparator) + "app" + string(os.PathSeparator) + "shared" + string(os.PathSeparator) + "article-helpers" + string(os.PathSeparator) + "article-preview.component.ts",
"src" + string(os.PathSeparator) + "app" + string(os.PathSeparator) + "shared" + string(os.PathSeparator) + "article-helpers" + string(os.PathSeparator) + "article-meta.component.ts",
"src" + string(os.PathSeparator) + "app" + string(os.PathSeparator) + "shared" + string(os.PathSeparator) + "article-helpers" + string(os.PathSeparator) + "index.ts",
"src" + string(os.PathSeparator) + "app" + string(os.PathSeparator) + "shared" + string(os.PathSeparator) + "article-helpers" + string(os.PathSeparator) + "article-meta.component.html",
"src" + string(os.PathSeparator) + "app" + string(os.PathSeparator) + "shared" + string(os.PathSeparator) + "article-helpers" + string(os.PathSeparator) + "article-preview.component.html",
"src" + string(os.PathSeparator) + "app" + string(os.PathSeparator) + "shared" + string(os.PathSeparator) + "article-helpers" + string(os.PathSeparator) + "article-list.component.html",
"src" + string(os.PathSeparator) + "app" + string(os.PathSeparator) + "shared" + string(os.PathSeparator) + "show-authed.directive.ts",
"src" + string(os.PathSeparator) + "app" + string(os.PathSeparator) + "shared" + string(os.PathSeparator) + "shared.module.ts",
"src" + string(os.PathSeparator) + "app" + string(os.PathSeparator) + "shared" + string(os.PathSeparator) + "index.ts",
"src" + string(os.PathSeparator) + "app" + string(os.PathSeparator) + "shared" + string(os.PathSeparator) + "list-errors.component.ts",
"src" + string(os.PathSeparator) + "app" + string(os.PathSeparator) + "app.module.ts",
"src" + string(os.PathSeparator) + "app" + string(os.PathSeparator) + "app.component.ts",
"src" + string(os.PathSeparator) + "app" + string(os.PathSeparator) + "profile" + string(os.PathSeparator) + "profile-favorites.component.ts",
"src" + string(os.PathSeparator) + "app" + string(os.PathSeparator) + "profile" + string(os.PathSeparator) + "profile.component.html",
"src" + string(os.PathSeparator) + "app" + string(os.PathSeparator) + "profile" + string(os.PathSeparator) + "profile-resolver.service.ts",
"src" + string(os.PathSeparator) + "app" + string(os.PathSeparator) + "profile" + string(os.PathSeparator) + "profile-articles.component.html",
"src" + string(os.PathSeparator) + "app" + string(os.PathSeparator) + "profile" + string(os.PathSeparator) + "profile.module.ts",
"src" + string(os.PathSeparator) + "app" + string(os.PathSeparator) + "profile" + string(os.PathSeparator) + "profile.component.ts",
"src" + string(os.PathSeparator) + "app" + string(os.PathSeparator) + "profile" + string(os.PathSeparator) + "profile-routing.module.ts",
"src" + string(os.PathSeparator) + "app" + string(os.PathSeparator) + "profile" + string(os.PathSeparator) + "profile-favorites.component.html",
"src" + string(os.PathSeparator) + "app" + string(os.PathSeparator) + "profile" + string(os.PathSeparator) + "profile-articles.component.ts",
"src" + string(os.PathSeparator) + "app" + string(os.PathSeparator) + "index.ts",
"src" + string(os.PathSeparator) + "app" + string(os.PathSeparator) + "article" + string(os.PathSeparator) + "article.component.html",
"src" + string(os.PathSeparator) + "app" + string(os.PathSeparator) + "article" + string(os.PathSeparator) + "article-comment.component.ts",
"src" + string(os.PathSeparator) + "app" + string(os.PathSeparator) + "article" + string(os.PathSeparator) + "article-comment.component.html",
"src" + string(os.PathSeparator) + "app" + string(os.PathSeparator) + "article" + string(os.PathSeparator) + "article.component.ts",
"src" + string(os.PathSeparator) + "app" + string(os.PathSeparator) + "article" + string(os.PathSeparator) + "article.module.ts",
"src" + string(os.PathSeparator) + "app" + string(os.PathSeparator) + "article" + string(os.PathSeparator) + "markdown.pipe.ts",
"src" + string(os.PathSeparator) + "app" + string(os.PathSeparator) + "article" + string(os.PathSeparator) + "article-resolver.service.ts",
"src" + string(os.PathSeparator) + "app" + string(os.PathSeparator) + "article" + string(os.PathSeparator) + "article-routing.module.ts",
"src" + string(os.PathSeparator) + "app" + string(os.PathSeparator) + "editor" + string(os.PathSeparator) + "editor.component.html",
"src" + string(os.PathSeparator) + "app" + string(os.PathSeparator) + "editor" + string(os.PathSeparator) + "editor-routing.module.ts",
"src" + string(os.PathSeparator) + "app" + string(os.PathSeparator) + "editor" + string(os.PathSeparator) + "editable-article-resolver.service.ts",
"src" + string(os.PathSeparator) + "app" + string(os.PathSeparator) + "editor" + string(os.PathSeparator) + "editor.module.ts",
"src" + string(os.PathSeparator) + "app" + string(os.PathSeparator) + "editor" + string(os.PathSeparator) + "editor.component.ts",
}
sort.Strings(expectedFilesInOutputZip)
sort.Strings(zipFileContents)
if !reflect.DeepEqual(zipFileContents, expectedFilesInOutputZip) {
t.Error("Test failed!")
t.Errorf("Got: %v", zipFileContents)
t.Errorf("Expected: %v", expectedFilesInOutputZip)
}
log.Info("---------- Finished Test: TestZipSourceWithAngularSample ----------\n\n")
}
func generateZipAndReturnItsFiles(sourcePath string, targetPath string, testsPath string) []string {
// generate the zip file, and omit all non-required files
if err := zipSource(sourcePath, targetPath, testsPath); err != nil {
log.Fatal(err)
}
// read the output zip file (e.g. `./test-output/test-output.zip`) into memory
zipReader := readZip(targetPath)
// iterate over all the files from the zip archive and get all a list of all files (similar to the output of the `tree` command)
var zipFileContents []string
for _, zipFile := range zipReader.File {
// `zipFile.Name` contains all the paths within the zip file. It contains an element for each file (e.g. `/src/app.js`),
// but also an element for each folder (e.g. `/src/`). We only care about files and thus, omit every path that does not
// belong to a file (i.e., does not end in `.<something>`)
if !isPathAFile(zipFile.Name) {
log.Info("Omitted path: ", zipFile.Name)
continue
}
// we keep everything that is a file
log.Info("Keeping file:", zipFile.Name)
zipFileContents = append(zipFileContents, zipFile.Name)
}
return zipFileContents
}
// reads the zip file for the provided `zipPath` into memory and returns it
func readZip(zipPath string) *zip.ReadCloser {
r, err := zip.OpenReader(zipPath)
if err != nil {
log.Fatal(err)
}
defer r.Close()
return r
}
// check if a provided path (e.g. `/some/thing/app.js` or `/some/thing/`) belongs to a file
func isPathAFile(path string) bool {
isFileRegex := ".+\\.[a-zA-Z0-9]{1,8}$"
doesMatch, err := regexp.MatchString(isFileRegex, path)
if err != nil {
log.Error(err)
}
return doesMatch
}