You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

361 lines
9.7 KiB

  1. /*
  2. * grunt
  3. * http://gruntjs.com/
  4. *
  5. * Copyright (c) 2013 "Cowboy" Ben Alman
  6. * Licensed under the MIT license.
  7. * https://github.com/gruntjs/grunt/blob/master/LICENSE-MIT
  8. */
  9. // For now, run this "test suite" with:
  10. // grunt --gruntfile ./test/gruntfile/multi-task-files.js
  11. 'use strict';
  12. module.exports = function(grunt) {
  13. grunt.file.setBase('../fixtures/files');
  14. grunt.initConfig({
  15. build: '123',
  16. mappings: {
  17. cwd: 'src/',
  18. dest: 'foo/',
  19. ext: '.bar',
  20. rename: function(destBase, destPath) {
  21. return destBase + 'baz/' + destPath.replace(/\.js$/, '<%= mappings.ext %>');
  22. },
  23. },
  24. run: {
  25. options: {a: 1, b: 11},
  26. // This is the "compact" format, where the target name is actually the
  27. // dest filename. Doesn't support per-target options, templated dest, or
  28. // >1 srcs-dest grouping.
  29. 'dist/built.js': 'src/*1.js',
  30. 'dist/built1.js': ['src/*1.js', 'src/*2.js'],
  31. // This is the "medium" format. The target name is arbitrary and can be
  32. // used like "grunt run:built". Supports per-target options, templated
  33. // dest, and arbitrary "extra" paramters. Doesn't support >1 srcs-dest
  34. // grouping.
  35. built: {
  36. options: {a: 2, c: 22},
  37. src: ['src/*1.js', 'src/*2.js'],
  38. dest: 'dist/built-<%= build %>.js',
  39. extra: 123,
  40. },
  41. // This is the "full" format. The target name is arbitrary and can be
  42. // used like "grunt run:long1". Supports per-target options, templated
  43. // dest and >1 srcs-dest grouping.
  44. long1: {
  45. options: {a: 3, c: 33},
  46. files: {
  47. 'dist/built-<%= build %>-a.js': ['src/*1.js'],
  48. 'dist/built-<%= build %>-b.js': ['src/*1.js', 'src/*2.js'],
  49. }
  50. },
  51. long2: {
  52. options: {a: 4, c: 44},
  53. files: [
  54. {'dist/built-<%= build %>-a.js': ['src/*.whoops']},
  55. {'dist/built-<%= build %>-b.js': ['src/*1.js', 'src/*2.js']},
  56. ]
  57. },
  58. // This "full" variant supports per srcs-dest arbitrary "extra" paramters.
  59. long3: {
  60. options: {a: 5, c: 55},
  61. files: [
  62. {dest: 'dist/built-<%= build %>-a.js', src: ['src/*2.js'], extra: 456},
  63. {dest: 'dist/built-<%= build %>-b.js', src: ['src/*1.js', 'src/*2.js'], extra: 789},
  64. ]
  65. },
  66. // File mapping options can be specified in these 2 formats.
  67. built_mapping: {
  68. options: {a: 6, c: 66},
  69. expand: true,
  70. cwd: '<%= mappings.cwd %>',
  71. src: ['*1.js', '*2.js'],
  72. dest: '<%= mappings.dest %>',
  73. rename: '<%= mappings.rename %>',
  74. extra: 123
  75. },
  76. long3_mapping: {
  77. options: {a: 7, c: 77},
  78. files: [
  79. {
  80. expand: true,
  81. cwd: '<%= mappings.cwd %>',
  82. src: ['*1.js', '*2.js'],
  83. dest: '<%= mappings.dest %>',
  84. rename: '<%= mappings.rename %>',
  85. extra: 123
  86. }
  87. ]
  88. },
  89. // Need to ensure the task function is run if no files or options were
  90. // specified!
  91. no_files_or_options: {},
  92. },
  93. });
  94. var results = {};
  95. var counters = [];
  96. var counter = -1;
  97. grunt.registerMultiTask('run', 'Store stuff for later testing.', function() {
  98. var key = this.nameArgs;
  99. results[key] = {
  100. options: this.options({d: 9}),
  101. files: this.files,
  102. };
  103. // Test asynchronous-ness.
  104. var done;
  105. if (counter++ % 2 === 0) {
  106. done = this.async();
  107. setTimeout(function() {
  108. counters.push(counter);
  109. done();
  110. }, 10);
  111. } else {
  112. counters.push(counter);
  113. }
  114. });
  115. var expecteds = {
  116. 'run:no_files_or_options': {
  117. options: {a: 1, b: 11, d: 9},
  118. files: [],
  119. },
  120. 'run:dist/built.js': {
  121. options: {a: 1, b: 11, d: 9},
  122. files: [
  123. {
  124. dest: 'dist/built.js',
  125. src: ['src/file1.js'],
  126. orig: {
  127. dest: 'dist/built.js',
  128. src: ['src/*1.js'],
  129. },
  130. },
  131. ]
  132. },
  133. 'run:dist/built1.js': {
  134. options: {a: 1, b: 11, d: 9},
  135. files: [
  136. {
  137. dest: 'dist/built1.js',
  138. src: ['src/file1.js', 'src/file2.js'],
  139. orig: {
  140. dest: 'dist/built1.js',
  141. src: ['src/*1.js', 'src/*2.js'],
  142. },
  143. },
  144. ]
  145. },
  146. 'run:built': {
  147. options: {a: 2, b: 11, c: 22, d: 9},
  148. files: [
  149. {
  150. dest: 'dist/built-123.js',
  151. src: ['src/file1.js', 'src/file2.js'],
  152. extra: 123,
  153. orig: {
  154. dest: 'dist/built-123.js',
  155. src: ['src/*1.js', 'src/*2.js'],
  156. extra: 123,
  157. },
  158. },
  159. ],
  160. },
  161. 'run:long1': {
  162. options: {a: 3, b: 11, c: 33, d: 9},
  163. files: [
  164. {
  165. dest: 'dist/built-123-a.js',
  166. src: ['src/file1.js'],
  167. orig: {
  168. dest: 'dist/built-123-a.js',
  169. src: ['src/*1.js'],
  170. },
  171. },
  172. {
  173. dest: 'dist/built-123-b.js',
  174. src: ['src/file1.js', 'src/file2.js'],
  175. orig: {
  176. dest: 'dist/built-123-b.js',
  177. src: ['src/*1.js', 'src/*2.js'],
  178. },
  179. },
  180. ],
  181. },
  182. 'run:long2': {
  183. options: {a: 4, b: 11, c: 44, d: 9},
  184. files: [
  185. {
  186. dest: 'dist/built-123-a.js',
  187. src: [],
  188. orig: {
  189. dest: 'dist/built-123-a.js',
  190. src: ['src/*.whoops'],
  191. },
  192. },
  193. {
  194. dest: 'dist/built-123-b.js',
  195. src: ['src/file1.js', 'src/file2.js'],
  196. orig: {
  197. dest: 'dist/built-123-b.js',
  198. src: ['src/*1.js', 'src/*2.js'],
  199. },
  200. },
  201. ],
  202. },
  203. 'run:long3': {
  204. options: {a: 5, b: 11, c: 55, d: 9},
  205. files: [
  206. {
  207. dest: 'dist/built-123-a.js',
  208. src: ['src/file2.js'],
  209. extra: 456,
  210. orig: {
  211. dest: 'dist/built-123-a.js',
  212. src: ['src/*2.js'],
  213. extra: 456,
  214. },
  215. },
  216. {
  217. dest: 'dist/built-123-b.js',
  218. src: ['src/file1.js', 'src/file2.js'],
  219. extra: 789,
  220. orig: {
  221. src: ['src/*1.js', 'src/*2.js'],
  222. dest: 'dist/built-123-b.js',
  223. extra: 789,
  224. },
  225. },
  226. ],
  227. },
  228. 'run:built_mapping': {
  229. options: {a: 6, b: 11, c: 66, d: 9},
  230. files: [
  231. {
  232. dest: 'foo/baz/file1.bar',
  233. src: ['src/file1.js'],
  234. extra: 123,
  235. orig: {
  236. expand: true,
  237. cwd: grunt.config.get('mappings.cwd'),
  238. src: ['*1.js', '*2.js'],
  239. dest: grunt.config.get('mappings.dest'),
  240. rename: grunt.config.get('run.built_mapping.rename'),
  241. extra: 123,
  242. },
  243. },
  244. {
  245. dest: 'foo/baz/file2.bar',
  246. src: ['src/file2.js'],
  247. extra: 123,
  248. orig: {
  249. expand: true,
  250. cwd: grunt.config.get('run.built_mapping.cwd'),
  251. src: ['*1.js', '*2.js'],
  252. dest: grunt.config.get('run.built_mapping.dest'),
  253. rename: grunt.config.get('run.built_mapping.rename'),
  254. extra: 123,
  255. },
  256. },
  257. ],
  258. },
  259. 'run:long3_mapping': {
  260. options: {a: 7, b: 11, c: 77, d: 9},
  261. files: [
  262. {
  263. dest: 'foo/baz/file1.bar',
  264. src: ['src/file1.js'],
  265. extra: 123,
  266. orig: {
  267. expand: true,
  268. cwd: grunt.config.get('mappings.cwd'),
  269. src: ['*1.js', '*2.js'],
  270. dest: grunt.config.get('mappings.dest'),
  271. rename: grunt.config.get('mappings.rename'),
  272. extra: 123,
  273. },
  274. },
  275. {
  276. dest: 'foo/baz/file2.bar',
  277. src: ['src/file2.js'],
  278. extra: 123,
  279. orig: {
  280. expand: true,
  281. cwd: grunt.config.get('mappings.cwd'),
  282. src: ['*1.js', '*2.js'],
  283. dest: grunt.config.get('mappings.dest'),
  284. rename: grunt.config.get('run.built_mapping.rename'),
  285. extra: 123,
  286. },
  287. },
  288. ],
  289. },
  290. };
  291. var assert = require('assert');
  292. var difflet = require('difflet')({indent: 2, comment: true});
  293. var test = function(name, fn) {
  294. try {
  295. fn();
  296. } catch (err) {
  297. grunt.log.subhead('Assertion Failure in ' + name);
  298. console.log(difflet.compare(err.expected, err.actual));
  299. throw new Error(err.message);
  300. }
  301. };
  302. grunt.registerTask('test', 'Test file and option objects.', function() {
  303. var key = 'run:' + this.nameArgs.replace(/^.*?:/, '');
  304. var all = key === 'run:all';
  305. var actual = all ? results : results[key];
  306. var expected = all ? expecteds : expecteds[key];
  307. test(this.name, function() {
  308. assert.deepEqual(actual, expected, 'Actual should match expected.');
  309. });
  310. if (all) {
  311. results = {};
  312. } else {
  313. delete results[key];
  314. }
  315. });
  316. grunt.registerTask('test:counters', 'Test function execution order.', function() {
  317. test(this.name, function() {
  318. assert.equal(counters.length, counter + 1, 'Task functions should have run the correct number of times.');
  319. var expected = [];
  320. for (var i = 0; i < counters.length; i++) { expected.push(i); }
  321. assert.deepEqual(counters, expected, 'Task functions should have actually executed in-order.');
  322. });
  323. });
  324. grunt.registerTask('default', [
  325. 'run:no_files_or_options',
  326. 'test:no_files_or_options',
  327. 'run:dist/built.js',
  328. 'test:dist/built.js',
  329. 'run:dist/built1.js',
  330. 'test:dist/built1.js',
  331. 'run:built',
  332. 'test:built',
  333. 'run:long1',
  334. 'test:long1',
  335. 'run:long2',
  336. 'test:long2',
  337. 'run:long3',
  338. 'test:long3',
  339. 'run:built_mapping',
  340. 'test:built_mapping',
  341. 'run:long3_mapping',
  342. 'test:long3_mapping',
  343. 'run',
  344. 'test:all',
  345. 'test:counters',
  346. ]);
  347. };