説明なし

exit_test.js 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. 'use strict';
  2. /*
  3. ======== A Handy Little Nodeunit Reference ========
  4. https://github.com/caolan/nodeunit
  5. Test methods:
  6. test.expect(numAssertions)
  7. test.done()
  8. Test assertions:
  9. test.ok(value, [message])
  10. test.equal(actual, expected, [message])
  11. test.notEqual(actual, expected, [message])
  12. test.deepEqual(actual, expected, [message])
  13. test.notDeepEqual(actual, expected, [message])
  14. test.strictEqual(actual, expected, [message])
  15. test.notStrictEqual(actual, expected, [message])
  16. test.throws(block, [error], [message])
  17. test.doesNotThrow(block, [error], [message])
  18. test.ifError(value)
  19. */
  20. var fs = require('fs');
  21. var exec = require('child_process').exec;
  22. var _which = require('which').sync;
  23. function which(command) {
  24. try {
  25. _which(command);
  26. return command;
  27. } catch (err) {
  28. return false;
  29. }
  30. }
  31. // Look for grep first (any OS). If not found (but on Windows) look for find,
  32. // which is Windows' horribly crippled grep alternative.
  33. var grep = which('grep') || process.platform === 'win32' && which('find');
  34. exports['exit'] = {
  35. setUp: function(done) {
  36. this.origCwd = process.cwd();
  37. process.chdir('test/fixtures');
  38. done();
  39. },
  40. tearDown: function(done) {
  41. process.chdir(this.origCwd);
  42. done();
  43. },
  44. 'grep': function(test) {
  45. test.expect(1);
  46. // Many unit tests depend on this.
  47. test.ok(grep, 'A suitable "grep" or "find" program was not found in the PATH.');
  48. test.done();
  49. },
  50. // The rest of the tests are built dynamically, to keep things sane.
  51. };
  52. // A few helper functions.
  53. function normalizeLineEndings(s) {
  54. return s.replace(/\r?\n/g, '\n');
  55. }
  56. // Capture command output, normalizing captured stdout to unix file endings.
  57. function run(command, callback) {
  58. exec(command, function(error, stdout) {
  59. callback(error ? error.code : 0, normalizeLineEndings(stdout));
  60. });
  61. }
  62. // Read a fixture file, normalizing file contents to unix file endings.
  63. function fixture(filename) {
  64. return normalizeLineEndings(String(fs.readFileSync(filename)));
  65. }
  66. function buildTests() {
  67. // Build individual unit tests for command output.
  68. var counts = [10, 100, 1000];
  69. var outputs = [' stdout stderr', ' stdout', ' stderr'];
  70. var pipes = ['', ' | ' + grep + ' "std"'];
  71. counts.forEach(function(count) {
  72. outputs.forEach(function(output) {
  73. pipes.forEach(function(pipe) {
  74. var command = 'node log.js 0 ' + count + output + ' 2>&1' + pipe;
  75. exports['exit']['output (' + command + ')'] = function(test) {
  76. test.expect(2);
  77. run(command, function(code, actual) {
  78. var expected = fixture(count + output.replace(/ /g, '-') + '.txt');
  79. // Sometimes, the actual file lines are out of order on Windows.
  80. // But since the point of this lib is to drain the buffer and not
  81. // guarantee output order, we only test the length.
  82. test.equal(actual.length, expected.length, 'should be the same length.');
  83. // The "fail" lines in log.js should NOT be output!
  84. test.ok(actual.indexOf('fail') === -1, 'should not output after exit is called.');
  85. test.done();
  86. });
  87. };
  88. });
  89. });
  90. });
  91. // Build individual unit tests for exit codes.
  92. var codes = [0, 1, 123];
  93. codes.forEach(function(code) {
  94. var command = 'node log.js ' + code + ' 10 stdout stderr';
  95. exports['exit']['exit code (' + command + ')'] = function(test) {
  96. test.expect(1);
  97. run(command, function(actual) {
  98. // The specified exit code should be passed through.
  99. test.equal(actual, code, 'should exit with ' + code + ' error code.');
  100. test.done();
  101. });
  102. };
  103. });
  104. }
  105. // Don't bother building tests if grep wasn't found, otherwise everything will
  106. // fail and the error will get lost.
  107. if (grep) {
  108. buildTests();
  109. }