Permalink
Browse files

handle default-assignment properly in non-tty case

  • Loading branch information...
1 parent 8b41843 commit 6d960cf87b0018024b33dfa5e3229dcc8741f908 @isaacs isaacs committed Jul 24, 2012
Showing with 62 additions and 13 deletions.
  1. +9 −13 lib/read.js
  2. +53 −0 test/defaults.js
View
@@ -33,19 +33,15 @@ function read (opts, cb) {
}
}
+ output.unmute()
+ rl.setPrompt(prompt)
+ rl.prompt()
if (silent) {
- output.unmute()
- rl.setPrompt(prompt)
- rl.prompt()
output.mute()
- } else {
- rl.setPrompt(prompt)
- rl.prompt()
- if (editDef) {
- rl.line = def
- rl.cursor = def.length
- rl._refreshLine()
- }
+ } else if (editDef) {
+ rl.line = def
+ rl.cursor = def.length
+ rl._refreshLine()
}
var called = false
@@ -84,13 +80,13 @@ function read (opts, cb) {
output.write('\r\n')
}
done()
+ // truncate the \n at the end.
+ line = line.replace(/\r?\n$/, '')
var isDefault = !!(editDef && line === def)
if (def && !line) {
isDefault = true
line = def
}
- // truncate the \n at the end.
- line = line.replace(/\r?\n$/, '')
cb(null, line, isDefault)
}
}
View
@@ -0,0 +1,53 @@
+var tap = require('tap')
+var read = require('../lib/read.js')
+
+if (process.argv[2] === 'child') {
+ return child()
+}
+
+var spawn = require('child_process').spawn
+
+tap.test('defaults', function (t) {
+ var child = spawn(process.execPath, [__filename, 'child'])
+ var output = ''
+ var write = child.stdin.write.bind(child.stdin)
+ child.stdout.on('data', function (c) {
+ console.error('data %s', c)
+ output += c
+ if (output.match(/Username: \(test-user\) $/)) {
+ process.nextTick(write.bind(null, '\n'))
+ } else if (output.match(/Password: \(<default hidden>\) $/)) {
+ process.nextTick(write.bind(null, '\n'))
+ } else if (output.match(/Password again: \(<default hidden>\) $/)) {
+ process.nextTick(write.bind(null, '\n'))
+ } else {
+ console.error('prompts done, output=%j', output)
+ }
+ })
+
+ var result = ''
+ child.stderr.on('data', function (c) {
+ result += c
+ console.error('result %j', c.toString())
+ })
+
+ child.on('close', function () {
+ result = JSON.parse(result)
+ t.same(result, {"user":"test-user","pass":"test-pass","verify":"test-pass","passMatch":true})
+ t.equal(output, 'Username: (test-user) Password: (<default hidden>) Password again: (<default hidden>) ')
+ t.end()
+ })
+})
+
+function child () {
+ read({prompt: "Username: ", default: "test-user" }, function (er, user) {
+ read({prompt: "Password: ", default: "test-pass", silent: true }, function (er, pass) {
+ read({prompt: "Password again: ", default: "test-pass", silent: true }, function (er, pass2) {
+ console.error(JSON.stringify({user: user,
+ pass: pass,
+ verify: pass2,
+ passMatch: (pass === pass2)}))
+ })
+ })
+ })
+}

0 comments on commit 6d960cf

Please sign in to comment.