包详细信息

suppose

jprichardson3.5kMIT0.6.2

Automate command line programs. Like UNIX expect.

test, testing, automate, expect

自述文件

build status

Node.js - suppose

Have you ever heard of the command line program expect? Basically, expect allows you to automate command line programs. suppose is a programmable Node.js module that allows the same behavior.

Why?

From the expect wikipedia page, you can see many examples of expect scripts automating tasks such as telnet or ftp sessions. Now you can easily write Node.js scripts to do the same. This may be most beneficial during testing.

Installation

npm install suppose

Example

Automate the command npm init, which initializes a new npm module.

var suppose = require('suppose')
  , fs = require('fs')
  , assert = require('assert')

process.chdir('/tmp/awesome');
fs.writeFileSync('/tmp/awesome/README.md', 'READ IT')
// debug is an optional writeable output stream
suppose('npm', ['init'], {debug: fs.createWriteStream('/tmp/debug.txt')})
  .when(/name\: \([\w|\-]+\)[\s]*/).respond('awesome_package\n')
  .when('version: (1.0.0) ').respond('0.0.1\n')
  // response can also be the second argument to .when
  .when('description: ', "It's an awesome package man!\n")
  .when('entry point: (index.js) ').respond("\n")
  .when('test command: ').respond('npm test\n')
  .when('git repository: ').respond("\n")
  .when('keywords: ').respond('awesome, cool\n')
  .when('author: ').respond('JP Richardson\n')
  .when('license: (ISC) ').respond('MIT\n')
  .when('ok? (yes) ' ).respond('yes\n')
.on('error', function(err){
  console.log(err.message);
})
.end(function(code){
  var packageFile = '/tmp/awesome/package.json';
  fs.readFile(packageFile, function(err, data){
    var packageObj = JSON.parse(data.toString());
    console.log(packageObj.name); //'awesome_package'
  })
})

.respond() may be called any number of times after .when(). Each response will be sent in the order defined when the condition matches. Once all conditions and responses are defined, call .end() to begin execution.

Contributors

License

(MIT License)

Copyright 2012-2013, JP Richardson

更新日志

0.4.0 / 2016-01-11

  • Breaking suppose() arguments have changed. It can be called as suppose(command[, args][, options]) to suppose against process input/output or suppose([options]) to suppose as a stream that receives input and outputs responses.
  • Breaking Use .when() instead of .on() for expectations.
  • Breaking Use .on('error') instead of .error() for error handling.
  • Breaking Use debug option instead of .debug() to provide a stream which receives debugging output.

0.3.1 / 2015-11-05

  • Support callback function as response (piranna / #8)
  • Support response as optional argument to .on()

0.3.0 / 2013-11-07

  • .end() now returns internal process object. (pstaender / #5)

0.2.0 / 2013-05-01

  • fixed but to make Node v0.10 compatible
  • dropped Node v0.6 suppport

0.1.0 / 2013-01-22

  • Updated deps.
  • Changed identing from 4 spaces to 2.
  • Updated tests for modern npm.

0.0.7 / 2012-09-06

  • Added Travis-CI support.

0.0.6 / 2012-08-09

  • Added error method.

0.0.5 / 2012-08-08

  • debug method now logs the command at the top of write stream.

0.0.4 / 2012-08-07

  • Added debug method to output stdio to writeable stream.

0.0.3 / 2012-08-06

  • Supports matching regular expressions now.

0.0.2 / 2012-08-03

  • Fixed self/this class level variable bug.

0.0.1 / 2012-08-03

  • Initial release.