From 79f582eeff12ce3e0ba68652bc8fde02deeabcfd Mon Sep 17 00:00:00 2001 From: pimlie Date: Sat, 24 Aug 2019 15:05:31 +0200 Subject: [PATCH] feat: use non-zero exit code when errors exists --- lib/formatters/stylish.js | 2 +- lib/index.js | 27 +++++++++++++++++++++-- test/cli.test.js | 45 +++++++++++++++++++++++++++++++-------- test/fixtures/good.js | 1 + 4 files changed, 63 insertions(+), 12 deletions(-) create mode 100644 test/fixtures/good.js diff --git a/lib/formatters/stylish.js b/lib/formatters/stylish.js index 71f6b45..0061440 100644 --- a/lib/formatters/stylish.js +++ b/lib/formatters/stylish.js @@ -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)) } diff --git a/lib/index.js b/lib/index.js index a49a7d8..f748622 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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) } @@ -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 }) }) } diff --git a/test/cli.test.js b/test/cli.test.js index 05626ad..7348f22 100644 --- a/test/cli.test.js +++ b/test/cli.test.js @@ -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')) @@ -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')) @@ -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')) @@ -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')) @@ -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')) @@ -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')) @@ -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')) }) @@ -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', @@ -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')) @@ -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('') + }) }) diff --git a/test/fixtures/good.js b/test/fixtures/good.js new file mode 100644 index 0000000..fa1736f --- /dev/null +++ b/test/fixtures/good.js @@ -0,0 +1 @@ +export function a() {}