Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: duplicated error message if a crash occurs (fixes #8964) #8965

Merged
merged 1 commit into from
Jul 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 3 additions & 4 deletions bin/eslint.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,10 @@ process.once("uncaughtException", err => {
if (typeof err.messageTemplate === "string" && err.messageTemplate.length > 0) {
const template = lodash.template(fs.readFileSync(path.resolve(__dirname, `../messages/${err.messageTemplate}.txt`), "utf-8"));

console.log("\nOops! Something went wrong! :(");
console.log(`\n${template(err.messageData || {})}`);
console.error("\nOops! Something went wrong! :(");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this might be considered as a breaking change? If somebody wrote an integration that listens for stdout they will get nothing in the case of the crash. But I think that's too much of an edge case.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose that's possible. Do you think it would be better to separate that into a different commit and schedule it for 5.0?

console.error(`\n${template(err.messageData || {})}`);
} else {
console.log(err.message);
console.log(err.stack);
console.error(err.stack);
}

process.exitCode = 1;
Expand Down
33 changes: 27 additions & 6 deletions tests/bin/eslint.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,16 @@ function assertExitCode(exitingProcess, expectedExitCode) {
/**
* Returns a Promise for the stdout of a process.
* @param {ChildProcess} runningProcess The child process
* @returns {Promise<string>} A Promise that fulfills with all of the stdout output produced by the process when it exits.
* @returns {Promise<{stdout: string, stderr: string}>} A Promise that fulfills with all of the
* stdout and stderr output produced by the process when it exits.
*/
function getStdout(runningProcess) {
function getOutput(runningProcess) {
let stdout = "";
let stderr = "";

runningProcess.stdout.on("data", data => (stdout += data));
return awaitExit(runningProcess).then(() => stdout);
runningProcess.stderr.on("data", data => (stderr += data));
return awaitExit(runningProcess).then(() => ({ stdout, stderr }));
}

describe("bin/eslint.js", () => {
Expand Down Expand Up @@ -88,8 +91,8 @@ describe("bin/eslint.js", () => {
const child = runESLint(["--stdin"], { cwd: "/" }); // Assumes the root directory has no .eslintrc file

const exitCodePromise = assertExitCode(child, 1);
const stdoutPromise = getStdout(child).then(stdout => {
assert.match(stdout, /ESLint couldn't find a configuration file/);
const stdoutPromise = getOutput(child).then(output => {
assert.match(output.stderr, /ESLint couldn't find a configuration file/);
});

child.stdin.write("var foo = bar\n");
Expand Down Expand Up @@ -131,7 +134,7 @@ describe("bin/eslint.js", () => {
it("has exit code 0, fixes errors in a file, and does not report or fix warnings if --quiet and --fix are used", () => {
const child = runESLint(["--fix", "--quiet", "--no-eslintrc", "--no-ignore", tempFilePath]);
const exitCodeAssertion = assertExitCode(child, 0);
const stdoutAssertion = getStdout(child).then(stdout => assert.strictEqual(stdout, ""));
const stdoutAssertion = getOutput(child).then(output => assert.strictEqual(output.stdout, ""));
const outputFileAssertion = awaitExit(child).then(() => {
assert.strictEqual(fs.readFileSync(tempFilePath).toString(), expectedFixedTextQuiet);
});
Expand Down Expand Up @@ -259,6 +262,24 @@ describe("bin/eslint.js", () => {
});
});

describe("handling crashes", () => {
it("prints the error message exactly once to stderr in the event of a crash", () => {
const child = runESLint(["--rule=no-restricted-syntax:[error, 'Invalid Selector [[[']", "Makefile.js"]);
const exitCodeAssertion = assertExitCode(child, 1);
const outputAssertion = getOutput(child).then(output => {
const expectedSubstring = "Syntax error in selector";

assert.strictEqual(output.stdout, "");
assert.include(output.stderr, expectedSubstring);

// The message should appear exactly once in stderr
assert.strictEqual(output.stderr.indexOf(expectedSubstring), output.stderr.lastIndexOf(expectedSubstring));
});

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

afterEach(() => {

// Clean up all the processes after every test.
Expand Down