/
nexpect-test.js
100 lines (96 loc) · 3.21 KB
/
nexpect-test.js
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
* nexpect-test.js: Tests for the `nexpect` module.
*
* (C) 2011, Elijah Insua, Marak Squires, Charlie Robbins.
*
*/
var assert = require('assert'),
path = require('path'),
vows = require('vows'),
spawn = require('child_process').spawn,
nexpect = require('../lib/nexpect');
function assertSpawn (expect) {
return {
topic: function () {
expect.run(this.callback)
},
"should respond with no error": function (err, stdout) {
assert.isTrue(!err);
assert.isArray(stdout);
}
}
}
function assertError (expect) {
return {
topic: function () {
expect.run(this.callback.bind(this, null))
},
"should respond with no error": function (_, err) {
assert.isObject(err);
}
}
}
vows.describe('nexpect').addBatch({
"When using the nexpect module": {
"it should have the correct methods defined": function () {
assert.isFunction(nexpect.spawn);
assert.isObject(nexpect.nspawn);
assert.isFunction(nexpect.nspawn.spawn);
},
"spawning": {
"`echo hello`": assertSpawn(
nexpect.spawn("echo", ["hello"])
.expect("hello")
),
"`ls -l /tmp/undefined`": assertSpawn(
nexpect.spawn("ls -la /tmp/undefined", { stream: 'stderr' })
.expect("No such file or directory")
),
"a command that does not exist": assertError(
nexpect.spawn("idontexist")
.expect("This will never work")
),
"and using the sendline() method": assertSpawn(
nexpect.spawn("node")
.expect(">")
.sendline("console.log('testing')")
.expect("testing")
.sendline("process.exit()")
),
"and using the wait() method": {
"when assertions are met": assertSpawn(
nexpect.spawn(path.join(__dirname, 'fixtures', 'prompt-and-respond'))
.wait('first')
.sendline('first-prompt')
.expect('first-prompt')
.wait('second')
.sendline('second-prompt')
.expect('second-prompt')
),
"when the last assertion is never met": assertError(
nexpect.spawn(path.join(__dirname, 'fixtures', 'prompt-and-respond'))
.wait('first')
.sendline('first-prompt')
.expect('first-prompt')
.wait('second')
.sendline('second-prompt')
.wait('this-never-shows-up')
)
},
"when options.stripColors is set": assertSpawn(
nexpect.spawn(path.join(__dirname, 'fixtures', 'log-colors'), { stripColors: true })
.wait('second has colors')
.expect('third has colors')
),
"when options.ignoreCase is set": assertSpawn(
nexpect.spawn(path.join(__dirname, 'fixtures', 'multiple-cases'), { ignoreCase: true })
.wait('this has many cases')
.expect('this also has many cases')
),
"when options.cwd is set": assertSpawn(
nexpect.spawn(path.join(__dirname, 'fixtures', 'show-cwd'), { cwd: path.join(__dirname, 'fixtures') })
.wait(path.join(__dirname, 'fixtures'))
)
}
}
}).export(module);