├── .gitignore ├── .npmignore ├── .travis.yml ├── LICENSE ├── README.md ├── index.d.ts ├── lib ├── index.js └── mock │ └── stdin.js ├── package.json ├── test └── stdin.spec.js └── tools └── travis.sh /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules 2 | /lib-cov 3 | *~ 4 | .DS_Store 5 | *.swp 6 | *.bak 7 | /npm-debug.log 8 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | /test /lib-cov /npm-debug.log -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - lts/* 4 | - node 5 | 6 | install: 7 | - npm install 8 | - npm install coveralls jscoverage mocha-lcov-reporter 9 | 10 | script: 11 | tools/travis.sh 12 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Caitlin Potter & Contributors 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # node-mock-stdin 2 | 3 | [![Build Status](https://travis-ci.org/caitp/node-mock-stdin.svg?branch=master)](https://travis-ci.org/caitp/node-mock-stdin) [![Coverage Status](https://img.shields.io/coveralls/caitp/node-mock-stdin.svg)](https://coveralls.io/r/caitp/node-mock-stdin?branch=master) [![NPM Version](http://img.shields.io/npm/v/mock-stdin.svg)](https://www.npmjs.org/package/mock-stdin) 4 | 5 | Provide a mock readable stream, useful for testing interactive CLI applications. 6 | 7 | Maybe simple mocks for other standard files wouldn't be a terrible idea, if anyone 8 | feels like those are needed. Patches welcome. 9 | 10 | ## API 11 | 12 | - **Module** 13 | - [stdin()](#modulestdin) 14 | - **MockSTDIN** 15 | - [send()](#mockstdinsenddata-encoding) 16 | - [end()](#mockstdinend) 17 | - [restore()](#mockstdinrestore) 18 | - [reset()](#mockstdinresetremovelisteners) 19 | 20 | --- 21 | 22 | ### Module.stdin() 23 | 24 | **example** 25 | 26 | ```js 27 | require('mock-stdin').stdin(); 28 | ``` 29 | 30 | Replaces the existing `process.stdin` value with a mock object exposing a `send` method (a 31 | `MockSTDIN` instance). This allows APIs like `process.openStdin()` or `process.stdin.on()` 32 | to operate on a mock instance. 33 | 34 | **note**: Event listeners from the original `process.stdin` instance are not added to the 35 | mock instance. Installation of the mock should occur before any event listeners are 36 | registered. 37 | 38 | **return value**: A `MockSTDIN` instance 39 | 40 | --- 41 | 42 | ### MockSTDIN.send(data, encoding) 43 | 44 | **example** 45 | 46 | ```js 47 | var stdin = require('mock-stdin').stdin(); 48 | stdin.send("Some text", "ascii"); 49 | stdin.send(Buffer("Some text", "Some optional encoding")); 50 | stdin.send([ 51 | "Array of lines", 52 | " which are joined with a linefeed." 53 | ]); 54 | 55 | // sending a null will trigger EOF and dispatch an 'end' event. 56 | stdin.send(null); 57 | ``` 58 | 59 | Queue up data to be read by the stream. Results in data (and possibly end) events being 60 | dispatched. 61 | 62 | **parameters** 63 | - `data`: A `String`, `Buffer`, `Array`, or `null`. The `data` parameter will result in 64 | the default encoding if specified as a string or array of strings. 65 | - `encoding`: An optional encoding which is used when `data` is a `String`. 66 | Node.js's internal Readable Stream will convert the specified encoding into the output 67 | encoding, which is transcoded if necessary. 68 | 69 | **return value**: The `MockSTDIN` instance, for chaining. 70 | 71 | --- 72 | 73 | ### MockSTDIN.end() 74 | 75 | **example** 76 | 77 | ```js 78 | var stdin = require('mock-stdin').stdin(); 79 | stdin.end(); 80 | ``` 81 | 82 | Alias for [MockSTDIN.send(null)](#mockstdinsend). Results in dispatching an `end` event. 83 | 84 | **return value**: The `MockSTDIN` instance, for chaining. 85 | 86 | --- 87 | 88 | ### MockSTDIN.restore() 89 | 90 | **example** 91 | 92 | ```js 93 | var stdin = require('mock-stdin').stdin(); 94 | // process.stdin is now a mock stream 95 | stdin.restore(); 96 | // process.stdin is returned to its original state 97 | ``` 98 | 99 | Restore the target of the mocked stream. If only a single mock stream is created, will restore 100 | the original `stdin` TTY stream. If multiple mock streams are created, it will restore the 101 | stream which was active at the time the mock was created. 102 | 103 | **return value**: The `MockSTDIN` instance, for chaining. 104 | 105 | --- 106 | 107 | ### MockSTDIN.reset(removeListeners) 108 | 109 | **example** 110 | 111 | ```js 112 | var stdin = require('mock-stdin').stdin(); 113 | stdin.end(); 114 | stdin.reset(); 115 | stdin.send("some data"); 116 | ``` 117 | 118 | Ordinarily, a Readable stream will throw when attempting to push after an EOF. This routine will 119 | reset the `ended` state of a Readable stream, preventing it from throwing post-EOF. This prevents 120 | being required to re-create a mock STDIN instance during certain tests where a fresh stdin is 121 | required. 122 | 123 | If the `removeListeners` flag is set to `true`, all event listeners will also be reset. This is 124 | useful in cases where you need to emulate restarting an entire application, without fully 125 | re-creating the mock object. 126 | 127 | **parameters** 128 | - `removeListeners`: Boolean value which, when set to `true`, will remove all event listeners 129 | attached to the stream. 130 | 131 | **return value**: The `MockSTDIN` instance, for chaining. 132 | 133 | --- 134 | 135 | ## [LICENSE](LICENSE) 136 | 137 | The MIT License (MIT) 138 | 139 | Copyright (c) 2014 Caitlin Potter & Contributors 140 | 141 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 142 | 143 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 144 | 145 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 146 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | // Type definitions for 'mock-stdin' 2 | // Project: mock-stdin 3 | // Definitions by: Ron S. http://github.com/nonara 4 | 5 | interface MockSTDIN { 6 | /** Queue up data to be read by the stream. Results in data (and possibly end) events being dispatched. */ 7 | send: (data: String | Buffer | string[] | null, encoding?: string) => MockSTDIN 8 | /** Alias for MockSTDIN.send(null). Results in dispatching an end event. */ 9 | end: () => MockSTDIN 10 | /** Restore the target of the mocked stream. If only a single mock stream is created, will restore the original stdin TTY stream. If multiple mock streams are created, it will restore the stream which was active at the time the mock was created. */ 11 | restore: () => MockSTDIN 12 | /** 13 | * Ordinarily, a Readable stream will throw when attempting to push after an EOF. This routine will reset the ended state of a Readable stream, preventing it from throwing post-EOF. This prevents being required to re-create a mock STDIN instance during certain tests where a fresh stdin is required. 14 | * @param removeListeners - When set to true, will remove all event listeners attached to the stream. 15 | */ 16 | reset: (removeListeners?: boolean) => MockSTDIN 17 | } 18 | 19 | /** 20 | * Replaces the existing process.stdin value with a mock object exposing a send method (a MockSTDIN instance). This allows APIs like process.openStdin() or process.stdin.on() to operate on a mock instance. 21 | * 22 | * - Note: Event listeners from the original process.stdin instance are not added to the mock instance. Installation of the mock should occur before any event listeners are registered. 23 | */ 24 | export function stdin(): MockSTDIN -------------------------------------------------------------------------------- /lib/index.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | stdin: require('./mock/stdin') 3 | }; 4 | -------------------------------------------------------------------------------- /lib/mock/stdin.js: -------------------------------------------------------------------------------- 1 | var stream = require('stream'); 2 | var inherits = require('util').inherits; 3 | 4 | function MockSTDIN(restoreTarget) { 5 | stream.Readable.call(this, { 6 | highWaterMark: 0, 7 | readable: true, 8 | writable: false 9 | }); 10 | Object.defineProperties(this, { 11 | target: { 12 | enumerable: false, 13 | writable: false, 14 | configurable: false, 15 | value: restoreTarget 16 | }, 17 | isMock: { 18 | enumerable: false, 19 | writable: false, 20 | configurable: false, 21 | value: true 22 | }, 23 | _mockData: { 24 | enumerable: false, 25 | writable: false, 26 | configurable: false, 27 | value: [] 28 | }, 29 | _flags: { 30 | enumerable: false, 31 | writable: false, 32 | configurable: false, 33 | value: { 34 | emittedData: false, 35 | lastChunk: null 36 | } 37 | } 38 | }); 39 | } 40 | 41 | inherits(MockSTDIN, stream.Readable); 42 | 43 | function MockData(chunk, encoding) { 44 | Object.defineProperties(this, { 45 | data: { 46 | value: chunk, 47 | writable: false, 48 | configurable: false, 49 | enumerable: false 50 | }, 51 | length: { 52 | get: function() { 53 | if (Buffer.isBuffer(chunk)) { 54 | return chunk.length; 55 | } else if (typeof chunk === 'string') { 56 | return chunk.length; 57 | } 58 | return 0; 59 | }, 60 | configurable: false, 61 | enumerable: false 62 | }, 63 | pos: { 64 | writable: true, 65 | value: 0, 66 | configurable: false, 67 | enumerable: false 68 | }, 69 | done: { 70 | writable: true, 71 | value: false, 72 | configurable: false, 73 | enumerable: false 74 | }, 75 | encoding: { 76 | writable: false, 77 | value: ((typeof encoding === "string") && encoding) || null, 78 | configurable: false, 79 | enumerable: false 80 | } 81 | }); 82 | } 83 | 84 | MockData.prototype.chunk = function(length) { 85 | if (this.pos <= this.length) { 86 | if (Buffer.isBuffer(this.data) || typeof this.data === 'string') { 87 | var value = this.data.slice(this.pos, this.pos + length); 88 | this.pos += length; 89 | if (this.pos >= this.length) { 90 | this.done = true; 91 | } 92 | return value; 93 | } 94 | } 95 | 96 | this.done = true; 97 | return null; 98 | } 99 | 100 | var Readable$emit = stream.Readable.prototype.emit; 101 | MockSTDIN.prototype.emit = function MockSTDINEmit(name) { 102 | if (name === 'data') { 103 | this._flags.emittedData = true; 104 | this._flags.lastChunk = null; 105 | } 106 | return Readable$emit.apply(this, arguments); 107 | }; 108 | 109 | MockSTDIN.prototype.send = function MockSTDINWrite(text, encoding) { 110 | if (Array.isArray(text)) { 111 | if (arguments.length > 1) { 112 | throw new TypeError("Cannot invoke MockSTDIN#send(): `encoding` " + 113 | "specified while text specified as an array."); 114 | } 115 | text = text.join('\n'); 116 | } 117 | if (Buffer.isBuffer(text) || typeof text === 'string' || text === null) { 118 | var data = new MockData(text, encoding); 119 | this._mockData.push(data); 120 | this._read(); 121 | if (!this._flags.emittedData && this._readableState.length) { 122 | drainData(this); 123 | } 124 | if (text === null) { 125 | // Trigger an end event synchronously... 126 | endReadable(this); 127 | } 128 | } 129 | return this; 130 | }; 131 | 132 | MockSTDIN.prototype.end = function MockSTDINEnd() { 133 | this.send(null); 134 | return this; 135 | }; 136 | 137 | MockSTDIN.prototype.restore = function MockSTDINRestore() { 138 | Object.defineProperty(process, 'stdin', { 139 | value: this.target, 140 | configurable: true, 141 | writable: false 142 | }); 143 | return this; 144 | }; 145 | 146 | MockSTDIN.prototype.reset = function MockSTDINReset(removeListeners) { 147 | var state = this._readableState; 148 | state.ended = false; 149 | state.endEmitted = false; 150 | if (removeListeners === true) { 151 | this.removeAllListeners(); 152 | } 153 | return this; 154 | }; 155 | 156 | MockSTDIN.prototype._read = function MockSTDINRead(size) { 157 | if (size === void 0) size = Infinity; 158 | var count = 0; 159 | var read = true; 160 | while (read && this._mockData.length && count < size) { 161 | var item = this._mockData[0]; 162 | var leftInChunk = item.length - item.pos; 163 | var remaining = size === Infinity ? leftInChunk : size - count; 164 | var encoding = item.encoding; 165 | var toProcess = Math.min(leftInChunk, remaining); 166 | var chunk = this._flags.lastChunk = item.chunk(toProcess); 167 | 168 | if (!(encoding === null ? this.push(chunk) : this.push(chunk, encoding))) { 169 | read = false; 170 | } 171 | 172 | if (item.done) { 173 | this._mockData.shift(); 174 | } 175 | 176 | count += toProcess; 177 | } 178 | }; 179 | 180 | MockSTDIN.prototype.setRawMode = function MockSTDINSetRawMode (bool) { 181 | if (typeof bool !== 'boolean') throw new TypeError('setRawMode only takes booleans'); 182 | return this; 183 | }; 184 | 185 | function endReadable(stream) { 186 | // Synchronously emit an end event, if possible. 187 | var state = stream._readableState; 188 | 189 | if (!state.length) { 190 | state.ended = true; 191 | state.endEmitted = true; 192 | stream.readable = false; 193 | stream.emit('end'); 194 | } 195 | } 196 | 197 | function drainData(stream) { 198 | var state = stream._readableState; 199 | var buffer = state.buffer; 200 | while (buffer.length) { 201 | var chunk = buffer.shift(); 202 | if (chunk !== null) { 203 | state.length -= chunk.length; 204 | stream.emit('data', chunk); 205 | stream._flags.emittedData = false; 206 | } 207 | } 208 | } 209 | 210 | function mock() { 211 | var mock = new MockSTDIN(process.stdin); 212 | Object.defineProperty(process, 'stdin', { 213 | value: mock, 214 | configurable: true, 215 | writable: false 216 | }); 217 | return mock; 218 | } 219 | 220 | mock.Class = MockSTDIN; 221 | 222 | module.exports = mock; 223 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "mock-stdin", 3 | "version": "1.0.0", 4 | "description": "Mock STDIN file descriptor in Node.js", 5 | "main": "./lib/index.js", 6 | "types": "./index.d.ts", 7 | "scripts": { 8 | "test": "mocha test/" 9 | }, 10 | "repository": { 11 | "type": "git", 12 | "url": "https://github.com/caitp/node-mock-stdin" 13 | }, 14 | "keywords": [ 15 | "test", 16 | "mock", 17 | "node", 18 | "fs", 19 | "stdin" 20 | ], 21 | "author": "Caitlin Potter ", 22 | "license": "MIT", 23 | "bugs": { 24 | "url": "https://github.com/caitp/node-mock-stdin/issues" 25 | }, 26 | "homepage": "https://github.com/caitp/node-mock-stdin", 27 | "devDependencies": { 28 | "mocha": "^6.2.2" 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /test/stdin.spec.js: -------------------------------------------------------------------------------- 1 | let mock = process.env.COVERAGE ? require("../lib-cov") : require("../lib"); 2 | let assert = require("assert"); 3 | 4 | describe("stdin", function() { 5 | let stdin; 6 | beforeEach(function() { 7 | stdin = mock.stdin(); 8 | }); 9 | afterEach(function() { 10 | process.stdin.restore(); 11 | stdin = void 0; 12 | }); 13 | 14 | it("process.stdin instanceof MockSTDIN", function() { 15 | assert(process.stdin instanceof mock.stdin.Class); 16 | }); 17 | 18 | it("MockSTDIN#openStdin() should not throw", function() { 19 | assert.doesNotThrow(function() { 20 | process.openStdin(); 21 | }); 22 | }); 23 | 24 | it("MockSTDIN#restore() should restore previous object", function() { 25 | process.stdin.restore(); 26 | assert(!(process.stdin instanceof mock.stdin.Class)); 27 | mock.stdin(); 28 | }); 29 | 30 | it("MockSTDIN#setEncoding() should not throw", function() { 31 | assert.doesNotThrow(function() { 32 | process.stdin.setEncoding("utf8"); 33 | }); 34 | }); 35 | 36 | it("MockSTDIN#send()", function() { 37 | let data = [ 38 | "To whom it may concern,", 39 | "", 40 | "I am a piece of mock data.", 41 | "", 42 | "Regards,", 43 | "Cortana" 44 | ]; 45 | let called = false; 46 | let received; 47 | let endCalled = false; 48 | let errors = []; 49 | process.stdin.setEncoding("utf8"); 50 | process.stdin.on("data", function(data) { 51 | called = true; 52 | received = data; 53 | }); 54 | process.stdin.on("error", function(error) { 55 | errors.push(error); 56 | }); 57 | process.stdin.on("end", function() { 58 | endCalled = true; 59 | }); 60 | process.stdin.resume(); 61 | stdin.send(data); 62 | assert(called, "'data' event was not received."); 63 | assert.equal(received, data.join("\n"), 64 | "received data should be array joined by linefeeds."); 65 | assert.deepEqual(errors, [], "'error' event should not be received."); 66 | assert(!endCalled, "'end' event should not be received."); 67 | }); 68 | 69 | it("MockSTDIN#send()", function() { 70 | let data = [ 71 | "To whom it may concern,", 72 | "", 73 | "I am a piece of mock data.", 74 | "", 75 | "Regards,", 76 | "Cortana" 77 | ].join("\n"); 78 | let received; 79 | let called = false; 80 | let errors = []; 81 | let endCalled = false; 82 | process.stdin.setEncoding("utf8"); 83 | process.stdin.on("data", function(data) { 84 | called = true; 85 | received = data; 86 | }); 87 | process.stdin.on("error", function(error) { 88 | errors.push(error); 89 | }); 90 | process.stdin.on("end", function() { 91 | endCalled = true; 92 | }); 93 | process.stdin.resume(); 94 | process.stdin.send(data); 95 | assert(called, "'data' event was not received."); 96 | assert.equal(received, data, "received data should match what was sent."); 97 | assert.deepEqual(errors, [], "'error' event should not be received."); 98 | assert(!endCalled, "'end' event should not be received."); 99 | }); 100 | 101 | it("MockSTDIN#send()", function () { 102 | let data = [ 103 | "To whom it may concern,", 104 | "", 105 | "I am a piece of mock data.", 106 | "", 107 | "Regards,", 108 | "Cortana" 109 | ].join("\n"); 110 | let received; 111 | let called = false; 112 | let errors = []; 113 | let endCalled = false; 114 | process.stdin.setEncoding("utf8"); 115 | process.stdin.on("data", function(data) { 116 | called = true; 117 | received = data; 118 | }); 119 | process.stdin.on("error", function(error) { 120 | errors.push(error); 121 | }); 122 | process.stdin.on("end", function() { 123 | endCalled = true; 124 | }); 125 | process.stdin.resume(); 126 | process.stdin.send(Buffer.from(data, "utf8")); 127 | assert(called, "'data' event was not received."); 128 | assert.equal(received, data, "received data should match what was sent."); 129 | assert.deepEqual(errors, [], "'error' event should not be received."); 130 | assert(!endCalled, "'end' event should not be received."); 131 | }); 132 | 133 | it("MockSTDIN#send()", function () { 134 | let called = false; 135 | let dataCalled = false; 136 | let errors = []; 137 | process.stdin.setEncoding("utf8"); 138 | process.stdin.on("error", function(error) { 139 | errors.push(error); 140 | }); 141 | process.stdin.on("end", function() { 142 | called = true; 143 | }); 144 | process.stdin.on("data", function() { 145 | dataCalled = true; 146 | }); 147 | process.stdin.resume(); 148 | process.stdin.send(null); 149 | assert(!dataCalled, "'data' event should not be received."); 150 | assert.deepEqual(errors, [], "'error' event should not be received."); 151 | assert(called, "'end' event was not received."); 152 | }); 153 | 154 | 155 | it("MockSTDIN#send(, )", function () { 156 | let endCalled = false; 157 | let data = ''; 158 | let errors = []; 159 | process.stdin.setEncoding("utf8"); 160 | process.stdin.on("error", function(error) { 161 | errors.push(error); 162 | }); 163 | process.stdin.on("end", function() { 164 | endCalled = true; 165 | }); 166 | process.stdin.on("data", function(text) { 167 | data += text; 168 | }); 169 | process.stdin.resume(); 170 | assert.throws(function() { 171 | process.stdin.send(["44GT44KT44Gr44Gh44Gv", "5LiW55WM"], "base64"); 172 | }, TypeError, "should have thrown."); 173 | }); 174 | 175 | it("MockSTDIN#send(, )", function() { 176 | let endCalled = false; 177 | let data = ''; 178 | let errors = []; 179 | process.stdin.setEncoding("utf8"); 180 | process.stdin.on("error", function(error) { 181 | errors.push(error); 182 | }); 183 | process.stdin.on("end", function() { 184 | endCalled = true; 185 | }); 186 | process.stdin.on("data", function(text) { 187 | data += text; 188 | }); 189 | process.stdin.resume(); 190 | process.stdin.send("44GT44KT44Gr44Gh44Gv5LiW55WM", "base64"); 191 | assert.equal(data, "こんにちは世界", "'data' should be decoded from base64."); 192 | assert.deepEqual(errors, [], "'error' event should not be received."); 193 | assert(!endCalled, "'end' event should not be received."); 194 | }); 195 | 196 | it("MockSTDIN#end()", function(done) { 197 | let called = false; 198 | let dataCalled = false; 199 | let errors = []; 200 | process.stdin.setEncoding("utf8"); 201 | process.stdin.on("error", function(error) { 202 | errors.push(error); 203 | }); 204 | process.stdin.on("end", function() { 205 | called = true; 206 | }); 207 | process.stdin.on("data", function() { 208 | dataCalled = true; 209 | }); 210 | process.stdin.resume(); 211 | process.stdin.end(); 212 | assert(!dataCalled, "'data' event should not be received."); 213 | assert.deepEqual(errors, [], "'error' event should not be received."); 214 | assert(called, "'end' event was not received."); 215 | 216 | called = false; 217 | setTimeout(function() { 218 | assert(!called, "'end' event should not be dispatched more than once."); 219 | done(); 220 | }); 221 | }); 222 | 223 | 224 | it("MockSTDIN#reset()", function() { 225 | let received = ''; 226 | process.stdin.setEncoding('utf8'); 227 | process.stdin.on("data", function(data) { 228 | received += data; 229 | }); 230 | process.stdin.end(); 231 | assert(process.stdin._readableState.ended, "stream should be 'ended'."); 232 | assert(process.stdin._readableState.endEmitted, "'end' event should be dispatched."); 233 | process.stdin.reset(); 234 | 235 | assert(!process.stdin._readableState.ended, "'ended' flag should be reset."); 236 | assert(!process.stdin._readableState.endEmitted, "'endEmitted' flag should be reset."); 237 | 238 | assert.doesNotThrow(function() { 239 | process.stdin.send("Please don't throw, little lamb!"); 240 | }, "should not throw when sending data after end when reset() called"); 241 | 242 | assert.equal(received, "Please don't throw, little lamb!"); 243 | }); 244 | 245 | it("MockSTDIN#reset(true)", function() { 246 | let received = ''; 247 | process.stdin.setEncoding('utf8'); 248 | process.stdin.on("data", function(data) { 249 | received += data; 250 | }); 251 | process.stdin.end(); 252 | assert(process.stdin._readableState.ended, "stream should be 'ended'."); 253 | assert(process.stdin._readableState.endEmitted, "'end' event should be dispatched."); 254 | process.stdin.reset(true); 255 | 256 | process.stdin.on("data", function(data) { 257 | received += data; 258 | }); 259 | 260 | assert(!process.stdin._readableState.ended, "'ended' flag should be reset."); 261 | assert(!process.stdin._readableState.endEmitted, "'endEmitted' flag should be reset."); 262 | 263 | assert.doesNotThrow(function() { 264 | process.stdin.send("Please don't throw, little lamb!"); 265 | }, "should not throw when sending data after end when reset() called"); 266 | 267 | assert.equal(received, "Please don't throw, little lamb!"); 268 | }); 269 | 270 | it("MockSTDIN#setRawMode()", function() { 271 | assert.throws(function() { 272 | process.stdin.setRawMode(''); 273 | }, TypeError); 274 | }); 275 | 276 | 277 | it("MockSTDIN#setRawMode()", function() { 278 | assert.doesNotThrow(function() { 279 | process.stdin.setRawMode(true); 280 | process.stdin.setRawMode(false); 281 | process.stdin.end(); 282 | }); 283 | }); 284 | }); 285 | -------------------------------------------------------------------------------- /tools/travis.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" 4 | cd "$SCRIPT_DIR/.." 5 | 6 | # TODO: Only run tests once, and use a custom reporter which outputs both LCOV and text, 7 | # for Travis. 8 | node_modules/.bin/mocha test 9 | STATUS=$? 10 | 11 | node_modules/.bin/jscoverage lib 12 | COVERAGE=1 node_modules/.bin/mocha -R mocha-lcov-reporter test > .lcov 13 | cat .lcov | node_modules/.bin/coveralls 14 | 15 | exit $STATUS 16 | --------------------------------------------------------------------------------