Permalink
Browse files

read@1.0

  • Loading branch information...
isaacs committed Jul 24, 2012
1 parent e179632 commit 18e4fa8b15ab268141efa083e530eaaea65e0e0f
Showing with 96 additions and 61 deletions.
  1. +1 −1 package.json
  2. +1 −1 promzard.js
  3. +43 −25 test/exports.js
  4. +51 −34 test/fn.js
View
@@ -7,7 +7,7 @@
"url": "git://github.com/isaacs/promzard"
},
"dependencies": {
- "read": "0"
+ "read": "1"
},
"devDependencies": {
"tap": "~0.2.5"
View
@@ -211,6 +211,6 @@ PromZard.prototype.prompt = function (pdt, cb) {
}}(cb).bind(this)
}
- read({ prompt: prompt + ': ' , default: def }, cb)
+ read({ prompt: prompt + ':' , default: def }, cb)
}
View
@@ -1,30 +1,48 @@
var test = require('tap').test;
var promzard = require('../');
+if (process.argv[2] === 'child') {
+ return child()
+}
+
test('exports', function (t) {
- t.plan(1);
-
- var ctx = { tmpdir : '/tmp' }
- var file = __dirname + '/exports.input';
- promzard(file, ctx, function (err, output) {
- t.same(
- {
- a : 3,
- b : '!2b',
- c : {
- x : 55,
- y : '/tmp/y/file.txt',
- }
- },
- output
- );
- });
-
- setTimeout(function () {
- process.stdin.emit('data', '\n');
- }, 100);
-
- setTimeout(function () {
- process.stdin.emit('data', '55\n');
- }, 200);
+ t.plan(1);
+
+ var spawn = require('child_process').spawn
+ var child = spawn(process.execPath, [__filename, 'child'])
+
+ var output = ''
+ child.stderr.on('data', function (c) {
+ output += c
+ })
+
+ setTimeout(function () {
+ child.stdin.write('\n');
+ }, 100)
+ setTimeout(function () {
+ child.stdin.write('55\n');
+ }, 200)
+
+ child.on('close', function () {
+ console.error('output=%j', output)
+ output = JSON.parse(output)
+ t.same({
+ a : 3,
+ b : '!2b',
+ c : {
+ x : 55,
+ y : '/tmp/y/file.txt',
+ }
+ }, output);
+ t.end()
+ })
});
+
+function child () {
+ var ctx = { tmpdir : '/tmp' }
+ var file = __dirname + '/exports.input';
+
+ promzard(file, ctx, function (err, output) {
+ console.error(JSON.stringify(output))
+ });
+}
View
@@ -1,39 +1,56 @@
var test = require('tap').test;
var promzard = require('../');
var fs = require('fs')
+var file = __dirname + '/fn.input';
+
+var expect = {
+ a : 3,
+ b : '!2B...',
+ c : {
+ x : 5500,
+ y : '/tmp/y/file.txt',
+ }
+}
+expect.a_function = fs.readFileSync(file, 'utf8')
+expect.asyncPrompt = 'async prompt'
+
+if (process.argv[2] === 'child') {
+ return child()
+}
test('prompt callback param', function (t) {
- t.plan(1);
-
- var ctx = { tmpdir : '/tmp' }
- var file = __dirname + '/fn.input';
- promzard(file, ctx, function (err, output) {
- var expect =
- {
- a : 3,
- b : '!2B...',
- c : {
- x : 5500,
- y : '/tmp/y/file.txt',
- }
- }
- expect.a_function = fs.readFileSync(file, 'utf8')
- expect.asyncPrompt = 'async prompt'
- t.same(
- output,
- expect
- );
- });
-
- setTimeout(function () {
- process.stdin.emit('data', '\n');
- }, 100);
-
- setTimeout(function () {
- process.stdin.emit('data', '55\n');
- }, 200);
-
- setTimeout(function () {
- process.stdin.emit('data', 'async prompt')
- }, 300)
-});
+ t.plan(1);
+
+ var spawn = require('child_process').spawn
+ var child = spawn(process.execPath, [__filename, 'child'])
+
+ var output = ''
+ child.stderr.on('data', function (c) {
+ output += c
+ })
+
+ child.on('close', function () {
+ console.error('output=%j', output)
+ output = JSON.parse(output)
+ t.same(output, expect);
+ t.end()
+ })
+
+ setTimeout(function () {
+ child.stdin.write('\n')
+ }, 100)
+ setTimeout(function () {
+ child.stdin.write('55\n')
+ }, 150)
+ setTimeout(function () {
+ child.stdin.write('async prompt\n')
+ }, 200)
+})
+
+function child () {
+ var ctx = { tmpdir : '/tmp' }
+ var file = __dirname + '/fn.input';
+ promzard(file, ctx, function (err, output) {
+ console.error(JSON.stringify(output))
+ })
+}

0 comments on commit 18e4fa8

Please sign in to comment.