Skip to content

Commit 79f582e

Browse files
committed
feat: use non-zero exit code when errors exists
1 parent 7af25cc commit 79f582e

File tree

4 files changed

+63
-12
lines changed

4 files changed

+63
-12
lines changed

lib/formatters/stylish.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const table = require('text-table')
3434
const { SourceCode } = require('eslint')
3535
const Multiplexer = require('../..')
3636

37-
module.exports = function (results, options) {
37+
module.exports = function (results, data, options) {
3838
if (!options) {
3939
({ results, options } = Multiplexer.runFromFormatter(results))
4040
}

lib/index.js

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,11 @@ class Multiplexer {
122122
if (!isPiped && this.options.format !== 'json') {
123123
const mergedResults = this.merge(results)
124124
const formatter = getFormatter(this.options.format)
125-
output = formatter(mergedResults, this.options)
125+
output = formatter(mergedResults, {}, this.options)
126+
127+
if (!process.exitCode) {
128+
process.exitCode = mergedResults.some(results => results.errorCount) ? 1 : 0
129+
}
126130
} else {
127131
output = JSON.stringify(results)
128132
}
@@ -162,8 +166,27 @@ class Multiplexer {
162166
data += chunk
163167
})
164168

169+
let closedOrExited
165170
sp.on('close', () => {
166-
resolve(data)
171+
/* istanbul ignore next */
172+
if (closedOrExited) {
173+
resolve(data)
174+
}
175+
closedOrExited = true
176+
})
177+
178+
sp.on('exit', (code) => {
179+
if (code > 0) {
180+
// if subcommand has non-zero exit code,
181+
// we have too
182+
process.exitCode = code
183+
}
184+
185+
/* istanbul ignore next */
186+
if (closedOrExited) {
187+
resolve(data)
188+
}
189+
closedOrExited = true
167190
})
168191
})
169192
}

test/cli.test.js

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ describe('cli', () => {
4747
'--no-ignore', './test/fixtures'
4848
])
4949

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

65-
expect(exitCode).toBe(0)
65+
expect(exitCode).toBe(1)
6666
expect(stderr).toBe('')
6767
expect(stdout).toEqual(expect.stringContaining('2x'))
6868
expect(stdout).toEqual(expect.stringContaining('index.js'))
@@ -78,7 +78,7 @@ describe('cli', () => {
7878
'--no-ignore', './test/fixtures'
7979
])
8080

81-
expect(exitCode).toBe(0)
81+
expect(exitCode).toBe(1)
8282
expect(stderr).toBe('')
8383
expect(stdout).toEqual(expect.stringContaining('2x'))
8484
expect(stdout).toEqual(expect.stringContaining('index.js'))
@@ -94,7 +94,7 @@ describe('cli', () => {
9494
'--no-ignore', './test/fixtures'
9595
])
9696

97-
expect(exitCode).toBe(0)
97+
expect(exitCode).toBe(1)
9898
expect(stderr).toBe('')
9999
expect(stdout).toEqual(expect.stringContaining('2x'))
100100
expect(stdout).toEqual(expect.stringContaining('index'))
@@ -110,7 +110,7 @@ describe('cli', () => {
110110
'--no-ignore', './test/fixtures'
111111
])
112112

113-
expect(exitCode).toBe(0)
113+
expect(exitCode).toBe(1)
114114
expect(stderr).toBe('')
115115
expect(stdout).toEqual(expect.stringContaining('2x'))
116116
expect(stdout).toEqual(expect.stringContaining('1x'))
@@ -126,7 +126,7 @@ describe('cli', () => {
126126
'--no-ignore', './test/fixtures'
127127
])
128128

129-
expect(exitCode).toBe(0)
129+
expect(exitCode).toBe(1)
130130
expect(stderr).toBe('')
131131
expect(stdout).toEqual(expect.stringContaining('2x'))
132132
expect(stdout).toEqual(expect.not.stringContaining('1x'))
@@ -142,7 +142,7 @@ describe('cli', () => {
142142
'--no-ignore', './test/fixtures'
143143
])
144144

145-
expect(exitCode).toBe(0)
145+
expect(exitCode).toBe(1)
146146
expect(stderr).toBe('')
147147
expect(stdout).toEqual(expect.stringContaining('0: function'))
148148
})
@@ -184,7 +184,7 @@ describe('cli', () => {
184184
})
185185

186186
test('multiple pipes', async () => {
187-
const { stdout, stderr } = await new Promise((resolve) => {
187+
const { stdout, stderr, exitCode } = await new Promise((resolve) => {
188188
const eslint1 = spawn(process.execPath, [
189189
'./bin/eslint-multiplexer',
190190
'eslint',
@@ -218,11 +218,25 @@ describe('cli', () => {
218218
stderr += chunk
219219
})
220220

221+
let exitCode
222+
let closedOrExited
221223
multiplexer.on('close', () => {
222-
resolve({ stdout, stderr })
224+
if (closedOrExited) {
225+
resolve({ stdout, stderr, exitCode })
226+
}
227+
closedOrExited = true
228+
})
229+
230+
multiplexer.on('exit', (code, signal) => {
231+
exitCode = code
232+
if (closedOrExited) {
233+
resolve({ stdout, stderr, exitCode })
234+
}
235+
closedOrExited = true
223236
})
224237
})
225238

239+
expect(exitCode).toBe(1)
226240
expect(stderr).toBe('')
227241
expect(stdout).toEqual(expect.stringContaining('2x'))
228242
expect(stdout).toEqual(expect.stringContaining('1x'))
@@ -254,4 +268,17 @@ describe('cli', () => {
254268
expect(stdout).toBe('')
255269
expect(stderr).toEqual(expect.stringContaining('There was a problem loading formatter'))
256270
})
271+
272+
test('exists with 0 status when no errors ', async () => {
273+
const { stdout, stderr, exitCode } = await spawnHelper([
274+
'./bin/eslint-multiplexer',
275+
'--nopipe',
276+
'eslint',
277+
'--no-ignore', './test/fixtures/good.js'
278+
])
279+
280+
expect(exitCode).toBe(0)
281+
expect(stderr).toBe('')
282+
expect(stdout).toBe('')
283+
})
257284
})

test/fixtures/good.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export function a() {}

0 commit comments

Comments
 (0)