Repositorio del curso CCOM4030 el semestre B91 del proyecto kilometro0

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. var defined = require('defined');
  2. var createDefaultStream = require('./lib/default_stream');
  3. var Test = require('./lib/test');
  4. var createResult = require('./lib/results');
  5. var through = require('through');
  6. var canEmitExit = typeof process !== 'undefined' && process
  7. && typeof process.on === 'function' && process.browser !== true
  8. ;
  9. var canExit = typeof process !== 'undefined' && process
  10. && typeof process.exit === 'function'
  11. ;
  12. var nextTick = typeof setImmediate !== 'undefined'
  13. ? setImmediate
  14. : process.nextTick
  15. ;
  16. exports = module.exports = (function () {
  17. var harness;
  18. var lazyLoad = function () {
  19. return getHarness().apply(this, arguments);
  20. };
  21. lazyLoad.only = function () {
  22. return getHarness().only.apply(this, arguments);
  23. };
  24. lazyLoad.createStream = function (opts) {
  25. if (!opts) opts = {};
  26. if (!harness) {
  27. var output = through();
  28. getHarness({ stream: output, objectMode: opts.objectMode });
  29. return output;
  30. }
  31. return harness.createStream(opts);
  32. };
  33. lazyLoad.onFinish = function () {
  34. return getHarness().onFinish.apply(this, arguments);
  35. };
  36. lazyLoad.onFailure = function () {
  37. return getHarness().onFailure.apply(this, arguments);
  38. };
  39. lazyLoad.getHarness = getHarness;
  40. return lazyLoad;
  41. function getHarness(opts) {
  42. if (!opts) opts = {};
  43. opts.autoclose = !canEmitExit;
  44. if (!harness) harness = createExitHarness(opts);
  45. return harness;
  46. }
  47. })();
  48. function createExitHarness(conf) {
  49. if (!conf) conf = {};
  50. var harness = createHarness({
  51. autoclose: defined(conf.autoclose, false)
  52. });
  53. var stream = harness.createStream({ objectMode: conf.objectMode });
  54. var es = stream.pipe(conf.stream || createDefaultStream());
  55. if (canEmitExit) {
  56. es.on('error', function (err) { harness._exitCode = 1; });
  57. }
  58. var ended = false;
  59. stream.on('end', function () { ended = true; });
  60. if (conf.exit === false) return harness;
  61. if (!canEmitExit || !canExit) return harness;
  62. process.on('exit', function (code) {
  63. // let the process exit cleanly.
  64. if (code !== 0) {
  65. return;
  66. }
  67. if (!ended) {
  68. var only = harness._results._only;
  69. for (var i = 0; i < harness._tests.length; i++) {
  70. var t = harness._tests[i];
  71. if (only && t !== only) continue;
  72. t._exit();
  73. }
  74. }
  75. harness.close();
  76. process.exit(code || harness._exitCode);
  77. });
  78. return harness;
  79. }
  80. exports.createHarness = createHarness;
  81. exports.Test = Test;
  82. exports.test = exports; // tap compat
  83. exports.test.skip = Test.skip;
  84. var exitInterval;
  85. function createHarness(conf_) {
  86. if (!conf_) conf_ = {};
  87. var results = createResult();
  88. if (conf_.autoclose !== false) {
  89. results.once('done', function () { results.close(); });
  90. }
  91. var test = function (name, conf, cb) {
  92. var t = new Test(name, conf, cb);
  93. test._tests.push(t);
  94. (function inspectCode(st) {
  95. st.on('test', function sub(st_) {
  96. inspectCode(st_);
  97. });
  98. st.on('result', function (r) {
  99. if (!r.todo && !r.ok && typeof r !== 'string') test._exitCode = 1;
  100. });
  101. })(t);
  102. results.push(t);
  103. return t;
  104. };
  105. test._results = results;
  106. test._tests = [];
  107. test.createStream = function (opts) {
  108. return results.createStream(opts);
  109. };
  110. test.onFinish = function (cb) {
  111. results.on('done', cb);
  112. };
  113. test.onFailure = function (cb) {
  114. results.on('fail', cb);
  115. };
  116. var only = false;
  117. test.only = function () {
  118. if (only) throw new Error('there can only be one only test');
  119. only = true;
  120. var t = test.apply(null, arguments);
  121. results.only(t);
  122. return t;
  123. };
  124. test._exitCode = 0;
  125. test.close = function () { results.close(); };
  126. return test;
  127. }