less-test.js
23.2 KB
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
/* jshint latedef: nofunc */
var semver = require('semver');
var logger = require('../lib/less/logger').default;
var isVerbose = process.env.npm_config_loglevel !== 'concise';
logger.addListener({
info(msg) {
if (isVerbose) {
process.stdout.write(msg + '\n');
}
},
warn(msg) {
process.stdout.write(msg + '\n');
},
erro(msg) {
process.stdout.write(msg + '\n');
}
});
module.exports = function() {
var path = require('path'),
fs = require('fs'),
clone = require('copy-anything').copy;
var less = require('../');
var stylize = require('../lib/less-node/lessc-helper').stylize;
var globals = Object.keys(global);
var oneTestOnly = process.argv[2],
isFinished = false;
var testFolder = path.dirname(require.resolve('@less/test-data'));
var lessFolder = path.join(testFolder, 'less');
// Define String.prototype.endsWith if it doesn't exist (in older versions of node)
// This is required by the testSourceMap function below
if (typeof String.prototype.endsWith !== 'function') {
String.prototype.endsWith = function (str) {
return this.slice(-str.length) === str;
}
}
var queueList = [],
queueRunning = false;
function queue(func) {
if (queueRunning) {
// console.log("adding to queue");
queueList.push(func);
} else {
// console.log("first in queue - starting");
queueRunning = true;
func();
}
}
function release() {
if (queueList.length) {
// console.log("running next in queue");
var func = queueList.shift();
setTimeout(func, 0);
} else {
// console.log("stopping queue");
queueRunning = false;
}
}
var totalTests = 0,
failedTests = 0,
passedTests = 0,
finishTimer = setInterval(endTest, 500);
less.functions.functionRegistry.addMultiple({
add: function (a, b) {
return new(less.tree.Dimension)(a.value + b.value);
},
increment: function (a) {
return new(less.tree.Dimension)(a.value + 1);
},
_color: function (str) {
if (str.value === 'evil red') { return new(less.tree.Color)('600'); }
}
});
function testSourcemap(name, err, compiledLess, doReplacements, sourcemap, baseFolder) {
if (err) {
fail('ERROR: ' + (err && err.message));
return;
}
// Check the sourceMappingURL at the bottom of the file
var expectedSourceMapURL = name + '.css.map',
sourceMappingPrefix = '/*# sourceMappingURL=',
sourceMappingSuffix = ' */',
expectedCSSAppendage = sourceMappingPrefix + expectedSourceMapURL + sourceMappingSuffix;
if (!compiledLess.endsWith(expectedCSSAppendage)) {
// To display a better error message, we need to figure out what the actual sourceMappingURL value was, if it was even present
var indexOfSourceMappingPrefix = compiledLess.indexOf(sourceMappingPrefix);
if (indexOfSourceMappingPrefix === -1) {
fail('ERROR: sourceMappingURL was not found in ' + baseFolder + '/' + name + '.css.');
return;
}
var startOfSourceMappingValue = indexOfSourceMappingPrefix + sourceMappingPrefix.length,
indexOfNextSpace = compiledLess.indexOf(' ', startOfSourceMappingValue),
actualSourceMapURL = compiledLess.substring(startOfSourceMappingValue, indexOfNextSpace === -1 ? compiledLess.length : indexOfNextSpace);
fail('ERROR: sourceMappingURL should be "' + expectedSourceMapURL + '" but is "' + actualSourceMapURL + '".');
}
fs.readFile(path.join('test/', name) + '.json', 'utf8', function (e, expectedSourcemap) {
process.stdout.write('- ' + path.join(baseFolder, name) + ': ');
if (sourcemap === expectedSourcemap) {
ok('OK');
} else if (err) {
fail('ERROR: ' + (err && err.message));
if (isVerbose) {
process.stdout.write('\n');
process.stdout.write(err.stack + '\n');
}
} else {
difference('FAIL', expectedSourcemap, sourcemap);
}
});
}
function testSourcemapWithoutUrlAnnotation(name, err, compiledLess, doReplacements, sourcemap, baseFolder) {
if (err) {
fail('ERROR: ' + (err && err.message));
return;
}
// This matches with strings that end($) with source mapping url annotation.
var sourceMapRegExp = /\/\*# sourceMappingURL=.+\.css\.map \*\/$/;
if (sourceMapRegExp.test(compiledLess)) {
fail('ERROR: sourceMappingURL found in ' + baseFolder + '/' + name + '.css.');
return;
}
// Even if annotation is not necessary, the map file should be there.
fs.readFile(path.join('test/', name) + '.json', 'utf8', function (e, expectedSourcemap) {
process.stdout.write('- ' + path.join(baseFolder, name) + ': ');
if (sourcemap === expectedSourcemap) {
ok('OK');
} else if (err) {
fail('ERROR: ' + (err && err.message));
if (isVerbose) {
process.stdout.write('\n');
process.stdout.write(err.stack + '\n');
}
} else {
difference('FAIL', expectedSourcemap, sourcemap);
}
});
}
function testEmptySourcemap(name, err, compiledLess, doReplacements, sourcemap, baseFolder) {
process.stdout.write('- ' + path.join(baseFolder, name) + ': ');
if (err) {
fail('ERROR: ' + (err && err.message));
} else {
var expectedSourcemap = undefined;
if ( compiledLess !== '' ) {
difference('\nCompiledLess must be empty', '', compiledLess);
} else if (sourcemap !== expectedSourcemap) {
fail('Sourcemap must be undefined');
} else {
ok('OK');
}
}
}
function testSourcemapWithVariableInSelector(name, err, compiledLess, doReplacements, sourcemap, baseFolder) {
if (err) {
fail('ERROR: ' + (err && err.message));
return;
}
// Even if annotation is not necessary, the map file should be there.
fs.readFile(path.join('test/', name) + '.json', 'utf8', function (e, expectedSourcemap) {
process.stdout.write('- ' + path.join(baseFolder, name) + ': ');
if (sourcemap === expectedSourcemap) {
ok('OK');
} else if (err) {
fail('ERROR: ' + (err && err.message));
if (isVerbose) {
process.stdout.write('\n');
process.stdout.write(err.stack + '\n');
}
} else {
difference('FAIL', expectedSourcemap, sourcemap);
}
});
}
function testImports(name, err, compiledLess, doReplacements, sourcemap, baseFolder, imports) {
if (err) {
fail('ERROR: ' + (err && err.message));
return;
}
function stringify(str) {
return JSON.stringify(imports, null, ' ')
}
/** Imports are not sorted */
const importsString = stringify(imports.sort())
fs.readFile(path.join(lessFolder, name) + '.json', 'utf8', function (e, expectedImports) {
if (e) {
fail('ERROR: ' + (e && e.message));
return;
}
process.stdout.write('- ' + path.join(baseFolder, name) + ': ');
expectedImports = stringify(JSON.parse(expectedImports).sort());
expectedImports = globalReplacements(expectedImports, baseFolder);
if (expectedImports === importsString) {
ok('OK');
} else if (err) {
fail('ERROR: ' + (err && err.message));
if (isVerbose) {
process.stdout.write('\n');
process.stdout.write(err.stack + '\n');
}
} else {
difference('FAIL', expectedImports, importsString);
}
});
}
function testErrors(name, err, compiledLess, doReplacements, sourcemap, baseFolder) {
fs.readFile(path.join(baseFolder, name) + '.txt', 'utf8', function (e, expectedErr) {
process.stdout.write('- ' + path.join(baseFolder, name) + ': ');
expectedErr = doReplacements(expectedErr, baseFolder, err && err.filename);
if (!err) {
if (compiledLess) {
fail('No Error', 'red');
} else {
fail('No Error, No Output');
}
} else {
var errMessage = err.toString();
if (errMessage === expectedErr) {
ok('OK');
} else {
difference('FAIL', expectedErr, errMessage);
}
}
});
}
// To fix ci fail about error format change in upstream v8 project
// https://github.com/v8/v8/commit/c0fd89c3c089e888c4f4e8582e56db7066fa779b
// Node 16.9.0+ include this change via https://github.com/nodejs/node/pull/39947
function testTypeErrors(name, err, compiledLess, doReplacements, sourcemap, baseFolder) {
const fileSuffix = semver.gte(process.version, 'v16.9.0') ? '-2.txt' : '.txt';
fs.readFile(path.join(baseFolder, name) + fileSuffix, 'utf8', function (e, expectedErr) {
process.stdout.write('- ' + path.join(baseFolder, name) + ': ');
expectedErr = doReplacements(expectedErr, baseFolder, err && err.filename);
if (!err) {
if (compiledLess) {
fail('No Error', 'red');
} else {
fail('No Error, No Output');
}
} else {
var errMessage = err.toString();
if (errMessage === expectedErr) {
ok('OK');
} else {
difference('FAIL', expectedErr, errMessage);
}
}
});
}
// https://github.com/less/less.js/issues/3112
function testJSImport() {
process.stdout.write('- Testing root function registry');
less.functions.functionRegistry.add('ext', function() {
return new less.tree.Anonymous('file');
});
var expected = '@charset "utf-8";\n';
toCSS({}, path.join(lessFolder, 'root-registry', 'root.less'), function(error, output) {
if (error) {
return fail('ERROR: ' + error);
}
if (output.css === expected) {
return ok('OK');
}
difference('FAIL', expected, output.css);
});
}
function globalReplacements(input, directory, filename) {
var path = require('path');
var p = filename ? path.join(path.dirname(filename), '/') : directory,
pathimport = path.join(directory + 'import/'),
pathesc = p.replace(/[.:/\\]/g, function(a) { return '\\' + (a == '\\' ? '\/' : a); }),
pathimportesc = pathimport.replace(/[.:/\\]/g, function(a) { return '\\' + (a == '\\' ? '\/' : a); });
return input.replace(/\{path\}/g, p)
.replace(/\{node\}/g, '')
.replace(/\{\/node\}/g, '')
.replace(/\{pathhref\}/g, '')
.replace(/\{404status\}/g, '')
.replace(/\{nodepath\}/g, path.join(process.cwd(), 'node_modules', '/'))
.replace(/\{pathrel\}/g, path.join(path.relative(lessFolder, p), '/'))
.replace(/\{pathesc\}/g, pathesc)
.replace(/\{pathimport\}/g, pathimport)
.replace(/\{pathimportesc\}/g, pathimportesc)
.replace(/\r\n/g, '\n');
}
function checkGlobalLeaks() {
return Object.keys(global).filter(function(v) {
return globals.indexOf(v) < 0;
});
}
function testSyncronous(options, filenameNoExtension) {
if (oneTestOnly && ('Test Sync ' + filenameNoExtension) !== oneTestOnly) {
return;
}
totalTests++;
queue(function() {
var isSync = true;
toCSS(options, path.join(lessFolder, filenameNoExtension + '.less'), function (err, result) {
process.stdout.write('- Test Sync ' + filenameNoExtension + ': ');
if (isSync) {
ok('OK');
} else {
fail('Not Sync');
}
release();
});
isSync = false;
});
}
function runTestSet(options, foldername, verifyFunction, nameModifier, doReplacements, getFilename) {
options = options ? clone(options) : {};
runTestSetInternal(lessFolder, options, foldername, verifyFunction, nameModifier, doReplacements, getFilename);
}
function runTestSetNormalOnly(options, foldername, verifyFunction, nameModifier, doReplacements, getFilename) {
runTestSetInternal(lessFolder, options, foldername, verifyFunction, nameModifier, doReplacements, getFilename);
}
function runTestSetInternal(baseFolder, opts, foldername, verifyFunction, nameModifier, doReplacements, getFilename) {
foldername = foldername || '';
var originalOptions = opts || {};
if (!doReplacements) {
doReplacements = globalReplacements;
}
function getBasename(file) {
return foldername + path.basename(file, '.less');
}
fs.readdirSync(path.join(baseFolder, foldername)).forEach(function (file) {
if (!/\.less$/.test(file)) { return; }
var options = clone(originalOptions);
var name = getBasename(file);
if (oneTestOnly && name !== oneTestOnly) {
return;
}
totalTests++;
if (options.sourceMap && !options.sourceMap.sourceMapFileInline) {
options.sourceMap = {
sourceMapOutputFilename: name + '.css',
sourceMapBasepath: baseFolder,
sourceMapRootpath: 'testweb/',
disableSourcemapAnnotation: options.sourceMap.disableSourcemapAnnotation
};
// This options is normally set by the bin/lessc script. Setting it causes the sourceMappingURL comment to be appended to the CSS
// output. The value is designed to allow the sourceMapBasepath option to be tested, as it should be removed by less before
// setting the sourceMappingURL value, leaving just the sourceMapOutputFilename and .map extension.
options.sourceMap.sourceMapFilename = options.sourceMap.sourceMapBasepath + '/' + options.sourceMap.sourceMapOutputFilename + '.map';
}
options.getVars = function(file) {
try {
return JSON.parse(fs.readFileSync(getFilename(getBasename(file), 'vars', baseFolder), 'utf8'));
}
catch (e) {
return {};
}
};
var doubleCallCheck = false;
queue(function() {
toCSS(options, path.join(baseFolder, foldername + file), function (err, result) {
if (doubleCallCheck) {
totalTests++;
fail('less is calling back twice');
process.stdout.write(doubleCallCheck + '\n');
process.stdout.write((new Error()).stack + '\n');
return;
}
doubleCallCheck = (new Error()).stack;
/**
* @todo - refactor so the result object is sent to the verify function
*/
if (verifyFunction) {
var verificationResult = verifyFunction(
name, err, result && result.css, doReplacements, result && result.map, baseFolder, result && result.imports
);
release();
return verificationResult;
}
if (err) {
fail('ERROR: ' + (err && err.message));
if (isVerbose) {
process.stdout.write('\n');
if (err.stack) {
process.stdout.write(err.stack + '\n');
} else {
// this sometimes happen - show the whole error object
console.log(err);
}
}
release();
return;
}
var css_name = name;
if (nameModifier) { css_name = nameModifier(name); }
fs.readFile(path.join(testFolder, 'css', css_name) + '.css', 'utf8', function (e, css) {
process.stdout.write('- ' + path.join(baseFolder, css_name) + ': ');
css = css && doReplacements(css, path.join(baseFolder, foldername));
if (result.css === css) { ok('OK'); }
else {
difference('FAIL', css, result.css);
}
release();
});
});
});
});
}
function diff(left, right) {
require('diff').diffLines(left, right).forEach(function(item) {
if (item.added || item.removed) {
var text = item.value && item.value.replace('\n', String.fromCharCode(182) + '\n').replace('\ufeff', '[[BOM]]');
process.stdout.write(stylize(text, item.added ? 'green' : 'red'));
} else {
process.stdout.write(item.value && item.value.replace('\ufeff', '[[BOM]]'));
}
});
process.stdout.write('\n');
}
function fail(msg) {
process.stdout.write(stylize(msg, 'red') + '\n');
failedTests++;
endTest();
}
function difference(msg, left, right) {
process.stdout.write(stylize(msg, 'yellow') + '\n');
failedTests++;
diff(left || '', right || '');
endTest();
}
function ok(msg) {
process.stdout.write(stylize(msg, 'green') + '\n');
passedTests++;
endTest();
}
function finished() {
isFinished = true;
endTest();
}
function endTest() {
if (isFinished && ((failedTests + passedTests) >= totalTests)) {
clearInterval(finishTimer);
var leaked = checkGlobalLeaks();
process.stdout.write('\n');
if (failedTests > 0) {
process.stdout.write(failedTests + stylize(' Failed', 'red') + ', ' + passedTests + ' passed\n');
} else {
process.stdout.write(stylize('All Passed ', 'green') + passedTests + ' run\n');
}
if (leaked.length > 0) {
process.stdout.write('\n');
process.stdout.write(stylize('Global leak detected: ', 'red') + leaked.join(', ') + '\n');
}
if (leaked.length || failedTests) {
process.on('exit', function() { process.reallyExit(1); });
}
}
}
function contains(fullArray, obj) {
for (var i = 0; i < fullArray.length; i++) {
if (fullArray[i] === obj) {
return true;
}
}
return false;
}
/**
*
* @param {Object} options
* @param {string} filePath
* @param {Function} callback
*/
function toCSS(options, filePath, callback) {
options = options || {};
var str = fs.readFileSync(filePath, 'utf8'), addPath = path.dirname(filePath);
if (typeof options.paths !== 'string') {
options.paths = options.paths || [];
if (!contains(options.paths, addPath)) {
options.paths.push(addPath);
}
} else {
options.paths = [options.paths]
}
options.paths = options.paths.map(searchPath => {
return path.resolve(lessFolder, searchPath)
})
options.filename = path.resolve(process.cwd(), filePath);
options.optimization = options.optimization || 0;
if (options.globalVars) {
options.globalVars = options.getVars(filePath);
} else if (options.modifyVars) {
options.modifyVars = options.getVars(filePath);
}
if (options.plugin) {
var Plugin = require(path.resolve(process.cwd(), options.plugin));
options.plugins = [Plugin];
}
less.render(str, options, callback);
}
function testNoOptions() {
if (oneTestOnly && 'Integration' !== oneTestOnly) {
return;
}
totalTests++;
try {
process.stdout.write('- Integration - creating parser without options: ');
less.render('');
} catch (e) {
fail(stylize('FAIL\n', 'red'));
return;
}
ok(stylize('OK\n', 'green'));
}
function testImportRedirect(nockScope) {
return (name, err, css, doReplacements, sourcemap, baseFolder) => {
process.stdout.write('- ' + path.join(baseFolder, name) + ': ');
if (err) {
fail('FAIL: ' + (err && err.message));
return;
}
const expected = 'h1 {\n color: red;\n}\n';
if (css !== expected) {
difference('FAIL', expected, css);
return;
}
nockScope.done();
ok('OK');
};
}
function testDisablePluginRule() {
less.render(
'@plugin "../../plugin/some_plugin";',
{disablePluginRule: true},
function(err) {
// TODO: Need a better way of identifing exactly which error is thrown. Checking
// text like this tends to be rather brittle.
const EXPECTED = '@plugin statements are not allowed when disablePluginRule is set to true';
if (!err || String(err).indexOf(EXPECTED) < 0) {
fail('ERROR: Expected "' + EXPECTED + '" error');
return;
}
ok(stylize('OK\n', 'green'));
}
);
}
return {
runTestSet: runTestSet,
runTestSetNormalOnly: runTestSetNormalOnly,
testSyncronous: testSyncronous,
testErrors: testErrors,
testTypeErrors: testTypeErrors,
testSourcemap: testSourcemap,
testSourcemapWithoutUrlAnnotation: testSourcemapWithoutUrlAnnotation,
testSourcemapWithVariableInSelector: testSourcemapWithVariableInSelector,
testImports: testImports,
testImportRedirect: testImportRedirect,
testEmptySourcemap: testEmptySourcemap,
testNoOptions: testNoOptions,
testDisablePluginRule: testDisablePluginRule,
testJSImport: testJSImport,
finished: finished
};
};