Skip to content

Commit

Permalink
Merge pull request #13 from TooTallNate/tests
Browse files Browse the repository at this point in the history
New test case
  • Loading branch information
chjj committed May 1, 2012
2 parents 8a52486 + 7ca5dcb commit 53e79c5
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 22 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@
"homepage": "https://github.com/chjj/pty.js",
"bugs": { "url": "https://github.com/chjj/pty.js/issues" },
"keywords": [ "pty", "tty", "terminal" ],
"scripts": {
"test": "node test"
},
"tags": [ "pty", "tty", "terminal" ]
}
58 changes: 58 additions & 0 deletions test/child-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@

/**
* This script gets run as a child process created by pty.js' spawn() function.
*/

var tty = require('tty');
var assert = require('assert');

// these two are huge. with vanilla node, it's impossible to spawn
// a child process that has the stdin and stdout fd's be isatty, except
// when passing through its own stdin and stdout which isn't always desirable
assert.ok(tty.isatty(0));
assert.ok(tty.isatty(1));

var size = process.stdout.getWindowSize();
assert.equal(size[0], 80);
assert.equal(size[1], 30);

// the test runner will call "term.resize(100, 100)" after 1 second
var gotSIGWINCH = false;
process.on('SIGWINCH', function () {
gotSIGWINCH = true;
size = process.stdout.getWindowSize();
assert.equal(size[0], 100);
assert.equal(size[1], 100);
});


// testing reading data from stdin, since that's a crucial feature
if (process.stdin.setRawMode) {
process.stdin.setRawMode(true);
} else {
tty.setRawMode(true);
}
process.stdin.resume();
process.stdin.setEncoding('utf8');

// the child script expects 1 data event, with the text "☃"
var dataCount = 0;
process.stdin.on('data', function (data) {
dataCount++;
assert.equal(data, '☃');

// done!
process.stdin.pause();
clearTimeout(timeout);
});

var timeout = setTimeout(function () {
console.error('TIMEOUT!');
process.exit(7);
}, 5000);

process.on('exit', function (code) {
if (code === 7) return; // timeout
assert.ok(gotSIGWINCH);
assert.equal(dataCount, 1);
});
67 changes: 45 additions & 22 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,48 @@
var assert = require('assert');
var pty = require('../');

var term = pty.fork('sh', [], { name: 'vt100' });

var buff = '';

// avoid the first bytes output
setTimeout(function() {
term.write('echo "$TERM"\r');
term.write('cat\r');
term.on('data', function(data) {
buff += data;
});

setTimeout(function() {
assert.equal(buff.substring(0, 19), 'echo "$TERM"\r\nvt100');
assert.equal(term.process, 'cat');
console.log('Completed successfully.');
process.exit(0);
}, 200);
}, 200);

// assert static constructor works
assert.equal(pty.fork('sh', [], { name: 'test' }).name, 'test');
var term = pty.fork(process.execPath, [ 'child-test.js' ], { cwd: __dirname });

// any output is considered failure. this is only a workaround
// until the actual error code is passed through
var count = 0;
term.on('data', function (data) {
count++;
});

// pipe output to our stderr
term.pipe(process.stderr);

// make sure 'file' gets set properly
assert.equal(term.file, process.execPath);

// wait 1 second for node to spawn, and do its initial checks
setTimeout(function () {

// test resize()
term.resize(100, 100);

// test writing unicode data
term.write('☃');

term.end();

}, 1000);

var gotTermExit = false;
term.on('exit', function (code) {
gotTermExit = true;
// TODO: ensure exit code is 0
if (count) {
process.exit(1);
}
});

process.on('exit', function (code) {
assert.equal(gotTermExit, true);
if (code) {
console.error('Tests FAILED');
} else {
console.error('Tests PASSED');
}
});

0 comments on commit 53e79c5

Please sign in to comment.