2018-06-21 16:40:20 +03:00
/ * T h i s S o u r c e C o d e F o r m i s s u b j e c t t o t h e t e r m s o f t h e M o z i l l a P u b l i c
* License , v . 2.0 . If a copy of the MPL was not distributed with this
* file , You can obtain one at http : //mozilla.org/MPL/2.0/. */
/* exported PerTestCoverageUtils */
"use strict" ;
var EXPORTED _SYMBOLS = [ "PerTestCoverageUtils" ] ;
2018-07-02 16:53:07 +03:00
ChromeUtils . import ( "resource://gre/modules/Services.jsm" ) ;
2018-06-21 16:40:20 +03:00
2018-06-28 11:35:45 +03:00
const env = Cc [ "@mozilla.org/process/environment;1" ] . getService ( Ci . nsIEnvironment ) ;
// This is the directory where gcov is emitting the gcda files.
2018-07-02 16:53:07 +03:00
const gcovPrefixPath = env . get ( "GCOV_PREFIX" ) ;
2018-06-28 11:35:45 +03:00
// This is the directory where codecoverage.py is expecting to see the gcda files.
2018-07-02 16:53:07 +03:00
const gcovResultsPath = env . get ( "GCOV_RESULTS_DIR" ) ;
2018-06-21 16:40:20 +03:00
2018-07-02 16:53:07 +03:00
const gcovPrefixDir = Cc [ "@mozilla.org/file/local;1" ] . createInstance ( Ci . nsIFile ) ;
if ( gcovPrefixPath ) {
gcovPrefixDir . initWithPath ( gcovPrefixPath ) ;
}
let gcovResultsDir = Cc [ "@mozilla.org/file/local;1" ] . createInstance ( Ci . nsIFile ) ;
if ( gcovResultsPath ) {
gcovResultsDir . initWithPath ( gcovResultsPath ) ;
}
const enabled = ! ! gcovResultsPath ;
2018-06-28 11:35:45 +03:00
function awaitPromise ( promise ) {
let ret ;
let complete = false ;
let error = null ;
promise . catch ( e => error = e ) . then ( v => {
ret = v ;
complete = true ;
} ) ;
Services . tm . spinEventLoopUntil ( ( ) => complete ) ;
if ( error ) {
throw new Error ( error ) ;
2018-06-21 16:40:20 +03:00
}
2018-06-28 11:35:45 +03:00
return ret ;
}
2018-06-21 16:40:20 +03:00
2018-07-02 16:53:07 +03:00
function removeDirectoryContents ( dir ) {
let entries = dir . directoryEntries ;
while ( entries . hasMoreElements ( ) ) {
entries . nextFile . remove ( true ) ;
}
}
function moveDirectoryContents ( src , dst ) {
let entries = src . directoryEntries ;
while ( entries . hasMoreElements ( ) ) {
entries . nextFile . moveTo ( dst , null ) ;
}
}
2018-06-28 11:35:45 +03:00
var PerTestCoverageUtils = class PerTestCoverageUtilsClass {
// Resets the counters to 0.
static async beforeTest ( ) {
if ( ! enabled ) {
return ;
2018-06-21 16:40:20 +03:00
}
2018-06-28 11:35:45 +03:00
2018-07-02 16:53:07 +03:00
// Reset the counters.
2018-06-28 11:35:45 +03:00
let codeCoverageService = Cc [ "@mozilla.org/tools/code-coverage;1" ] . getService ( Ci . nsICodeCoverage ) ;
await codeCoverageService . resetCounters ( ) ;
2018-07-02 16:53:07 +03:00
// Remove any gcda file 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).
removeDirectoryContents ( gcovPrefixDir ) ;
// Move gcda files from the GCOV_RESULTS_DIR directory, so we can accumulate the counters.
moveDirectoryContents ( gcovResultsDir , gcovPrefixDir ) ;
2018-06-21 16:40:20 +03:00
}
2018-06-28 11:35:45 +03:00
static beforeTestSync ( ) {
awaitPromise ( this . beforeTest ( ) ) ;
2018-06-21 16:40:20 +03:00
}
// Dumps counters and moves the gcda files in the directory expected by codecoverage.py.
2018-06-28 11:35:45 +03:00
static async afterTest ( ) {
if ( ! enabled ) {
return ;
}
2018-07-02 16:53:07 +03:00
// Dump the counters.
2018-06-28 11:35:45 +03:00
let codeCoverageService = Cc [ "@mozilla.org/tools/code-coverage;1" ] . getService ( Ci . nsICodeCoverage ) ;
await codeCoverageService . dumpCounters ( ) ;
2018-06-21 16:40:20 +03:00
2018-07-02 16:53:07 +03:00
// Move the gcda files in the GCOV_RESULTS_DIR, so that the execution from now to shutdown (or next test) is not counted.
moveDirectoryContents ( gcovPrefixDir , gcovResultsDir ) ;
2018-06-21 16:40:20 +03:00
}
2018-06-28 11:35:45 +03:00
static afterTestSync ( ) {
awaitPromise ( this . afterTest ( ) ) ;
}
} ;