-
Notifications
You must be signed in to change notification settings - Fork 324
/
binary-test.js
632 lines (615 loc) · 23.5 KB
/
binary-test.js
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
var vows = require('vows');
var assert = require('assert');
var exec = require('child_process').exec;
var fs = require('fs');
var http = require('http');
var httpProxy = require('http-proxy');
var path = require('path');
var url = require('url');
var SourceMapConsumer = require('source-map').SourceMapConsumer;
var isWindows = process.platform == 'win32';
var lineBreakRegExp = new RegExp(require('os').EOL, 'g');
var binaryContext = function (options, context) {
if (isWindows)
return {};
context.topic = function () {
// We add __DIRECT__=1 to force binary into 'non-piped' mode
exec('__DIRECT__=1 ./bin/cleancss ' + options, this.callback);
};
return context;
};
var pipedContext = function (css, options, context) {
if (isWindows)
return {};
context.topic = function () {
exec('echo "' + css + '" | ./bin/cleancss ' + options, this.callback);
};
return context;
};
var unixOnlyContext = function (context) {
return isWindows ? {} : context;
};
var readFile = function (filename) {
return fs.readFileSync(filename, { encoding: 'utf-8' }).replace(lineBreakRegExp, '');
};
var deleteFile = function (filename) {
if (isWindows)
exec('del /q /f ' + filename);
else
exec('rm ' + filename);
};
vows.describe('./bin/cleancss')
.addBatch({
'no options': binaryContext('', {
'should output help': function (stdout) {
assert.match(stdout, /Usage[:]/);
}
})
})
.addBatch({
'help': binaryContext('-h', {
'should output help': function (error, stdout) {
assert.match(stdout, /Usage[:]/);
},
'should output one file example': function (error, stdout) {
assert.include(stdout, 'cleancss -o one-min.css one.css');
},
'should output multiple files example': function (error, stdout) {
assert.include(stdout, 'cleancss -o merged-and-minified.css one.css two.css three.css');
},
'should output gzipping multiple files example': function (error, stdout) {
assert.include(stdout, 'cleancss one.css two.css three.css | gzip -9 -c > merged-minified-and-gzipped.css.gz');
}
})
})
.addBatch({
'version': binaryContext('-v', {
'should output help': function (error, stdout) {
var version = JSON.parse(fs.readFileSync('./package.json')).version;
assert.equal(stdout, version + '\n');
}
})
})
.addBatch({
'stdin': pipedContext('a{color: #f00}', '', {
'should output data': function (error, stdout) {
assert.equal(stdout, 'a{color:red}');
}
})
})
.addBatch({
'format': pipedContext('a{color: #f00}', '--format beautify', {
'outputs right styles': function (error, stdout) {
assert.equal(stdout, 'a {\n color: red\n}');
}
})
})
.addBatch({
'strip all but first comment': pipedContext('/*!1st*//*! 2nd */a{display:block}', '-O1 specialComments:1', {
'should keep the 2nd comment': function (error, stdout) {
assert.equal(stdout, '/*!1st*/a{display:block}');
}
})
})
.addBatch({
'strip all comments': pipedContext('/*!1st*//*! 2nd */a{display:block}', '-O1 specialComments:0', {
'should keep the 2nd comment': function (error, stdout) {
assert.equal(stdout, 'a{display:block}');
}
})
})
.addBatch({
'piped with debug info': pipedContext('a{color: #f00;}', '-d', {
'should output content to stdout and debug info to stderr': function (error, stdout, stderr) {
assert.equal(stdout, 'a{color:red}');
assert.notEqual(stderr, '');
assert.include(stderr, 'Time spent:');
assert.include(stderr, 'Original: 16 bytes');
assert.include(stderr, 'Minified: 12 bytes');
assert.include(stderr, 'Efficiency: 25%');
}
})
})
.addBatch({
'piped with debug info on inlining 123': pipedContext('@import url(test/fixtures/imports-min.css);', '-d', {
'should output inlining info': function (error, stdout, stderr) {
assert.include(stderr, path.join(process.cwd(), 'test/fixtures/imports-min.css'));
}
})
})
.addBatch({
'piped with correct debug info on inlining': pipedContext('@import url(test/fixtures/imports.css);', '-d', {
'should output correct info': function (error, stdout, stderr) {
assert.include(stderr, 'Original: 339 bytes');
assert.include(stderr, 'Minified: 86 bytes');
assert.include(stderr, 'Efficiency: 74.63%');
}
})
})
.addBatch({
'to output file with debug info': pipedContext('a{color: #f00;}', '-d -o debug.css', {
'should output nothing to stdout and debug info to stderr': function (error, stdout, stderr) {
assert.isEmpty(stdout);
assert.notEqual(stderr, '');
assert.include(stderr, 'Time spent:');
assert.include(stderr, 'Original: 16 bytes');
assert.include(stderr, 'Minified: 12 bytes');
assert.include(stderr, 'Efficiency: 25%');
},
'should output content to file': function () {
var minimized = readFile('debug.css');
assert.equal(minimized, 'a{color:red}');
},
teardown: function () {
deleteFile('debug.css');
}
})
})
.addBatch({
'skip level 2 optimizations': pipedContext('a{color:red}p{color:red}', '-O1', {
'should do basic optimizations only': function (error, stdout) {
assert.equal(stdout, 'a{color:red}p{color:red}');
}
})
})
.addBatch({
'level 1 and 2 optimizations': pipedContext('a{font:"Arial"}a{color:red}', '-O1 all:false,removeQuotes:true -O2 all:false', {
'should do basic optimizations only': function (error, stdout) {
assert.equal(stdout, 'a{font:Arial}a{color:red}');
}
})
})
.addBatch({
'enable restructuring optimizations': pipedContext('div{margin-top:0}.one{margin:0}.two{display:block;margin-top:0}', '-O2 restructureRules:on', {
'should do basic optimizations only': function (error, stdout) {
assert.equal(stdout, '.two,div{margin-top:0}.one{margin:0}.two{display:block}');
}
})
})
.addBatch({
'no relative to path': binaryContext('./fixtures/partials-absolute/base.css', {
'should not be able to resolve it fully': function (error, stdout, stderr) {
assert.isEmpty(stdout);
assert.notEqual(error, null);
assert.notEqual(stderr, '');
}
})
})
.addBatch({
'from source': binaryContext('-O2 ./test/fixtures/reset.css', {
'should minimize': function (error, stdout) {
var minimized = fs.readFileSync('./test/fixtures/reset-min.css', 'utf-8').replace(lineBreakRegExp, '');
assert.equal(stdout, minimized);
}
})
})
.addBatch({
'from multiple sources': binaryContext('./test/fixtures/partials/one.css ./test/fixtures/partials/five.css', {
'should minimize all': function (error, stdout) {
assert.equal(stdout, '.one{color:red}.five{background:url(data:image/jpeg;base64,/9j/)}');
}
})
})
.addBatch({
'to file': binaryContext('-O2 -o ./reset1-min.css ./test/fixtures/reset.css', {
'should give no output': function (error, stdout) {
assert.isEmpty(stdout);
},
'should minimize': function () {
var preminified = readFile('./test/fixtures/reset-min.css');
var minified = readFile('./reset1-min.css');
assert.equal(minified, preminified);
},
teardown: function () {
deleteFile('./reset1-min.css');
}
})
})
.addBatch({
'disable @import': binaryContext('--inline none ./test/fixtures/imports.css', {
'should disable the import processing': function (error, stdout) {
assert.equal(stdout, '@import url(test/fixtures/partials/one.css);@import url(test/fixtures/partials/two.css);.imports{color:#000}');
}
})
})
.addBatch({
'disable all @import': pipedContext('@import url(http://127.0.0.1/remote.css);@import url(test/fixtures/partials/one.css);', '--inline none', {
'keeps original import rules': function (error, stdout) {
assert.equal(stdout, '@import url(http://127.0.0.1/remote.css);@import url(test/fixtures/partials/one.css);');
}
}),
'disable remote @import': pipedContext('@import url(http://127.0.0.1/remote.css);@import url(test/fixtures/partials/one.css);', '--inline !remote', {
'keeps remote import rule': function (error, stdout) {
assert.equal(stdout, '@import url(http://127.0.0.1/remote.css);.one{color:red}');
}
}),
'disable remote @import as default': pipedContext('@import url(http://127.0.0.1/remote.css);@import url(test/fixtures/partials/one.css);', '', {
'keeps remote import rule': function (error, stdout) {
assert.equal(stdout, '@import url(http://127.0.0.1/remote.css);.one{color:red}');
}
}),
'disable remote @import by host': pipedContext('@import url(http://127.0.0.1/remote.css);@import url(test/fixtures/partials/one.css);', '--inline !127.0.0.1', {
'keeps remote import rule': function (error, stdout) {
assert.equal(stdout, '@import url(http://127.0.0.1/remote.css);.one{color:red}');
}
})
})
.addBatch({
'relative image paths': {
'no output': binaryContext('./test/fixtures/partials-relative/base.css', {
'should leave paths': function (error, stdout) {
assert.equal(stdout, 'a{background:url(test/fixtures/partials/extra/down.gif) 0 0 no-repeat}');
}
}),
'output': binaryContext('-o ./base1-min.css ./test/fixtures/partials-relative/base.css', {
'should rewrite path relative to current path': function () {
var minimized = readFile('./base1-min.css');
assert.equal(minimized, 'a{background:url(test/fixtures/partials/extra/down.gif) 0 0 no-repeat}');
},
teardown: function () {
deleteFile('./base1-min.css');
}
}),
'piped with output': pipedContext('a{background:url(test/fixtures/partials/extra/down.gif)}', '-o base3-min.css', {
'should keep paths as they are': function () {
var minimized = readFile('base3-min.css');
assert.equal(minimized, 'a{background:url(test/fixtures/partials/extra/down.gif)}');
},
teardown: function () {
deleteFile('base3-min.css');
}
})
}
})
.addBatch({
'import rebasing': binaryContext('test/fixtures/partials/quoted-svg.css', {
'should keep quoting intact': function (error, stdout) {
assert.include(stdout, 'div{background:url("data:image');
assert.include(stdout, 'svg%3E")}');
}
})
})
.addBatch({
'complex import and url rebasing': {
'absolute': binaryContext('./test/fixtures/129-assets/assets/ui.css', {
'should rebase urls correctly': function (error, stdout) {
assert.include(stdout, 'url(test/fixtures/129-assets/components/bootstrap/images/glyphs.gif)');
assert.include(stdout, 'url(test/fixtures/129-assets/components/jquery-ui/images/prev.gif)');
assert.include(stdout, 'url(test/fixtures/129-assets/components/jquery-ui/images/next.gif)');
}
}),
'relative': binaryContext('-o test/ui.bundled.css ./test/fixtures/129-assets/assets/ui.css', {
'should rebase urls correctly': function () {
var minimized = readFile('test/ui.bundled.css');
assert.include(minimized, 'url(fixtures/129-assets/components/bootstrap/images/glyphs.gif)');
assert.include(minimized, 'url(fixtures/129-assets/components/jquery-ui/images/prev.gif)');
assert.include(minimized, 'url(fixtures/129-assets/components/jquery-ui/images/next.gif)');
},
teardown: function () {
deleteFile('test/ui.bundled.css');
}
})
}
})
.addBatch({
'complex import and skipped url rebasing': {
'absolute': binaryContext('--skip-rebase ./test/fixtures/129-assets/assets/ui.css', {
'should rebase urls correctly': function (error, stdout) {
assert.isNull(error);
assert.include(stdout, 'url(../images/glyphs.gif)');
assert.include(stdout, 'url(../images/prev.gif)');
assert.include(stdout, 'url(../images/next.gif)');
}
})
}
})
.addBatch({
'remote import': {
topic: function () {
this.server = http.createServer(function (req, res) {
res.writeHead(200);
res.end('p{font-size:13px}');
}).listen(31991, '127.0.0.1');
this.callback(null);
},
'of a file': binaryContext('http://127.0.0.1:31991/present.css', {
succeeds: function (error, stdout) {
assert.isNull(error);
assert.equal(stdout, 'p{font-size:13px}');
}
}),
teardown: function () {
this.server.close();
}
}
})
.addBatch({
'timeout': unixOnlyContext({
topic: function () {
var self = this;
var source = '@import url(http://localhost:24682/timeout.css);';
this.server = http.createServer(function () {
setTimeout(function () {}, 1000);
});
this.server.listen('24682', function () {
exec('echo "' + source + '" | ./bin/cleancss --inline all --inline-timeout 0.01', self.callback);
});
},
'should raise warning': function (error, stdout, stderr) {
assert.include(stderr, 'Broken @import declaration of "http://localhost:24682/timeout.css" - timeout');
},
'should output empty response': function (error, stdout) {
assert.isEmpty(stdout);
},
teardown: function () {
this.server.close();
}
})
})
.addBatch({
'HTTP proxy': unixOnlyContext({
topic: function () {
var self = this;
this.proxied = false;
var proxy = httpProxy.createProxyServer();
this.proxyServer = http.createServer(function (req, res) {
self.proxied = true;
proxy.web(req, res, { target: 'http://' + url.parse(req.url).host }, function () {});
});
this.proxyServer.listen(8081);
this.server = http.createServer(function (req, res) {
res.writeHead(200);
res.end('a{color:red}');
});
this.server.listen(8080);
exec('echo "@import url(http://127.0.0.1:8080/test.css);" | HTTP_PROXY=http://127.0.0.1:8081 ./bin/cleancss --inline all', this.callback);
},
'proxies the connection': function () {
assert.isTrue(this.proxied);
},
'gives right output': function (error, stdout) {
assert.equal(stdout, 'a{color:red}');
},
teardown: function () {
this.proxyServer.close();
this.server.close();
}
})
})
.addBatch({
'ie7 compatibility': binaryContext('--compatibility ie7 ./test/fixtures/unsupported/selectors-ie7.css', {
'should not transform source': function (error, stdout) {
assert.equal(stdout, readFile('./test/fixtures/unsupported/selectors-ie7.css'));
}
})
})
.addBatch({
'ie8 compatibility': binaryContext('--compatibility ie8 ./test/fixtures/unsupported/selectors-ie8.css', {
'should not transform source': function (error, stdout) {
assert.equal(stdout, readFile('./test/fixtures/unsupported/selectors-ie8.css'));
}
})
})
.addBatch({
'custom compatibility': pipedContext('a{_color:red}', '--compatibility "+properties.iePrefixHack"', {
'should not transform source': function (error, stdout) {
assert.equal(stdout, 'a{_color:red}');
}
})
})
.addBatch({
'rounding precision': {
'default': pipedContext('div{width:0.10051px}', '', {
'should keep 2 decimal places': function (error, stdout) {
assert.equal(stdout, 'div{width:.10051px}');
}
}),
'custom': pipedContext('div{width:0.00051px}', '-O1 roundingPrecision:4', {
'should keep 4 decimal places': function (error, stdout) {
assert.equal(stdout, 'div{width:.0005px}');
}
}),
'zero': pipedContext('div{width:1.5051px}', '-O1 roundingPrecision:0', {
'should keep 0 decimal places': function (error, stdout) {
assert.equal(stdout, 'div{width:2px}');
}
}),
'disabled': pipedContext('div{width:0.12345px}', '-O1 roundingPrecision:off', {
'should keep all decimal places': function (error, stdout) {
assert.equal(stdout, 'div{width:.12345px}');
}
}),
'disabled via -1': pipedContext('div{width:0.12345px}', '-O1 roundingPrecision:\\\\-1', {
'should keep all decimal places': function (error, stdout) {
assert.equal(stdout, 'div{width:.12345px}');
}
}),
'fine-grained': pipedContext('div{height:10.515rem;width:12.12345px}', '-O1 roundingPrecision:rem=2,px=1', {
'should keep all decimal places': function (error, stdout) {
assert.equal(stdout, 'div{height:10.52rem;width:12.1px}');
}
})
}
})
.addBatch({
'neighbour merging': {
'of unmergeable properties': pipedContext('a{display:inline-block;color:red;display:-moz-block}', '-O2', {
'gets right result': function (error, stdout) {
assert.equal(stdout, 'a{display:inline-block;color:red;display:-moz-block}');
}
}),
'of mergeable properties': pipedContext('a{background:red;display:block;background:white}', '-O2', {
'gets right result': function (error, stdout) {
assert.equal(stdout, 'a{background:#fff;display:block}');
}
})
}
})
.addBatch({
'@media merging': pipedContext('@media screen{a{color:red}}@media screen{a{display:block}}', '-O2 mergeMedia:off', {
'gets right result': function (error, stdout) {
assert.equal(stdout, '@media screen{a{color:red}}@media screen{a{display:block}}');
}
})
})
.addBatch({
'shorthand merging': {
'of (yet) unmergeable properties': pipedContext('a{background:url(image.png);background-color:red}', '-O2 mergeIntoShorthands:off', {
'gets right result': function (error, stdout) {
assert.equal(stdout, 'a{background:url(image.png);background-color:red}');
}
})
}
})
.addBatch({
'source maps - no target file': binaryContext('--source-map ./test/fixtures/reset.css', {
'warns about source map not being build': function (error, stdout, stderr) {
assert.include(stderr, 'Source maps will not be built because you have not specified an output file.');
},
'does not include map in stdout': function (error, stdout) {
assert.notInclude(stdout, '/*# sourceMappingURL');
}
})
})
.addBatch({
'source maps - output file': binaryContext('--source-map -o ./reset.min.css ./test/fixtures/reset.css', {
'includes map in minified file': function () {
assert.include(readFile('./reset.min.css'), '/*# sourceMappingURL=reset.min.css.map */');
},
'creates a map file': function () {
assert.isTrue(fs.existsSync('./reset.min.css.map'));
},
'includes right content in map file': function () {
var sourceMap = new SourceMapConsumer(readFile('./reset.min.css.map'));
assert.deepEqual(
sourceMap.originalPositionFor({ line: 1, column: 1 }),
{
source: 'test/fixtures/reset.css',
line: 4,
column: 0,
name: null
}
);
},
'teardown': function () {
deleteFile('reset.min.css');
deleteFile('reset.min.css.map');
}
})
})
.addBatch({
'source maps - output file in same folder as input': unixOnlyContext({
topic: function () {
var self = this;
exec('cp test/fixtures/reset.css .', function () {
exec('__DIRECT__=1 ./bin/cleancss --source-map -o ./reset.min.css ./reset.css', self.callback);
});
},
'includes right content in map file': function () {
var sourceMap = new SourceMapConsumer(readFile('./reset.min.css.map'));
assert.deepEqual(
sourceMap.originalPositionFor({ line: 1, column: 1 }),
{
source: 'reset.css',
line: 4,
column: 0,
name: null
}
);
},
'teardown': function () {
deleteFile('reset.css');
deleteFile('reset.min.css');
deleteFile('reset.min.css.map');
}
})
})
.addBatch({
'source maps - output file with existing map': binaryContext('--source-map -o ./styles.min.css ./test/fixtures/source-maps/styles.css', {
'includes right content in map file': function () {
var sourceMap = new SourceMapConsumer(readFile('./styles.min.css.map'));
assert.deepEqual(
sourceMap.originalPositionFor({ line: 1, column: 1 }),
{
source: 'test/fixtures/source-maps/styles.less',
line: 1,
column: 4,
name: null
}
);
},
'teardown': function () {
deleteFile('styles.min.css');
deleteFile('styles.min.css.map');
}
})
})
.addBatch({
'source maps - output file for existing map in different folder': binaryContext('--source-map -o ./styles-relative.min.css ./test/fixtures/source-maps/relative.css', {
'includes right content in map file': function () {
var sourceMap = new SourceMapConsumer(readFile('./styles-relative.min.css.map'));
assert.deepEqual(
sourceMap.originalPositionFor({ line: 1, column: 1 }),
{
source: 'test/fixtures/source-maps/sub/styles.less',
line: 2,
column: 2,
name: null
}
);
},
'teardown': function () {
deleteFile('styles-relative.min.css');
deleteFile('styles-relative.min.css.map');
}
})
})
.addBatch({
'source maps - with input source map': binaryContext('--source-map -o ./import.min.css ./test/fixtures/source-maps/import.css', {
'includes map in minified file': function () {
assert.include(readFile('./import.min.css'), '/*# sourceMappingURL=import.min.css.map */');
},
'includes right content in map file': function () {
var sourceMap = new SourceMapConsumer(readFile('./import.min.css.map'));
var count = 0;
sourceMap.eachMapping(function () { count++; });
assert.equal(count, 6);
},
'teardown': function () {
deleteFile('import.min.css');
deleteFile('import.min.css.map');
}
})
})
.addBatch({
'source maps - with input source map and source inlining': binaryContext('--source-map --source-map-inline-sources -o ./import-inline.min.css ./test/fixtures/source-maps/import.css', {
'includes map in minified file': function () {
assert.include(readFile('./import-inline.min.css'), '/*# sourceMappingURL=import-inline.min.css.map */');
},
'includes embedded sources': function () {
var sourceMap = new SourceMapConsumer(readFile('./import-inline.min.css.map'));
var count = 0;
sourceMap.eachMapping(function () { count++; });
assert.equal(count, 6);
},
'teardown': function () {
deleteFile('import-inline.min.css');
deleteFile('import-inline.min.css.map');
}
})
})
.addBatch({
'semantic merging': {
'disabled': pipedContext('.a{margin:0}.b{margin:10px;padding:0}.c{margin:0}', '', {
'should output right data': function (error, stdout) {
assert.equal(stdout, '.a{margin:0}.b{margin:10px;padding:0}.c{margin:0}');
}
}),
'enabled': pipedContext('.a{margin:0}.b{margin:10px;padding:0}.c{margin:0}', '-O2 mergeSemantically:on', {
'should output right data': function (error, stdout) {
assert.equal(stdout, '.a,.c{margin:0}.b{margin:10px;padding:0}');
}
})
}
})
.export(module);