/
cov.js
143 lines (127 loc) · 3.73 KB
/
cov.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/* istanbul ignore next */
'use strict';
const debug = require('debug')('egg-bin');
const path = require('path');
const fs = require('fs/promises');
const Command = require('./test');
const { defaultExcludes } = require('../utils');
const EXCLUDES = Symbol('cov#excludes');
/* istanbul ignore next */
class CovCommand extends Command {
constructor(argv) {
super(argv);
this.usage = 'Usage: egg-bin cov';
this.options = {
x: {
description: 'istanbul coverage ignore, one or more fileset patterns',
type: 'string',
},
prerequire: {
description: 'prerequire files for coverage instrument',
type: 'boolean',
},
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: 'use c8 to report coverage, default to false',
type: 'boolean',
default: false,
},
};
// you can add ignore dirs here
this[EXCLUDES] = new Set(defaultExcludes);
}
get description() {
return 'Run test with coverage';
}
async run(context) {
const { cwd, argv, execArgv, env } = context;
if (argv.prerequire) {
env.EGG_BIN_PREREQUIRE = 'true';
}
delete argv.prerequire;
// ignore coverage
if (argv.x) {
if (Array.isArray(argv.x)) {
for (const exclude of argv.x) {
this.addExclude(exclude);
}
} else {
this.addExclude(argv.x);
}
argv.x = undefined;
}
const excludes = (process.env.COV_EXCLUDES && process.env.COV_EXCLUDES.split(',')) || [];
for (const exclude of excludes) {
this.addExclude(exclude);
}
const opt = {
cwd,
execArgv,
env: Object.assign({
NODE_ENV: 'test',
EGG_TYPESCRIPT: context.argv.typescript,
}, env),
};
// https://github.com/eggjs/egg/issues/3930
if (context.argv.typescript) {
opt.env.SPAWN_WRAP_SHIM_ROOT = path.join(cwd, 'node_modules');
}
const cli = require.resolve('c8/bin/c8.js');
const outputDir = path.join(cwd, 'node_modules/.c8_output');
await fs.rm(outputDir, { force: true, recursive: true });
const coverageDir = path.join(cwd, 'coverage');
await fs.rm(coverageDir, { force: true, recursive: true });
const covArgs = await this.getCovArgs(context);
if (!covArgs) return;
debug('run cov: %s %s', cli, covArgs.join(' '));
await this.helper.forkNode(cli, covArgs, opt);
}
/**
* add istanbul coverage ignore
* @param {String} exclude - glob pattern
*/
addExclude(exclude) {
this[EXCLUDES].add(exclude);
}
/**
* get coverage args
* @param {Object} context - { cwd, argv, ...}
* @return {Array} args for c8
* @protected
*/
async getCovArgs(context) {
let covArgs = [
// '--show-process-tree',
];
// typescript support
if (context.argv.typescript) {
covArgs.push('--extension', '.ts');
this.addExclude('typings/');
this.addExclude('**/*.d.ts');
}
// c8 args passthrough
const passthroughArgs = context.argv.c8;
context.argv['c8-report'] = undefined;
context.argv.c8 = undefined;
if (passthroughArgs) {
covArgs = covArgs.concat(passthroughArgs.split(' '));
}
for (const exclude of this[EXCLUDES]) {
covArgs.push('-x');
covArgs.push(exclude);
}
const testArgs = await this.formatTestArgs(context);
if (!testArgs) return;
covArgs.push(...this.getTestCommandAndArgs());
covArgs = covArgs.concat(testArgs);
return covArgs;
}
getTestCommandAndArgs() {
return [ require.resolve('mocha/bin/_mocha') ];
}
}
module.exports = CovCommand;