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: support c8 report #172

Merged
merged 8 commits into from Mar 31, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion README.md
Expand Up @@ -161,7 +161,7 @@ TEST_TIMEOUT=2000 egg-bin test

### cov

Using [nyc] to run code coverage, it support all test params above.
Using [nyc] or [c8] to run code coverage, it support all test params above.

Coverage reporter will output text-summary, json and lcov.

Expand All @@ -177,6 +177,11 @@ You can pass any mocha argv.
> - when same key exists in `.nycrc` and cmd instruments, nyc prefers instrument.
> - egg-bin have some default instruments passed to nyc like `-r` and `--temp-directory`
> - `egg-bin cov --nyc="-r teamcity -r text"`
- `--c8-report` use c8 to report coverage not nyc, default to `false`.
- `--c8` c8 instruments passthrough. you can use this to overwrite egg-bin's default c8 instruments and add additional ones.
> - egg-bin have some default instruments passed to c8 like `-r` and `--temp-directory`
> - `egg-bin cov --c8="-r teamcity -r text" --c8-report=true`
- `--c8-report-only` no run test, only use c8 to report coverage if you have run test and set node env `NODE_V8_COVERAGE`, default to `false`.

- also support all test params above.

Expand Down
5 changes: 5 additions & 0 deletions bin/c8.js
@@ -0,0 +1,5 @@
#!/usr/bin/env node

'use strict';

require('c8/bin/c8');
75 changes: 55 additions & 20 deletions lib/cmd/cov.js
Expand Up @@ -30,6 +30,19 @@ class CovCommand extends Command {
type: 'string',
default: '--temp-directory ./node_modules/.nyc_output -r text-summary -r json-summary -r json -r lcov',
},
c8: {
description: 'c8 instruments passthrough',
type: 'string',
default: '--temp-directory ./node_modules/.c8_output -r text-summary -r json-summary -r json -r lcov',
},
'c8-report': {
description: 'run test && use c8 to report coverage',
type: 'boolean',
},
'c8-report-only': {
onlylovermb marked this conversation as resolved.
Show resolved Hide resolved
description: 'no run test, only use c8 to report coverage',
type: 'boolean',
},
};

// you can add ignore dirs here
Expand Down Expand Up @@ -68,12 +81,6 @@ class CovCommand extends Command {
this.addExclude(exclude);
}

const nycCli = require.resolve('nyc/bin/nyc.js');
mansonchor marked this conversation as resolved.
Show resolved Hide resolved
const coverageDir = path.join(cwd, 'coverage');
yield rimraf(coverageDir);
const outputDir = path.join(cwd, 'node_modules/.nyc_output');
yield rimraf(outputDir);

const opt = {
cwd,
execArgv,
Expand All @@ -87,12 +94,34 @@ class CovCommand extends Command {
if (context.argv.typescript) {
opt.env.SPAWN_WRAP_SHIM_ROOT = path.join(cwd, 'node_modules');
}

// save coverage-xxxx.json to $PWD/coverage
const covArgs = yield this.getCovArgs(context);
if (!covArgs) return;
debug('covArgs: %j', covArgs);
yield this.helper.forkNode(nycCli, covArgs, opt);
if (argv['c8-report-only']) {
const c8Cli = require.resolve('c8/bin/c8.js');
const covArgs = yield this.getCovArgs(context);
covArgs.unshift('report');
yield this.helper.forkNode(c8Cli, covArgs, opt);
} else if (argv['c8-report']) {
// save coverage-xxxx.json to $PWD/coverage
const c8Cli = require.resolve('c8/bin/c8.js');
const coverageDir = path.join(cwd, 'coverage');
yield rimraf(coverageDir);
const outputDir = path.join(cwd, 'node_modules/.nyc_output');
yield rimraf(outputDir);
const covArgs = yield this.getCovArgs(context);
if (!covArgs) return;
debug('covArgs: %j', covArgs);
yield this.helper.forkNode(c8Cli, covArgs, opt);
} else {
const nycCli = require.resolve('nyc/bin/nyc.js');
const coverageDir = path.join(cwd, 'coverage');
yield rimraf(coverageDir);
const outputDir = path.join(cwd, 'node_modules/.nyc_output');
yield rimraf(outputDir);
// save coverage-xxxx.json to $PWD/coverage
const covArgs = yield this.getCovArgs(context);
if (!covArgs) return;
debug('covArgs: %j', covArgs);
yield this.helper.forkNode(nycCli, covArgs, opt);
}
}

/**
Expand All @@ -102,7 +131,6 @@ class CovCommand extends Command {
addExclude(exclude) {
this[EXCLUDES].add(exclude);
}

/**
* get coverage args
* @param {Object} context - { cwd, argv, ...}
Expand All @@ -121,21 +149,28 @@ class CovCommand extends Command {
this.addExclude('**/*.d.ts');
}

