Skip to content

Commit

Permalink
Updating nexpect to emit events. Fixes nodejitsu#14
Browse files Browse the repository at this point in the history
This allows nexpect to work with conditional branching. It will
emit 'wait' right now, and the data will be the line of output
which matches the expected output. From there, you can re-test the data
and call sendline, expect, or wait again. Note that the user MUST
call wait BEFORE the end of the stackframe on which the child was spawned.
  • Loading branch information
gcochard committed May 4, 2015
1 parent 9d279db commit 50b74bd
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,25 @@ Lets take a look at some sample usage:
console.log(err)
}
});

emitter = nexpect.spawn("node --interactive")
.run(function (err) {
if (!err) {
console.log("node process started, console logged, process exited");
}
else {
console.log(err)
}
});
emitter.expect(">");
emitter.on('wait',function(data){
if(data === '>'){
emitter.sendline("console.log('testing')")
.expect("testing")
} else if(data === 'testing') {
emitter.sendline("process.exit()")
}
});
```

If you are looking for more examples take a look at the [examples][2], and [tests][3].
Expand Down
16 changes: 15 additions & 1 deletion lib/nexpect.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
var spawn = require('child_process').spawn;
var util = require('util');
var AssertionError = require('assert').AssertionError;
var EventEmitter = require('events').EventEmitter;

function chain (context) {
return {
Expand Down Expand Up @@ -36,6 +37,9 @@ function chain (context) {
context.queue.push(_wait);
return chain(context);
},
on: function() {
context.on.apply(this, arguments);
},
sendline: function (line) {
var _sendline = function _sendline () {
context.process.stdin.write(line + '\n');
Expand Down Expand Up @@ -146,6 +150,7 @@ function chain (context) {
// If this is an `_expect` function, then evaluate it and attempt
// to evaluate the next function (in case it is a `_sendline` function).
//
context.emit('expect');
return currentFn(data) === true ?
evalContext(data, '_expect') :
onError(createExpectationError(currentFn.expectation, data), true);
Expand All @@ -156,6 +161,7 @@ function chain (context) {
// then evaluate the function (in case it is a `_sendline` function).
//
if (currentFn(data) === true) {
context.emit('wait',data);
context.queue.shift();
evalContext(data, '_expect');
}
Expand Down Expand Up @@ -295,7 +301,7 @@ function chain (context) {
callback(null, stdout, signal || code);
});

return context.process;
return context;
}
};
}
Expand Down Expand Up @@ -366,6 +372,14 @@ function nspawn (command, params, options) {
stripColors: options.stripColors,
verbose: options.verbose
};
_emitter = new EventEmitter();
context._emitter = _emitter;
context.on = function(){
_emitter.on.apply(_emitter,arguments);
};
context.emit = function(){
_emitter.emit.apply(_emitter,arguments);
};

return chain(context);
}
Expand Down
17 changes: 17 additions & 0 deletions test/nexpect-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,23 @@ vows.describe('nexpect').addBatch({
.expect("testing")
.sendline("process.exit()")
),
"and using the event driven method": {
"should respond with no error": function () {
child = nexpect.spawn('node', ['--interactive']);
child.run(function(err,stdout,exitcode){
assert.isTrue(!err);
assert.isArray(stdout);
});
child.on('wait',function(data){
if(data === '>'){
child.sendline('console.log("testing")').wait('testing');
} else if (data === 'testing'){
child.sendline('process.exit()');
}
});
child.wait('>');
}
},
"and using the expect() method": {
"when RegExp expectation is met": assertSpawn(
nexpect.spawn("echo", ["hello"])
Expand Down

0 comments on commit 50b74bd

Please sign in to comment.