Skip to content

Commit

Permalink
Increase code quality by increasing code coverage: lib/parse-args.js
Browse files Browse the repository at this point in the history
refactor: bumping code coverage to 100% for lib/parse-args.js. (bcoe#447)
test: Adding a test case for NODE_V8_COVERAGE and changing the describe block title. (bcoe#447)
test: Early exit branch test for hideInstrumenteeArgs function. (bcoe#447)
test: Adding test for relative paths for relative report directory cli option. (bcoe#447)
test: Adding test case for relative paths for temporary directory cli option. (bcoe#447)
test: Adding test for passing node.js arguments to c8. (bcoe#447)
  • Loading branch information
mcknasty committed Jul 29, 2023
1 parent a13584d commit 909e209
Show file tree
Hide file tree
Showing 4 changed files with 988 additions and 641 deletions.
41 changes: 32 additions & 9 deletions lib/parse-args.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,26 @@ 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 cwd = process.cwd()

const reportsDirCoerce = (opt) => {
defaultReportDir = opt = (isAbsolute(opt) === false) ? resolve(cwd, opt) : opt
return opt
}

const tempDirCoerce = (opt) => {
if (typeof opt !== 'undefined' && isAbsolute(opt) === false) {
process.env.NODE_V8_COVERAGE = opt = resolve(cwd, opt)
} else if (typeof process.env.NODE_V8_COVERAGE !== 'string') {
process.env.NODE_V8_COVERAGE = opt = resolve(cwd, defaultReportDir, 'tmp')
}
return opt
}

const yargs = Yargs([])
.usage('$0 [opts] [script] [opts]')
.options('config', {
Expand All @@ -30,7 +47,8 @@ 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: reportsDirCoerce
})
.options('all', {
default: false,
Expand Down Expand Up @@ -126,7 +144,9 @@ 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: tempDirCoerce
})
.option('clean', {
default: true,
Expand Down Expand Up @@ -160,6 +180,7 @@ function buildYargs (withCommands = false) {
})
.pkgConf('c8')
.demandCommand(1)
// Todo: These next 6 lines are not getting covered
.check((argv) => {
if (!argv.tempDirectory) {
argv.tempDirectory = resolve(argv.reportsDir, 'tmp')
Expand Down Expand Up @@ -197,22 +218,24 @@ function buildYargs (withCommands = false) {
function hideInstrumenterArgs (yargv) {
let argv = process.argv.slice(1)
argv = argv.slice(argv.indexOf(yargv._[0]))

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 909e209

Please sign in to comment.