Skip to content

Commit

Permalink
refactor: Increasing code coverage of lib/parse-args.js to 100%. (bco…
Browse files Browse the repository at this point in the history
…e#447)

test: Adding a test case for NODE_V8_COVERAGE and changing the describe block title. (bcoe#447)
test: Ealry exit branch test for hideInstrumenteeArgs function. (bcoe#447)
test: Adding test for relative paths for relative report directories. (bcoe#447)
  • Loading branch information
mcknasty committed Jan 29, 2023
1 parent 3646e6e commit 0eef52a
Show file tree
Hide file tree
Showing 5 changed files with 1,002 additions and 3,651 deletions.
44 changes: 35 additions & 9 deletions lib/parse-args.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ const { readFileSync } = require('fs')
const Yargs = require('yargs/yargs')
const { applyExtends } = require('yargs/helpers')
const parser = require('yargs-parser')
const { resolve } = require('path')
const { resolve, isAbsolute } = require('path')

function buildYargs (withCommands = false) {
let defaultReportDir = resolve(process.cwd(), 'coverage')

const yargs = Yargs([])
.usage('$0 [opts] [script] [opts]')
.options('config', {
Expand All @@ -30,7 +32,14 @@ function buildYargs (withCommands = false) {
alias: ['o', 'report-dir'],
group: 'Reporting options',
describe: 'directory where coverage reports will be output to',
default: './coverage'
default: defaultReportDir,
coerce: (opt) => {
if (isAbsolute(opt) === false) {
defaultReportDir = opt = resolve(process.cwd(), opt)
}

return opt
}
})
.options('all', {
default: false,
Expand Down Expand Up @@ -126,7 +135,14 @@ function buildYargs (withCommands = false) {
})
.option('temp-directory', {
describe: 'directory V8 coverage data is written to and read from',
default: process.env.NODE_V8_COVERAGE
default: process.env.NODE_V8_COVERAGE,
type: 'string',
coerce: (opt) => {
if (typeof process.env.NODE_V8_COVERAGE !== 'string') {
process.env.NODE_V8_COVERAGE = opt = resolve(process.cwd(), defaultReportDir, 'tmp')
}
return opt
}
})
.option('clean', {
default: true,
Expand Down Expand Up @@ -154,6 +170,10 @@ function buildYargs (withCommands = false) {
})
.pkgConf('c8')
.demandCommand(1)
// Todo: These next 6 lines might need to get removed
// This logic is covered with a default argument in
// the config options declaration in this file for yargs
/* c8 ignore next 6 */
.check((argv) => {
if (!argv.tempDirectory) {
argv.tempDirectory = resolve(argv.reportsDir, 'tmp')
Expand Down Expand Up @@ -191,22 +211,28 @@ function buildYargs (withCommands = false) {
function hideInstrumenterArgs (yargv) {
let argv = process.argv.slice(1)
argv = argv.slice(argv.indexOf(yargv._[0]))

// Todo: These next 3 lines might need to be removed.
// Not sure if there is a possible execution stack
// for this logic
/* c8 ignore next 3 */
if (argv[0][0] === '-') {
argv.unshift(process.execPath)
}

return argv
}

function hideInstrumenteeArgs () {
let argv = process.argv.slice(2)
const yargv = parser(argv)

if (!yargv._.length) return argv

// drop all the arguments after the bin being
// instrumented by c8.
argv = argv.slice(0, argv.indexOf(yargv._[0]))
argv.push(yargv._[0])
if (yargv._.length !== 0) {
// drop all the arguments after the bin being
// instrumented by c8.
argv = argv.slice(0, argv.indexOf(yargv._[0]))
argv.push(yargv._[0])
}

return argv
}
Expand Down
Loading

0 comments on commit 0eef52a

Please sign in to comment.