describe('#inspect', () => { it('should throw an error if inspect fails', () => { // We need this chain of stubs to send our call down an explicit line that // will eventually throw a solid exception. let bogusContainer = dummyContainer(); const inspectStub = sinon.stub(bogusContainer, 'inspect').throws(); const stub = sinon.stub(landerode, 'getContainer').returns(bogusContainer); // Throw needs a function to check rather than a function result const cptPhasma = () => landerode.inspect('fn-2187'); expect(cptPhasma).to.throw(); inspectStub.restore(); stub.restore(); }); });
beforeEach(function () { stubs = { es: sinon.stub(), // thrown to stop execution cvi: sinon.stub().throws(new Error()) }; });
it('returns if pidfile not found', function () { const readFileStub = sinon.stub(fs, 'readFileSync').throws({code: 'ENOENT'}); const fkillStub = sinon.stub(); const LocalProcess = proxyquire(modulePath, { fkill: fkillStub }); const instance = new LocalProcess({}, {}, {}); return instance.stop('/var/www/ghost').then(() => { expect(readFileStub.calledWithExactly('/var/www/ghost/.ghostpid')).to.be.true; expect(fkillStub.called).to.be.false; }); });
it('forwards error from readJsonSync', function () { const stub = sinon.stub(fs, 'readJsonSync').throws(new Error('file not found')); try { parseExport('notfoundfile.json'); } catch (error) { expect(error).to.be.an.instanceof(SystemError); expect(error.message).to.include('file not found'); expect(stub.calledOnce).to.be.true; expect(stub.calledWithExactly('notfoundfile.json')).to.be.true; return; } expect.fail('parseImport should have errored'); });
it('isSupported fn', function () { const shell = sinon.stub(execa, 'shellSync'); const inst = new Nginx({}, {}, {}, '/some/dir'); expect(inst.isSupported()).to.be.true; expect(shell.calledOnce).to.be.true; shell.reset(); shell.throws(new Error()); expect(inst.isSupported()).to.be.false; expect(shell.calledOnce).to.be.true; });
describe('getOrderAsync', () => { test('returns ExchangeApiLibError if getOrderAsync raise %s ', async () => { sandbox.stub(exchangeApiLib, 'callAsync').throws(new ExchangeApiLibError()) return expect(orderService.getOrderAsync(tradingWalletAddress, orderId)).rejects .toBeInstanceOf(ExchangeApiLibError) }) test('returns correct order data', async () => { const orderResponse = { status: 'success', data: MockOrderFactory.build(), } sandbox.stub(exchangeApiLib, 'callAsync').returns(orderResponse) const order = await orderService.getOrderAsync(tradingWalletAddress, orderId) expect(order).toMatchObject(orderResponse.data) }) })
it('returns a 500 error if json cannot be parsed to string', async () => { sinon.stub(JSON, 'stringify').throws("Error"); router.get('/test/errors', function () { return { "test": "test" }; }); (await router.resolveRoute({ method: 'GET', url: '/test/errors' }, response)).should.equal('Server error.'); });
it('calls findValidInstall when global option is not set', async function () { const findValidInstall = sinon.stub(); const Command = proxyquire(modulePath, { './utils/find-valid-install': findValidInstall }); const TestCommand = class extends Command {}; findValidInstall.throws(); try { await TestCommand._run('test', {}); throw new Error('findValidInstall not called'); } catch (e) { expect(e.message).to.not.equal('findValidInstall not called'); expect(findValidInstall.calledOnce).to.be.true; expect(findValidInstall.args[0][0]).to.equal('test'); } });
it('rejects if any unexpected error occurs during reading of pidfile', function (done) { const readFileStub = sinon.stub(fs, 'readFileSync').throws(new Error('test error')); const fkillStub = sinon.stub(); const LocalProcess = proxyquire(modulePath, { fkill: fkillStub }); const instance = new LocalProcess({}, {}, {}); instance.stop('/var/www/ghost').then(() => { done(new Error('stop should have rejected')); }).catch((error) => { expect(error).to.be.an.instanceof(errors.CliError); expect(error.message).to.equal('An unexpected error occurred when reading the pidfile.'); expect(readFileStub.calledWithExactly('/var/www/ghost/.ghostpid')).to.be.true; expect(fkillStub.called).to.be.false; done(); }); });
it('Error on buildRequest', function () { var callback = sinon.spy(); var errorMessage = 'Fail on request'; var promise; var callbackErrors; sinon.stub(requestManager, 'buildRequest').throws(new Error(errorMessage)); promise = requestManager.exec({}, callback); callbackErrors = callback.args[0][0]; assert.equal(callbackErrors.message, errorMessage); requestManager.buildRequest.restore(); assert.isRejected(promise, errorMessage); });
it('logs if stdin is tty', function () { const logStub = sinon.stub().throws(new Error('throw me')); const RunCommand = require(modulePath); const instance = new RunCommand({log: logStub}, {}); const oldIsTTY = process.stdin.isTTY; process.stdin.isTTY = true; try { instance.run(); expect(false, 'error should have been thrown').to.be.true; } catch (e) { process.stdin.isTTY = oldIsTTY; expect(e).to.be.an.instanceof(Error); expect(e.message).to.equal('throw me'); expect(logStub.calledOnce).to.be.true; expect(logStub.args[0][0]).to.match(/used by the configured Ghost process manager and for debugging/); } });
it('skips check if command run as non root user', function () { const osStub = sinon.stub(os, 'platform').returns('linux'); const processStub = sinon.stub(process, 'getuid').returns(501); const exitStub = sinon.stub(process, 'exit').throws(); const errorStub = sinon.stub(console, 'error'); checkRootUser('update'); expect(osStub.calledOnce).to.be.true; expect(processStub.calledOnce).to.be.true; expect(errorStub.calledOnce).to.be.false; expect(exitStub.calledOnce).to.be.false; });
it('errors if no command name matches', function () { this.timeout(10000); // this test can take awhile depending on the system const error = sinon.stub(console, 'error'); const exit = sinon.stub(process, 'exit'); discoverCommands.returns({ ls: path.resolve(__dirname, '../../lib/commands/ls') }); exit.throws(); try { bootstrap.run(['notls']); throw new Error('Exit wasn\'t called'); } catch (e) { expect(e.message).to.not.equal('Exit wasn\'t called'); expect(error.args[0][0]).to.match(/^Unrecognized command/); expect(exit.args[0][0]).to.equal(1); } });
it('will not run command when executed as root user', async function () { const checkRootUserStub = sinon.stub(); const Command = proxyquire(modulePath, { './utils/check-root-user': checkRootUserStub }); const TestCommand = class extends Command {}; TestCommand.global = true; checkRootUserStub.throws(); try { await TestCommand._run('test'); } catch (e) { expect(e).to.exist; expect(checkRootUserStub.calledOnce).to.be.true; } });
it('checks for valid install if name not specified', async function () { const findStub = sinon.stub().throws(new Error('findValidInstall')); const Command = proxyquire(modulePath, { '../utils/find-valid-install': findStub }); const stop = new Command(); try { await stop.run({}); } catch (error) { expect(error.message).to.equal('findValidInstall'); expect(findStub.calledOnce).to.be.true; return; } expect.fail('run should have thrown an error'); });