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