Gruntfile.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. "use strict";
  2. var resolve = require('resolve');
  3. var path = require('path');
  4. var testFolder = path.relative(process.cwd(), path.dirname(resolve.sync('@less/test-data')));
  5. var lessFolder = path.join(testFolder, 'less');
  6. module.exports = function(grunt) {
  7. grunt.option("stack", true);
  8. // Report the elapsed execution time of tasks.
  9. require("time-grunt")(grunt);
  10. var git = require("git-rev");
  11. // Sauce Labs browser
  12. var browsers = [
  13. // Desktop browsers
  14. {
  15. browserName: "chrome",
  16. version: "latest",
  17. platform: "Windows 7"
  18. },
  19. {
  20. browserName: "firefox",
  21. version: "latest",
  22. platform: "Linux"
  23. },
  24. {
  25. browserName: "safari",
  26. version: "9",
  27. platform: "OS X 10.11"
  28. },
  29. {
  30. browserName: "internet explorer",
  31. version: "8",
  32. platform: "Windows XP"
  33. },
  34. {
  35. browserName: "internet explorer",
  36. version: "11",
  37. platform: "Windows 8.1"
  38. },
  39. {
  40. browserName: "edge",
  41. version: "13",
  42. platform: "Windows 10"
  43. },
  44. // Mobile browsers
  45. {
  46. browserName: "ipad",
  47. deviceName: "iPad Air Simulator",
  48. deviceOrientation: "portrait",
  49. version: "8.4",
  50. platform: "OS X 10.9"
  51. },
  52. {
  53. browserName: "iphone",
  54. deviceName: "iPhone 5 Simulator",
  55. deviceOrientation: "portrait",
  56. version: "9.3",
  57. platform: "OS X 10.11"
  58. },
  59. {
  60. browserName: "android",
  61. deviceName: "Google Nexus 7 HD Emulator",
  62. deviceOrientation: "portrait",
  63. version: "4.4",
  64. platform: "Linux"
  65. }
  66. ];
  67. var sauceJobs = {};
  68. var browserTests = [
  69. "filemanager-plugin",
  70. "visitor-plugin",
  71. "global-vars",
  72. "modify-vars",
  73. "production",
  74. "rootpath-relative",
  75. "rootpath-rewrite-urls",
  76. "rootpath",
  77. "relative-urls",
  78. "rewrite-urls",
  79. "browser",
  80. "no-js-errors",
  81. "legacy"
  82. ];
  83. function makeJob(testName) {
  84. sauceJobs[testName] = {
  85. options: {
  86. urls:
  87. testName === "all"
  88. ? browserTests.map(function(name) {
  89. return (
  90. "http://localhost:8081/tmp/browser/test-runner-" +
  91. name +
  92. ".html"
  93. );
  94. })
  95. : [
  96. "http://localhost:8081/tmp/browser/test-runner-" +
  97. testName +
  98. ".html"
  99. ],
  100. testname:
  101. testName === "all" ? "Unit Tests for Less.js" : testName,
  102. browsers: browsers,
  103. public: "public",
  104. recordVideo: false,
  105. videoUploadOnPass: false,
  106. recordScreenshots: process.env.TRAVIS_BRANCH !== "master",
  107. build:
  108. process.env.TRAVIS_BRANCH === "master"
  109. ? process.env.TRAVIS_JOB_ID
  110. : undefined,
  111. tags: [
  112. process.env.TRAVIS_BUILD_NUMBER,
  113. process.env.TRAVIS_PULL_REQUEST,
  114. process.env.TRAVIS_BRANCH
  115. ],
  116. statusCheckAttempts: -1,
  117. sauceConfig: {
  118. "idle-timeout": 100
  119. },
  120. throttled: 5,
  121. onTestComplete: function(result, callback) {
  122. // Called after a unit test is done, per page, per browser
  123. // 'result' param is the object returned by the test framework's reporter
  124. // 'callback' is a Node.js style callback function. You must invoke it after you
  125. // finish your work.
  126. // Pass a non-null value as the callback's first parameter if you want to throw an
  127. // exception. If your function is synchronous you can also throw exceptions
  128. // directly.
  129. // Passing true or false as the callback's second parameter passes or fails the
  130. // test. Passing undefined does not alter the test result. Please note that this
  131. // only affects the grunt task's result. You have to explicitly update the Sauce
  132. // Labs job's status via its REST API, if you want so.
  133. // This should be the encrypted value in Travis
  134. var user = process.env.SAUCE_USERNAME;
  135. var pass = process.env.SAUCE_ACCESS_KEY;
  136. git.short(function(hash) {
  137. require("phin")(
  138. {
  139. method: "PUT",
  140. url: [
  141. "https://saucelabs.com/rest/v1",
  142. user,
  143. "jobs",
  144. result.job_id
  145. ].join("/"),
  146. auth: { user: user, pass: pass },
  147. data: {
  148. passed: result.passed,
  149. build: "build-" + hash
  150. }
  151. },
  152. function(error, response) {
  153. if (error) {
  154. console.log(error);
  155. callback(error);
  156. } else if (response.statusCode !== 200) {
  157. console.log(response);
  158. callback(
  159. new Error("Unexpected response status")
  160. );
  161. } else {
  162. callback(null, result.passed);
  163. }
  164. }
  165. );
  166. });
  167. }
  168. }
  169. };
  170. }
  171. // Make the SauceLabs jobs
  172. ["all"].concat(browserTests).map(makeJob);
  173. // Project configuration.
  174. grunt.initConfig({
  175. shell: {
  176. options: {
  177. stdout: true,
  178. failOnError: true,
  179. execOptions: {
  180. maxBuffer: Infinity
  181. }
  182. },
  183. build: {
  184. command: [
  185. /** Browser runtime */
  186. "node build/rollup.js --dist",
  187. /** Copy to repo root */
  188. "npm run copy:root",
  189. /** Node.js runtime */
  190. "npm run build"
  191. ].join(" && ")
  192. },
  193. testbuild: {
  194. command: [
  195. "npm run build",
  196. "node build/rollup.js --browser --out=./tmp/browser/less.min.js"
  197. ].join(" && ")
  198. },
  199. testcjs: {
  200. command: "npm run build"
  201. },
  202. testbrowser: {
  203. command: "node build/rollup.js --browser --out=./tmp/browser/less.min.js"
  204. },
  205. test: {
  206. command: 'ts-node test/test-es6.ts && node test/index.js'
  207. },
  208. generatebrowser: {
  209. command: 'node test/browser/generator/generate.js'
  210. },
  211. runbrowser: {
  212. command: 'node test/browser/generator/runner.js'
  213. },
  214. benchmark: {
  215. command: "node benchmark/index.js"
  216. },
  217. opts: {
  218. // test running with all current options (using `opts` since `options` means something already)
  219. command: [
  220. // @TODO: make this more thorough
  221. // CURRENT OPTIONS
  222. `node bin/lessc --ie-compat ${lessFolder}/_main/lazy-eval.less tmp/lazy-eval.css`,
  223. // --math
  224. `node bin/lessc --math=always ${lessFolder}/_main/lazy-eval.less tmp/lazy-eval.css`,
  225. `node bin/lessc --math=parens-division ${lessFolder}/_main/lazy-eval.less tmp/lazy-eval.css`,
  226. `node bin/lessc --math=parens ${lessFolder}/_main/lazy-eval.less tmp/lazy-eval.css`,
  227. `node bin/lessc --math=strict ${lessFolder}/_main/lazy-eval.less tmp/lazy-eval.css`,
  228. `node bin/lessc --math=strict-legacy ${lessFolder}/_main/lazy-eval.less tmp/lazy-eval.css`,
  229. // DEPRECATED OPTIONS
  230. // --strict-math
  231. `node bin/lessc --strict-math=on ${lessFolder}/_main/lazy-eval.less tmp/lazy-eval.css`
  232. ].join(" && ")
  233. },
  234. plugin: {
  235. command: [
  236. `node bin/lessc --clean-css="--s1 --advanced" ${lessFolder}/_main/lazy-eval.less tmp/lazy-eval.css`,
  237. "cd lib",
  238. `node ../bin/lessc --clean-css="--s1 --advanced" ../${lessFolder}/_main/lazy-eval.less ../tmp/lazy-eval.css`,
  239. `node ../bin/lessc --source-map=lazy-eval.css.map --autoprefix ../${lessFolder}/_main/lazy-eval.less ../tmp/lazy-eval.css`,
  240. "cd ..",
  241. // Test multiple plugins
  242. `node bin/lessc --plugin=clean-css="--s1 --advanced" --plugin=autoprefix="ie 11,Edge >= 13,Chrome >= 47,Firefox >= 45,iOS >= 9.2,Safari >= 9" ${lessFolder}/_main/lazy-eval.less tmp/lazy-eval.css`
  243. ].join(" && ")
  244. },
  245. "sourcemap-test": {
  246. // quoted value doesn't seem to get picked up by time-grunt, or isn't output, at least; maybe just "sourcemap" is fine?
  247. command: [
  248. `node bin/lessc --source-map=test/sourcemaps/maps/import-map.map ${lessFolder}/_main/import.less test/sourcemaps/import.css`,
  249. `node bin/lessc --source-map ${lessFolder}/sourcemaps/basic.less test/sourcemaps/basic.css`
  250. ].join(" && ")
  251. }
  252. },
  253. eslint: {
  254. target: [
  255. "test/**/*.js",
  256. "src/less*/**/*.js",
  257. "!test/less/errors/plugin/plugin-error.js"
  258. ],
  259. options: {
  260. configFile: ".eslintrc.js",
  261. fix: true
  262. }
  263. },
  264. connect: {
  265. server: {
  266. options: {
  267. port: 8081,
  268. base: '../..'
  269. }
  270. }
  271. },
  272. "saucelabs-mocha": sauceJobs,
  273. // Clean the version of less built for the tests
  274. clean: {
  275. test: ["test/browser/less.js", "tmp", "test/less-bom"],
  276. "sourcemap-test": [
  277. "test/sourcemaps/*.css",
  278. "test/sourcemaps/*.map"
  279. ],
  280. sauce_log: ["sc_*.log"]
  281. }
  282. });
  283. // Load these plugins to provide the necessary tasks
  284. grunt.loadNpmTasks("grunt-saucelabs");
  285. require("jit-grunt")(grunt);
  286. // by default, run tests
  287. grunt.registerTask("default", ["test"]);
  288. // Release
  289. grunt.registerTask("dist", [
  290. "shell:build"
  291. ]);
  292. // Create the browser version of less.js
  293. grunt.registerTask("browsertest-lessjs", [
  294. "shell:testbrowser"
  295. ]);
  296. // Run all browser tests
  297. grunt.registerTask("browsertest", [
  298. "browsertest-lessjs",
  299. "connect",
  300. "shell:runbrowser"
  301. ]);
  302. // setup a web server to run the browser tests in a browser rather than phantom
  303. grunt.registerTask("browsertest-server", [
  304. "browsertest-lessjs",
  305. "shell:generatebrowser",
  306. "connect::keepalive"
  307. ]);
  308. var previous_force_state = grunt.option("force");
  309. grunt.registerTask("force", function(set) {
  310. if (set === "on") {
  311. grunt.option("force", true);
  312. } else if (set === "off") {
  313. grunt.option("force", false);
  314. } else if (set === "restore") {
  315. grunt.option("force", previous_force_state);
  316. }
  317. });
  318. grunt.registerTask("sauce", [
  319. "browsertest-lessjs",
  320. "shell:generatebrowser",
  321. "connect",
  322. "sauce-after-setup"
  323. ]);
  324. grunt.registerTask("sauce-after-setup", [
  325. "saucelabs-mocha:all",
  326. "clean:sauce_log"
  327. ]);
  328. var testTasks = [
  329. "clean",
  330. "eslint",
  331. "shell:testbuild",
  332. "shell:test",
  333. "shell:opts",
  334. "shell:plugin",
  335. "connect",
  336. "shell:runbrowser"
  337. ];
  338. if (
  339. isNaN(Number(process.env.TRAVIS_PULL_REQUEST, 10)) &&
  340. (process.env.TRAVIS_BRANCH === "master")
  341. ) {
  342. testTasks.push("force:on");
  343. testTasks.push("sauce-after-setup");
  344. testTasks.push("force:off");
  345. }
  346. // Run all tests
  347. grunt.registerTask("test", testTasks);
  348. // Run shell option tests (includes deprecated options)
  349. grunt.registerTask("shell-options", ["shell:opts"]);
  350. // Run shell plugin test
  351. grunt.registerTask("shell-plugin", ["shell:plugin"]);
  352. // Quickly build and run Node tests
  353. grunt.registerTask("quicktest", [
  354. "shell:testcjs",
  355. "shell:test"
  356. ]);
  357. // generate a good test environment for testing sourcemaps
  358. grunt.registerTask("sourcemap-test", [
  359. "clean:sourcemap-test",
  360. "shell:build:lessc",
  361. "shell:sourcemap-test",
  362. "connect::keepalive"
  363. ]);
  364. // Run benchmark
  365. grunt.registerTask("benchmark", [
  366. "shell:testcjs",
  367. "shell:benchmark"
  368. ]);
  369. };