Like Unix Expect: Automate Command Line Programs in Node.js with 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 would you do this? Maybe you want to automate a ssh session? Or, maybe you want to test the external interface of on of your Node.js command line scripts.

Install: npm install suppose

Example:

process . chdir ( '/tmp/awesome' ); suppose ( 'npm' , [ 'init' ]) . on ( 'name: (awesome) ' ). respond ( 'awesome_package

' ) . on ( 'version: (0.0.0) ' ). respond ( '0.0.1

' ) . on ( 'description: ' ). respond ( "It's an awesome package man!

" ) . on ( 'entry point: (index.js) ' ). respond ( "

" ) . on ( 'test command: ' ). respond ( 'npm test

' ) . on ( 'git repository: ' ). respond ( "

" ) . on ( 'keywords: ' ). respond ( 'awesome, cool

' ) . on ( 'author: ' ). respond ( 'JP Richardson

' ) . on ( 'license: (BSD) ' ). respond ( 'MIT

' ) . on ( 'ok? (yes) ' ). respond ( 'yes

' ) . end ( function ( code ){ assert ( code === 0 ); var packageFile = '/tmp/awesome/package.json' ; fs . readFile ( packageFile , function ( err , data ){ var packageObj = JSON . parse ( data . toString ()); assert ( packageObj . name === 'awesome_package' ); assert ( packageObj . version === '0.0.1' ); assert ( packageObj . description === "It's an awesome package man!" ); assert ( packageObj . main === 'index.js' ); assert ( packageObj . scripts . test === 'npm test' ); assert ( packageObj . keywords [ 0 ] === 'awesome' ); assert ( packageObj . keywords [ 1 ] === 'cool' ); assert ( packageObj . author === 'JP Richardson' ); assert ( packageObj . license === 'MIT' ); done (); }); });

Pretty easy, huh? You can grab the source on Github.

If you made it this far, you should follow me on Twitter.

-JP

Want to test-drive Bitcoin without any risk? Check out my bitcoin wallet Coinbolt. It includes test coins for free.

Please enable JavaScript to view the comments powered by Disqus.

Disqus