Gruntfile.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * grunt-execute
  3. * https://github.com/Bartvds/grunt-execute
  4. *
  5. * Copyright (c) 2013 Bart van der Schoor
  6. * Licensed under the MIT license.
  7. */
  8. 'use strict';
  9. /*jshint -W098 */
  10. module.exports = function (grunt) {
  11. // Project configuration.
  12. var help = require('./test/helper.js');
  13. var assert = require('assert');
  14. function getShellFail(name) {
  15. return {
  16. options: {
  17. callback: function (err, stdout, stderr, cb) {
  18. if (!err) {
  19. console.log('\nExpected error for test execute: '.red + 'execute:' + name + '\n');
  20. cb(new Error('expected task to fail'));
  21. return;
  22. }
  23. assert(/^Command failed: /.test(err.message), 'invalid error message: ' + err.message);
  24. cb();
  25. }
  26. },
  27. command: 'grunt execute:' + name
  28. };
  29. }
  30. grunt.initConfig({
  31. jshint: {
  32. all: [
  33. 'Gruntfile.js',
  34. 'tasks/*.js',
  35. '<%= nodeunit.tests %>'
  36. ],
  37. options: {
  38. jshintrc: '.jshintrc'
  39. }
  40. },
  41. // Before generating any new files, remove any previously-created files.
  42. clean: {
  43. tests: ['tmp', 'test/tmp']
  44. },
  45. // Configuration to be run (and then tested).
  46. execute: {
  47. error: {src: ['test/fixtures/error.js']},
  48. error_module: {
  49. options: {
  50. module: true
  51. },
  52. src: ['test/fixtures/error.js']},
  53. node_sync: {
  54. src: ['test/fixtures/**/node.sync.*js']
  55. },
  56. node_async: {
  57. src: ['test/fixtures/node.async.js']
  58. },
  59. node_args: {
  60. options: {
  61. args: ['foo', 'bar']
  62. },
  63. src: ['test/fixtures/**/node.args.*js']
  64. },
  65. node_exit: {
  66. // will crash
  67. src: ['test/fixtures/node.exit.one.js']
  68. },
  69. zero: {
  70. src: ['test/fixtures/nonexisting.js']
  71. },
  72. module_sync: {
  73. options: {
  74. module: true
  75. },
  76. src: ['test/fixtures/module.sync.js']
  77. },
  78. module_async: {
  79. options: {
  80. module: true
  81. },
  82. src: ['test/fixtures/module.async.js']
  83. },
  84. harmony_on: {
  85. options: {
  86. nodeargs: ['--harmony']
  87. },
  88. src: ['test/fixtures/node.harmony.js']
  89. },
  90. call_sync: {
  91. call: function (grunt, options, async) {
  92. var ctx = help.getContext('call.sync.gruntfile.js');
  93. grunt.file.write(ctx.dest, ctx.data);
  94. }
  95. },
  96. call_async: {
  97. call: function (grunt, options, async) {
  98. var ctx = help.getContext('call.async.gruntfile.js');
  99. // callback get, makes grunt-execute run async
  100. var done = async();
  101. var fs = require('fs');
  102. setTimeout(function () {
  103. fs.writeFile(ctx.dest, ctx.data, function (err) {
  104. // call callback, passing error will fail the task
  105. done(err);
  106. });
  107. }, 500);
  108. }
  109. },
  110. beforeAfter: {
  111. options: {
  112. before: function (grunt, options, async) {
  113. var ctx = help.getContext('before.options.sync.gruntfile.js');
  114. grunt.file.write(ctx.dest, ctx.data);
  115. },
  116. after: function (grunt, options, async) {
  117. var ctx = help.getContext('after.options.sync.gruntfile.js');
  118. grunt.file.write(ctx.dest, ctx.data);
  119. }
  120. },
  121. before: function (grunt, options, async) {
  122. var ctx = help.getContext('before.sync.gruntfile.js');
  123. grunt.file.write(ctx.dest, ctx.data);
  124. },
  125. after: function (grunt, options, async) {
  126. var ctx = help.getContext('after.sync.gruntfile.js');
  127. grunt.file.write(ctx.dest, ctx.data);
  128. },
  129. src: ['test/fixtures/node.async.js']
  130. }
  131. },
  132. shell: {
  133. options: {
  134. stdout: false,
  135. stderr: false,
  136. execOptions: {
  137. cwd: '.'
  138. }
  139. },
  140. node_exit: getShellFail('node_exit:'),
  141. error: getShellFail('error'),
  142. error_module: getShellFail('error_module')
  143. },
  144. // Unit tests.
  145. nodeunit: {
  146. tests: ['test/*_test.js']
  147. }
  148. });
  149. grunt.loadTasks('tasks');
  150. grunt.loadNpmTasks('grunt-contrib-jshint');
  151. grunt.loadNpmTasks('grunt-contrib-clean');
  152. grunt.loadNpmTasks('grunt-contrib-nodeunit');
  153. grunt.loadNpmTasks('grunt-shell');
  154. grunt.registerTask('pass', [
  155. 'execute:node_args',
  156. 'execute:node_async',
  157. 'execute:node_sync',
  158. 'execute:harmony_on',
  159. 'execute:zero',
  160. 'execute:module_sync',
  161. 'execute:module_async',
  162. 'execute:call_sync',
  163. 'execute:call_async',
  164. 'execute:beforeAfter'
  165. ]);
  166. grunt.registerTask('fail', [
  167. 'shell:node_exit',
  168. 'shell:error',
  169. 'shell:error_module'
  170. ]);
  171. grunt.registerTask('test', ['jshint', 'clean', 'pass', 'fail', 'nodeunit']);
  172. grunt.registerTask('default', ['test']);
  173. grunt.registerTask('dev', ['error']);
  174. };