Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Better error handling for main execution, reporting #1229

Merged
merged 1 commit into from Nov 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
48 changes: 26 additions & 22 deletions bin/nyc.js
Expand Up @@ -2,6 +2,7 @@
'use strict'

const configUtil = require('../lib/config-util')
const { cliWrapper, suppressEPIPE } = require('../lib/commands/helpers')
const foreground = require('foreground-child')
const resolveFrom = require('resolve-from')
const NYC = require('../index.js')
Expand Down Expand Up @@ -81,29 +82,32 @@ async function main () {
// a non-zero exit codes in either one leads to an overall non-zero exit code.
process.exitCode = 0
foreground(childArgs, async () => {
var mainChildExitCode = process.exitCode

await nyc.writeProcessIndex()

nyc.maybePurgeSourceMapCache()
if (argv.checkCoverage) {
await nyc.checkCoverage({
lines: argv.lines,
functions: argv.functions,
branches: argv.branches,
statements: argv.statements
}, argv['per-file'])
process.exitCode = process.exitCode || mainChildExitCode
}

if (!argv.silent) {
await nyc.report()
const mainChildExitCode = process.exitCode

try {
await nyc.writeProcessIndex()

nyc.maybePurgeSourceMapCache()
if (argv.checkCoverage) {
await nyc.checkCoverage({
lines: argv.lines,
functions: argv.functions,
branches: argv.branches,
statements: argv.statements
}, argv['per-file']).catch(suppressEPIPE)
process.exitCode = process.exitCode || mainChildExitCode
}

if (!argv.silent) {
await nyc.report().catch(suppressEPIPE)
}
} catch (error) {
/* istanbul ignore next */
process.exitCode = process.exitCode || mainChildExitCode || 1
/* istanbul ignore next */
console.error(error.message)
}
})
}

/* istanbul ignore next: the error branch should be unreachable */
main().catch(error => {
console.error(error.message)
process.exit(1)
})
cliWrapper(main)()
4 changes: 2 additions & 2 deletions lib/commands/check-coverage.js
@@ -1,7 +1,7 @@
'use strict'

const NYC = require('../../index.js')
const { cliWrapper, setupOptions } = require('./helpers.js')
const { cliWrapper, suppressEPIPE, setupOptions } = require('./helpers.js')

exports.command = 'check-coverage'

Expand All @@ -24,5 +24,5 @@ exports.handler = cliWrapper(async argv => {
functions: argv.functions,
branches: argv.branches,
statements: argv.statements
}, argv['per-file'])
}, argv['per-file']).catch(suppressEPIPE)
})
15 changes: 14 additions & 1 deletion lib/commands/helpers.js
Expand Up @@ -50,10 +50,23 @@ module.exports = {
}
})
},
/* istanbul ignore next: unsure how to test this */
suppressEPIPE (error) {
/* Prevent dumping error when `nyc npm t|head` causes stdout to
* be closed when reporting runs. */
if (error.code !== 'EPIPE') {
throw error
}
},
cliWrapper (execute) {
return argv => {
execute(argv).catch(error => {
console.error(error.message)
try {
console.error(error.message)
} catch (_) {
/* We need to run process.exit(1) even if stderr is destroyed */
}

process.exit(1)
})
}
Expand Down
6 changes: 3 additions & 3 deletions lib/commands/report.js
@@ -1,7 +1,7 @@
'use strict'

const NYC = require('../../index.js')
const { cliWrapper, setupOptions } = require('./helpers.js')
const { cliWrapper, suppressEPIPE, setupOptions } = require('./helpers.js')

exports.command = 'report'

Expand All @@ -18,13 +18,13 @@ exports.builder = function (yargs) {
exports.handler = cliWrapper(async argv => {
process.env.NYC_CWD = process.cwd()
var nyc = new NYC(argv)
await nyc.report()
await nyc.report().catch(suppressEPIPE)
if (argv.checkCoverage) {
await nyc.checkCoverage({
lines: argv.lines,
functions: argv.functions,
branches: argv.branches,
statements: argv.statements
}, argv['per-file'])
}, argv['per-file']).catch(suppressEPIPE)
}
})