Skip to content

Commit

Permalink
refactor: bumping parse args code coverage to 100%;
Browse files Browse the repository at this point in the history
test: adding two tests for temp-directory flag in command arguments;
  • Loading branch information
mcknasty committed Jan 17, 2023
1 parent ccbad85 commit 8978fe1
Show file tree
Hide file tree
Showing 4 changed files with 208 additions and 3,102 deletions.
23 changes: 9 additions & 14 deletions lib/parse-args.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@ const { readFileSync } = require('fs')
const Yargs = require('yargs/yargs')
const { applyExtends } = require('yargs/helpers')
const parser = require('yargs-parser')
const { resolve, extname } = require('path')
const { extname } = require('path')

function buildYargs (withCommands = false) {
let tempDir = './coverage/tmp'
if (typeof process.env.NODE_V8_COVERAGE === 'string') {
tempDir = process.env.NODE_V8_COVERAGE
}

const yargs = Yargs([])
.usage('$0 [opts] [script] [opts]')
.options('config', {
Expand Down Expand Up @@ -127,7 +132,8 @@ 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: tempDir,
type: 'string'
})
.option('clean', {
default: true,
Expand Down Expand Up @@ -155,12 +161,6 @@ function buildYargs (withCommands = false) {
})
.pkgConf('c8')
.demandCommand(1)
.check((argv) => {
if (!argv.tempDirectory) {
argv.tempDirectory = resolve(argv.reportsDir, 'tmp')
}
return true
})
.epilog('visit https://git.io/vHysA for list of available reporters')

// TODO: enable once yargs upgraded to v17: https://github.com/bcoe/c8/pull/332#discussion_r721636191
Expand Down Expand Up @@ -206,26 +206,21 @@ function loadConfigFile (path, ext) {
function getConfigFileNames () {
const files = ['nyc.config.js']
const prefixes = ['.c8rc', '.nycrc']
const exts = ['.yaml', '.yml', '.js', '.cjs', '.mjs', '.json', '']
const exts = ['.yaml', '.yml', '.js', '.cjs', '.json', '']
prefixes.forEach((prefix) => exts.forEach((ext) => files.push(`${prefix}${ext}`)))
return files
}

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]))
Expand Down
Loading

0 comments on commit 8978fe1

Please sign in to comment.