{"name":"coffee","version":"3.3.0","description":"Test command line on nodejs","main":"index.js","dependencies":{"debug":"~2.2.0","childprocess":"~2.0.0"},"devDependencies":{"autod":"2","eslint":"2","eslint-config-egg":"^2.0.0","istanbul":"0","mm":"^1.3.5","mocha":"2","should":"6","spy":"^0.1.3"},"repository":{"type":"git","url":"git://github.com/popomore/coffee.git"},"homepage":"https://github.com/popomore/coffee","author":{"name":"popomore","email":"sakura9515@gmail.com"},"keywords":["test","shell","spawn","fork","child_process","exec"],"license":"MIT","scripts":{"lint":"eslint index.js lib test","test":"mocha -R spec -t 80000","cover":"rm -rf coverage && istanbul cover --report none --print none node_modules/mocha/bin/_mocha -- -R spec -t 80000","cov":"npm run clean && npm run lint && npm run cover && istanbul report --root coverage text-summary json lcov","autod":"autod -e test/fixtures -f ~ -w","clean":"rm -rf coverage"},"files":["index.js","lib"],"gitHead":"2c947901cf75f4360fc582c0ea34ebc99617adf5","bugs":{"url":"https://github.com/popomore/coffee/issues"},"_id":"coffee@3.3.0","_shasum":"3cd8ec513427b4b301573caa0640c031172ae4f6","_from":".","_npmVersion":"3.10.3","_nodeVersion":"6.7.0","_npmUser":{"name":"popomore","email":"sakura9515@gmail.com"},"dist":{"shasum":"3cd8ec513427b4b301573caa0640c031172ae4f6","tarball":"http://registry-npm.ywwl.com/coffee/download/coffee-3.3.0.tgz","integrity":"sha512-TTGuTE3NYJXeOqj5Q+Ji88PvFhIPWfUlEju9n8l/KdLl5jUHcb2RB9fzm0clcHWHJ6UJhAwvkFeNM5Bx4Jmq0w==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQDOWpW4bvcVVXlMQU3nQydchURHGs8vDbrWNi+E8KUDDwIgRnXu46MaaA7b4uLLMCMJ7YGCeOQF5nIPNezpCDlDgnM="}],"size":5788,"noattachment":false,"key":"/coffee/-/coffee-3.3.0.tgz"},"maintainers":[{"name":"fengmk2","email":"fengmk2@gmail.com"},{"name":"popomore","email":"sakura9515@gmail.com"}],"_npmOperationalInternal":{"host":"packages-16-east.internal.npmjs.com","tmp":"tmp/coffee-3.3.0.tgz_1476396406222_0.984143900219351"},"directories":{},"readme":"# Coffee\n\nTest command line on Node.js.\n\n---\n\n[![NPM version](https://img.shields.io/npm/v/coffee.svg?style=flat)](https://npmjs.org/package/coffee)\n[![Build Status](https://img.shields.io/travis/node-modules/coffee.svg?style=flat)](https://travis-ci.org/node-modules/coffee)\n[![codecov.io](https://img.shields.io/codecov/c/github/node-modules/coffee.svg?style=flat)](http://codecov.io/github/node-modules/coffee?branch=master)\n[![NPM downloads](http://img.shields.io/npm/dm/coffee.svg?style=flat)](https://npmjs.org/package/coffee)\n\n## Install\n\n```bash\n$ npm i coffee --save-dev\n```\n\n## Usage\n\nCoffee is useful for test command line in test frammework (like Mocha).\n\n### Fork\n\nYou can use `fork` for spawning Node processes.\n\n```js\nconst coffee = require('coffee');\n\ndescribe('cli', () => {\n  it('should fork node cli', () => {\n    return coffee.fork('/path/to/file.js')\n    .expect('stdout', '12\\n')\n    .expect('stderr', /34/)\n    .expect('code', 0)\n    .end();\n  });\n});\n```\n\nIn file.js\n\n```js\nconsole.log(12);\nconsole.error(34);\n```\n\nYou can pass `args` and `opts` to [child_process fork](https://nodejs.org/api/child_process.html#child_process_child_process_fork_modulepath_args_options).\n\n```js\ncoffee.fork('/path/to/file.js', [ 'args' ], { execArgv: [ '--inspect' ]})\n  .expect('stdout', '12\\n')\n  .expect('stderr', '34\\n')\n  .expect('code', 0)\n  .end();\n```\n\nAnd more:\n\n```js\ncoffee.fork('/path/to/file.js')\n  // print origin stdio\n  .debug()\n\n  // inject a script\n  .beforeScript(mockScript)\n\n  // interact with prompt\n  .waitForPrompt()\n  .write('tz\\n')\n\n  // string strict equals\n  .expect('stdout', 'abcdefg')\n  // regex\n  .expect('stdout', /^abc/)\n  // multiple\n  .expect('stdout', [ 'abcdefg', /abc/ ])\n  .expect('code', 0)\n  .end();\n```\n\nsee the API chapter below for more details.\n\n### Spawn\n\nYou can also use `spawn` for spawning normal shell scripts.\n\n```js\ncoffee.spawn('cat')\n  .write('1')\n  .write('2')\n  .expect('stdout', '12')\n  .expect('code', 0)\n  .end();\n```\n\n## Rule\n\n### code\n\nCheck the exit code.\n\n```js\ncoffee.fork('/path/to/file.js', [ 'args' ])\n  .expect('code', 0)\n  // .expect('code', 1)\n  .end();\n```\n\n### stdout / stderr\n\nCheck the stdout and stderr.\n\n```js\ncoffee.fork('/path/to/file.js', [ 'args' ])\n  .expect('stdout', '12\\n')\n  .expect('stderr', '34\\n')\n  .expect('code', 0)\n  .end();\n```\n\n### custom\n\nSupport custom rules, see `test/fixtures/extendable` for more details.\n\n```js\nconst { Coffee, Rule } = require('coffee');\n\nclass FileRule extends Rule {\n  constructor(opts) {\n    super(opts);\n    // `args` is which pass to `expect(type, ...args)`, `expected` is the first args.\n    const { args, expected } = opts;\n  }\n\n  assert(actual, expected, message) {\n    // do sth\n    return super.assert(fs.existsSync(expected), true, `should exists file ${expected}`);\n  }\n}\n\nclass MyCoffee extends Coffee {\n  constructor(...args) {\n    super(...args);\n    this.setRule('file', FileRule);\n  }\n\n  static fork(modulePath, args, opt) {\n    return new MyCoffee({\n      method: 'fork',\n      cmd: modulePath,\n      args,\n      opt,\n    });\n  }\n}\n```\n\nUsage:\n\n```js\n// test/custom.test.js\nconst coffee = require('MyCoffee');\ncoffee.fork('/path/to/file.js', [ 'args' ])\n  .expect('file', `${root}/README.md`);\n  .notExpect('file', `${root}/not-exist`);\n```\n\n## Support multiple process coverage with nyc\n\nRecommend to use [nyc] for coverage, you can use [any test frammework supported by nyc](https://istanbul.js.org/docs/tutorials/).\n\n## API\n\n### coffee.spawn\n\nRun command using `child_process.spawn`, then return `Coffee` instance.\n\nArguments see [child_process.spawn](http://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options)\n\n### coffee.fork\n\nRun command using `child_process.fork`, then return `Coffee` instance.\n\nArguments see [child_process.fork](http://nodejs.org/api/child_process.html#child_process_child_process_fork_modulepath_args_options)\n\n### coffee.Coffee\n\nAssertion object\n\n#### coffee.expect(type, ...args)\n\nAssert type with expected value, expected value can be string, regular expression, and array.\n\n```js\ncoffee.spawn('echo', [ 'abcdefg' ])\n  .expect('stdout', 'abcdefg')\n  .expect('stdout', /^abc/)\n  .expect('stdout', [ 'abcdefg', /abc/ ])\n  .end();\n```\n\nAccept type: `stdout` / `stderr` / `code` / `error`, see built-in rules description above.\n\n#### coffee.notExpect(type, ...args)\n\nThe opposite assertion of `expect`.\n\n#### coffee.includes(type, ...args)\n\nAssert type with expected string value, expected value should be string only.\n\n```js\ncoffee.spawn('echo', [ 'abcdefg' ])\n  .includes('stdout', 'abc')\n  .expect('stdout', [ 'abc', 'efg' ])\n  .end();\n```\n\nAccept type: `stdout` / `stderr`, see built-in rules description above.\n\n#### coffee.notIncludes(type, ...args)\n\nThe opposite assertion of `includes`.\n\n#### coffee.write(data)\n\nWrite data to stdin.\n\n```js\ncoffee.fork(path.join(fixtures, 'stdin.js'))\n  .write('1\\n')\n  .write('2')\n  .expect('stdout', '1\\n2')\n  .end();\n```\n\n#### coffee.writeKey(...args)\n\nWrite special key sequence to stdin, support `UP` / `DOWN` / `LEFT` / `RIGHT` / `ENTER` / `SPACE`.\n\nAll args will join as one key.\n\n```js\ncoffee.fork(path.join(fixtures, 'stdin.js'))\n  .writeKey('1', 'ENTER', '2')\n  .expect('stdout', '1\\n2')\n  .end();\n```\n\n#### coffee.waitForPrompt(bool)\n\nIf you set false, coffee will write stdin immediately, otherwise will wait for `prompt` message.\n\n```js\ncoffee.fork('/path/to/cli', [ 'abcdefg' ])\n  .waitForPrompt()\n  .write('tz\\n')\n  // choose the second item\n  .writeKey('DOWN', 'DOWN', 'ENTER');\n  .end(done);\n```\n\n**cli process should emit `prompt` message:**\n\n> Or use `coffee.on('stdout', callback)` instead, see docs below.\n\n```js\nconst readline = require('readline');\n\nconst rl = readline.createInterface({\n  input: process.stdin,\n  output: process.stdout\n});\n\nfunction ask(q, callback) {\n  process.send({ type: 'prompt' });\n  rl.question(q, callback);\n}\n\nask('What\\'s your name? ', answer => {\n  console.log(`hi, ${answer}`);\n  ask('How many coffee do you want? ', answer => {\n    console.log(`here is your ${answer} coffee`);\n    rl.close();\n  });\n});\n```\n\n#### coffee.end([callback])\n\nCallback will be called after completing the assertion, the first argument is Error if throw exception.\n\n```js\ncoffee.fork('path/to/cli')\n  .expect('stdout', 'abcdefg')\n  .end(done);\n\n// recommended to left undefind and use promise style.\nconst { stdout, stderr, code } = await coffee.fork('path/to/cli').end();\nassert(stdout.includes(abcdefg));\n```\n\n#### coffee.on(event, callback)\n\nEmit `stdout/stderr` event.\n\nuse for kill long-run process:\n\n```js\ncoffee.fork('path/to/cli')\n  .on('stdout', (buf, { proc }) => {\n    if (buf.includes('egg-ready')) {\n      proc.exitCode = 0;\n      proc.kill();\n    }\n  })\n  .expect('stdout', 'egg-ready')\n  .end(done);\n```\n\nuse for prompt:\n\n```js\n// do not call `waitForPrompt` / `write` / `writeKey`\ncoffee.fork('path/to/cli')\n  .on('stdout', (buf, { proc }) => {\n    if (buf.includes('Your Name: ')) {\n      proc.stdin.write('TZ\\n');\n    }\n  })\n  .expect('stdout', 'Your Name: TZ\\n')\n  .end(done);\n```\n\n#### coffee.debug(level)\n\nWrite data to process.stdout and process.stderr for debug\n\n`level` can be\n\n- 0 (default): pipe stdout + stderr\n- 1: pipe stdout\n- 2: pipe stderr\n- false: disable\n\nAlternative you can use `COFFEE_DEBUG` env.\n\n#### coffee.coverage()\n\nIf you set false, coffee will not generate coverage.json, default: true.\n\n#### coffee.beforeScript(scriptFile)\n\nAdd a hook script before fork child process run.\n\n### coffee.Rule\n\nAssertion Rule base class.\n\n## LICENSE\n\nCopyright (c) 2017 - 2019 node-modules. Licensed under the MIT license.\n\n[nyc]: https://github.com/istanbuljs/nyc\n","publish_time":1476396407279,"_cnpm_publish_time":1476396407279,"dist-tags":{"latest":"5.5.1","latest-3":"3.3.3","latest-4":"4.2.1"}}