Bug 1839315: part 4) Link from `SheetLoadData::mWasAlternate` to spec. r=emilio DONTBUILD
[gecko.git] / tools / code-coverage / PerTestCoverageUtils.sys.mjs
blob179dd905a51baf071eb5531d54dcc999a5cd5e39
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) {
15   let ret;
16   let complete = false;
17   let error = null;
18   promise
19     .catch(e => (error = e))
20     .then(v => {
21       ret = v;
22       complete = true;
23     });
24   Services.tm.spinEventLoopUntil(
25     "PerTestCoverageUtils.sys.mjs:awaitPromise",
26     () => complete
27   );
28   if (error) {
29     throw new Error(error);
30   }
31   return ret;
34 export var PerTestCoverageUtils = class PerTestCoverageUtilsClass {
35   // Resets the counters to 0.
36   static async beforeTest() {
37     if (!PerTestCoverageUtils.enabled) {
38       return;
39     }
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, {
49       recursive: true,
50       ignoreAbsent: true,
51     });
52     await IOUtils.remove(jsvmPrefixPath, {
53       recursive: true,
54       ignoreAbsent: true,
55     });
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);
60   }
62   static beforeTestSync() {
63     awaitPromise(this.beforeTest());
64   }
66   // Dumps counters and moves the gcda files in the directory expected by codecoverage.py.
67   static async afterTest() {
68     if (!PerTestCoverageUtils.enabled) {
69       return;
70     }
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);
81   }
83   static afterTestSync() {
84     awaitPromise(this.afterTest());
85   }
88 PerTestCoverageUtils.enabled = !!gcovResultsPath;