Skip to content

Commit

Permalink
chore(stop): abstract instance getter to its own utility
Browse files Browse the repository at this point in the history
  • Loading branch information
vikaspotluri123 committed Jul 28, 2018
1 parent 9e27b59 commit 9a7ddc2
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 11 deletions.
17 changes: 6 additions & 11 deletions lib/commands/stop.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,23 @@ class StopCommand extends Command {
}

run(argv) {
const errors = require('../errors');
const ProcessManager = require('../process-manager');
const checkValidInstall = require('../utils/check-valid-install');
const getInstance = require('../utils/get-instance');

const runOptions = {quiet: argv.quiet};

if (argv.all) {
return this.stopAll();
}

const instance = this.system.getInstance(argv.name);
let instance;

if (argv.name) {
if (!instance) {
return Promise.reject(new errors.SystemError(`Ghost instance '${argv.name}' does not exist`));
}

process.chdir(instance.dir);
try {
instance = getInstance(argv.name, this.system, 'stop');
} catch (error) {
return Promise.reject(error);
}

checkValidInstall('stop');

return instance.running().then((isRunning) => {
if (!isRunning) {
this.ui.log('Ghost is already stopped! Nothing to do here.', 'green');
Expand Down
22 changes: 22 additions & 0 deletions lib/utils/get-instance.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';

const checkValidInstall = require('./check-valid-install');
const {SystemError} = require('../errors');

function findInstance(instanceName, system, commandName) {
const instance = system.getInstance(instanceName);

if (instanceName) {
if (!instance) {
throw new SystemError(`Ghost instance "${instanceName}" does not exist`);
}

process.chdir(instance.dir);
}

checkValidInstall(commandName);

return instance;
}

module.exports = findInstance;
59 changes: 59 additions & 0 deletions test/unit/utils/get-instance-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
'use strict';
const {expect} = require('chai');
const sinon = require('sinon');
const proxyquire = require('proxyquire').noCallThru();
const {SystemError} = require('../../../lib/errors');
const modulePath = '../../../lib/utils/get-instance';

describe('Unit: Utils > getInstance', function () {
let getInstance, stubs, system;
beforeEach(function () {
stubs = {
checkValidInstall: sinon.stub().callsFake(a => a),
chdir: sinon.stub(process, 'chdir'),
getInstance: sinon.stub().returns('It\'s-a Me, Mario!')
};

system = {getInstance: stubs.getInstance};
getInstance = proxyquire(modulePath, {
'./check-valid-install': stubs.checkValidInstall
});
});

this.afterEach(function () {
sinon.restore();
});

it('Doesn\'t change directory by default', function () {
const result = getInstance(undefined, system, 'test');

expect(result).to.equal('It\'s-a Me, Mario!');
expect(stubs.getInstance.calledOnce).to.be.true;
expect(stubs.chdir.called).to.be.false;
expect(stubs.checkValidInstall.calledOnce).to.be.true;
expect(stubs.checkValidInstall.calledWithExactly('test')).to.be.true;
});

it('Fails if the instance cannot be found', function () {
stubs.getInstance.returns(null);

try {
getInstance('ghosted', system, 'test');
expect(false, 'Promise should have rejected').to.be.true;
} catch (error) {
expect(error).to.be.instanceof(SystemError);
expect(error.message).to.equal('Ghost instance "ghosted" does not exist');
}
});

it('Chdirs into instance directory when it exists', function () {
const dir = '/path/to/ghost';
stubs.getInstance.returns({dir});

const result = getInstance('i', system, 'test');

expect(result.dir).to.equal(dir);
expect(stubs.chdir.calledOnce).to.to.true;
expect(stubs.chdir.calledWithExactly(dir)).to.be.true;
});
})

0 comments on commit 9a7ddc2

Please sign in to comment.