exec.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. var assert = require('assert')
  2. , h = require('./helpers')
  3. , utils = require('../lib/utils');
  4. utils.mixin(utils, utils);
  5. var tests = {
  6. 'before': function () {
  7. process.chdir('./test');
  8. }
  9. , 'after': function () {
  10. process.chdir('../');
  11. }
  12. , 'test basic exec': function (next) {
  13. var ex = utils.createExec('ls', function () {})
  14. , evts = { // Events should fire in this order
  15. cmdStart: [0, null]
  16. , stdout: [1, null]
  17. , cmdEnd: [2, null]
  18. , end: [3, null]
  19. }
  20. , incr = 0; // Increment with each event to check order
  21. assert.ok(ex instanceof utils.Exec);
  22. var addListenerAndIncrement = function (p) {
  23. ex.addListener(p, function () {
  24. evts[p][1] = incr;
  25. incr++;
  26. });
  27. };
  28. // Make sure basic events fire and fire in the right order
  29. for (var p in evts) {
  30. addListenerAndIncrement(p);
  31. }
  32. ex.run();
  33. ex.addListener('end', function () {
  34. for (var p in evts) {
  35. assert.equal(evts[p][0], evts[p][1]);
  36. }
  37. next();
  38. });
  39. }
  40. , 'test an exec failure': function (next) {
  41. var ex = utils.createExec('false', function () {});
  42. ex.addListener('error', function (msg, code) {
  43. assert.equal(1, code);
  44. next();
  45. });
  46. ex.run();
  47. }
  48. , 'test exec stdout events': function (next) {
  49. var ex = utils.createExec('echo "foo"', function () {});
  50. ex.addListener('stdout', function (data) {
  51. assert.equal("foo", h.trim(data.toString()));
  52. next();
  53. });
  54. ex.run();
  55. }
  56. , 'test exec stderr events': function (next) {
  57. var ex = utils.createExec('echo "foo" 1>&2', function () {});
  58. ex.addListener('stderr', function (data) {
  59. assert.equal("foo", h.trim(data.toString()));
  60. next();
  61. });
  62. ex.run();
  63. }
  64. , 'test piping results into next command': function (next) {
  65. var ex = utils.createExec('ls', function () {})
  66. , data
  67. , appended = false;
  68. ex.addListener('stdout', function (d) {
  69. data += h.trim(d.toString());
  70. });
  71. // After the first command ends, get the accumulated result,
  72. // and use it to build a new command to append to the queue.
  73. // Grep through the result for files that end in .js
  74. ex.addListener('cmdEnd', function () {
  75. // Only append after the first cmd, avoid infinite loop
  76. if (appended) {
  77. return;
  78. }
  79. appended = true;
  80. // Take the original output and build the new command
  81. ex.append('echo "' + data + '" | grep "\\.js$"');
  82. // Clear out data
  83. data = '';
  84. });
  85. // And the end, the stdout data has been cleared once, and the new
  86. // data should be the result of the second command
  87. ex.addListener('end', function () {
  88. // Should be a list of files ending in .js
  89. data = data.split('\n');
  90. data.forEach(function (d) {
  91. assert.ok(/\.js$/.test(d));
  92. });
  93. next();
  94. });
  95. ex.run();
  96. }
  97. };
  98. module.exports = tests;