summaryrefslogtreecommitdiff
path: root/deps/npm/node_modules/read/test/defaults.js
blob: e3d2ac71061ab08ff88a2a9d309aad7a89fa11b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
var tap = require('tap')
var read = require('../lib/read.js')

if (process.argv[2] === 'child') {
  return child()
}

var CLOSE = 'close'
if (process.version.match(/^v0\.6/)) {
  CLOSE = 'exit'
}

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)}))
        if (process.stdin.unref)
          process.stdin.unref()
      })
    })
  })
}