runner.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. "use strict";
  5. module.metadata = {
  6. "stability": "experimental"
  7. };
  8. var { exit, stdout } = require("../system");
  9. var cfxArgs = require("@test/options");
  10. function runTests(findAndRunTests) {
  11. var harness = require("./harness");
  12. function onDone(tests) {
  13. stdout.write("\n");
  14. var total = tests.passed + tests.failed;
  15. stdout.write(tests.passed + " of " + total + " tests passed.\n");
  16. if (tests.failed == 0) {
  17. if (tests.passed === 0)
  18. stdout.write("No tests were run\n");
  19. exit(0);
  20. } else {
  21. if (cfxArgs.verbose || cfxArgs.parseable)
  22. printFailedTests(tests, stdout.write);
  23. exit(1);
  24. }
  25. };
  26. // We may have to run test on next cycle, otherwise XPCOM components
  27. // are not correctly updated.
  28. // For ex: nsIFocusManager.getFocusedElementForWindow may throw
  29. // NS_ERROR_ILLEGAL_VALUE exception.
  30. require("../timers").setTimeout(function () {
  31. harness.runTests({
  32. findAndRunTests: findAndRunTests,
  33. iterations: cfxArgs.iterations || 1,
  34. filter: cfxArgs.filter,
  35. profileMemory: cfxArgs.profileMemory,
  36. stopOnError: cfxArgs.stopOnError,
  37. verbose: cfxArgs.verbose,
  38. parseable: cfxArgs.parseable,
  39. print: stdout.write,
  40. onDone: onDone
  41. });
  42. }, 0);
  43. }
  44. function printFailedTests(tests, print) {
  45. let iterationNumber = 0;
  46. let singleIteration = tests.testRuns.length == 1;
  47. let padding = singleIteration ? "" : " ";
  48. print("\nThe following tests failed:\n");
  49. for each (let testRun in tests.testRuns) {
  50. iterationNumber++;
  51. if (!singleIteration)
  52. print(" Iteration " + iterationNumber + ":\n");
  53. for each (let test in testRun) {
  54. if (test.failed > 0) {
  55. print(padding + " " + test.name + ": " + test.errors +"\n");
  56. }
  57. }
  58. print("\n");
  59. }
  60. }
  61. function main() {
  62. var testsStarted = false;
  63. if (!testsStarted) {
  64. testsStarted = true;
  65. runTests(function findAndRunTests(loader, nextIteration) {
  66. loader.require("../deprecated/unit-test").findAndRunTests({
  67. testOutOfProcess: false,
  68. testInProcess: true,
  69. stopOnError: cfxArgs.stopOnError,
  70. filter: cfxArgs.filter,
  71. onDone: nextIteration
  72. });
  73. });
  74. }
  75. };
  76. if (require.main === module)
  77. main();
  78. exports.runTestsFromModule = function runTestsFromModule(module) {
  79. let id = module.id;
  80. // Make a copy of exports as it may already be frozen by module loader
  81. let exports = {};
  82. Object.keys(module.exports).forEach(function(key) {
  83. exports[key] = module.exports[key];
  84. });
  85. runTests(function findAndRunTests(loader, nextIteration) {
  86. // Consider that all these tests are CommonJS ones
  87. loader.require('../../test').run(exports);
  88. // Reproduce what is done in unit-test-finder.findTests()
  89. let tests = [];
  90. for each (let name in Object.keys(exports).sort()) {
  91. tests.push({
  92. setup: exports.setup,
  93. teardown: exports.teardown,
  94. testFunction: exports[name],
  95. name: id + "." + name
  96. });
  97. }
  98. // Reproduce what is done by unit-test.findAndRunTests()
  99. var { TestRunner } = loader.require("../deprecated/unit-test");
  100. var runner = new TestRunner();
  101. runner.startMany({
  102. tests: tests,
  103. stopOnError: cfxArgs.stopOnError,
  104. onDone: nextIteration
  105. });
  106. });
  107. }