test.serial('Running `carbon-now` fails without file or stdin', async t => { try { // https://github.com/sindresorhus/get-stdin/issues/13#issuecomment-279234249 const command = exec(`node ${SCRIPT}`); command.childProcess.stdin.end(); await command; t.fail(); } catch (error) { t.pass(); } });
it('should get help message when using no valid command', function (done) { exec('./cli.js', (err, stdout) => { assert.strictEqual(stdout.indexOf('Usage:'), 1); done(err); }).stdin.end(); });
/** * spams ENTER every second for prompt defaults */ const applyInputs = () => { timerId = setTimeout(function () { gasket.stdin.write(exports.ENTER); applyInputs(); }, 1000); }
function runOnly({ cmd, expected }) { return function (done) { exec(cmd, (err, stdout) => { if (err) { done(err); } assert.strictEqual(stdout.trim(), expected); done(); }).stdin.end(); }; }
it('should print messages to console when using --verbose option', function (done) { this.timeout(6000); exec('./cli.js add http://localhost:9194/data', err => { if (err) { return done(err); } const child = spawn('./cli.js', ['start', '--verbose']); child.stderr.on('data', err => { throw new Error(err.toString()); }); child.stdin.end(); child.stdout.on('data', data => { assert.notEqual(data.toString().split('pathToMocks'), -1); child.kill(); done(err); }); }).stdin.end(); });
function regGetValue (key, value, addOpts, cb) { const outReValue = value.replace(/\W/g, '.') const outRe = new RegExp(`^\\s+${outReValue}\\s+REG_\\w+\\s+(\\S.*)$`, 'im') const reg = path.join(process.env.SystemRoot, 'System32', 'reg.exe') const regArgs = ['query', key, '/v', value].concat(addOpts) log.silly('reg', 'running', reg, regArgs) const child = execFile(reg, regArgs, { encoding: 'utf8' }, function (err, stdout, stderr) { log.silly('reg', 'reg.exe stdout = %j', stdout) if (err || stderr.trim() !== '') { log.silly('reg', 'reg.exe err = %j', err && (err.stack || err)) log.silly('reg', 'reg.exe stderr = %j', stderr) return cb(err, stderr) } const result = outRe.exec(stdout) if (!result) { log.silly('reg', 'error parsing stdout') return cb(new Error('Could not parse output of reg.exe')) } log.silly('reg', 'found: %j', result[1]) cb(null, result[1]) }) child.stdin.end() }
// <<< key part, you must use newline char process.nextTick(function () { n.stdin.end(); });
function runMockTest({ cmd, port, method, middleware, test }) { return function (done) { const emptyMiddleware = (a, b, n) => { n(); }; express() .use(middleware || emptyMiddleware)[method]('/data', (req, res) => { test(req, res); done(); }) .listen(port, e => { if (e) { done(e); } exec(cmd, err => { if (err) { done(err); } }).stdin.end(); }); }; }
gasket.on('close', (code, signal) => { clearTimeout(timerId); gasket.stdin.end(); const ctx = { code, signal, argv, spawnWith, stdout, stderr }; if (assert) { try { assert(ctx); return fulfill(); } catch (err) { return reject(err); } } // In the absence of an explicit assert to call // simply respond with all of the output from the process. // It is up to the caller to decide what "failure" means // for the given test. fulfill(ctx); });
it('should not print messages to console when using --silent option', function (done) { this.timeout(6000); exec('./cli.js add http://localhost:9194/data', err => { if (err) { return done(err); } const child = spawn('./cli.js', ['start', '--silent']); child.stderr.on('data', err => { throw new Error(err.toString()); }); child.stdout.on('data', data => { throw new Error(data); }); child.stdin.end(); setTimeout(() => { request('http://localhost:8059') .post('/data') .expect(200); child.kill(); done(); }, 2000); }).stdin.end(); });
it('should get version number when using --version flag', function (done) { exec('./cli.js --version', (err, stdout) => { assert.strictEqual(stdout.trim(), require('./package.json').version); done(err); }).stdin.end(); });
/** * spams ENTER every second for prompt defaults */ const applyInputs = () => { timerId = setTimeout(function () { gasket.stdin.write(exports.ENTER); applyInputs(); }, 1000); }
// <<< key part, you must use newline char process.nextTick(function () { n.stdin.end(); });
process.nextTick(function () { n.stdin.end(); });
gasket.on('close', (code, signal) => { clearTimeout(timerId); gasket.stdin.end(); const ctx = { code, signal, argv, spawnWith, stdout, stderr }; if (assert) { try { assert(ctx); return fulfill(); } catch (err) { return reject(err); } } // In the absence of an explicit assert to call // simply respond with all of the output from the process. // It is up to the caller to decide what "failure" means // for the given test. fulfill(ctx); });