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/. */
5 // This is the directory where gcov is emitting the gcda files.
6 const gcovPrefixPath = Services.env.get("GCOV_PREFIX");
7 // This is the directory where codecoverage.py is expecting to see the gcda files.
8 const gcovResultsPath = Services.env.get("GCOV_RESULTS_DIR");
9 // This is the directory where the JS engine is emitting the lcov files.
10 const jsvmPrefixPath = Services.env.get("JS_CODE_COVERAGE_OUTPUT_DIR");
11 // This is the directory where codecoverage.py is expecting to see the lcov files.
12 const jsvmResultsPath = Services.env.get("JSVM_RESULTS_DIR");
14 function awaitPromise(promise) {
19 .catch(e => (error = e))
24 Services.tm.spinEventLoopUntil(
25 "PerTestCoverageUtils.sys.mjs:awaitPromise",
29 throw new Error(error);
34 export var PerTestCoverageUtils = class PerTestCoverageUtilsClass {
35 // Resets the counters to 0.
36 static async beforeTest() {
37 if (!PerTestCoverageUtils.enabled) {
41 // Flush the counters.
42 let codeCoverageService = Cc[
43 "@mozilla.org/tools/code-coverage;1"
44 ].getService(Ci.nsICodeCoverage);
45 await codeCoverageService.flushCounters();
47 // Remove coverage files created by the flush, and those that might have been created between the end of a previous test and the beginning of the next one (e.g. some tests can create a new content process for every sub-test).
48 await IOUtils.remove(gcovPrefixPath, {
52 await IOUtils.remove(jsvmPrefixPath, {
57 // Move coverage files from the GCOV_RESULTS_DIR and JSVM_RESULTS_DIR directories, so we can accumulate the counters.
58 await IOUtils.move(gcovResultsPath, gcovPrefixPath);
59 await IOUtils.move(jsvmResultsPath, jsvmPrefixPath);
62 static beforeTestSync() {
63 awaitPromise(this.beforeTest());
66 // Dumps counters and moves the gcda files in the directory expected by codecoverage.py.
67 static async afterTest() {
68 if (!PerTestCoverageUtils.enabled) {
72 // Flush the counters.
73 let codeCoverageService = Cc[
74 "@mozilla.org/tools/code-coverage;1"
75 ].getService(Ci.nsICodeCoverage);
76 await codeCoverageService.flushCounters();
78 // Move the coverage files in GCOV_RESULTS_DIR and JSVM_RESULTS_DIR, so that the execution from now to shutdown (or next test) is not counted.
79 await IOUtils.move(gcovPrefixPath, gcovResultsPath);
80 await IOUtils.move(jsvmPrefixPath, jsvmResultsPath);
83 static afterTestSync() {
84 awaitPromise(this.afterTest());
88 PerTestCoverageUtils.enabled = !!gcovResultsPath;