// nyc args passthrough
const nycArgs = context.argv.nyc;
// nyc or c8 args passthrough
let passthroughArgs = context.argv.nyc;
if (context.argv['c8-report'] || context.argv['c8-report-only']) {
passthroughArgs = context.argv.c8;
context.argv['c8-report'] = undefined;
}
context.argv.nyc = undefined;
if (nycArgs) {
covArgs = covArgs.concat(nycArgs.split(' '));
context.argv.c8 = undefined;
if (passthroughArgs) {
covArgs = covArgs.concat(passthroughArgs.split(' '));
}

for (const exclude of this[EXCLUDES]) {
covArgs.push('-x');
covArgs.push(exclude);
}
covArgs.push(require.resolve('mocha/bin/_mocha'));
const testArgs = yield this.formatTestArgs(context);
if (!testArgs) return;
covArgs = covArgs.concat(testArgs);
if (!context.argv['c8-report-only']) {
covArgs.push(require.resolve('mocha/bin/_mocha'));
covArgs = covArgs.concat(testArgs);
}
context.argv['c8-report-only'] = undefined;
return covArgs;
}
}
Expand Down
4 changes: 3 additions & 1 deletion package.json
Expand Up @@ -6,7 +6,8 @@
"bin": {
"egg-bin": "bin/egg-bin.js",
"mocha": "bin/mocha.js",
"ets": "bin/ets.js"
"ets": "bin/ets.js",
"c8": "bin/c8.js"
},
"dependencies": {
"chalk": "^4.1.1",
Expand All @@ -25,6 +26,7 @@
"mocha": "^6.0.2",
"mz-modules": "^2.1.0",
"nyc": "^13.3.0",
"c8": "^7.11.0",
"power-assert": "^1.6.1",
"semver": "^7.3.5",
"source-map-support": "^0.5.19",
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/test-files-c8-report-only/app/assets/index.js
@@ -0,0 +1,3 @@
'use strict';

module.exports = '';
@@ -0,0 +1,3 @@
'use strict';

module.exports = '';
3 changes: 3 additions & 0 deletions test/fixtures/test-files-c8-report-only/app/public/bar.js
@@ -0,0 +1,3 @@
'use strict';

module.exports = '';
@@ -0,0 +1,3 @@
'use strict';

module.exports = '';
@@ -0,0 +1,3 @@
'use strict';

module.exports = '';
@@ -0,0 +1,3 @@
'use strict';

module.exports = '';
3 changes: 3 additions & 0 deletions test/fixtures/test-files-c8-report-only/config/config.prod.js
@@ -0,0 +1,3 @@
'use strict';

module.exports = '';
3 changes: 3 additions & 0 deletions test/fixtures/test-files-c8-report-only/config/plugin.js
@@ -0,0 +1,3 @@
'use strict';

module.exports = '';
3 changes: 3 additions & 0 deletions test/fixtures/test-files-c8-report-only/config/proxy.js
@@ -0,0 +1,3 @@
'use strict';

module.exports = '';
3 changes: 3 additions & 0 deletions test/fixtures/test-files-c8-report-only/docs/home.js
@@ -0,0 +1,3 @@
'use strict';

module.exports = '';
3 changes: 3 additions & 0 deletions test/fixtures/test-files-c8-report-only/example/foo.js
@@ -0,0 +1,3 @@
'use strict';

module.exports = '';
3 changes: 3 additions & 0 deletions test/fixtures/test-files-c8-report-only/examples/foo.js
@@ -0,0 +1,3 @@
'use strict';

module.exports = '';
3 changes: 3 additions & 0 deletions test/fixtures/test-files-c8-report-only/ignore/a.js
@@ -0,0 +1,3 @@
'use strict';

module.exports = '';
8 changes: 8 additions & 0 deletions test/fixtures/test-files-c8-report-only/lib/a.js
@@ -0,0 +1,8 @@
'use strict';

module.exports = condition => {
if (condition) {
return 'a';
}
return 'b';
};
3 changes: 3 additions & 0 deletions test/fixtures/test-files-c8-report-only/mocks/foo.js
@@ -0,0 +1,3 @@
'use strict';

module.exports = '';
3 changes: 3 additions & 0 deletions test/fixtures/test-files-c8-report-only/mocks_data/foo/foo.js
@@ -0,0 +1,3 @@
'use strict';

module.exports = '';
6 changes: 6 additions & 0 deletions test/fixtures/test-files-c8-report-only/package.json
@@ -0,0 +1,6 @@
{
"name": "test-files",
"files": [
"lib"
]
}
9 changes: 9 additions & 0 deletions test/fixtures/test-files-c8-report-only/test/a.js
@@ -0,0 +1,9 @@
'use strict';

const a = require('../lib/a');

describe('a.js', () => {
it('should success', () => {
a(true);
});
});
15 changes: 15 additions & 0 deletions test/fixtures/test-files-c8-report-only/test/a.test.js
@@ -0,0 +1,15 @@
'use strict';

const fs = require('fs');
const a = require('../lib/a');

describe('a.test.js', () => {
it('should success', () => {
a(true);
});

it('should show tmp', () => {
const tmpdir = process.env.TMPDIR;
console.log(tmpdir, fs.existsSync(tmpdir));
});
});
5 changes: 5 additions & 0 deletions test/fixtures/test-files-c8-report-only/test/b/b.test.js
@@ -0,0 +1,5 @@
'use strict';

describe('b/b.test.js', () => {
it('should success', () => {});
});
7 changes: 7 additions & 0 deletions test/fixtures/test-files-c8-report-only/test/fail.js
@@ -0,0 +1,7 @@
'use strict';

describe('fail.js', () => {
it('should fail', () => {
throw new Error('fail.js throw');
});
});
10 changes: 10 additions & 0 deletions test/fixtures/test-files-c8-report-only/test/ignore.test.js
@@ -0,0 +1,10 @@
'use strict';

const assert = require('assert');
const a = require('../ignore/a');

describe('ignore.test.js', () => {
it('should success', () => {
assert(a === '');
});
});
@@ -0,0 +1,7 @@
'use strict';

describe('no-timeouts.test.js', () => {
it('should success', function() {
console.log(`timeout: ${this.timeout()}`);
});
});
@@ -0,0 +1,9 @@
'use strict';

const assert = require('power-assert');

describe('power-assert-fail.js', () => {
it('should fail', () => {
assert(1 === 2);
});
});
3 changes: 3 additions & 0 deletions test/fixtures/test-files-c8/app/assets/index.js
@@ -0,0 +1,3 @@
'use strict';

module.exports = '';
3 changes: 3 additions & 0 deletions test/fixtures/test-files-c8/app/assets/subdir/home.js
@@ -0,0 +1,3 @@
'use strict';

module.exports = '';
3 changes: 3 additions & 0 deletions test/fixtures/test-files-c8/app/public/bar.js
@@ -0,0 +1,3 @@
'use strict';

module.exports = '';
3 changes: 3 additions & 0 deletions test/fixtures/test-files-c8/app/public/subdir/home.js
@@ -0,0 +1,3 @@
'use strict';

module.exports = '';
3 changes: 3 additions & 0 deletions test/fixtures/test-files-c8/app/view/subdir/home.js
@@ -0,0 +1,3 @@
'use strict';

module.exports = '';
3 changes: 3 additions & 0 deletions test/fixtures/test-files-c8/config/config.default.js
@@ -0,0 +1,3 @@
'use strict';

module.exports = '';
3 changes: 3 additions & 0 deletions test/fixtures/test-files-c8/config/config.prod.js
@@ -0,0 +1,3 @@
'use strict';

module.exports = '';
3 changes: 3 additions & 0 deletions test/fixtures/test-files-c8/config/plugin.js
@@ -0,0 +1,3 @@
'use strict';

module.exports = '';
3 changes: 3 additions & 0 deletions test/fixtures/test-files-c8/config/proxy.js
@@ -0,0 +1,3 @@
'use strict';

module.exports = '';
3 changes: 3 additions & 0 deletions test/fixtures/test-files-c8/docs/home.js
@@ -0,0 +1,3 @@
'use strict';

module.exports = '';
3 changes: 3 additions & 0 deletions test/fixtures/test-files-c8/example/foo.js
@@ -0,0 +1,3 @@
'use strict';

module.exports = '';
3 changes: 3 additions & 0 deletions test/fixtures/test-files-c8/examples/foo.js
@@ -0,0 +1,3 @@
'use strict';

module.exports = '';
3 changes: 3 additions & 0 deletions test/fixtures/test-files-c8/ignore/a.js
@@ -0,0 +1,3 @@
'use strict';

module.exports = '';
8 changes: 8 additions & 0 deletions test/fixtures/test-files-c8/lib/a.js
@@ -0,0 +1,8 @@
'use strict';

module.exports = condition => {
if (condition) {
return 'a';
}
return 'b';
};
3 changes: 3 additions & 0 deletions test/fixtures/test-files-c8/mocks/foo.js
@@ -0,0 +1,3 @@
'use strict';

module.exports = '';
3 changes: 3 additions & 0 deletions test/fixtures/test-files-c8/mocks_data/foo/foo.js
@@ -0,0 +1,3 @@
'use strict';

module.exports = '';
6 changes: 6 additions & 0 deletions test/fixtures/test-files-c8/package.json
@@ -0,0 +1,6 @@
{
"name": "test-files",
"files": [
"lib"
]
}
9 changes: 9 additions & 0 deletions test/fixtures/test-files-c8/test/a.js
@@ -0,0 +1,9 @@
'use strict';

const a = require('../lib/a');

describe('a.js', () => {
it('should success', () => {
a(true);
});
});