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. (#447)
test: Adding a test case for NODE_V8_COVERAGE and changing the describe block title. (#447)
test: Early exit branch test for hideInstrumenteeArgs function. (#447)
test: Adding test for relative paths for relative report directory cli option. (#447)
test: Adding test case for relative paths for temporary directory cli option. (#447)
test: Adding test for passing node.js arguments to c8. (#447)
  • Loading branch information
mcknasty committed Jul 30, 2023
1 parent a13584d commit 80fe2ac
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 17 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
16 changes: 12 additions & 4 deletions test/integration.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ hey
---------------------------------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
---------------------------------------|---------|----------|---------|---------|-------------------
All files | 1.81 | 12.24 | 6.38 | 1.81 |
All files | 1.53 | 11.53 | 6 | 1.53 |
c8 | 0 | 0 | 0 | 0 |
index.js | 0 | 0 | 0 | 0 | 1
c8/bin | 0 | 0 | 0 | 0 |
Expand All @@ -165,9 +165,13 @@ All files | 1.81 | 12.24 | 6.38 | 1.81
block-navigation.js | 0 | 0 | 0 | 0 | 1-87
prettify.js | 0 | 0 | 0 | 0 | 1-2
sorter.js | 0 | 0 | 0 | 0 | 1-196
c8/coverage/lcov-report | 0 | 0 | 0 | 0 |
block-navigation.js | 0 | 0 | 0 | 0 | 1-87
prettify.js | 0 | 0 | 0 | 0 | 1-2
sorter.js | 0 | 0 | 0 | 0 | 1-196
c8/lib | 0 | 0 | 0 | 0 |
is-cjs-esm-bridge.js | 0 | 0 | 0 | 0 | 1-10
parse-args.js | 0 | 0 | 0 | 0 | 1-224
parse-args.js | 0 | 0 | 0 | 0 | 1-247
report.js | 0 | 0 | 0 | 0 | 1-417
source-map-from-file.js | 0 | 0 | 0 | 0 | 1-100
c8/lib/commands | 0 | 0 | 0 | 0 |
Expand Down Expand Up @@ -522,7 +526,7 @@ hey
---------------------------------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
---------------------------------------|---------|----------|---------|---------|-------------------
All files | 1.81 | 12.24 | 6.38 | 1.81 |
All files | 1.53 | 11.53 | 6 | 1.53 |
c8 | 0 | 0 | 0 | 0 |
index.js | 0 | 0 | 0 | 0 | 1
c8/bin | 0 | 0 | 0 | 0 |
Expand All @@ -531,9 +535,13 @@ All files | 1.81 | 12.24 | 6.38 | 1.81
block-navigation.js | 0 | 0 | 0 | 0 | 1-87
prettify.js | 0 | 0 | 0 | 0 | 1-2
sorter.js | 0 | 0 | 0 | 0 | 1-196
c8/coverage/lcov-report | 0 | 0 | 0 | 0 |
block-navigation.js | 0 | 0 | 0 | 0 | 1-87
prettify.js | 0 | 0 | 0 | 0 | 1-2
sorter.js | 0 | 0 | 0 | 0 | 1-196
c8/lib | 0 | 0 | 0 | 0 |
is-cjs-esm-bridge.js | 0 | 0 | 0 | 0 | 1-10
parse-args.js | 0 | 0 | 0 | 0 | 1-224
parse-args.js | 0 | 0 | 0 | 0 | 1-247
report.js | 0 | 0 | 0 | 0 | 1-417
source-map-from-file.js | 0 | 0 | 0 | 0 | 1-100
c8/lib/commands | 0 | 0 | 0 | 0 |
Expand Down
42 changes: 38 additions & 4 deletions test/parse-args.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const {
hideInstrumenterArgs
} = require('../lib/parse-args')

const { join } = require('path')
const { join, resolve } = require('path')

describe('parse-args', () => {
describe('hideInstrumenteeArgs', () => {
Expand All @@ -15,6 +15,11 @@ describe('parse-args', () => {
const instrumenterArgs = hideInstrumenteeArgs()
instrumenterArgs.should.eql(['--foo=99', 'my-app'])
})
it('test early exit from function if no arguments are passed', () => {
process.argv = []
const instrumenterArgs = hideInstrumenteeArgs()
instrumenterArgs.length.should.eql(0)
})
})

describe('hideInstrumenterArgs', () => {
Expand All @@ -25,9 +30,16 @@ describe('parse-args', () => {
instrumenteeArgs.should.eql(['my-app', '--help'])
argv.tempDirectory.endsWith(join('coverage', 'tmp')).should.be.equal(true)
})
it('interprets first args after -- as Node.js execArgv', () => {
const expected = [process.execPath, '--expose-gc', 'index.js']
process.argv = ['node', 'c8', '--', '--expose-gc', 'index.js']
const argv = buildYargs().parse(hideInstrumenteeArgs())
const munged = hideInstrumenterArgs(argv)
munged.should.deep.equal(expected)
})
})

describe('with NODE_V8_COVERAGE already set', () => {
describe('NODE_V8_COVERAGE environment variable tests', () => {
it('should not override it', () => {
const NODE_V8_COVERAGE = process.env.NODE_V8_COVERAGE
process.env.NODE_V8_COVERAGE = './coverage/tmp_'
Expand All @@ -36,6 +48,14 @@ describe('parse-args', () => {
argv.tempDirectory.endsWith('/coverage/tmp_').should.be.equal(true)
process.env.NODE_V8_COVERAGE = NODE_V8_COVERAGE
})
it('should set it if undefined', () => {
const NODE_V8_COVERAGE = process.env.NODE_V8_COVERAGE
delete process.env.NODE_V8_COVERAGE
process.argv = ['node', 'c8', '--foo=99', 'my-app', '--help']
const argv = buildYargs().parse(hideInstrumenteeArgs())
argv.tempDirectory.endsWith('/coverage/tmp').should.be.equal(true)
process.env.NODE_V8_COVERAGE = NODE_V8_COVERAGE
})
})

describe('--config', () => {
Expand All @@ -44,14 +64,16 @@ describe('parse-args', () => {
argv.lines.should.be.equal(95)
})
it('should use config file specified in --config', () => {
const tmpDir = resolve(process.cwd(), 'foo')
const argv = buildYargs().parse(['node', 'c8', '--config', require.resolve('./fixtures/config/.c8rc.json')])
argv.lines.should.be.equal(101)
argv.tempDirectory.should.be.equal('./foo')
argv.tempDirectory.should.be.equal(tmpDir)
})
it('should have -c as an alias', () => {
const tmpDir = resolve(process.cwd(), 'foo')
const argv = buildYargs().parse(['node', 'c8', '-c', require.resolve('./fixtures/config/.c8rc.json')])
argv.lines.should.be.equal(101)
argv.tempDirectory.should.be.equal('./foo')
argv.tempDirectory.should.be.equal(tmpDir)
})
it('should respect options on the command line over config file', () => {
const argv = buildYargs().parse(['node', 'c8', '--lines', '100', '--config', require.resolve('./fixtures/config/.c8rc.json')])
Expand All @@ -63,6 +85,18 @@ describe('parse-args', () => {
argv.lines.should.be.equal(100)
argv.functions.should.be.equal(24)
})
it('should allow relative reports directories', () => {
const tmpDir = resolve(process.cwd(), 'coverage_')
const argsArray = ['node', 'c8', '--lines', '100', '--reports-dir', './coverage_']
const argv = buildYargs().parse(argsArray)
argv.reportsDir.should.be.equal(tmpDir)
})
it('should allow relative temporary directories', () => {
const tmpDir = resolve(process.cwd(), 'coverage/tmp_')
const argsArray = ['node', 'c8', '--lines', '100', '--temp-directory', './coverage/tmp_']
const argv = buildYargs().parse(argsArray)
argv.tempDirectory.should.be.equal(tmpDir)
})
})

describe('--merge-async', () => {
Expand Down

0 comments on commit 80fe2ac

Please sign in to comment.