Skip to content

Commit

Permalink
Chore: add integration tests for autofixing (fixes #5909) (#7349)
Browse files Browse the repository at this point in the history
  • Loading branch information
not-an-aardvark authored and mysticatea committed Oct 14, 2016
1 parent c8796e9 commit ee60acf
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 16 deletions.
66 changes: 50 additions & 16 deletions tests/bin/eslint.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,33 @@
/**
* @fileoverview Tests for the eslint.js executable.
* @fileoverview Integration tests for the eslint.js executable.

This comment has been minimized.

Copy link
@khalbaz

khalbaz Oct 18, 2016

001 2

* @author Teddy Katz
*/

"use strict";

const childProcess = require("child_process");
const fs = require("fs");
const assert = require("chai").assert;
const EXECUTABLE_PATH = require("path").resolve(`${__dirname}/../../bin/eslint.js`);

/**
* Returns a Promise for when a child process exits
* @param {ChildProcess} exitingProcess The child process
* @returns {Promise<number>} A Promise that fulfills with the exit code when the child process exits
*/
function awaitExit(exitingProcess) {
return new Promise(resolve => exitingProcess.once("exit", resolve));
}

/**
* Asserts that the exit code of a given child process will equal the given value.
* @param {ChildProcess} exitingProcess The child process
* @param {number} expectedExitCode The expected exit code of the child process
* @returns {Promise} A Promise that fufills if the exit code ends up matching, and rejects otherwise.
*/
function assertExitCode(exitingProcess, expectedExitCode) {
return new Promise((resolve, reject) => {
exitingProcess.once("exit", exitCode => {
if (exitCode === expectedExitCode) {
resolve();
} else {
reject(new Error(`Expected an exit code of ${expectedExitCode} but got ${exitCode}.`));
}
});
return awaitExit(exitingProcess).then(exitCode => {
assert.strictEqual(exitCode, expectedExitCode, `Expected an exit code of ${expectedExitCode} but got ${exitCode}.`);
});
}

Expand All @@ -35,13 +39,8 @@ function assertExitCode(exitingProcess, expectedExitCode) {
function getStdout(runningProcess) {
let stdout = "";

runningProcess.stdout.on("data", data => {
stdout += data;
});

return new Promise(resolve => {
runningProcess.once("exit", () => resolve(stdout));
});
runningProcess.stdout.on("data", data => (stdout += data));
return awaitExit(runningProcess).then(() => stdout);
}

describe("bin/eslint.js", () => {
Expand Down Expand Up @@ -108,6 +107,41 @@ describe("bin/eslint.js", () => {
it("has exit code 1 if a syntax error is thrown", () => assertExitCode(runESLint(["README.md"]), 1));
});

describe("automatically fixing files", () => {
const fixturesPath = `${__dirname}/../fixtures/autofix-integration`;
const tempFilePath = `${fixturesPath}/temp.js`;
const startingText = fs.readFileSync(`${fixturesPath}/left-pad.js`).toString();
const expectedFixedText = fs.readFileSync(`${fixturesPath}/left-pad-expected.js`).toString();

beforeEach(() => {
fs.writeFileSync(tempFilePath, startingText);
});

it("has exit code 0 and fixes a file if all rules can be fixed", () => {
const child = runESLint(["--fix", "--no-eslintrc", "--no-ignore", tempFilePath]);
const exitCodeAssertion = assertExitCode(child, 0);
const outputFileAssertion = awaitExit(child).then(() => {
assert.strictEqual(fs.readFileSync(tempFilePath).toString(), expectedFixedText);
});

return Promise.all([exitCodeAssertion, outputFileAssertion]);
});

it("has exit code 1 and fixes a file if not all rules can be fixed", () => {
const child = runESLint(["--fix", "--no-eslintrc", "--no-ignore", "--rule", "max-len: [2, 10]", tempFilePath]);
const exitCodeAssertion = assertExitCode(child, 1);
const outputFileAssertion = awaitExit(child).then(() => {
assert.strictEqual(fs.readFileSync(tempFilePath).toString(), expectedFixedText);
});

return Promise.all([exitCodeAssertion, outputFileAssertion]);
});

afterEach(() => {
fs.unlinkSync(tempFilePath);
});
});

afterEach(() => {

// Clean up all the processes after every test.
Expand Down
32 changes: 32 additions & 0 deletions tests/fixtures/autofix-integration/left-pad-expected.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/* eslint dot-notation: 2 */
/* eslint indent: [2, 2] */
/* eslint no-extra-parens: 2 */
/* eslint no-implicit-coercion: 2 */
/* eslint no-whitespace-before-property: 2 */
/* eslint quotes: [2, "single"] */
/* eslint semi: 2 */
/* eslint semi-spacing: 2 */
/* eslint space-before-function-paren: 2 */

/*
* left-pad@0.0.3
* WTFPL https://github.com/stevemao/left-pad/blob/aff6d744155a70b81f09effb8185a1564f348462/COPYING
*/

module.exports = leftpad;

function leftpad (str, len, ch) {
str = String(str);

var i = -1;

ch || (ch = ' ');
len = len - str.length;


while (++i < len) {
str = ch + str;
}

return str;
}
32 changes: 32 additions & 0 deletions tests/fixtures/autofix-integration/left-pad.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/* eslint dot-notation: 2 */
/* eslint indent: [2, 2] */
/* eslint no-extra-parens: 2 */
/* eslint no-implicit-coercion: 2 */
/* eslint no-whitespace-before-property: 2 */
/* eslint quotes: [2, "single"] */
/* eslint semi: 2 */
/* eslint semi-spacing: 2 */
/* eslint space-before-function-paren: 2 */

/*
* left-pad@0.0.3
* WTFPL https://github.com/stevemao/left-pad/blob/aff6d744155a70b81f09effb8185a1564f348462/COPYING
*/

module.exports = (leftpad)

function leftpad(str, len, ch) {
str = ("" + str);

var i = -1 ;

ch || (ch = " ");
len = (len - str [ "length"])


while (++i < len) {
str = ch + str;
}

return str;
}

0 comments on commit ee60acf

Please sign in to comment.