From e1bf19411c47c6bd07436a7fcdfd7808645c38fd Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Mon, 30 Apr 2012 15:17:49 -0700 Subject: [PATCH 1/3] test: rewrite the test runner to spawn a node child script This new test is more comprehensive and tests a few aspects of pty.js like writing to stdin and ensuring the "resize()" function works as expected. --- test/child-test.js | 58 +++++++++++++++++++++++++++++++++++++++ test/index.js | 67 +++++++++++++++++++++++++++++++--------------- 2 files changed, 103 insertions(+), 22 deletions(-) create mode 100644 test/child-test.js diff --git a/test/child-test.js b/test/child-test.js new file mode 100644 index 0000000..b612cce --- /dev/null +++ b/test/child-test.js @@ -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); +}); diff --git a/test/index.js b/test/index.js index 0d17830..5b0ca2c 100644 --- a/test/index.js +++ b/test/index.js @@ -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('\nTests FAILED'); + } else { + console.error('\nTests PASSED'); + } +}); From d264d6dda388acc3e15f374d15f7d351741bfadc Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Mon, 30 Apr 2012 15:20:47 -0700 Subject: [PATCH 2/3] package: add a "test" script for `npm test` --- package.json | 3 +++ 1 file changed, 3 insertions(+) diff --git a/package.json b/package.json index 97bd1f1..63aecf7 100644 --- a/package.json +++ b/package.json @@ -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" ] } From 7ca5dcb4181b074a47ad93e024b66b693ffdc1d1 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Mon, 30 Apr 2012 17:34:41 -0700 Subject: [PATCH 3/3] test: remove unnecessary newline output --- test/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/index.js b/test/index.js index 5b0ca2c..d6920f5 100644 --- a/test/index.js +++ b/test/index.js @@ -45,8 +45,8 @@ term.on('exit', function (code) { process.on('exit', function (code) { assert.equal(gotTermExit, true); if (code) { - console.error('\nTests FAILED'); + console.error('Tests FAILED'); } else { - console.error('\nTests PASSED'); + console.error('Tests PASSED'); } });