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.

105 lines
2.8 KiB

  1. var grunt = require('grunt');
  2. var path = require('path');
  3. module.exports = helper = {};
  4. // where are fixtures are
  5. helper.fixtures = path.join(__dirname, '..', 'fixtures');
  6. // If verbose flag set, display output
  7. helper.verboseLog = function() {};
  8. if (grunt.util._.indexOf(process.argv, '-v') !== -1) {
  9. helper.verboseLog = function() { console.log.apply(null, arguments); };
  10. }
  11. // helper for creating assertTasks for testing tasks in child processes
  12. helper.assertTask = function assertTask(task, options) {
  13. var spawn = require('child_process').spawn;
  14. task = task || 'default';
  15. options = options || {};
  16. // get next/kill process trigger
  17. var trigger = options.trigger || '.*(Waiting).*';
  18. delete options.trigger;
  19. // CWD to spawn
  20. var cwd = options.cwd || process.cwd();
  21. delete options.cwd;
  22. // Use grunt this process uses
  23. var spawnOptions = [process.argv[1]];
  24. // Turn options into spawn options
  25. grunt.util._.each(options, function(val, key) {
  26. spawnOptions.push('--' + key);
  27. spawnOptions.push(val);
  28. });
  29. // Add the tasks to run
  30. spawnOptions = spawnOptions.concat(task);
  31. // Return an interface for testing this task
  32. function returnFunc(runs, done) {
  33. // Spawn the node this process uses
  34. var spawnGrunt = spawn(process.argv[0], spawnOptions, {cwd:cwd});
  35. var out = '';
  36. if (!grunt.util._.isArray(runs)) {
  37. runs = [runs];
  38. }
  39. // Append a last function to kill spawnGrunt
  40. runs.push(function() {
  41. spawnGrunt.kill('SIGINT');
  42. });
  43. // After watch starts waiting, run our commands then exit
  44. spawnGrunt.stdout.on('data', function(data) {
  45. data = grunt.log.uncolor(String(data));
  46. out += data;
  47. // If we should run the next function
  48. var shouldRun = true;
  49. // If our trigger has been found
  50. if (trigger !== false) {
  51. shouldRun = (new RegExp(trigger, 'gm')).test(data);
  52. }
  53. // Run the function
  54. if (shouldRun) {
  55. setTimeout(function() {
  56. var run = runs.shift();
  57. if (typeof run === 'function') { run(); }
  58. }, 500);
  59. }
  60. });
  61. // Throw errors for better testing
  62. spawnGrunt.stderr.on('data', function(data) {
  63. throw new Error(data);
  64. });
  65. // On process exit return what has been outputted
  66. spawnGrunt.on('exit', function() {
  67. done(out);
  68. });
  69. }
  70. returnFunc.options = options;
  71. return returnFunc;
  72. };
  73. // clean up files within fixtures
  74. helper.cleanUp = function cleanUp(files) {
  75. if (typeof files === 'string') files = [files];
  76. files.forEach(function(filepath) {
  77. filepath = path.join(helper.fixtures, filepath);
  78. if (grunt.file.exists(filepath)) {
  79. grunt.file.delete(filepath);
  80. }
  81. });
  82. };
  83. // Helper for testing cross platform
  84. helper.unixify = function(str) {
  85. str = grunt.util.normalizelf(str);
  86. return str.replace(/\\/g, '/');
  87. };