-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
146 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,100 @@ | ||
import { Args, Command, Flags } from '@oclif/core'; | ||
import path from 'node:path'; | ||
import fs from 'node:fs/promises'; | ||
import { Flags } from '@oclif/core'; | ||
import Test from './test.js'; | ||
import { importResolve } from '@eggjs/utils'; | ||
import { ForkNodeOptions } from '../baseCommand.js'; | ||
|
||
export default class Cov extends Command { | ||
static override args = { | ||
file: Args.string({ description: 'file to read' }), | ||
}; | ||
|
||
static override description = 'describe the command here'; | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
export default class Cov<T extends typeof Cov> extends Test<T> { | ||
static override description = 'Run the test with coverage'; | ||
|
||
static override examples = [ | ||
'<%= config.bin %> <%= command.id %>', | ||
'<%= config.bin %> <%= command.id %> test/index.test.ts', | ||
]; | ||
|
||
static override flags = { | ||
// flag with no value (-f, --force) | ||
force: Flags.boolean({ char: 'f' }), | ||
// flag with a value (-n, --name=VALUE) | ||
name: Flags.string({ char: 'n', description: 'name to print' }), | ||
...Test.flags, | ||
// will use on egg-mock https://github.com/eggjs/egg-mock/blob/84a64bd19d0569ec94664c898fb1b28367b95d60/index.js#L7 | ||
prerequire: Flags.boolean({ | ||
description: 'prerequire files for coverage instrument', | ||
}), | ||
exclude: Flags.string({ | ||
description: 'coverage ignore, one or more files patterns`', | ||
multiple: true, | ||
char: 'x', | ||
}), | ||
c8: Flags.string({ | ||
description: 'c8 instruments passthrough`', | ||
default: '--temp-directory node_modules/.c8_output -r text-summary -r json-summary -r json -r lcov -r cobertura', | ||
}), | ||
}; | ||
|
||
public async run(): Promise<void> { | ||
const { args, flags } = await this.parse(Cov); | ||
protected get defaultExcludes() { | ||
return [ | ||
'example/', | ||
'examples/', | ||
'mocks**/', | ||
'docs/', | ||
// https://github.com/JaKXz/test-exclude/blob/620a7be412d4fc2070d50f0f63e3228314066fc9/index.js#L73 | ||
'test/**', | ||
'test{,-*}.js', | ||
'**/*.test.js', | ||
'**/__tests__/**', | ||
'**/node_modules/**', | ||
'typings', | ||
'**/*.d.ts', | ||
]; | ||
} | ||
|
||
protected override async forkNode(modulePath: string, forkArgs: string[], options: ForkNodeOptions = {}) { | ||
const { flags } = this; | ||
if (flags.prerequire) { | ||
this.env.EGG_BIN_PREREQUIRE = 'true'; | ||
} | ||
|
||
const name = flags.name ?? 'world'; | ||
this.log(`hello ${name} from /Users/fengmk2/git/github.com/eggjs/bin/src/commands/cov.ts`); | ||
if (args.file && flags.force) { | ||
this.log(`you input --force and --file: ${args.file}`); | ||
// add c8 args | ||
// https://github.com/eggjs/egg/issues/3930 | ||
const c8Args = [ | ||
// '--show-process-tree', | ||
...flags.c8.split(' ').filter(a => a.trim()), | ||
]; | ||
if (flags.typescript) { | ||
this.env.SPAWN_WRAP_SHIM_ROOT = path.join(flags.base, 'node_modules'); | ||
c8Args.push('--extension'); | ||
c8Args.push('.ts'); | ||
} | ||
|
||
const excludes = new Set([ | ||
...process.env.COV_EXCLUDES?.split(',') ?? [], | ||
...this.defaultExcludes, | ||
...Array.from(flags.exclude ?? []), | ||
]); | ||
for (const exclude of excludes) { | ||
c8Args.push('-x'); | ||
c8Args.push(exclude); | ||
} | ||
const c8File = importResolve('c8/bin/c8.js'); | ||
const outputDir = path.join(flags.base, 'node_modules/.c8_output'); | ||
await fs.rm(outputDir, { force: true, recursive: true }); | ||
const coverageDir = path.join(flags.base, 'coverage'); | ||
await fs.rm(coverageDir, { force: true, recursive: true }); | ||
|
||
const execArgv = [ | ||
...this.globalExecArgv, | ||
...options.execArgv || [], | ||
]; | ||
this.globalExecArgv = []; | ||
|
||
// $ c8 node mocha | ||
await super.forkNode(c8File, [ | ||
...c8Args, | ||
process.execPath, | ||
...execArgv, | ||
modulePath, | ||
...forkArgs, | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
// import { Hook } from '@oclif/core'; | ||
|
||
// const hook: Hook<'init'> = async function(opts) { | ||
// process.stdout.write(`example hook running ${opts.id}\n`); | ||
// if (opts.argv.includes('-h')) { | ||
// this.config.runCommand('help'); | ||
// } | ||
// }; | ||
|
||
// export default hook; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters