Skip to content

Commit

Permalink
reworked test output for phabricator/arcanist, it now prints the json…
Browse files Browse the repository at this point in the history
… to a file

Reviewed By: jeffmo

Differential Revision: D2743491

fb-gh-sync-id: 8132991431cb34c48a9f0b48acdd1310c0d0e0a1
  • Loading branch information
Alexander Juarez authored and facebook-github-bot-5 committed Dec 11, 2015
1 parent 02d8803 commit 22588a8
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 92 deletions.
22 changes: 18 additions & 4 deletions src/IstanbulTestReporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,18 @@
const DefaultTestReporter = require('./DefaultTestReporter');
const istanbul = require('istanbul');
const collector = new istanbul.Collector();
const testCollectors = Object.create(null);
const reporter = new istanbul.Reporter();

class IstanbulTestReporter extends DefaultTestReporter {
onTestResult(config, testResult, aggregatedResults) {
super.onTestResult(config, testResult, aggregatedResults);

if (config.collectCoverage && testResult.coverage) {
collector.add(testResult.coverage);
if (!testCollectors[testResult.testFilePath]) {
testCollectors[testResult.testFilePath] = new istanbul.Collector();
}
testCollectors[testResult.testFilePath].add(testResult.coverage);
}
}

Expand All @@ -26,11 +30,21 @@ class IstanbulTestReporter extends DefaultTestReporter {

if (config.collectCoverage) {
reporter.addAll(config.coverageReporters);
reporter.write(collector, true, function() {
console.log('All reports generated');
});
reporter.write(collector, true, () => {});
}
}

static getReporter() {
return reporter;
}

static getCollector() {
return collector;
}

static getTestCollectors() {
return testCollectors;
}
}

module.exports = IstanbulTestReporter;
33 changes: 28 additions & 5 deletions src/lib/formatTestResults.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

const utils = require('./utils');

const formatResult = (testResult, codeCoverageFormatter) => {
const formatResult = (testResult, codeCoverageFormatter, reporter) => {
const output = {
name: testResult.testFilePath,
summary: '', // TODO
Expand All @@ -28,7 +28,7 @@ const formatResult = (testResult, codeCoverageFormatter) => {
output.status = allTestsPassed ? 'passed' : 'failed';
output.startTime = testResult.perfStats.start;
output.endTime = testResult.perfStats.end;
output.coverage = codeCoverageFormatter(testResult.coverage);
output.coverage = codeCoverageFormatter(testResult.coverage, reporter);

if (!allTestsPassed) {
output.message = utils.formatFailureMessage(testResult, {
Expand All @@ -41,13 +41,34 @@ const formatResult = (testResult, codeCoverageFormatter) => {
return output;
};

module.exports = (results, codeCoverageFormatter) => {
/**
* @callback codeCoverageFormatter
* @param {*} results
* @param {*} reporter - an instance of the testReporter
*/

/**
* Formats the test results.
* @param {*} results - a results hash determined by the reporter
* @param {codeCoverageFormatter} codeCoverageFormatter
* @param {*} reporter - an instance of the testReporter
* @returns {{success: *, startTime: (*|number|Number),
* numTotalTests: *,
* numTotalTestSuites: *,
* numRuntimeErrorTestSuites: *,
* numPassedTests: *,
* numFailedTests: *,
* testResults: (*|{}|Array),
* postSuiteHeaders: *}}
*/

function formatTestResults(results, codeCoverageFormatter, reporter) {
if (!codeCoverageFormatter) {
codeCoverageFormatter = coverage => coverage;
}

const testResults = results.testResults.map(
testResult => formatResult(testResult, codeCoverageFormatter)
testResult => formatResult(testResult, codeCoverageFormatter, reporter)
);

return {
Expand All @@ -61,4 +82,6 @@ module.exports = (results, codeCoverageFormatter) => {
testResults,
postSuiteHeaders: results.postSuiteHeaders,
};
};
}

module.exports = formatTestResults;
83 changes: 0 additions & 83 deletions src/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,86 +86,6 @@ function escapeStrForRegex(str) {
return str.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
}


/**
* Given the coverage info for a single file (as output by
* CoverageCollector.js), return an array whose entries are bools indicating
* whether anything on the line could have been covered and was, or null if the
* line wasn't measurable (like empty lines, declaration keywords, etc).
*
* For example, for the following coverage info:
*
* COVERED: var a = [];
* NO CODE:
* COVERED: for (var i = 0; i < a.length; i++)
* NOT COVERED: console.log('hai!');
*
* You'd get an array that looks like this:
*
* [true, null, true, false]
*/
function getLineCoverageFromCoverageInfo(coverageInfo) {
const coveredLines = {};
coverageInfo.coveredSpans.forEach(function(coveredSpan) {
const startLine = coveredSpan.start.line;
const endLine = coveredSpan.end.line;
for (let i = startLine - 1; i < endLine; i++) {
coveredLines[i] = true;
}
});

const uncoveredLines = {};
coverageInfo.uncoveredSpans.forEach(function(uncoveredSpan) {
const startLine = uncoveredSpan.start.line;
const endLine = uncoveredSpan.end.line;
for (let i = startLine - 1; i < endLine; i++) {
uncoveredLines[i] = true;
}
});

const sourceLines = coverageInfo.sourceText.trim().split('\n');

return sourceLines.map(function(line, lineIndex) {
if (uncoveredLines[lineIndex] === true) {
return false;
} else if (coveredLines[lineIndex] === true) {
return true;
} else {
return null;
}
});
}

/**
* Given the coverage info for a single file (as output by
* CoverageCollector.js), return the decimal percentage of lines in the file
* that had any coverage info.
*
* For example, for the following coverage info:
*
* COVERED: var a = [];
* NO CODE:
* COVERED: for (var i = 0; i < a.length; i++)
* NOT COVERED: console.log('hai');
*
* You'd get: 2/3 = 0.666666
*/
function getLinePercentCoverageFromCoverageInfo(coverageInfo) {
const lineCoverage = getLineCoverageFromCoverageInfo(coverageInfo);
let numMeasuredLines = 0;
const numCoveredLines = lineCoverage.reduce(function(counter, lineIsCovered) {
if (lineIsCovered !== null) {
numMeasuredLines++;
if (lineIsCovered === true) {
counter++;
}
}
return counter;
}, 0);

return numCoveredLines / numMeasuredLines;
}

function normalizeConfig(config) {
const newConfig = {};

Expand Down Expand Up @@ -449,9 +369,6 @@ const STACK_TRACE_LINE_IGNORE_RE = new RegExp([

exports.deepCopy = deepCopy;
exports.escapeStrForRegex = escapeStrForRegex;
exports.getLineCoverageFromCoverageInfo = getLineCoverageFromCoverageInfo;
exports.getLinePercentCoverageFromCoverageInfo =
getLinePercentCoverageFromCoverageInfo;
exports.loadConfigFromFile = loadConfigFromFile;
exports.loadConfigFromPackageJson = loadConfigFromPackageJson;
exports.normalizeConfig = normalizeConfig;
Expand Down

0 comments on commit 22588a8

Please sign in to comment.