From 28745bc633164fb7c7bcdfcc370181720846235d Mon Sep 17 00:00:00 2001 From: fatfisz Date: Sat, 5 Aug 2017 14:08:40 +0200 Subject: [PATCH] New: Enable the fix flag for stdio --- lib/cli-engine.js | 18 ++++++++++++++++-- lib/cli.js | 8 +------- tests/lib/cli.js | 28 ++++++++++++++++++++++++---- 3 files changed, 41 insertions(+), 13 deletions(-) diff --git a/lib/cli-engine.js b/lib/cli-engine.js index 1abc1fd2c6bc..a756ff51cc71 100644 --- a/lib/cli-engine.js +++ b/lib/cli-engine.js @@ -458,11 +458,25 @@ class CLIEngine { /** * Outputs fixes from the given results to files. * @param {Object} report The report object created by CLIEngine. + * @param {boolean} isPipedIn True if the input comes from stdin. * @returns {void} */ - static outputFixes(report) { + static outputFixes(report, isPipedIn) { report.results.filter(result => result.hasOwnProperty("output")).forEach(result => { - fs.writeFileSync(result.filePath, result.output); + if (isPipedIn) { + try { + fs.writeFileSync(3, result.output); + } catch (ex) { + if (ex.code === "EBADF") { + + // Ignore, that just means nothing is listening on that descriptor. + } else { + throw ex; + } + } + } else { + fs.writeFileSync(result.filePath, result.output); + } }); } diff --git a/lib/cli.js b/lib/cli.js index d398477184b5..c990844beaa1 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -170,19 +170,13 @@ const cli = { debug(`Running on ${text ? "text" : "files"}`); - // disable --fix for piped-in code until we know how to do it correctly - if (text && currentOptions.fix) { - log.error("The --fix option is not available for piped-in code."); - return 1; - } - const engine = new CLIEngine(translateOptions(currentOptions)); const report = text ? engine.executeOnText(text, currentOptions.stdinFilename, true) : engine.executeOnFiles(files); if (currentOptions.fix) { debug("Fix mode enabled - applying fixes"); - CLIEngine.outputFixes(report); + CLIEngine.outputFixes(report, Boolean(text)); } if (currentOptions.quiet) { diff --git a/tests/lib/cli.js b/tests/lib/cli.js index b75640d4abde..c506247fa25e 100644 --- a/tests/lib/cli.js +++ b/tests/lib/cli.js @@ -53,6 +53,7 @@ describe("cli", () => { fakeCLIEngine.prototype = leche.fake(CLIEngine.prototype); sandbox.stub(fakeCLIEngine.prototype, "executeOnFiles").returns({}); sandbox.stub(fakeCLIEngine.prototype, "getFormatter").returns(sinon.spy()); + sandbox.stub(fakeCLIEngine, "outputFixes").returns({}); const localCLI = proxyquire("../../lib/cli", { "./cli-engine": fakeCLIEngine, @@ -791,7 +792,7 @@ describe("cli", () => { fakeCLIEngine.prototype = leche.fake(CLIEngine.prototype); sandbox.stub(fakeCLIEngine.prototype, "executeOnFiles").returns(report); sandbox.stub(fakeCLIEngine.prototype, "getFormatter").returns(() => "done"); - fakeCLIEngine.outputFixes = sandbox.mock().withExactArgs(report); + fakeCLIEngine.outputFixes = sandbox.mock().withExactArgs(report, false); localCLI = proxyquire("../../lib/cli", { "./cli-engine": fakeCLIEngine, @@ -828,7 +829,7 @@ describe("cli", () => { sandbox.stub(fakeCLIEngine.prototype, "executeOnFiles").returns(report); sandbox.stub(fakeCLIEngine.prototype, "getFormatter").returns(() => "done"); fakeCLIEngine.getErrorResults = sandbox.stub().returns([]); - fakeCLIEngine.outputFixes = sandbox.mock().withExactArgs(report); + fakeCLIEngine.outputFixes = sandbox.mock().withExactArgs(report, false); localCLI = proxyquire("../../lib/cli", { "./cli-engine": fakeCLIEngine, @@ -841,10 +842,29 @@ describe("cli", () => { }); - it("should not call CLIEngine and return 1 when executing on text", () => { + it("should rewrite files when executing on text", () => { + + const report = { + errorCount: 1, + warningCount: 0, + results: [{ + output: "bar", + messages: [ + { + severity: 2, + message: "Fake message" + } + ] + }] + }; // create a fake CLIEngine to test with - const fakeCLIEngine = sandbox.mock().never(); + const fakeCLIEngine = sandbox.mock().withExactArgs(sinon.match({ fix: true })); + + fakeCLIEngine.prototype = leche.fake(CLIEngine.prototype); + sandbox.stub(fakeCLIEngine.prototype, "executeOnText").returns(report); + sandbox.stub(fakeCLIEngine.prototype, "getFormatter").returns(() => "done"); + fakeCLIEngine.outputFixes = sandbox.mock().withExactArgs(report, true); localCLI = proxyquire("../../lib/cli", { "./cli-engine": fakeCLIEngine,