Skip to content

Commit

Permalink
fix stdin TTY check and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Nov 23, 2014
1 parent 20ccfcc commit 47062c7
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 13 deletions.
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module.exports = function (cb) {
var ret = '';

if (!process.stdin.isTTY) {
if (process.stdin.isTTY) {
cb('');
return;
}
Expand All @@ -23,7 +23,7 @@ module.exports.buffer = function (cb) {
var ret = [];
var len = 0;

if (!process.stdin.isTTY) {
if (process.stdin.isTTY) {
cb(new Buffer(''));
return;
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"node": ">=0.10.0"
},
"scripts": {
"test": "node test.js"
"test": "node test.js && echo unicorns | node test2.js"
},
"files": [
"index.js"
Expand Down
15 changes: 5 additions & 10 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var stdin = require('./');

test('get stdin', function (t) {
t.plan(1);
process.stdin.isTTY = false;

stdin(function (data) {
t.assert(data.trim() === 'unicorns');
Expand All @@ -16,6 +17,7 @@ test('get stdin', function (t) {

test('get stdin as a buffer', function (t) {
t.plan(2);
process.stdin.isTTY = false;

stdin.buffer(function (data) {
t.assert(bufferEqual(data, new Buffer('unicorns')));
Expand All @@ -27,27 +29,20 @@ test('get stdin as a buffer', function (t) {
});

test('get empty string when no stdin', function (t) {
var _ = process.stdin.isTTY;
process.stdin.isTTY = false;

t.plan(1);
process.stdin.isTTY = true;

stdin(function (data) {
t.assert(data === '');
});

process.stdin.isTTY = _;
});

test('get empty buffer when no stdin', function (t) {
var _ = process.stdin.isTTY;
process.stdin.isTTY = false;

t.plan(1);
process.stdin.isTTY = true;

stdin.buffer(function (data) {
t.assert(bufferEqual(data, new Buffer('')));
});

process.stdin.isTTY = _;
});

6 changes: 6 additions & 0 deletions test2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict';
var stdin = require('./');

stdin(function (data) {
process.exit(data ? 0 : 1);
});

0 comments on commit 47062c7

Please sign in to comment.