Skip to content

Commit

Permalink
feat: use non-zero exit code when errors exists
Browse files Browse the repository at this point in the history
  • Loading branch information
pimlie committed Aug 24, 2019
1 parent 7af25cc commit 79f582e
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 12 deletions.
2 changes: 1 addition & 1 deletion lib/formatters/stylish.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const table = require('text-table')
const { SourceCode } = require('eslint')
const Multiplexer = require('../..')

module.exports = function (results, options) {
module.exports = function (results, data, options) {
if (!options) {
({ results, options } = Multiplexer.runFromFormatter(results))
}
Expand Down
27 changes: 25 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,11 @@ class Multiplexer {
if (!isPiped && this.options.format !== 'json') {
const mergedResults = this.merge(results)
const formatter = getFormatter(this.options.format)
output = formatter(mergedResults, this.options)
output = formatter(mergedResults, {}, this.options)

if (!process.exitCode) {
process.exitCode = mergedResults.some(results => results.errorCount) ? 1 : 0
}
} else {
output = JSON.stringify(results)
}
Expand Down Expand Up @@ -162,8 +166,27 @@ class Multiplexer {
data += chunk
})

let closedOrExited
sp.on('close', () => {
resolve(data)
/* istanbul ignore next */
if (closedOrExited) {
resolve(data)
}
closedOrExited = true
})

sp.on('exit', (code) => {
if (code > 0) {
// if subcommand has non-zero exit code,
// we have too
process.exitCode = code
}

/* istanbul ignore next */
if (closedOrExited) {
resolve(data)
}
closedOrExited = true
})
})
}
Expand Down
45 changes: 36 additions & 9 deletions test/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ describe('cli', () => {
'--no-ignore', './test/fixtures'
])

expect(exitCode).toBe(0)
expect(exitCode).toBe(1)
expect(stderr).toBe('')
expect(stdout).toEqual(expect.stringContaining(`first${sep}index.js`))
expect(stdout).toEqual(expect.not.stringContaining('2x'))
Expand All @@ -62,7 +62,7 @@ describe('cli', () => {
'--no-ignore', './test/fixtures'
])

expect(exitCode).toBe(0)
expect(exitCode).toBe(1)
expect(stderr).toBe('')
expect(stdout).toEqual(expect.stringContaining('2x'))
expect(stdout).toEqual(expect.stringContaining('index.js'))
Expand All @@ -78,7 +78,7 @@ describe('cli', () => {
'--no-ignore', './test/fixtures'
])

expect(exitCode).toBe(0)
expect(exitCode).toBe(1)
expect(stderr).toBe('')
expect(stdout).toEqual(expect.stringContaining('2x'))
expect(stdout).toEqual(expect.stringContaining('index.js'))
Expand All @@ -94,7 +94,7 @@ describe('cli', () => {
'--no-ignore', './test/fixtures'
])

expect(exitCode).toBe(0)
expect(exitCode).toBe(1)
expect(stderr).toBe('')
expect(stdout).toEqual(expect.stringContaining('2x'))
expect(stdout).toEqual(expect.stringContaining('index'))
Expand All @@ -110,7 +110,7 @@ describe('cli', () => {
'--no-ignore', './test/fixtures'
])

expect(exitCode).toBe(0)
expect(exitCode).toBe(1)
expect(stderr).toBe('')
expect(stdout).toEqual(expect.stringContaining('2x'))
expect(stdout).toEqual(expect.stringContaining('1x'))
Expand All @@ -126,7 +126,7 @@ describe('cli', () => {
'--no-ignore', './test/fixtures'
])

expect(exitCode).toBe(0)
expect(exitCode).toBe(1)
expect(stderr).toBe('')
expect(stdout).toEqual(expect.stringContaining('2x'))
expect(stdout).toEqual(expect.not.stringContaining('1x'))
Expand All @@ -142,7 +142,7 @@ describe('cli', () => {
'--no-ignore', './test/fixtures'
])

expect(exitCode).toBe(0)
expect(exitCode).toBe(1)
expect(stderr).toBe('')
expect(stdout).toEqual(expect.stringContaining('0: function'))
})
Expand Down Expand Up @@ -184,7 +184,7 @@ describe('cli', () => {
})

test('multiple pipes', async () => {
const { stdout, stderr } = await new Promise((resolve) => {
const { stdout, stderr, exitCode } = await new Promise((resolve) => {
const eslint1 = spawn(process.execPath, [
'./bin/eslint-multiplexer',
'eslint',
Expand Down Expand Up @@ -218,11 +218,25 @@ describe('cli', () => {
stderr += chunk
})

let exitCode
let closedOrExited
multiplexer.on('close', () => {
resolve({ stdout, stderr })
if (closedOrExited) {
resolve({ stdout, stderr, exitCode })
}
closedOrExited = true
})

multiplexer.on('exit', (code, signal) => {
exitCode = code
if (closedOrExited) {
resolve({ stdout, stderr, exitCode })
}
closedOrExited = true
})
})

expect(exitCode).toBe(1)
expect(stderr).toBe('')
expect(stdout).toEqual(expect.stringContaining('2x'))
expect(stdout).toEqual(expect.stringContaining('1x'))
Expand Down Expand Up @@ -254,4 +268,17 @@ describe('cli', () => {
expect(stdout).toBe('')
expect(stderr).toEqual(expect.stringContaining('There was a problem loading formatter'))
})

test('exists with 0 status when no errors ', async () => {
const { stdout, stderr, exitCode } = await spawnHelper([
'./bin/eslint-multiplexer',
'--nopipe',
'eslint',
'--no-ignore', './test/fixtures/good.js'
])

expect(exitCode).toBe(0)
expect(stderr).toBe('')
expect(stdout).toBe('')
})
})
1 change: 1 addition & 0 deletions test/fixtures/good.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export function a() {}

0 comments on commit 79f582e

Please sign in to comment.