michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: "use strict"; michael@0: michael@0: module.metadata = { michael@0: "stability": "experimental" michael@0: }; michael@0: michael@0: var { exit, stdout } = require("../system"); michael@0: var cfxArgs = require("@test/options"); michael@0: michael@0: function runTests(findAndRunTests) { michael@0: var harness = require("./harness"); michael@0: michael@0: function onDone(tests) { michael@0: stdout.write("\n"); michael@0: var total = tests.passed + tests.failed; michael@0: stdout.write(tests.passed + " of " + total + " tests passed.\n"); michael@0: michael@0: if (tests.failed == 0) { michael@0: if (tests.passed === 0) michael@0: stdout.write("No tests were run\n"); michael@0: exit(0); michael@0: } else { michael@0: if (cfxArgs.verbose || cfxArgs.parseable) michael@0: printFailedTests(tests, stdout.write); michael@0: exit(1); michael@0: } michael@0: }; michael@0: michael@0: // We may have to run test on next cycle, otherwise XPCOM components michael@0: // are not correctly updated. michael@0: // For ex: nsIFocusManager.getFocusedElementForWindow may throw michael@0: // NS_ERROR_ILLEGAL_VALUE exception. michael@0: require("../timers").setTimeout(function () { michael@0: harness.runTests({ michael@0: findAndRunTests: findAndRunTests, michael@0: iterations: cfxArgs.iterations || 1, michael@0: filter: cfxArgs.filter, michael@0: profileMemory: cfxArgs.profileMemory, michael@0: stopOnError: cfxArgs.stopOnError, michael@0: verbose: cfxArgs.verbose, michael@0: parseable: cfxArgs.parseable, michael@0: print: stdout.write, michael@0: onDone: onDone michael@0: }); michael@0: }, 0); michael@0: } michael@0: michael@0: function printFailedTests(tests, print) { michael@0: let iterationNumber = 0; michael@0: let singleIteration = tests.testRuns.length == 1; michael@0: let padding = singleIteration ? "" : " "; michael@0: michael@0: print("\nThe following tests failed:\n"); michael@0: michael@0: for each (let testRun in tests.testRuns) { michael@0: iterationNumber++; michael@0: michael@0: if (!singleIteration) michael@0: print(" Iteration " + iterationNumber + ":\n"); michael@0: michael@0: for each (let test in testRun) { michael@0: if (test.failed > 0) { michael@0: print(padding + " " + test.name + ": " + test.errors +"\n"); michael@0: } michael@0: } michael@0: print("\n"); michael@0: } michael@0: } michael@0: michael@0: function main() { michael@0: var testsStarted = false; michael@0: michael@0: if (!testsStarted) { michael@0: testsStarted = true; michael@0: runTests(function findAndRunTests(loader, nextIteration) { michael@0: loader.require("../deprecated/unit-test").findAndRunTests({ michael@0: testOutOfProcess: false, michael@0: testInProcess: true, michael@0: stopOnError: cfxArgs.stopOnError, michael@0: filter: cfxArgs.filter, michael@0: onDone: nextIteration michael@0: }); michael@0: }); michael@0: } michael@0: }; michael@0: michael@0: if (require.main === module) michael@0: main(); michael@0: michael@0: exports.runTestsFromModule = function runTestsFromModule(module) { michael@0: let id = module.id; michael@0: // Make a copy of exports as it may already be frozen by module loader michael@0: let exports = {}; michael@0: Object.keys(module.exports).forEach(function(key) { michael@0: exports[key] = module.exports[key]; michael@0: }); michael@0: michael@0: runTests(function findAndRunTests(loader, nextIteration) { michael@0: // Consider that all these tests are CommonJS ones michael@0: loader.require('../../test').run(exports); michael@0: michael@0: // Reproduce what is done in unit-test-finder.findTests() michael@0: let tests = []; michael@0: for each (let name in Object.keys(exports).sort()) { michael@0: tests.push({ michael@0: setup: exports.setup, michael@0: teardown: exports.teardown, michael@0: testFunction: exports[name], michael@0: name: id + "." + name michael@0: }); michael@0: } michael@0: michael@0: // Reproduce what is done by unit-test.findAndRunTests() michael@0: var { TestRunner } = loader.require("../deprecated/unit-test"); michael@0: var runner = new TestRunner(); michael@0: runner.startMany({ michael@0: tests: tests, michael@0: stopOnError: cfxArgs.stopOnError, michael@0: onDone: nextIteration michael@0: }); michael@0: }); michael@0: }