提交 c0c5de44 编写于 作者: J Johannes Rieken

add --coverage option #23742

上级 396f25dd
......@@ -622,7 +622,8 @@ var AMDLoader;
errorback(err);
return;
}
var vmScriptSrc = _this._path.normalize(scriptSrc);
var normalizedScriptSrc = _this._path.normalize(scriptSrc);
var vmScriptSrc = normalizedScriptSrc;
// Make the script src friendly towards electron
if (AMDLoader.isElectronRenderer) {
var driveLetterMatch = vmScriptSrc.match(/^([a-z])\:(.*)/i);
......@@ -638,7 +639,7 @@ var AMDLoader;
else {
contents = prefix + data + suffix;
}
contents = nodeInstrumenter(contents, vmScriptSrc);
contents = nodeInstrumenter(contents, normalizedScriptSrc);
if (!opts.nodeCachedDataDir) {
_this._loadAndEvalScript(scriptSrc, vmScriptSrc, contents, { filename: vmScriptSrc }, recorder);
callback();
......
......@@ -11,6 +11,7 @@ const optimist = require('optimist')
.describe('grep', 'only run tests matching <pattern>').string('grep').alias('grep', 'g').string('g')
.describe('run', 'only run tests from <file>').string('run')
.describe('build', 'run with build output (out-build)').boolean('build')
.describe('coverage', 'generate coverage report').boolean('coverage')
.describe('debug', 'open dev tools, keep window open, reuse app data').string('debug');
const argv = optimist.argv;
......
......@@ -7,17 +7,24 @@
const { ipcRenderer } = require('electron');
const assert = require('assert');
const glob = require('glob');
const path = require('path');
const glob = require('glob');
const minimatch = require('minimatch');
const istanbul = require('istanbul');
const i_remap = require('remap-istanbul/lib/remap');
let _tests_glob = '**/test/**/*.test.js';
let loader;
let _out;
function initLoader(opts) {
let outdir = opts.build ? 'out-build' : 'out';
_out = path.join(__dirname, `../../${outdir}`);
// setup loader
loader = require(`${_out}/vs/loader`);
loader.require.config({
const loaderConfig = {
nodeRequire: require,
nodeMain: __filename,
catchError: true,
......@@ -27,6 +34,78 @@ function initLoader(opts) {
'lib': `../${outdir}/lib`,
'bootstrap': `../${outdir}/bootstrap`
}
};
// nodeInstrumenter when coverage is requested
if (opts.coverage) {
const instrumenter = new istanbul.Instrumenter();
loaderConfig.nodeInstrumenter = function (contents, source) {
return minimatch(source, _tests_glob)
? contents // don't instrument tests itself
: instrumenter.instrumentSync(contents, source);
};
}
loader.require.config(loaderConfig);
}
function createCoverageReport(opts) {
return new Promise(resolve => {
if (!opts.coverage) {
return resolve(undefined);
}
const exclude = /\b((winjs\.base)|(marked)|(raw\.marked)|(nls)|(css))\.js$/;
const remappedCoverage = i_remap(global.__coverage__, { exclude: exclude }).getFinalCoverage();
// The remapped coverage comes out with broken paths
function toUpperDriveLetter(str) {
if (/^[a-z]:/.test(str)) {
return str.charAt(0).toUpperCase() + str.substr(1);
}
return str;
};
function toLowerDriveLetter(str) {
if (/^[A-Z]:/.test(str)) {
return str.charAt(0).toLowerCase() + str.substr(1);
}
return str;
};
const REPO_PATH = toUpperDriveLetter(path.join(__dirname, '../..'));
const fixPath = function (brokenPath) {
const startIndex = brokenPath.indexOf(REPO_PATH);
if (startIndex === -1) {
return toLowerDriveLetter(brokenPath);
}
return toLowerDriveLetter(brokenPath.substr(startIndex));
};
const finalCoverage = Object.create(null);
for (const entryKey in remappedCoverage) {
const entry = remappedCoverage[entryKey];
entry.path = fixPath(entry.path);
finalCoverage[fixPath(entryKey)] = entry;
}
const collector = new istanbul.Collector();
collector.add(finalCoverage);
let coveragePath = path.join(path.dirname(__dirname), '../.build/coverage');
let reportTypes = [];
if (opts.run || opts.grep) {
// single file running
coveragePath += '-single';
reportTypes = ['lcovonly'];
} else {
reportTypes = ['json', 'lcov', 'html'];
}
const reporter = new istanbul.Reporter(null, coveragePath);
reporter.addAll(reportTypes);
reporter.write(collector, true, resolve);
});
}
......@@ -43,7 +122,7 @@ function loadTestModules(opts) {
}
return new Promise((resolve, reject) => {
glob('**/test/**/*.test.js', { cwd: _out }, (err, files) => {
glob(_tests_glob, { cwd: _out }, (err, files) => {
if (err) {
reject(err);
return;
......@@ -107,7 +186,9 @@ function runTests(opts) {
}
const runner = mocha.run(() => {
ipcRenderer.send('done');
createCoverageReport(opts).then(() => {
ipcRenderer.send('done');
});
});
runner.on('fail', function (test) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册