Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating nexpect to emit events. #19

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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){
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This child.run(...) should probably be after the child.on(...) and child.wait(...), but it will work properly in either case as long as they are in the same stack frame.

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