1 | var grunt = require('grunt'), |
---|
2 | fs = require('fs'), |
---|
3 | _ = require('underscore.string'); |
---|
4 | |
---|
5 | /* |
---|
6 | ======== A Handy Little Nodeunit Reference ======== |
---|
7 | https://github.com/caolan/nodeunit |
---|
8 | |
---|
9 | Test methods: |
---|
10 | test.expect(numAssertions) |
---|
11 | test.done() |
---|
12 | Test assertions: |
---|
13 | test.ok(value, [message]) |
---|
14 | test.equal(actual, expected, [message]) |
---|
15 | test.notEqual(actual, expected, [message]) |
---|
16 | test.deepEqual(actual, expected, [message]) |
---|
17 | test.notDeepEqual(actual, expected, [message]) |
---|
18 | test.strictEqual(actual, expected, [message]) |
---|
19 | test.notStrictEqual(actual, expected, [message]) |
---|
20 | test.throws(block, [error], [message]) |
---|
21 | test.doesNotThrow(block, [error], [message]) |
---|
22 | test.ifError(value) |
---|
23 | */ |
---|
24 | |
---|
25 | function addMethods(test) { |
---|
26 | // Assert two files are equal |
---|
27 | test.equalFiles = function (filename) { |
---|
28 | // Read in content |
---|
29 | var expectedContent = fs.readFileSync('expected/' + filename, 'binary'), |
---|
30 | actualContent = fs.readFileSync('actual/' + filename, 'binary'); |
---|
31 | |
---|
32 | // Assert that the content is *exactly* the same |
---|
33 | test.strictEqual(actualContent, expectedContent, filename + ' does not have the same content in `expected` as `actual`'); |
---|
34 | }; |
---|
35 | |
---|
36 | // Assert two files are close enough |
---|
37 | // ANTI-PATTERN: 3 specifically ordered/non-modular parameters =( |
---|
38 | test.closeFiles = function (filename, distance) { |
---|
39 | // Read in the content |
---|
40 | var expectedContent = fs.readFileSync('expected/' + filename, 'binary'), |
---|
41 | actualContent = fs.readFileSync('actual/' + filename, 'binary'); |
---|
42 | |
---|
43 | // Calculate the difference in bits (accounts for random bits) |
---|
44 | var difference = _.levenshtein(expectedContent, actualContent); |
---|
45 | |
---|
46 | // Assert that we are under our threshold |
---|
47 | var underThreshold = difference <= distance; |
---|
48 | test.ok(underThreshold, 'Bitwise difference of zip files "' + difference + '" should be under ' + distance + ' (' + filename + ')'); |
---|
49 | }; |
---|
50 | |
---|
51 | // Assert file does not exist |
---|
52 | test.noFile = function (filename) { |
---|
53 | try { |
---|
54 | // Attempt to grab stats on the would-be location |
---|
55 | fs.statSync('actual/' + filename); |
---|
56 | |
---|
57 | // Fail since there are statistics (should be file not found) |
---|
58 | test.fail('File "' + filename + '" was found when it was expected to not exist'); |
---|
59 | } catch (e) { |
---|
60 | // Verify the error is ENOENT |
---|
61 | test.strictEqual(e.code, 'ENOENT', filename + ' exists'); |
---|
62 | } |
---|
63 | }; |
---|
64 | } |
---|
65 | |
---|
66 | exports['zip'] = { |
---|
67 | 'singleZip': function (test) { |
---|
68 | // Set up |
---|
69 | test.expect(1); |
---|
70 | addMethods(test); |
---|
71 | |
---|
72 | // Assert single_zip is close enough and return |
---|
73 | test.closeFiles('single_zip/file.zip', 15); |
---|
74 | test.done(); |
---|
75 | }, |
---|
76 | 'multiZip': function (test) { |
---|
77 | // Set up |
---|
78 | test.expect(1); |
---|
79 | addMethods(test); |
---|
80 | |
---|
81 | // Assert single_zip is close enough and return |
---|
82 | test.closeFiles('multi_zip/file.zip', 30); |
---|
83 | test.done(); |
---|
84 | }, |
---|
85 | 'singleUnzip': function (test) { |
---|
86 | // Add in test methods |
---|
87 | test.expect(2); |
---|
88 | addMethods(test); |
---|
89 | |
---|
90 | // Compare a and b |
---|
91 | test.equalFiles('single_unzip/a.js'); |
---|
92 | test.equalFiles('single_unzip/b.js'); |
---|
93 | |
---|
94 | // Return |
---|
95 | test.done(); |
---|
96 | }, |
---|
97 | 'nestedUnzip': function (test) { |
---|
98 | test.expect(8); |
---|
99 | addMethods(test); |
---|
100 | |
---|
101 | // Compare all nested unzip files |
---|
102 | test.equalFiles('nested_unzip/bootstrap/css/bootstrap-responsive.css'); |
---|
103 | test.equalFiles('nested_unzip/bootstrap/css/bootstrap-responsive.min.css'); |
---|
104 | test.equalFiles('nested_unzip/bootstrap/css/bootstrap.css'); |
---|
105 | test.equalFiles('nested_unzip/bootstrap/css/bootstrap.min.css'); |
---|
106 | test.equalFiles('nested_unzip/bootstrap/img/glyphicons-halflings-white.png'); |
---|
107 | test.equalFiles('nested_unzip/bootstrap/img/glyphicons-halflings.png'); |
---|
108 | test.equalFiles('nested_unzip/bootstrap/js/bootstrap.js'); |
---|
109 | test.equalFiles('nested_unzip/bootstrap/js/bootstrap.min.js'); |
---|
110 | |
---|
111 | test.done(); |
---|
112 | }, |
---|
113 | 'image': function (test) { |
---|
114 | // Set up |
---|
115 | test.expect(1); |
---|
116 | addMethods(test); |
---|
117 | |
---|
118 | // Assert the image is the same as when it went in |
---|
119 | test.equalFiles('image_zip/unzip/test_files/smile.gif'); |
---|
120 | test.done(); |
---|
121 | }, |
---|
122 | 'nestedZip': function (test) { |
---|
123 | // Set up |
---|
124 | test.expect(5); |
---|
125 | addMethods(test); |
---|
126 | |
---|
127 | // Assert all files are the same as they went in |
---|
128 | test.equalFiles('nested_zip/unzip/test_files/nested/hello.js'); |
---|
129 | test.equalFiles('nested_zip/unzip/test_files/nested/world.txt'); |
---|
130 | test.equalFiles('nested_zip/unzip/test_files/nested/glyphicons-halflings.png'); |
---|
131 | test.equalFiles('nested_zip/unzip/test_files/nested/nested2/hello10.txt'); |
---|
132 | test.equalFiles('nested_zip/unzip/test_files/nested/nested2/hello20.js'); |
---|
133 | |
---|
134 | // Return |
---|
135 | test.done(); |
---|
136 | }, |
---|
137 | 'routerZip': function (test) { |
---|
138 | // Set up |
---|
139 | test.expect(2); |
---|
140 | addMethods(test); |
---|
141 | |
---|
142 | // Assert all files are the same as they went in |
---|
143 | test.equalFiles('router_zip/unzip/hello.js'); |
---|
144 | test.equalFiles('router_zip/unzip/hello10.txt'); |
---|
145 | |
---|
146 | // Return |
---|
147 | test.done(); |
---|
148 | }, |
---|
149 | 'routerUnzip': function (test) { |
---|
150 | test.expect(8); |
---|
151 | addMethods(test); |
---|
152 | |
---|
153 | // Compare all router unzip files |
---|
154 | test.equalFiles('router_unzip/bootstrap-responsive.css'); |
---|
155 | test.equalFiles('router_unzip/bootstrap-responsive.min.css'); |
---|
156 | test.equalFiles('router_unzip/bootstrap.css'); |
---|
157 | test.equalFiles('router_unzip/bootstrap.min.css'); |
---|
158 | test.equalFiles('router_unzip/glyphicons-halflings-white.png'); |
---|
159 | test.equalFiles('router_unzip/glyphicons-halflings.png'); |
---|
160 | test.equalFiles('router_unzip/bootstrap.js'); |
---|
161 | test.equalFiles('router_unzip/bootstrap.min.js'); |
---|
162 | |
---|
163 | test.done(); |
---|
164 | }, |
---|
165 | 'cwdZip': function (test) { |
---|
166 | // Set up |
---|
167 | test.expect(2); |
---|
168 | addMethods(test); |
---|
169 | |
---|
170 | // Assert all files are the same as they went in |
---|
171 | test.equalFiles('cwd_zip/unzip/hello.js'); |
---|
172 | test.equalFiles('cwd_zip/unzip/nested2/hello10.txt'); |
---|
173 | |
---|
174 | // Return |
---|
175 | test.done(); |
---|
176 | }, |
---|
177 | 'dotZip': function (test) { |
---|
178 | // Set up |
---|
179 | test.expect(2); |
---|
180 | addMethods(test); |
---|
181 | |
---|
182 | // Assert all files are the same as they went in |
---|
183 | test.equalFiles('dot_zip/unzip/test_files/dot/.test/hello.js'); |
---|
184 | test.equalFiles('dot_zip/unzip/test_files/dot/test/.examplerc'); |
---|
185 | |
---|
186 | // Return |
---|
187 | test.done(); |
---|
188 | }, |
---|
189 | 'skipFilesZip': function (test) { |
---|
190 | // Set up |
---|
191 | test.expect(2); |
---|
192 | addMethods(test); |
---|
193 | |
---|
194 | // Assert all files are the same as they went in |
---|
195 | test.equalFiles('skip_files_zip/unzip/test_files/nested/hello.js'); |
---|
196 | test.noFile('skip_files_zip/unzip/test_files/nested/nested2/hello10.txt'); |
---|
197 | |
---|
198 | // Return |
---|
199 | test.done(); |
---|
200 | }, |
---|
201 | 'skipFilesUnzip': function (test) { |
---|
202 | test.expect(8); |
---|
203 | addMethods(test); |
---|
204 | |
---|
205 | // Assert CSS files do not exist |
---|
206 | test.noFile('skip_files_unzip/bootstrap/css/bootstrap-responsive.css'); |
---|
207 | test.noFile('skip_files_unzip/bootstrap/css/bootstrap-responsive.min.css'); |
---|
208 | test.noFile('skip_files_unzip/bootstrap/css/bootstrap.css'); |
---|
209 | test.noFile('skip_files_unzip/bootstrap/css/bootstrap.min.css'); |
---|
210 | |
---|
211 | // Assert other files do exist |
---|
212 | test.equalFiles('skip_files_unzip/bootstrap/img/glyphicons-halflings-white.png'); |
---|
213 | test.equalFiles('skip_files_unzip/bootstrap/img/glyphicons-halflings.png'); |
---|
214 | test.equalFiles('skip_files_unzip/bootstrap/js/bootstrap.js'); |
---|
215 | test.equalFiles('skip_files_unzip/bootstrap/js/bootstrap.min.js'); |
---|
216 | |
---|
217 | test.done(); |
---|
218 | } |
---|
219 | }; |
---|