nodejitsu / nexpect

spawn and control child processes in node.js with ease
http://github.com/nodejitsu/nexpect
Other
290 stars 37 forks source link

nexpect

nexpect is a node.js module for spawning child applications (such as ssh) and seamlessly controlling them using javascript callbacks. nexpect is based on the ideas of the expect library by Don Libes and the pexpect library by Noah Spurrier.

Motivation

node.js has good built in control for spawning child processes. nexpect builds on these core methods and allows developers to easily pipe data to child processes and assert the expected response. nexpect also chains, so you can compose complex terminal interactions.

Installation

  $ npm install --save nexpect

Usage

require('nexpect')

The module exposes a single function, .spawn.

function spawn (command, [params], [options])

Top-level entry point for nexpect that liberally parses the arguments and then returns a new chain with the specified command, params, and options.

function expect (expectation)

Expect that the next line of output matches the expectation. Throw an error if it does not.

The expectation can be a string (the line should contain the expected value as a substring) or a RegExp (the line should match the expression).

function wait (expectation, callback)

Wait for a line of output that matches the expectation, discarding lines that do not match.

Throw an error if no such line was found.

The expectation can be a string (the line should contain the expected value as a substring) or a RegExp (the line should match the expression).

The callback will be called for every line that matches the expectation.

function sendline (line)

Adds a write line to context.process.stdin to the context.queue for the current chain.

function sendEof ()

Close child's stdin stream, let the child know there are no more data coming.

This is useful for testing apps that are using inquirer, as inquirer.prompt() calls stdin.resume() at some point, which causes the app to block on input when the input stream is a pipe.

function run (callback)

Runs the context against the specified context.command and context.params.

Example

Lets take a look at some sample usage:

  var nexpect = require('nexpect');

  nexpect.spawn("echo", ["hello"])
         .expect("hello")
         .run(function (err, stdout, exitcode) {
           if (!err) {
             console.log("hello was echoed");
           }
         });

  nexpect.spawn("ls -la /tmp/undefined", { stream: 'stderr' })
         .expect("No such file or directory")
         .run(function (err) {
           if (!err) {
             console.log("checked that file doesn't exists");
           }
         });

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

If you are looking for more examples take a look at the examples, and tests.

Tests

All tests are written with vows:

  $ npm test

Authors

Elijah Insua Marak Squires, and Charlie Robbins.