Skip to content

Commit

Permalink
New: Enable the fix flag for stdio
Browse files Browse the repository at this point in the history
  • Loading branch information
fatfisz committed Aug 5, 2017
1 parent 37158c5 commit 7cd53ae
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 13 deletions.
18 changes: 16 additions & 2 deletions lib/cli-engine.js
Expand Up @@ -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.writeSync(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);
}
});
}

Expand Down
8 changes: 1 addition & 7 deletions lib/cli.js
Expand Up @@ -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) {
Expand Down
28 changes: 24 additions & 4 deletions tests/lib/cli.js
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit 7cd53ae

Please sign in to comment.