Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

handle default-assignment properly in non-tty case

  • Loading branch information...
commit 6d960cf87b0018024b33dfa5e3229dcc8741f908 1 parent 8b41843
Isaac Z. Schlueter authored
22  lib/read.js
@@ -33,19 +33,15 @@ function read (opts, cb) {
33 33
     }
34 34
   }
35 35
 
  36
+  output.unmute()
  37
+  rl.setPrompt(prompt)
  38
+  rl.prompt()
36 39
   if (silent) {
37  
-    output.unmute()
38  
-    rl.setPrompt(prompt)
39  
-    rl.prompt()
40 40
     output.mute()
41  
-  } else {
42  
-    rl.setPrompt(prompt)
43  
-    rl.prompt()
44  
-    if (editDef) {
45  
-      rl.line = def
46  
-      rl.cursor = def.length
47  
-      rl._refreshLine()
48  
-    }
  41
+  } else if (editDef) {
  42
+    rl.line = def
  43
+    rl.cursor = def.length
  44
+    rl._refreshLine()
49 45
   }
50 46
 
51 47
   var called = false
@@ -84,13 +80,13 @@ function read (opts, cb) {
84 80
       output.write('\r\n')
85 81
     }
86 82
     done()
  83
+    // truncate the \n at the end.
  84
+    line = line.replace(/\r?\n$/, '')
87 85
     var isDefault = !!(editDef && line === def)
88 86
     if (def && !line) {
89 87
       isDefault = true
90 88
       line = def
91 89
     }
92  
-    // truncate the \n at the end.
93  
-    line = line.replace(/\r?\n$/, '')
94 90
     cb(null, line, isDefault)
95 91
   }
96 92
 }
53  test/defaults.js
... ...
@@ -0,0 +1,53 @@
  1
+var tap = require('tap')
  2
+var read = require('../lib/read.js')
  3
+
  4
+if (process.argv[2] === 'child') {
  5
+  return child()
  6
+}
  7
+
  8
+var spawn = require('child_process').spawn
  9
+
  10
+tap.test('defaults', function (t) {
  11
+  var child = spawn(process.execPath, [__filename, 'child'])
  12
+  var output = ''
  13
+  var write = child.stdin.write.bind(child.stdin)
  14
+  child.stdout.on('data', function (c) {
  15
+    console.error('data %s', c)
  16
+    output += c
  17
+    if (output.match(/Username: \(test-user\) $/)) {
  18
+      process.nextTick(write.bind(null, '\n'))
  19
+    } else if (output.match(/Password: \(<default hidden>\) $/)) {
  20
+      process.nextTick(write.bind(null, '\n'))
  21
+    } else if (output.match(/Password again: \(<default hidden>\) $/)) {
  22
+      process.nextTick(write.bind(null, '\n'))
  23
+    } else {
  24
+      console.error('prompts done, output=%j', output)
  25
+    }
  26
+  })
  27
+
  28
+  var result = ''
  29
+  child.stderr.on('data', function (c) {
  30
+    result += c
  31
+    console.error('result %j', c.toString())
  32
+  })
  33
+
  34
+  child.on('close', function () {
  35
+    result = JSON.parse(result)
  36
+    t.same(result, {"user":"test-user","pass":"test-pass","verify":"test-pass","passMatch":true})
  37
+    t.equal(output, 'Username: (test-user) Password: (<default hidden>) Password again: (<default hidden>) ')
  38
+    t.end()
  39
+  })
  40
+})
  41
+
  42
+function child () {
  43
+  read({prompt: "Username: ", default: "test-user" }, function (er, user) {
  44
+    read({prompt: "Password: ", default: "test-pass", silent: true }, function (er, pass) {
  45
+      read({prompt: "Password again: ", default: "test-pass", silent: true }, function (er, pass2) {
  46
+        console.error(JSON.stringify({user: user,
  47
+                       pass: pass,
  48
+                       verify: pass2,
  49
+                       passMatch: (pass === pass2)}))
  50
+      })
  51
+    })
  52
+  })
  53
+}

0 notes on commit 6d960cf

Please sign in to comment.
Something went wrong with that request. Please try again.