Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add temporary istambul report file #70

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 18 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ module.exports = function(config) {
// base output directory. If you include %browser% in the path it will be replaced with the karma browser name
dir: path.join(__dirname, 'coverage'),

// temporary output directory. Directory to output raw coverage information to
tempDirectory: path.join(__dirname, '.nyc_output'),

// Combines coverage information from multiple browsers into one report rather than outputting a report
// for each browser.
combineBrowserReports: true,
Expand Down Expand Up @@ -97,24 +100,24 @@ module.exports = function(config) {

### List of reporters and options

* [clover](https://github.com/istanbuljs/istanbuljs/blob/aae256fb8b9a3d19414dcf069c592e88712c32c6/packages/istanbul-reports/lib/clover/index.js#L8-L9)
* [cobertura](https://github.com/istanbuljs/istanbuljs/blob/aae256fb8b9a3d19414dcf069c592e88712c32c6/packages/istanbul-reports/lib/cobertura/index.js#L9-L10)
* [html](https://github.com/istanbuljs/istanbuljs/blob/aae256fb8b9a3d19414dcf069c592e88712c32c6/packages/istanbul-reports/lib/html/index.js#L135-L137)
* [json-summary](https://github.com/istanbuljs/istanbuljs/blob/aae256fb8b9a3d19414dcf069c592e88712c32c6/packages/istanbul-reports/lib/json-summary/index.js#L8)
* [json](https://github.com/istanbuljs/istanbuljs/blob/aae256fb8b9a3d19414dcf069c592e88712c32c6/packages/istanbul-reports/lib/json/index.js#L8)
* lcov
* [lcovonly](https://github.com/istanbuljs/istanbuljs/blob/aae256fb8b9a3d19414dcf069c592e88712c32c6/packages/istanbul-reports/lib/lcovonly/index.js#L8)
* none
* [teamcity](https://github.com/istanbuljs/istanbuljs/blob/aae256fb8b9a3d19414dcf069c592e88712c32c6/packages/istanbul-reports/lib/teamcity/index.js#L9-L10)
* [text-lcov](https://github.com/istanbuljs/istanbuljs/blob/aae256fb8b9a3d19414dcf069c592e88712c32c6/packages/istanbul-reports/lib/text-lcov/index.js#L9)
* [text-summary](https://github.com/istanbuljs/istanbuljs/blob/aae256fb8b9a3d19414dcf069c592e88712c32c6/packages/istanbul-reports/lib/text-summary/index.js#L9)
* [text](https://github.com/istanbuljs/istanbuljs/blob/aae256fb8b9a3d19414dcf069c592e88712c32c6/packages/istanbul-reports/lib/text/index.js#L159-L160)
- [clover](https://github.com/istanbuljs/istanbuljs/blob/aae256fb8b9a3d19414dcf069c592e88712c32c6/packages/istanbul-reports/lib/clover/index.js#L8-L9)
- [cobertura](https://github.com/istanbuljs/istanbuljs/blob/aae256fb8b9a3d19414dcf069c592e88712c32c6/packages/istanbul-reports/lib/cobertura/index.js#L9-L10)
- [html](https://github.com/istanbuljs/istanbuljs/blob/aae256fb8b9a3d19414dcf069c592e88712c32c6/packages/istanbul-reports/lib/html/index.js#L135-L137)
- [json-summary](https://github.com/istanbuljs/istanbuljs/blob/aae256fb8b9a3d19414dcf069c592e88712c32c6/packages/istanbul-reports/lib/json-summary/index.js#L8)
- [json](https://github.com/istanbuljs/istanbuljs/blob/aae256fb8b9a3d19414dcf069c592e88712c32c6/packages/istanbul-reports/lib/json/index.js#L8)
- lcov
- [lcovonly](https://github.com/istanbuljs/istanbuljs/blob/aae256fb8b9a3d19414dcf069c592e88712c32c6/packages/istanbul-reports/lib/lcovonly/index.js#L8)
- none
- [teamcity](https://github.com/istanbuljs/istanbuljs/blob/aae256fb8b9a3d19414dcf069c592e88712c32c6/packages/istanbul-reports/lib/teamcity/index.js#L9-L10)
- [text-lcov](https://github.com/istanbuljs/istanbuljs/blob/aae256fb8b9a3d19414dcf069c592e88712c32c6/packages/istanbul-reports/lib/text-lcov/index.js#L9)
- [text-summary](https://github.com/istanbuljs/istanbuljs/blob/aae256fb8b9a3d19414dcf069c592e88712c32c6/packages/istanbul-reports/lib/text-summary/index.js#L9)
- [text](https://github.com/istanbuljs/istanbuljs/blob/aae256fb8b9a3d19414dcf069c592e88712c32c6/packages/istanbul-reports/lib/text/index.js#L159-L160)

## Credits

* [Original karma-coverage source](https://github.com/karma-runner/karma-coverage/blob/master/lib/reporter.js)
* [Example of using the new reporter API](https://github.com/facebook/jest/blob/master/scripts/mapCoverage.js)
* [Karma remap istanbul](https://github.com/marcules/karma-remap-istanbul)
- [Original karma-coverage source](https://github.com/karma-runner/karma-coverage/blob/master/lib/reporter.js)
- [Example of using the new reporter API](https://github.com/facebook/jest/blob/master/scripts/mapCoverage.js)
- [Karma remap istanbul](https://github.com/marcules/karma-remap-istanbul)

## License

Expand Down
52 changes: 38 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
},
"homepage": "https://github.com/mattlewis92/karma-coverage-istanbul-reporter#readme",
"dependencies": {
"fs-extra": "^8.0.1",
"istanbul-api": "^2.1.1",
"minimatch": "^3.0.4"
},
Expand All @@ -46,6 +47,7 @@
"@types/chai": "^4.1.7",
"@types/mocha": "^5.2.6",
"chai": "^4.2.0",
"chai-spies": "^1.0.0",
"codecov-lite": "^0.1.3",
"commitizen": "^3.0.5",
"husky": "^1.3.1",
Expand Down
33 changes: 30 additions & 3 deletions src/reporter.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const path = require('path');
const fs = require('fs-extra');
const istanbul = require('istanbul-api');
const util = require('./util');

Expand Down Expand Up @@ -86,7 +88,6 @@ function CoverageIstanbulReporter(baseReporterDecorator, logger, config) {
reporting: Object.assign({}, coverageConfig, reportConfigOverride)
});
const reportTypes = reportConfig.reporting.config.reports;

const reporter = istanbul.createReporter(reportConfig);
reporter.addAll(reportTypes);

Expand Down Expand Up @@ -165,6 +166,8 @@ function CoverageIstanbulReporter(baseReporterDecorator, logger, config) {
);
});

const temporaryFileDateResult = {};

remappedCoverageMap.files().forEach(file => {
const fileThresholds = Object.assign(
{},
Expand All @@ -176,8 +179,12 @@ function CoverageIstanbulReporter(baseReporterDecorator, logger, config) {
)
);
delete fileThresholds.overrides;
const fileSummary = remappedCoverageMap.fileCoverageFor(file).toSummary()
.data;
const coverageFile = remappedCoverageMap.fileCoverageFor(file);

const fileSummary = coverageFile.toSummary().data;

temporaryFileDateResult[coverageFile.data.path] = coverageFile.data;

const failedFileTypes = checkThresholds(fileThresholds, fileSummary);

failedFileTypes.forEach(type => {
Expand All @@ -197,6 +204,26 @@ function CoverageIstanbulReporter(baseReporterDecorator, logger, config) {
});
});

if (
Object.keys(temporaryFileDateResult).length > 0 &&
coverageConfig.tempDirectory
) {
const tempFolder = coverageConfig.tempDirectory;

if (fs.existsSync(tempFolder) === true) {
fs.removeSync(tempFolder);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doing this will wipe the temp directory on every run, so from what I can tell only one coverage file will ever be written there and when you run nyc merge .nyc_output coverage.json it won't include output files from all runs.

I guess we also want an option to control whether to delete the temp directory, so you could set it to delete it only on the first repo that runs and then subsequent repos in your mono repo wouldn't delete it. This is kind of how the nyc example works, where cover:integration uses --no-clean to not wipe the output directory before running.

WDYT?

}

fs.mkdirSync(tempFolder, { recursive: true });

fs.writeFileSync(
path.join(tempFolder, `${Date.now()}.json`),
JSON.stringify(temporaryFileDateResult)
);
} else {
log.warn('No coverage data found');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will always log when you're not using the tempDirectory option. Can you also make it debug level as well?

}

if (thresholdCheckFailed && results && !thresholds.emitWarning) {
results.exitCode = 1;
}
Expand Down
59 changes: 58 additions & 1 deletion test/reporter.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/* eslint-disable max-nested-callbacks */
const fs = require('fs');
const path = require('path');
const chai = require('chai');
const fs = require('fs-extra');
const spies = require('chai-spies');
const karma = require('karma');
const rimraf = require('rimraf');
const karmaCoverageIstanbulReporter = require('../src/reporter');
Expand All @@ -13,6 +14,8 @@ const OUTPUT_FILE = path.join(OUTPUT_PATH, 'coverage-summary.json');
const OUTPUT_DETAILS_FILE = path.join(OUTPUT_PATH, 'coverage-final.json');
const fileReadTimeout = 300;

chai.use(spies);

function createServer(config) {
config = config || {};
return new karma.Server(
Expand Down Expand Up @@ -78,6 +81,60 @@ describe('karma-coverage-istanbul-reporter', () => {
});
});

it('should generate a temporary istanbul coverage file', done => {
const tempDirectory = path.join(__dirname, 'fixtures', '.nyc_output');
chai.spy.on(fs, ['writeFileSync']);
const server = createServer({
coverageIstanbulReporter: {
tempDirectory,
dir: path.join(__dirname, 'fixtures', 'outputs')
}
});
server.start();
server.on('run_complete', () => {
setTimeout(() => {
// Hacky workaround to make sure the file has been written
const tempDirFiles = fs.readdirSync(tempDirectory);
const temporaryCoverageReport = JSON.parse(
fs.readFileSync(path.join(tempDirectory, tempDirFiles[0]))
);
expect(tempDirFiles).length(1);
expect(temporaryCoverageReport).to.be.a('object');
expect(fs.writeFileSync).to.have.been.called();
chai.spy.restore(fs, ['writeFileSync']);
done();
}, fileReadTimeout);
});
});

it('should recreate the temp directory', done => {
const tempDirectory = path.join(__dirname, 'fixtures', '.nyc_output');
fs.removeSync(tempDirectory);
fs.mkdirSync(tempDirectory);

chai.spy.on(fs, ['existsSync', 'removeSync', 'mkdirSync']);

const server = createServer({
coverageIstanbulReporter: {
tempDirectory,
dir: path.join(__dirname, 'fixtures', 'outputs')
}
});
server.start();
server.on('run_complete', () => {
setTimeout(() => {
// Hacky workaround to make sure the file has been written
expect(fs.existsSync).to.have.be.called.with(tempDirectory);
expect(fs.removeSync).to.have.be.called.with(tempDirectory);
expect(fs.mkdirSync).to.have.be.called.with(tempDirectory, {
recursive: true
});
chai.spy.restore(fs, ['existsSync', 'removeSync', 'mkdirSync']);
done();
}, fileReadTimeout);
});
});

it('should fix webpack loader source paths', done => {
const server = createServer({
coverageIstanbulReporter: {
Expand Down