Skip to content

Commit 6aa6d4b

Browse files
committed
first commit
0 parents  commit 6aa6d4b

20 files changed

+4797
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

.jshintrc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"curly": true,
3+
"eqeqeq": true,
4+
"immed": true,
5+
"latedef": "nofunc",
6+
"newcap": true,
7+
"noarg": true,
8+
"sub": true,
9+
"undef": true,
10+
"unused": true,
11+
"boss": true,
12+
"eqnull": true,
13+
"node": true
14+
}

.travis.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
language: node_js
2+
node_js:
3+
- 0.8
4+
- '0.10'
5+
before_script:
6+
- npm install -g grunt-cli

Gruntfile.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
'use strict';
2+
3+
module.exports = function(grunt) {
4+
5+
// Project configuration.
6+
grunt.initConfig({
7+
nodeunit: {
8+
files: ['test/**/*_test.js'],
9+
},
10+
jshint: {
11+
options: {
12+
jshintrc: '.jshintrc'
13+
},
14+
gruntfile: {
15+
src: 'Gruntfile.js'
16+
},
17+
lib: {
18+
src: ['lib/**/*.js']
19+
},
20+
test: {
21+
src: ['test/**/*.js']
22+
},
23+
},
24+
watch: {
25+
gruntfile: {
26+
files: '<%= jshint.gruntfile.src %>',
27+
tasks: ['jshint:gruntfile']
28+
},
29+
lib: {
30+
files: '<%= jshint.lib.src %>',
31+
tasks: ['jshint:lib', 'nodeunit']
32+
},
33+
test: {
34+
files: '<%= jshint.test.src %>',
35+
tasks: ['jshint:test', 'nodeunit']
36+
},
37+
},
38+
});
39+
40+
// These plugins provide necessary tasks.
41+
grunt.loadNpmTasks('grunt-contrib-nodeunit');
42+
grunt.loadNpmTasks('grunt-contrib-jshint');
43+
grunt.loadNpmTasks('grunt-contrib-watch');
44+
45+
// Default task.
46+
grunt.registerTask('default', ['jshint', 'nodeunit']);
47+
48+
};

