less-test.js 17 KB

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