less-test.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. /* jshint latedef: nofunc */
  2. module.exports = function() {
  3. var path = require('path'),
  4. fs = require('fs'),
  5. copyBom = require('./copy-bom')(),
  6. doBomTest = false,
  7. clone = require('clone');
  8. var less;
  9. // Dist fallback for NPM-installed Less (for plugins that do testing)
  10. try {
  11. less = require('../tmp/less.cjs.js');
  12. }
  13. catch (e) {
  14. less = require('../dist/less.cjs.js');
  15. }
  16. var stylize = require('../lib/less-node/lessc-helper').stylize;
  17. var globals = Object.keys(global);
  18. var oneTestOnly = process.argv[2],
  19. isFinished = false;
  20. var isVerbose = process.env.npm_config_loglevel !== 'concise';
  21. var normalFolder = 'test/less';
  22. var bomFolder = 'test/less-bom';
  23. // Define String.prototype.endsWith if it doesn't exist (in older versions of node)
  24. // This is required by the testSourceMap function below
  25. if (typeof String.prototype.endsWith !== 'function') {
  26. String.prototype.endsWith = function (str) {
  27. return this.slice(-str.length) === str;
  28. }
  29. }
  30. less.logger.addListener({
  31. info: function(msg) {
  32. if (isVerbose) {
  33. process.stdout.write(msg + '\n');
  34. }
  35. },
  36. warn: function(msg) {
  37. process.stdout.write(msg + '\n');
  38. },
  39. error: function(msg) {
  40. process.stdout.write(msg + '\n');
  41. }
  42. });
  43. var queueList = [],
  44. queueRunning = false;
  45. function queue(func) {
  46. if (queueRunning) {
  47. // console.log("adding to queue");
  48. queueList.push(func);
  49. } else {
  50. // console.log("first in queue - starting");
  51. queueRunning = true;
  52. func();
  53. }
  54. }
  55. function release() {
  56. if (queueList.length) {
  57. // console.log("running next in queue");
  58. var func = queueList.shift();
  59. setTimeout(func, 0);
  60. } else {
  61. // console.log("stopping queue");
  62. queueRunning = false;
  63. }
  64. }
  65. var totalTests = 0,
  66. failedTests = 0,
  67. passedTests = 0,
  68. finishTimer = setInterval(endTest, 500);
  69. less.functions.functionRegistry.addMultiple({
  70. add: function (a, b) {
  71. return new(less.tree.Dimension)(a.value + b.value);
  72. },
  73. increment: function (a) {
  74. return new(less.tree.Dimension)(a.value + 1);
  75. },
  76. _color: function (str) {
  77. if (str.value === 'evil red') { return new(less.tree.Color)('600'); }
  78. }
  79. });
  80. function testSourcemap(name, err, compiledLess, doReplacements, sourcemap, baseFolder) {
  81. if (err) {
  82. fail('ERROR: ' + (err && err.message));
  83. return;
  84. }
  85. // Check the sourceMappingURL at the bottom of the file
  86. var expectedSourceMapURL = name + '.css.map',
  87. sourceMappingPrefix = '/*# sourceMappingURL=',
  88. sourceMappingSuffix = ' */',
  89. expectedCSSAppendage = sourceMappingPrefix + expectedSourceMapURL + sourceMappingSuffix;
  90. if (!compiledLess.endsWith(expectedCSSAppendage)) {
  91. // To display a better error message, we need to figure out what the actual sourceMappingURL value was, if it was even present
  92. var indexOfSourceMappingPrefix = compiledLess.indexOf(sourceMappingPrefix);
  93. if (indexOfSourceMappingPrefix === -1) {
  94. fail('ERROR: sourceMappingURL was not found in ' + baseFolder + '/' + name + '.css.');
  95. return;
  96. }
  97. var startOfSourceMappingValue = indexOfSourceMappingPrefix + sourceMappingPrefix.length,
  98. indexOfNextSpace = compiledLess.indexOf(' ', startOfSourceMappingValue),
  99. actualSourceMapURL = compiledLess.substring(startOfSourceMappingValue, indexOfNextSpace === -1 ? compiledLess.length : indexOfNextSpace);
  100. fail('ERROR: sourceMappingURL should be "' + expectedSourceMapURL + '" but is "' + actualSourceMapURL + '".');
  101. }
  102. fs.readFile(path.join('test/', name) + '.json', 'utf8', function (e, expectedSourcemap) {
  103. process.stdout.write('- ' + path.join(baseFolder, name) + ': ');
  104. if (sourcemap === expectedSourcemap) {
  105. ok('OK');
  106. } else if (err) {
  107. fail('ERROR: ' + (err && err.message));
  108. if (isVerbose) {
  109. process.stdout.write('\n');
  110. process.stdout.write(err.stack + '\n');
  111. }
  112. } else {
  113. difference('FAIL', expectedSourcemap, sourcemap);
  114. }
  115. });
  116. }
  117. function testEmptySourcemap(name, err, compiledLess, doReplacements, sourcemap, baseFolder) {
  118. process.stdout.write('- ' + path.join(baseFolder, name) + ': ');
  119. if (err) {
  120. fail('ERROR: ' + (err && err.message));
  121. } else {
  122. var expectedSourcemap = undefined;
  123. if ( compiledLess !== '' ) {
  124. difference('\nCompiledLess must be empty', '', compiledLess);
  125. } else if (sourcemap !== expectedSourcemap) {
  126. fail('Sourcemap must be undefined');
  127. } else {
  128. ok('OK');
  129. }
  130. }
  131. }
  132. function testErrors(name, err, compiledLess, doReplacements, sourcemap, baseFolder) {
  133. fs.readFile(path.join(baseFolder, name) + '.txt', 'utf8', function (e, expectedErr) {
  134. process.stdout.write('- ' + path.join(baseFolder, name) + ': ');
  135. expectedErr = doReplacements(expectedErr, baseFolder, err && err.filename);
  136. if (!err) {
  137. if (compiledLess) {
  138. fail('No Error', 'red');
  139. } else {
  140. fail('No Error, No Output');
  141. }
  142. } else {
  143. var errMessage = err.toString();
  144. if (errMessage === expectedErr) {
  145. ok('OK');
  146. } else {
  147. difference('FAIL', expectedErr, errMessage);
  148. }
  149. }
  150. });
  151. }
  152. // https://github.com/less/less.js/issues/3112
  153. function testJSImport() {
  154. process.stdout.write('- Testing root function registry');
  155. less.functions.functionRegistry.add('ext', function() {
  156. return new less.tree.Anonymous('file');
  157. });
  158. var expected = '@charset "utf-8";\n';
  159. toCSS({}, require('path').join(process.cwd(), 'test/less/root-registry/root.less'), function(error, output) {
  160. if (error) {
  161. return fail('ERROR: ' + error);
  162. }
  163. if (output.css === expected) {
  164. return ok('OK');
  165. }
  166. difference('FAIL', expected, output.css);
  167. });
  168. }
  169. function globalReplacements(input, directory, filename) {
  170. var path = require('path');
  171. var p = filename ? path.join(path.dirname(filename), '/') : path.join(process.cwd(), directory),
  172. pathimport = path.join(process.cwd(), directory + 'import/'),
  173. pathesc = p.replace(/[.:/\\]/g, function(a) { return '\\' + (a == '\\' ? '\/' : a); }),
  174. pathimportesc = pathimport.replace(/[.:/\\]/g, function(a) { return '\\' + (a == '\\' ? '\/' : a); });
  175. return input.replace(/\{path\}/g, p)
  176. .replace(/\{node\}/g, '')
  177. .replace(/\{\/node\}/g, '')
  178. .replace(/\{pathhref\}/g, '')
  179. .replace(/\{404status\}/g, '')
  180. .replace(/\{nodepath\}/g, path.join(process.cwd(), 'node_modules', '/'))
  181. .replace(/\{pathrel\}/g, path.join(path.relative(process.cwd(), p), '/'))
  182. .replace(/\{pathesc\}/g, pathesc)
  183. .replace(/\{pathimport\}/g, pathimport)
  184. .replace(/\{pathimportesc\}/g, pathimportesc)
  185. .replace(/\r\n/g, '\n');
  186. }
  187. function checkGlobalLeaks() {
  188. return Object.keys(global).filter(function(v) {
  189. return globals.indexOf(v) < 0;
  190. });
  191. }
  192. function testSyncronous(options, filenameNoExtension) {
  193. if (oneTestOnly && ('Test Sync ' + filenameNoExtension) !== oneTestOnly) {
  194. return;
  195. }
  196. totalTests++;
  197. queue(function() {
  198. var isSync = true;
  199. toCSS(options, path.join(normalFolder, filenameNoExtension + '.less'), function (err, result) {
  200. process.stdout.write('- Test Sync ' + filenameNoExtension + ': ');
  201. if (isSync) {
  202. ok('OK');
  203. } else {
  204. fail('Not Sync');
  205. }
  206. release();
  207. });
  208. isSync = false;
  209. });
  210. }
  211. function prepBomTest() {
  212. copyBom.copyFolderWithBom(normalFolder, bomFolder);
  213. doBomTest = true;
  214. }
  215. function runTestSet(options, foldername, verifyFunction, nameModifier, doReplacements, getFilename) {
  216. options = options ? clone(options) : {};
  217. runTestSetInternal(normalFolder, options, foldername, verifyFunction, nameModifier, doReplacements, getFilename);
  218. if (doBomTest) {
  219. runTestSetInternal(bomFolder, options, foldername, verifyFunction, nameModifier, doReplacements, getFilename);
  220. }
  221. }
  222. function runTestSetNormalOnly(options, foldername, verifyFunction, nameModifier, doReplacements, getFilename) {
  223. runTestSetInternal(normalFolder, options, foldername, verifyFunction, nameModifier, doReplacements, getFilename);
  224. }
  225. function runTestSetInternal(baseFolder, opts, foldername, verifyFunction, nameModifier, doReplacements, getFilename) {
  226. foldername = foldername || '';
  227. var originalOptions = opts || {};
  228. if (!doReplacements) {
  229. doReplacements = globalReplacements;
  230. }
  231. function getBasename(file) {
  232. return foldername + path.basename(file, '.less');
  233. }
  234. fs.readdirSync(path.join(baseFolder, foldername)).forEach(function (file) {
  235. if (!/\.less$/.test(file)) { return; }
  236. var options = clone(originalOptions);
  237. var name = getBasename(file);
  238. if (oneTestOnly && name !== oneTestOnly) {
  239. return;
  240. }
  241. totalTests++;
  242. if (options.sourceMap && !options.sourceMap.sourceMapFileInline) {
  243. options.sourceMap = {
  244. sourceMapOutputFilename: name + '.css',
  245. sourceMapBasepath: path.join(process.cwd(), baseFolder),
  246. sourceMapRootpath: 'testweb/'
  247. };
  248. // This options is normally set by the bin/lessc script. Setting it causes the sourceMappingURL comment to be appended to the CSS
  249. // output. The value is designed to allow the sourceMapBasepath option to be tested, as it should be removed by less before
  250. // setting the sourceMappingURL value, leaving just the sourceMapOutputFilename and .map extension.
  251. options.sourceMap.sourceMapFilename = options.sourceMap.sourceMapBasepath + '/' + options.sourceMap.sourceMapOutputFilename + '.map';
  252. }
  253. options.getVars = function(file) {
  254. try {
  255. return JSON.parse(fs.readFileSync(getFilename(getBasename(file), 'vars', baseFolder), 'utf8'));
  256. }
  257. catch (e) {
  258. return {};
  259. }
  260. };
  261. var doubleCallCheck = false;
  262. queue(function() {
  263. toCSS(options, path.join(baseFolder, foldername + file), function (err, result) {
  264. if (doubleCallCheck) {
  265. totalTests++;
  266. fail('less is calling back twice');
  267. process.stdout.write(doubleCallCheck + '\n');
  268. process.stdout.write((new Error()).stack + '\n');
  269. return;
  270. }
  271. doubleCallCheck = (new Error()).stack;
  272. if (verifyFunction) {
  273. var verificationResult = verifyFunction(name, err, result && result.css, doReplacements, result && result.map, baseFolder);
  274. release();
  275. return verificationResult;
  276. }
  277. if (err) {
  278. fail('ERROR: ' + (err && err.message));
  279. if (isVerbose) {
  280. process.stdout.write('\n');
  281. if (err.stack) {
  282. process.stdout.write(err.stack + '\n');
  283. } else {
  284. // this sometimes happen - show the whole error object
  285. console.log(err);
  286. }
  287. }
  288. release();
  289. return;
  290. }
  291. var css_name = name;
  292. if (nameModifier) { css_name = nameModifier(name); }
  293. fs.readFile(path.join('test/css', css_name) + '.css', 'utf8', function (e, css) {
  294. process.stdout.write('- ' + path.join(baseFolder, css_name) + ': ');
  295. css = css && doReplacements(css, path.join(baseFolder, foldername));
  296. if (result.css === css) { ok('OK'); }
  297. else {
  298. difference('FAIL', css, result.css);
  299. }
  300. release();
  301. });
  302. });
  303. });
  304. });
  305. }
  306. function diff(left, right) {
  307. require('diff').diffLines(left, right).forEach(function(item) {
  308. if (item.added || item.removed) {
  309. var text = item.value && item.value.replace('\n', String.fromCharCode(182) + '\n').replace('\ufeff', '[[BOM]]');
  310. process.stdout.write(stylize(text, item.added ? 'green' : 'red'));
  311. } else {
  312. process.stdout.write(item.value && item.value.replace('\ufeff', '[[BOM]]'));
  313. }
  314. });
  315. process.stdout.write('\n');
  316. }
  317. function fail(msg) {
  318. process.stdout.write(stylize(msg, 'red') + '\n');
  319. failedTests++;
  320. endTest();
  321. }
  322. function difference(msg, left, right) {
  323. process.stdout.write(stylize(msg, 'yellow') + '\n');
  324. failedTests++;
  325. diff(left, right);
  326. endTest();
  327. }
  328. function ok(msg) {
  329. process.stdout.write(stylize(msg, 'green') + '\n');
  330. passedTests++;
  331. endTest();
  332. }
  333. function finished() {
  334. isFinished = true;
  335. endTest();
  336. }
  337. function endTest() {
  338. if (isFinished && ((failedTests + passedTests) >= totalTests)) {
  339. clearInterval(finishTimer);
  340. var leaked = checkGlobalLeaks();
  341. process.stdout.write('\n');
  342. if (failedTests > 0) {
  343. process.stdout.write(failedTests + stylize(' Failed', 'red') + ', ' + passedTests + ' passed\n');
  344. } else {
  345. process.stdout.write(stylize('All Passed ', 'green') + passedTests + ' run\n');
  346. }
  347. if (leaked.length > 0) {
  348. process.stdout.write('\n');
  349. process.stdout.write(stylize('Global leak detected: ', 'red') + leaked.join(', ') + '\n');
  350. }
  351. if (leaked.length || failedTests) {
  352. process.on('exit', function() { process.reallyExit(1); });
  353. }
  354. }
  355. }
  356. function contains(fullArray, obj) {
  357. for (var i = 0; i < fullArray.length; i++) {
  358. if (fullArray[i] === obj) {
  359. return true;
  360. }
  361. }
  362. return false;
  363. }
  364. function toCSS(options, path, callback) {
  365. options = options || {};
  366. var str = fs.readFileSync(path, 'utf8'), addPath = require('path').dirname(path);
  367. if (typeof options.paths !== 'string') {
  368. options.paths = options.paths || [];
  369. if (!contains(options.paths, addPath)) {
  370. options.paths.push(addPath);
  371. }
  372. }
  373. options.filename = require('path').resolve(process.cwd(), path);
  374. options.optimization = options.optimization || 0;
  375. if (options.globalVars) {
  376. options.globalVars = options.getVars(path);
  377. } else if (options.modifyVars) {
  378. options.modifyVars = options.getVars(path);
  379. }
  380. if (options.plugin) {
  381. var Plugin = require(require('path').resolve(process.cwd(), options.plugin));
  382. options.plugins = [Plugin];
  383. }
  384. less.render(str, options, callback);
  385. }
  386. function testNoOptions() {
  387. if (oneTestOnly && 'Integration' !== oneTestOnly) {
  388. return;
  389. }
  390. totalTests++;
  391. try {
  392. process.stdout.write('- Integration - creating parser without options: ');
  393. less.render('');
  394. } catch (e) {
  395. fail(stylize('FAIL\n', 'red'));
  396. return;
  397. }
  398. ok(stylize('OK\n', 'green'));
  399. }
  400. return {
  401. runTestSet: runTestSet,
  402. runTestSetNormalOnly: runTestSetNormalOnly,
  403. testSyncronous: testSyncronous,
  404. testErrors: testErrors,
  405. testSourcemap: testSourcemap,
  406. testEmptySourcemap: testEmptySourcemap,
  407. testNoOptions: testNoOptions,
  408. prepBomTest: prepBomTest,
  409. testJSImport: testJSImport,
  410. finished: finished
  411. };
  412. };