Permalink
Browse files

Issue #1542: Use exit module instead of process.exit. Fixes #1541 and #…

…1408.

Signed-off-by: Anton Kovalyov <anton@kovalyov.net>
  • Loading branch information...
1 parent e2d617d commit 121be49e7bd93603952227106bef27f124e90cdc @aq1018 aq1018 committed with valueof Feb 21, 2014
Showing with 28 additions and 90 deletions.
  1. +2 −1 package.json
  2. +7 −17 src/cli.js
  3. +19 −72 tests/cli.js
View
@@ -38,7 +38,8 @@
"cli": "0.4.x",
"minimatch": "0.x.x",
"htmlparser2": "3.3.x",
- "console-browserify": "0.1.x"
+ "console-browserify": "0.1.x",
+ "exit": "0.1.x"
},
"devDependencies": {
View
@@ -6,6 +6,7 @@ var path = require("path");
var shjs = require("shelljs");
var minimatch = require("minimatch");
var htmlparser = require("htmlparser2");
+var exit = require("exit");
var JSHINT = require("./jshint.js").JSHINT;
var defReporter = require("./reporters/default").reporter;
@@ -398,6 +399,7 @@ function lint(code, results, config, data, file) {
var exports = {
extract: extract,
+ exit: exit,
/**
* Loads and parses a configuration file.
@@ -412,7 +414,7 @@ var exports = {
if (!shjs.test("-e", fp)) {
cli.error("Can't find config file: " + fp);
- process.exit(1);
+ exports.exit(1);
}
try {
@@ -427,7 +429,7 @@ var exports = {
return config;
} catch (err) {
cli.error("Can't parse config file: " + fp);
- process.exit(1);
+ exports.exit(1);
}
},
@@ -496,7 +498,7 @@ var exports = {
code = shjs.cat(file);
} catch (err) {
cli.error("Can't open " + file);
- process.exit(1);
+ exports.exit(1);
}
lint(extract(code, opts.extract), results, config, data, file);
@@ -563,7 +565,7 @@ var exports = {
if (reporter === null) {
cli.error("Can't load reporter file: " + options.reporter);
- process.exit(1);
+ exports.exit(1);
}
}
@@ -577,19 +579,7 @@ var exports = {
if (passed == null)
return;
- // Patch as per https://github.com/visionmedia/mocha/issues/333
- // fixes issues with piped output on Windows.
- // Root issue is here https://github.com/joyent/node/issues/3584
- function exit() { process.exit(passed ? 0 : 2); }
- try {
- if (exports.getBufferSize()) {
- process.stdout.once('drain', exit);
- } else {
- exit();
- }
- } catch (err) {
- exit();
- }
+ exports.exit(passed ? 0 : 2);
}
done(exports.run({
View
@@ -7,16 +7,12 @@ var cli = require("../src/cli.js");
exports.group = {
setUp: function (cb) {
- if (!process.stdout.flush) {
- process.stdout.flush = function () { return true; };
- }
-
- sinon.stub(process, "exit");
+ sinon.stub(cli, "exit");
cb();
},
tearDown: function (cb) {
- process.exit.restore();
+ cli.exit.restore();
cb();
},
@@ -42,8 +38,8 @@ exports.group = {
.withArgs("-e", sinon.match(/file5\.json$/)).returns(true)
.withArgs("-e", sinon.match(/file6\.json$/)).returns(true);
- process.exit.restore();
- sinon.stub(process, "exit").throws("ProcessExit");
+ cli.exit.restore();
+ sinon.stub(cli, "exit").throws("ProcessExit");
// File doesn't exist.
try {
@@ -81,8 +77,8 @@ exports.group = {
test.equal(err, "ProcessExit");
}
- process.exit.restore();
- sinon.stub(process, "exit");
+ cli.exit.restore();
+ sinon.stub(cli, "exit");
// Merges existing valid files
cli.interpret([
@@ -122,8 +118,8 @@ exports.group = {
.withArgs("-e", sinon.match(/prereq.js$/)).returns(true)
.withArgs("-e", sinon.match(/config.json$/)).returns(true);
- process.exit.restore();
- sinon.stub(process, "exit")
+ cli.exit.restore();
+ sinon.stub(cli, "exit")
.withArgs(0).returns(true)
.withArgs(1).throws("ProcessExit");
@@ -147,8 +143,8 @@ exports.group = {
var dir = __dirname + "/../examples/";
sinon.stub(process, "cwd").returns(dir);
- process.exit.restore();
- sinon.stub(process, "exit").throws("ProcessExit");
+ cli.exit.restore();
+ sinon.stub(cli, "exit").throws("ProcessExit");
// Test failed attempt.
try {
@@ -336,7 +332,7 @@ exports.group = {
cli.interpret([
"node", "jshint", "file.js"
]);
- test.equal(process.exit.args[0][0], 0); // lint with wrong package.json
+ test.equal(cli.exit.args[0][0], 0); // lint with wrong package.json
shjs.test.restore();
shjs.cat.restore();
@@ -361,7 +357,7 @@ exports.group = {
cli.interpret([
"node", "jshint", "file.js"
]);
- test.equal(process.exit.args[0][0], 0); // eval allowed = rc file found
+ test.equal(cli.exit.args[0][0], 0); // eval allowed = rc file found
shjs.test.restore();
shjs.cat.restore();
@@ -385,7 +381,7 @@ exports.group = {
cli.interpret([
"node", "jshint", "/file.js"
]);
- test.equal(process.exit.args[0][0], 0); // eval allowed = rc file found
+ test.equal(cli.exit.args[0][0], 0); // eval allowed = rc file found
shjs.test.restore();
shjs.cat.restore();
@@ -413,7 +409,7 @@ exports.group = {
cli.interpret([
"node", "jshint", "file.js"
]);
- test.equal(process.exit.args[0][0], 0); // eval allowed = rc file found
+ test.equal(cli.exit.args[0][0], 0); // eval allowed = rc file found
shjs.test.restore();
shjs.cat.restore();
@@ -440,7 +436,7 @@ exports.group = {
cli.interpret([
"node", "jshint", srcFile
]);
- test.equal(process.exit.args[0][0], 0); // eval allowed = rc file found
+ test.equal(cli.exit.args[0][0], 0); // eval allowed = rc file found
shjs.test.restore();
shjs.cat.restore();
@@ -765,8 +761,8 @@ exports.group = {
.withArgs(sinon.match(/\.jshintrc$/)).returns("{}")
.withArgs(sinon.match(/\.jshintignore$/)).returns("");
- process.exit.restore();
- sinon.stub(process, "exit");
+ cli.exit.restore();
+ sinon.stub(cli, "exit");
cli.interpret([
"node", "jshint", "pass.js", "--reporter=reporter.js"
@@ -776,63 +772,14 @@ exports.group = {
"node", "jshint", "fail.js", "--reporter=reporter.js"
]);
- test.strictEqual(process.exit.args[0][0], 0);
- test.equal(process.exit.args[1][0], 2);
+ test.strictEqual(cli.exit.args[0][0], 0);
+ test.equal(cli.exit.args[1][0], 2);
rep.reporter.restore();
process.cwd.restore();
shjs.test.restore();
shjs.cat.restore();
test.done();
- },
-
- testDrainCalledWhenThereIsBufferedOutput: function (test) {
- var dir = __dirname + "/../examples/";
- sinon.stub(cli, "run").returns(false);
- sinon.stub(cli, "getBufferSize").returns(1);
- sinon.stub(process, "cwd").returns(dir);
- sinon.stub(process.stdout, "on", function (name, func) {
- func();
- });
-
- process.exit.restore();
- sinon.stub(process, "exit");
-
- cli.interpret(["node", "jshint", "reporter.js"]);
- test.equal(process.stdout.on.callCount, 1);
- test.equal(process.stdout.on.args[0][0], "drain");
- test.strictEqual(process.exit.args[0][0], 2);
-
- process.cwd.restore();
- process.stdout.on.restore();
- cli.run.restore();
- cli.getBufferSize.restore();
-
- test.done();
- },
-
- testDrainNotCalledWhenThereIsNoBufferedOutput: function (test) {
- var dir = __dirname + "/../examples/";
- sinon.stub(cli, "run").returns(false);
- sinon.stub(cli, "getBufferSize").returns(0);
- sinon.stub(process, "cwd").returns(dir);
- sinon.stub(process.stdout, "on", function (name, func) {
- func();
- });
-
- process.exit.restore();
- sinon.stub(process, "exit");
-
- cli.interpret(["node", "jshint", "reporter.js"]);
- test.equal(process.stdout.on.callCount, 0);
- test.strictEqual(process.exit.args[0][0], 2);
-
- process.cwd.restore();
- process.stdout.on.restore();
- cli.run.restore();
- cli.getBufferSize.restore();
-
- test.done();
}
};

0 comments on commit 121be49

Please sign in to comment.