Skip to content

Commit

Permalink
New: Add the fix-dry-run flag
Browse files Browse the repository at this point in the history
  • Loading branch information
fatfisz committed Aug 22, 2017
1 parent ffa021e commit 24ba1c6
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/cli.js
Expand Up @@ -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
};
}
Expand Down
6 changes: 6 additions & 0 deletions lib/options.js
Expand Up @@ -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",
Expand Down
142 changes: 142 additions & 0 deletions tests/lib/cli.js
Expand Up @@ -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");
Expand Down

0 comments on commit 24ba1c6

Please sign in to comment.