Gruntfile.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. 'use strict';
  2. module.exports = function (grunt) {
  3. grunt.option('stack', true)
  4. // Report the elapsed execution time of tasks.
  5. require('time-grunt')(grunt);
  6. var COMPRESS_FOR_TESTS = false;
  7. var git = require('git-rev');
  8. // Sauce Labs browser
  9. var browsers = [
  10. // Desktop browsers
  11. {
  12. browserName: "chrome",
  13. version: 'latest',
  14. platform: 'Windows 7'
  15. },
  16. {
  17. browserName: "firefox",
  18. version: 'latest',
  19. platform: 'Linux'
  20. },
  21. {
  22. browserName: 'safari',
  23. version: '9',
  24. platform: 'OS X 10.11'
  25. },
  26. {
  27. browserName: "internet explorer",
  28. version: '8',
  29. platform: 'Windows XP'
  30. },
  31. {
  32. browserName: "internet explorer",
  33. version: '11',
  34. platform: 'Windows 8.1'
  35. },
  36. {
  37. browserName: "edge",
  38. version: '13',
  39. platform: 'Windows 10'
  40. },
  41. // Mobile browsers
  42. {
  43. browserName: 'ipad',
  44. deviceName: 'iPad Air Simulator',
  45. deviceOrientation: 'portrait',
  46. version: '8.4',
  47. platform: 'OS X 10.9'
  48. },
  49. {
  50. browserName: 'iphone',
  51. deviceName: 'iPhone 5 Simulator',
  52. deviceOrientation: 'portrait',
  53. version: '9.3',
  54. platform: 'OS X 10.11'
  55. },
  56. {
  57. browserName: 'android',
  58. deviceName: 'Google Nexus 7 HD Emulator',
  59. deviceOrientation: 'portrait',
  60. version: '4.4',
  61. platform: 'Linux'
  62. }
  63. ];
  64. var sauceJobs = {};
  65. var browserTests = [ "filemanager-plugin",
  66. "visitor-plugin",
  67. "global-vars",
  68. "modify-vars",
  69. "production",
  70. "rootpath-relative",
  71. "rootpath",
  72. "relative-urls",
  73. "browser",
  74. "no-js-errors",
  75. "legacy"
  76. ];
  77. function makeJob(testName) {
  78. sauceJobs[testName] = {
  79. options: {
  80. urls: testName === 'all' ?
  81. browserTests.map(function(name) {
  82. return "http://localhost:8081/tmp/browser/test-runner-" + name + ".html";
  83. }) :
  84. ["http://localhost:8081/tmp/browser/test-runner-" + testName + ".html"],
  85. testname: testName === 'all' ? 'Unit Tests for Less.js' : testName,
  86. browsers: browsers,
  87. public: 'public',
  88. recordVideo: false,
  89. videoUploadOnPass: false,
  90. recordScreenshots: process.env.TRAVIS_BRANCH !== "master",
  91. build: process.env.TRAVIS_BRANCH === "master" ? process.env.TRAVIS_JOB_ID : undefined,
  92. tags: [process.env.TRAVIS_BUILD_NUMBER, process.env.TRAVIS_PULL_REQUEST, process.env.TRAVIS_BRANCH],
  93. statusCheckAttempts: -1,
  94. sauceConfig: {
  95. 'idle-timeout': 100
  96. },
  97. throttled: 5,
  98. onTestComplete: function(result, callback) {
  99. // Called after a unit test is done, per page, per browser
  100. // 'result' param is the object returned by the test framework's reporter
  101. // 'callback' is a Node.js style callback function. You must invoke it after you
  102. // finish your work.
  103. // Pass a non-null value as the callback's first parameter if you want to throw an
  104. // exception. If your function is synchronous you can also throw exceptions
  105. // directly.
  106. // Passing true or false as the callback's second parameter passes or fails the
  107. // test. Passing undefined does not alter the test result. Please note that this
  108. // only affects the grunt task's result. You have to explicitly update the Sauce
  109. // Labs job's status via its REST API, if you want so.
  110. // This should be the encrypted value in Travis
  111. var user = process.env.SAUCE_USERNAME;
  112. var pass = process.env.SAUCE_ACCESS_KEY;
  113. git.short(function(hash) {
  114. require('phin')({
  115. method: 'PUT',
  116. url: ['https://saucelabs.com/rest/v1', user, 'jobs', result.job_id].join('/'),
  117. auth: { user: user, pass: pass },
  118. data: {
  119. passed: result.passed,
  120. build: 'build-' + hash
  121. }
  122. }, function (error, response) {
  123. if (error) {
  124. console.log(error);
  125. callback(error);
  126. } else if (response.statusCode !== 200) {
  127. console.log(response);
  128. callback(new Error('Unexpected response status'));
  129. } else {
  130. callback(null, result.passed);
  131. }
  132. });
  133. });
  134. }
  135. }
  136. };
  137. }
  138. // Make the SauceLabs jobs
  139. (['all'].concat(browserTests)).map(makeJob);
  140. // Project configuration.
  141. grunt.initConfig({
  142. // Metadata required for build.
  143. build: grunt.file.readYAML('build/build.yml'),
  144. pkg: grunt.file.readJSON('package.json'),
  145. meta: {
  146. copyright: 'Copyright (c) 2009-<%= grunt.template.today("yyyy") %>',
  147. banner: '/*!\n' +
  148. ' * Less - <%= pkg.description %> v<%= pkg.version %>\n' +
  149. ' * http://lesscss.org\n' +
  150. ' *\n' +
  151. ' * <%= meta.copyright %>, <%= pkg.author.name %> <<%= pkg.author.email %>>\n' +
  152. ' * Licensed under the <%= pkg.license %> License.\n' +
  153. ' *\n' +
  154. ' */\n\n' +
  155. ' /**' +
  156. ' * @license <%= pkg.license %>\n' +
  157. ' */\n\n'
  158. },
  159. shell: {
  160. options: {
  161. stdout: true,
  162. failOnError: true,
  163. execOptions: {
  164. maxBuffer: Infinity
  165. }
  166. },
  167. test: {
  168. command: 'node test/index.js'
  169. },
  170. benchmark: {
  171. command: 'node benchmark/index.js'
  172. },
  173. plugin: {
  174. command: [
  175. 'node bin/lessc --clean-css="--s1 --advanced" test/less/lazy-eval.less tmp/lazy-eval.css',
  176. 'cd lib',
  177. 'node ../bin/lessc --clean-css="--s1 --advanced" ../test/less/lazy-eval.less ../tmp/lazy-eval.css'
  178. ].join(' && ')
  179. },
  180. "sourcemap-test": {
  181. command: [
  182. 'node bin/lessc --source-map=test/sourcemaps/maps/import-map.map test/less/import.less test/sourcemaps/import.css',
  183. 'node bin/lessc --source-map test/less/sourcemaps/basic.less test/sourcemaps/basic.css'
  184. ].join(' && ')
  185. }
  186. },
  187. browserify: {
  188. browser: {
  189. src: ['./lib/less-browser/bootstrap.js'],
  190. options: {
  191. exclude: ["promise"],
  192. browserifyOptions: {
  193. standalone: 'less'
  194. }
  195. },
  196. dest: 'tmp/less.js'
  197. }
  198. },
  199. concat: {
  200. options: {
  201. stripBanners: 'all',
  202. banner: '<%= meta.banner %>'
  203. },
  204. browsertest: {
  205. src: COMPRESS_FOR_TESTS ? '<%= uglify.test.dest %>' : '<%= browserify.browser.dest %>',
  206. dest: 'test/browser/less.js'
  207. },
  208. dist: {
  209. src: '<%= browserify.browser.dest %>',
  210. dest: 'dist/less.js'
  211. },
  212. // Rhino
  213. rhino: {
  214. options: {
  215. banner: '/* Less.js v<%= pkg.version %> RHINO | <%= meta.copyright %>, <%= pkg.author.name %> <<%= pkg.author.email %>> */\n\n',
  216. footer: '' // override task-level footer
  217. },
  218. src: ['<%= build.rhino %>'],
  219. dest: 'dist/less-rhino.js'
  220. },
  221. // lessc for Rhino
  222. rhinolessc: {
  223. options: {
  224. banner: '/* Less.js v<%= pkg.version %> RHINO | <%= meta.copyright %>, <%= pkg.author.name %> <<%= pkg.author.email %>> */\n\n',
  225. footer: '' // override task-level footer
  226. },
  227. src: ['<%= build.rhinolessc %>'],
  228. dest: 'dist/lessc-rhino.js'
  229. }
  230. },
  231. uglify: {
  232. options: {
  233. banner: '<%= meta.banner %>',
  234. mangle: true,
  235. compress: {
  236. pure_getters: true
  237. }
  238. },
  239. dist: {
  240. src: ['<%= concat.dist.dest %>'],
  241. dest: 'dist/less.min.js'
  242. },
  243. test: {
  244. src: '<%= browserify.browser.dest %>',
  245. dest: 'tmp/less.min.js'
  246. }
  247. },
  248. eslint: {
  249. target: ["Gruntfile.js",
  250. "test/**/*.js",
  251. "lib/less*/**/*.js",
  252. "bin/lessc",
  253. "!test/browser/jasmine-jsreporter.js",
  254. "!test/less/errors/plugin/plugin-error.js"
  255. ],
  256. options: {
  257. configFile: ".eslintrc.json"
  258. }
  259. },
  260. connect: {
  261. server: {
  262. options: {
  263. port: 8081
  264. }
  265. }
  266. },
  267. jasmine: {
  268. options: {
  269. keepRunner: true,
  270. host: 'http://localhost:8081/',
  271. vendor: ['test/browser/jasmine-jsreporter.js', 'test/browser/common.js', 'test/browser/less.js'],
  272. template: 'test/browser/test-runner-template.tmpl'
  273. },
  274. main: {
  275. // src is used to build list of less files to compile
  276. src: [
  277. 'test/less/*.less',
  278. // Don't test NPM import, obviously
  279. '!test/less/plugin-module.less',
  280. '!test/less/import-module.less',
  281. '!test/less/javascript.less',
  282. '!test/less/urls.less',
  283. '!test/less/empty.less'
  284. ],
  285. options: {
  286. helpers: 'test/browser/runner-main-options.js',
  287. specs: 'test/browser/runner-main-spec.js',
  288. outfile: 'tmp/browser/test-runner-main.html'
  289. }
  290. },
  291. legacy: {
  292. src: ['test/less/legacy/*.less'],
  293. options: {
  294. helpers: 'test/browser/runner-legacy-options.js',
  295. specs: 'test/browser/runner-legacy-spec.js',
  296. outfile: 'tmp/browser/test-runner-legacy.html'
  297. }
  298. },
  299. strictUnits: {
  300. src: ['test/less/strict-units/*.less'],
  301. options: {
  302. helpers: 'test/browser/runner-strict-units-options.js',
  303. specs: 'test/browser/runner-strict-units-spec.js',
  304. outfile: 'tmp/browser/test-runner-strict-units.html'
  305. }
  306. },
  307. errors: {
  308. src: ['test/less/errors/*.less', '!test/less/errors/javascript-error.less', 'test/browser/less/errors/*.less'],
  309. options: {
  310. timeout: 20000,
  311. helpers: 'test/browser/runner-errors-options.js',
  312. specs: 'test/browser/runner-errors-spec.js',
  313. outfile: 'tmp/browser/test-runner-errors.html'
  314. }
  315. },
  316. noJsErrors: {
  317. src: ['test/less/no-js-errors/*.less'],
  318. options: {
  319. helpers: 'test/browser/runner-no-js-errors-options.js',
  320. specs: 'test/browser/runner-no-js-errors-spec.js',
  321. outfile: 'tmp/browser/test-runner-no-js-errors.html'
  322. }
  323. },
  324. browser: {
  325. src: ['test/browser/less/*.less', 'test/browser/less/plugin/*.less'],
  326. options: {
  327. helpers: 'test/browser/runner-browser-options.js',
  328. specs: 'test/browser/runner-browser-spec.js',
  329. outfile: 'tmp/browser/test-runner-browser.html'
  330. }
  331. },
  332. relativeUrls: {
  333. src: ['test/browser/less/relative-urls/*.less'],
  334. options: {
  335. helpers: 'test/browser/runner-relative-urls-options.js',
  336. specs: 'test/browser/runner-relative-urls-spec.js',
  337. outfile: 'tmp/browser/test-runner-relative-urls.html'
  338. }
  339. },
  340. rootpath: {
  341. src: ['test/browser/less/rootpath/*.less'],
  342. options: {
  343. helpers: 'test/browser/runner-rootpath-options.js',
  344. specs: 'test/browser/runner-rootpath-spec.js',
  345. outfile: 'tmp/browser/test-runner-rootpath.html'
  346. }
  347. },
  348. rootpathRelative: {
  349. src: ['test/browser/less/rootpath-relative/*.less'],
  350. options: {
  351. helpers: 'test/browser/runner-rootpath-relative-options.js',
  352. specs: 'test/browser/runner-rootpath-relative-spec.js',
  353. outfile: 'tmp/browser/test-runner-rootpath-relative.html'
  354. }
  355. },
  356. production: {
  357. src: ['test/browser/less/production/*.less'],
  358. options: {
  359. helpers: 'test/browser/runner-production-options.js',
  360. specs: 'test/browser/runner-production-spec.js',
  361. outfile: 'tmp/browser/test-runner-production.html'
  362. }
  363. },
  364. modifyVars: {
  365. src: ['test/browser/less/modify-vars/*.less'],
  366. options: {
  367. helpers: 'test/browser/runner-modify-vars-options.js',
  368. specs: 'test/browser/runner-modify-vars-spec.js',
  369. outfile: 'tmp/browser/test-runner-modify-vars.html'
  370. }
  371. },
  372. globalVars: {
  373. src: ['test/browser/less/global-vars/*.less'],
  374. options: {
  375. helpers: 'test/browser/runner-global-vars-options.js',
  376. specs: 'test/browser/runner-global-vars-spec.js',
  377. outfile: 'tmp/browser/test-runner-global-vars.html'
  378. }
  379. },
  380. postProcessorPlugin: {
  381. src: ['test/less/postProcessorPlugin/*.less'],
  382. options: {
  383. helpers: ['test/plugins/postprocess/index.js','test/browser/runner-postProcessorPlugin-options.js'],
  384. specs: 'test/browser/runner-postProcessorPlugin.js',
  385. outfile: 'tmp/browser/test-runner-post-processor-plugin.html'
  386. }
  387. },
  388. preProcessorPlugin: {
  389. src: ['test/less/preProcessorPlugin/*.less'],
  390. options: {
  391. helpers: ['test/plugins/preprocess/index.js','test/browser/runner-preProcessorPlugin-options.js'],
  392. specs: 'test/browser/runner-preProcessorPlugin.js',
  393. outfile: 'tmp/browser/test-runner-pre-processor-plugin.html'
  394. }
  395. },
  396. visitorPlugin: {
  397. src: ['test/less/visitorPlugin/*.less'],
  398. options: {
  399. helpers: ['test/plugins/visitor/index.js','test/browser/runner-VisitorPlugin-options.js'],
  400. specs: 'test/browser/runner-VisitorPlugin.js',
  401. outfile: 'tmp/browser/test-runner-visitor-plugin.html'
  402. }
  403. },
  404. filemanagerPlugin: {
  405. src: ['test/less/filemanagerPlugin/*.less'],
  406. options: {
  407. helpers: ['test/plugins/filemanager/index.js','test/browser/runner-filemanagerPlugin-options.js'],
  408. specs: 'test/browser/runner-filemanagerPlugin.js',
  409. outfile: 'tmp/browser/test-runner-filemanager-plugin.html'
  410. }
  411. }
  412. },
  413. 'saucelabs-jasmine': sauceJobs,
  414. // Clean the version of less built for the tests
  415. clean: {
  416. test: ['test/browser/less.js', 'tmp', 'test/less-bom'],
  417. "sourcemap-test": ['test/sourcemaps/*.css', 'test/sourcemaps/*.map'],
  418. sauce_log: ["sc_*.log"]
  419. }
  420. });
  421. // Load these plugins to provide the necessary tasks
  422. grunt.loadNpmTasks('grunt-saucelabs');
  423. require('jit-grunt')(grunt);
  424. // Actually load this plugin's task(s).
  425. grunt.loadTasks('build/tasks');
  426. // by default, run tests
  427. grunt.registerTask('default', [
  428. 'test'
  429. ]);
  430. // Release
  431. grunt.registerTask('dist', [
  432. 'browserify:browser',
  433. 'concat:dist',
  434. 'uglify:dist'
  435. ]);
  436. // Release Rhino Version (UNSUPPORTED)
  437. grunt.registerTask('rhino', [
  438. 'browserify:rhino',
  439. 'concat:rhino',
  440. 'concat:rhinolessc'
  441. ]);
  442. // Create the browser version of less.js
  443. grunt.registerTask('browsertest-lessjs', [
  444. 'browserify:browser',
  445. 'uglify:test',
  446. 'concat:browsertest'
  447. ]);
  448. // Run all browser tests
  449. grunt.registerTask('browsertest', [
  450. 'browsertest-lessjs',
  451. 'connect',
  452. 'jasmine'
  453. ]);
  454. // setup a web server to run the browser tests in a browser rather than phantom
  455. grunt.registerTask('browsertest-server', [
  456. 'browsertest-lessjs',
  457. 'jasmine::build',
  458. 'connect::keepalive'
  459. ]);
  460. var previous_force_state = grunt.option("force");
  461. grunt.registerTask("force",function(set) {
  462. if (set === "on") {
  463. grunt.option("force",true);
  464. }
  465. else if (set === "off") {
  466. grunt.option("force",false);
  467. }
  468. else if (set === "restore") {
  469. grunt.option("force",previous_force_state);
  470. }
  471. });
  472. grunt.registerTask('sauce', [
  473. 'browsertest-lessjs',
  474. 'jasmine::build',
  475. 'connect',
  476. 'sauce-after-setup'
  477. ]);
  478. grunt.registerTask('sauce-after-setup', [
  479. 'saucelabs-jasmine:all',
  480. 'clean:sauce_log'
  481. ]);
  482. var testTasks = [
  483. 'clean',
  484. 'eslint',
  485. 'shell:test',
  486. 'shell:plugin',
  487. 'browsertest'
  488. ];
  489. if (isNaN(Number(process.env.TRAVIS_PULL_REQUEST, 10)) &&
  490. Number(process.env.TRAVIS_NODE_VERSION) === 4 &&
  491. (process.env.TRAVIS_BRANCH === "master" || process.env.TRAVIS_BRANCH === "3.x")) {
  492. testTasks.push("force:on");
  493. testTasks.push("sauce-after-setup");
  494. testTasks.push("force:off");
  495. }
  496. // Run all tests
  497. grunt.registerTask('test', testTasks);
  498. // Run shell plugin test
  499. grunt.registerTask('shell-plugin', ['shell:plugin']);
  500. // Run all tests
  501. grunt.registerTask('quicktest', testTasks.slice(0, -1));
  502. // generate a good test environment for testing sourcemaps
  503. grunt.registerTask('sourcemap-test', [
  504. 'clean:sourcemap-test',
  505. 'shell:sourcemap-test',
  506. 'connect::keepalive'
  507. ]);
  508. // Run benchmark
  509. grunt.registerTask('benchmark', [
  510. 'shell:benchmark'
  511. ]);
  512. };