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)
test: relative directories for tempDirectoy and reportDir
  • Loading branch information
mcknasty committed Jan 15, 2024
1 parent 4ae2a4d commit 7e61196
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 12 deletions.
21 changes: 13 additions & 8 deletions lib/parse-args.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ function buildYargs (withCommands = false) {
alias: ['o', 'report-dir'],
group: 'Reporting options',
describe: 'directory where coverage reports will be output to',
default: './coverage'
default: './coverage',
type: 'string'
})
.options('all', {
default: false,
Expand Down Expand Up @@ -126,7 +127,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: process.env.NODE_V8_COVERAGE,
type: 'string'
})
.option('clean', {
default: true,
Expand Down Expand Up @@ -160,6 +162,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 +200,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
8 changes: 4 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 | 3.52 | 12.5 | 6.52 | 3.52 |
All files | 3.51 | 12.5 | 6.52 | 3.51 |
c8 | 0 | 0 | 0 | 0 |
index.js | 0 | 0 | 0 | 0 | 1
c8/bin | 0 | 0 | 0 | 0 |
Expand All @@ -166,7 +166,7 @@ All files | 3.52 | 12.5 | 6.52 | 3.52
prettify.js | 0 | 0 | 0 | 0 | 1-2
sorter.js | 0 | 0 | 0 | 0 | 1-196
c8/lib | 0 | 0 | 0 | 0 |
parse-args.js | 0 | 0 | 0 | 0 | 1-224
parse-args.js | 0 | 0 | 0 | 0 | 1-229
report.js | 0 | 0 | 0 | 0 | 1-402
source-map-from-file.js | 0 | 0 | 0 | 0 | 1-100
c8/lib/commands | 0 | 0 | 0 | 0 |
Expand Down Expand Up @@ -521,7 +521,7 @@ hey
---------------------------------------|---------|----------|---------|---------|------------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
---------------------------------------|---------|----------|---------|---------|------------------------
All files | 3.52 | 12.5 | 6.52 | 3.52 |
All files | 3.51 | 12.5 | 6.52 | 3.51 |
c8 | 0 | 0 | 0 | 0 |
index.js | 0 | 0 | 0 | 0 | 1
c8/bin | 0 | 0 | 0 | 0 |
Expand All @@ -531,7 +531,7 @@ All files | 3.52 | 12.5 | 6.52 | 3.52
prettify.js | 0 | 0 | 0 | 0 | 1-2
sorter.js | 0 | 0 | 0 | 0 | 1-196
c8/lib | 0 | 0 | 0 | 0 |
parse-args.js | 0 | 0 | 0 | 0 | 1-224
parse-args.js | 0 | 0 | 0 | 0 | 1-229
report.js | 0 | 0 | 0 | 0 | 1-402
source-map-from-file.js | 0 | 0 | 0 | 0 | 1-100
c8/lib/commands | 0 | 0 | 0 | 0 |
Expand Down
30 changes: 30 additions & 0 deletions test/parse-args.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,6 +30,13 @@ 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', () => {
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 Down Expand Up @@ -63,6 +83,16 @@ describe('parse-args', () => {
argv.lines.should.be.equal(100)
argv.functions.should.be.equal(24)
})
it('should allow relative reports directories', () => {
const argsArray = ['node', 'c8', '--lines', '100', '--reports-dir', './coverage_']
const argv = buildYargs().parse(argsArray)
argv.reportsDir.should.be.equal('./coverage_')
})
it('should allow relative temporary directories', () => {
const argsArray = ['node', 'c8', '--lines', '100', '--temp-directory', './coverage/tmp_']
const argv = buildYargs().parse(argsArray)
argv.tempDirectory.should.be.equal('./coverage/tmp_')
})
})

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

0 comments on commit 7e61196

Please sign in to comment.