LICENSE-MIT

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Copyright (c) 2013 "Cowboy" Ben Alman
2+
3+
Permission is hereby granted, free of charge, to any person
4+
obtaining a copy of this software and associated documentation
5+
files (the "Software"), to deal in the Software without
6+
restriction, including without limitation the rights to use,
7+
copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
copies of the Software, and to permit persons to whom the
9+
Software is furnished to do so, subject to the following
10+
conditions:
11+
12+
The above copyright notice and this permission notice shall be
13+
included in all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22+
OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# exit [![Build Status](https://secure.travis-ci.org/cowboy/node-exit.png?branch=master)](http://travis-ci.org/cowboy/node-exit)
2+
3+
A replacement for process.exit that ensures stdio are fully drained before exiting.
4+
5+
## Getting Started
6+
Install the module with: `npm install exit`
7+
8+
```javascript
9+
var exit = require('exit');
10+
11+
// These lines should appear in the output.
12+
console.log("foo");
13+
console.error("bar");
14+
15+
// process.exit(5);
16+
exit(5);
17+
18+
// These lines shouldn't appear in the output.
19+
console.log("foo");
20+
console.error("bar");
21+
```
22+
23+
## Contributing
24+
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using [Grunt](http://gruntjs.com/).
25+
26+
## Release History
27+
_(Nothing yet)_
28+
29+
## License
30+
Copyright (c) 2013 "Cowboy" Ben Alman
31+
Licensed under the MIT license.

lib/exit.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* exit
3+
* https://github.com/cowboy/node-exit
4+
*
5+
* Copyright (c) 2013 "Cowboy" Ben Alman
6+
* Licensed under the MIT license.
7+
*/
8+
9+
'use strict';
10+
11+
module.exports = function exit(exitCode, streams) {
12+
if (!streams) { streams = [process.stdout, process.stderr]; }
13+
var drainCount = 0;
14+
// Actually exit if all streams are drained.
15+
function tryToExit() {
16+
if (drainCount === streams.length) {
17+
process.exit(exitCode);
18+
}
19+
}
20+
streams.forEach(function(stream) {
21+
// Prevent further writing.
22+
stream.write = function() {};
23+
// Count drained streams now, but monitor non-drained streams.
24+
if (stream.bufferSize === 0) {
25+
drainCount++;
26+
} else {
27+
stream.once('drain', function() {
28+
drainCount++;
29+
tryToExit();
30+
});
31+
}
32+
});
33+
// If all streams were already drained, exit now.
34+
tryToExit();
35+
};

package.json

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{
2+
"name": "exit",
3+
"description": "A replacement for process.exit that ensures stdio are fully drained before exiting.",
4+
"version": "0.1.0",
5+
"homepage": "https://github.com/cowboy/node-exit",
6+
"author": {
7+
"name": "\"Cowboy\" Ben Alman",
8+
"url": "http://benalman.com/"
9+
},
10+
"repository": {
11+
"type": "git",
12+
"url": "git://github.com/cowboy/node-exit.git"
13+
},
14+
"bugs": {
15+
"url": "https://github.com/cowboy/node-exit/issues"
16+
},
17+
"licenses": [
18+
{
19+
"type": "MIT",
20+
"url": "https://github.com/cowboy/node-exit/blob/master/LICENSE-MIT"
21+
}
22+
],
23+
"main": "lib/exit",
24+
"engines": {
25+
"node": ">= 0.8.0"
26+
},
27+
"scripts": {
28+
"test": "grunt nodeunit"
29+
},
30+
"devDependencies": {
31+
"grunt-contrib-jshint": "~0.6.4",
32+
"grunt-contrib-nodeunit": "~0.2.0",
33+
"grunt-contrib-watch": "~0.5.3",
34+
"grunt": "~0.4.1",
35+
"async": "~0.2.9",
36+
"diff": "~1.0.7"
37+
},
38+
"keywords": [
39+
"exit",
40+
"process",
41+
"stdio",
42+
"stdout",
43+
"stderr",
44+
"drain",
45+
"flush",
46+
"3584"
47+
]
48+
}

test/exit_test.js

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
'use strict';
2+
3+
/*
4+
======== A Handy Little Nodeunit Reference ========
5+
https://github.com/caolan/nodeunit
6+
7+
Test methods:
8+
test.expect(numAssertions)
9+
test.done()
10+
Test assertions:
11+
test.ok(value, [message])
12+
test.equal(actual, expected, [message])
13+
test.notEqual(actual, expected, [message])
14+
test.deepEqual(actual, expected, [message])
15+
test.notDeepEqual(actual, expected, [message])
16+
test.strictEqual(actual, expected, [message])
17+
test.notStrictEqual(actual, expected, [message])
18+
test.throws(block, [error], [message])
19+
test.doesNotThrow(block, [error], [message])
20+
test.ifError(value)
21+
*/
22+
23+
var fs = require('fs');
24+
var exec = require('child_process').exec;
25+
var async = require('async');
26+
var jsdiff = require('diff');
27+
28+
function run(command, options, callback) {
29+
if (typeof options === 'function') {
30+
callback = options;
31+
options = {};
32+
}
33+
command += ' 2>&1';
34+
if (options.pipe) {
35+
command += ' | ' + (process.platform === 'win32' ? 'find' : 'grep') + ' "std"';
36+
}
37+
exec(command, function(error, stdout) {
38+
callback(error ? error.code : 0, stdout);
39+
});
40+
}
41+
42+
function showDiff(actual, expected) {
43+
if (actual === expected) {
44+
return true;
45+
} else {
46+
return jsdiff.diffLines(expected, actual).map(function(d) {
47+
if (d.removed) {
48+
return 'MISSING: ' + d.value;
49+
} else if (d.added) {
50+
return 'EXTRA: ' + d.value;
51+
}
52+
}).filter(Boolean).join('');
53+
}
54+
}
55+
56+
function fixture(filename) {
57+
return String(fs.readFileSync(filename));
58+
}
59+
60+
exports['awesome'] = {
61+
setUp: function(done) {
62+
this.origCwd = process.cwd();
63+
process.chdir('test/fixtures');
64+
done();
65+
},
66+
tearDown: function(done) {
67+
process.chdir(this.origCwd);
68+
done();
69+
},
70+
'stdout stderr': function(test) {
71+
var counts = [10, 100, 1000];
72+
test.expect(counts.length);
73+
async.eachSeries(counts, function(n, next) {
74+
var command = 'node log.js 0 ' + n + ' stdout stderr';
75+
run(command, {pipe: true}, function(code, actual) {
76+
var expected = fixture(n + '-stdout-stderr.txt');
77+
test.equal(true, showDiff(actual, expected), command);
78+
next();
79+
});
80+
}, test.done);
81+
},
82+
'stdout': function(test) {
83+
var counts = [10, 100, 1000];
84+
test.expect(counts.length);
85+
async.eachSeries(counts, function(n, next) {
86+
var command = 'node log.js 0 ' + n + ' stdout';
87+
run(command, {pipe: true}, function(code, actual) {
88+
var expected = fixture(n + '-stdout.txt');
89+
test.equal(true, showDiff(actual, expected), command);
90+
next();
91+
});
92+
}, test.done);
93+
},
94+
'stderr': function(test) {
95+
var counts = [10, 100, 1000];
96+
test.expect(counts.length);
97+
async.eachSeries(counts, function(n, next) {
98+
var command = 'node log.js 0 ' + n + ' stderr';
99+
run(command, {pipe: true}, function(code, actual) {
100+
var expected = fixture(n + '-stderr.txt');
101+
test.equal(true, showDiff(actual, expected), command);
102+
next();
103+
});
104+
}, test.done);
105+
},
106+
'exit codes': function(test) {
107+
var codes = [0, 1, 123];
108+
test.expect(codes.length * 2);
109+
async.eachSeries(codes, function(n, next) {
110+
var command = 'node log.js ' + n + ' 10 stdout stderr';
111+
run(command, {pipe: false}, function(code, actual) {
112+
test.equal(code, n, 'should have exited with ' + n + ' error code.');
113+
var expected = fixture('10-stdout-stderr.txt');
114+
test.equal(true, showDiff(actual, expected), command);
115+
next();
116+
});
117+
}, test.done);
118+
},
119+
};

test/fixtures/10-stderr.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[stderr] testing 0
2+
[stderr] testing 1
3+
[stderr] testing 2
4+
[stderr] testing 3
5+
[stderr] testing 4
6+
[stderr] testing 5
7+
[stderr] testing 6
8+
[stderr] testing 7
9+
[stderr] testing 8
10+
[stderr] testing 9

0 commit comments

Comments
 (0)