diff --git a/lib/cli.js b/lib/cli.js index d398477184b5..2783b89da99c 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -63,7 +63,7 @@ function translateOptions(cliOptions) { cache: cliOptions.cache, cacheFile: cliOptions.cacheFile, cacheLocation: cliOptions.cacheLocation, - fix: cliOptions.fix && (cliOptions.quiet ? quietFixPredicate : true), + fix: (cliOptions.fix || cliOptions.fixDryRun) && (cliOptions.quiet ? quietFixPredicate : true), allowInlineConfig: cliOptions.inlineConfig }; } diff --git a/lib/options.js b/lib/options.js index 5669104c0311..4cef3de54cfd 100644 --- a/lib/options.js +++ b/lib/options.js @@ -190,6 +190,12 @@ module.exports = optionator({ default: false, description: "Automatically fix problems" }, + { + option: "fix-dry-run", + type: "Boolean", + default: false, + description: "Automatically fix problems without saving the changes to the file system" + }, { option: "debug", type: "Boolean", diff --git a/tests/lib/cli.js b/tests/lib/cli.js index c994bac07152..f5653bc56d6f 100644 --- a/tests/lib/cli.js +++ b/tests/lib/cli.js @@ -858,6 +858,148 @@ describe("cli", () => { }); + describe("when passed --fix-dry-run", () => { + const sandbox = sinon.sandbox.create(); + let localCLI; + + afterEach(() => { + sandbox.verifyAndRestore(); + }); + + it("should pass fix:true to CLIEngine when executing on files", () => { + + // create a fake CLIEngine to test with + const fakeCLIEngine = sandbox.mock().withExactArgs(sinon.match({ fix: true })); + + fakeCLIEngine.prototype = leche.fake(CLIEngine.prototype); + sandbox.stub(fakeCLIEngine.prototype, "executeOnFiles").returns({ + errorCount: 0, + warningCount: 0, + results: [] + }); + sandbox.stub(fakeCLIEngine.prototype, "getFormatter").returns(() => "done"); + fakeCLIEngine.outputFixes = sandbox.stub(); + + localCLI = proxyquire("../../lib/cli", { + "./cli-engine": fakeCLIEngine, + "./logging": log + }); + + const exitCode = localCLI.execute("--fix-dry-run ."); + + assert.equal(exitCode, 0); + + }); + + it("should not rewrite files when in fix-dry-run mode", () => { + + const report = { + errorCount: 1, + warningCount: 0, + results: [{ + filePath: "./foo.js", + output: "bar", + messages: [ + { + severity: 2, + message: "Fake message" + } + ] + }] + }; + + // create a fake CLIEngine to test with + const fakeCLIEngine = sandbox.mock().withExactArgs(sinon.match({ fix: true })); + + fakeCLIEngine.prototype = leche.fake(CLIEngine.prototype); + sandbox.stub(fakeCLIEngine.prototype, "executeOnFiles").returns(report); + sandbox.stub(fakeCLIEngine.prototype, "getFormatter").returns(() => "done"); + fakeCLIEngine.outputFixes = sandbox.mock().never(); + + localCLI = proxyquire("../../lib/cli", { + "./cli-engine": fakeCLIEngine, + "./logging": log + }); + + const exitCode = localCLI.execute("--fix-dry-run ."); + + assert.equal(exitCode, 1); + + }); + + it("should provide fix predicate when in fix-dry-run mode and quiet mode", () => { + + const report = { + errorCount: 0, + warningCount: 1, + results: [{ + filePath: "./foo.js", + output: "bar", + messages: [ + { + severity: 1, + message: "Fake message" + } + ] + }] + }; + + // create a fake CLIEngine to test with + const fakeCLIEngine = sandbox.mock().withExactArgs(sinon.match({ fix: sinon.match.func })); + + fakeCLIEngine.prototype = leche.fake(CLIEngine.prototype); + sandbox.stub(fakeCLIEngine.prototype, "executeOnFiles").returns(report); + sandbox.stub(fakeCLIEngine.prototype, "getFormatter").returns(() => "done"); + fakeCLIEngine.getErrorResults = sandbox.stub().returns([]); + fakeCLIEngine.outputFixes = sandbox.mock().never(); + + localCLI = proxyquire("../../lib/cli", { + "./cli-engine": fakeCLIEngine, + "./logging": log + }); + + const exitCode = localCLI.execute("--fix-dry-run --quiet ."); + + assert.equal(exitCode, 0); + + }); + + it("should allow executing on text", () => { + + const report = { + errorCount: 1, + warningCount: 0, + results: [{ + filePath: "./foo.js", + output: "bar", + messages: [ + { + severity: 2, + message: "Fake message" + } + ] + }] + }; + + // create a fake CLIEngine to test with + 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().never(); + + localCLI = proxyquire("../../lib/cli", { + "./cli-engine": fakeCLIEngine, + "./logging": log + }); + + const exitCode = localCLI.execute("--fix-dry-run .", "foo = bar;"); + + assert.equal(exitCode, 1); + }); + }); + describe("when passing --print-config", () => { it("should print out the configuration", () => { const filePath = getFixturePath("files");