Skip to content

Commit

Permalink
refactor(restart): use instance start/restart methods (#1048)
Browse files Browse the repository at this point in the history
  • Loading branch information
acburdine committed Nov 6, 2019
1 parent 83ea37a commit 23e48b4
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 28 deletions.
6 changes: 3 additions & 3 deletions lib/commands/restart.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ const Command = require('../command');
class RestartCommand extends Command {
async run() {
const instance = this.system.getInstance();

const isRunning = await instance.isRunning();
if (!isRunning) {
const StartCommand = require('./start');
this.ui.log('Ghost instance is not running! Starting...', 'yellow');
return this.runCommand(StartCommand);
return this.ui.run(() => instance.start(), 'Starting Ghost');
}

instance.loadRunningEnvironment(true);
return this.ui.run(instance.process.restart(process.cwd(), this.system.environment), 'Restarting Ghost');
await this.ui.run(() => instance.restart(), 'Restarting Ghost');
}
}

Expand Down
11 changes: 11 additions & 0 deletions lib/instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,17 @@ class Instance {
await this.process.disable();
}

/**
* Restarts the Ghost instance
*
* @returns {Promise<void>}
* @method restart
* @public
*/
async restart() {
await this.process.restart(this.dir, this.system.environment);
}

/**
* Gets a summary of this instance, comprised of various values from
* the cliConfig and the ghost config
Expand Down
59 changes: 34 additions & 25 deletions test/unit/commands/restart-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,49 @@ const RestartCommand = require(modulePath);

describe('Unit: Command > Restart', function () {
it('warns of stopped instance and starts instead', async function () {
const instance = {isRunning: () => Promise.resolve(false)};
const logStub = sinon.stub();

const command = new RestartCommand({log: logStub}, {getInstance: () => instance});
const runCommand = sinon.stub(command, 'runCommand').resolves();
const instance = {
isRunning: sinon.stub().resolves(false),
start: sinon.stub().resolves()
};
const ui = {
log: sinon.stub(),
run: sinon.stub().callsFake(fn => fn())
};
const system = {
getInstance: sinon.stub().returns(instance)
};

const command = new RestartCommand(ui, system);
await command.run();
expect(runCommand.calledOnce).to.be.true;
expect(runCommand.args[0][0].description).to.equal('Start an instance of Ghost');
expect(logStub.calledOnce).to.be.true;
expect(logStub.args[0][0]).to.match(/not running!/);

expect(instance.isRunning.calledOnce).to.be.true;
expect(ui.log.calledOnce).to.be.true;
expect(ui.log.args[0][0]).to.match(/not running!/);
expect(ui.run.calledOnce).to.be.true;
expect(instance.start.calledOnce).to.be.true;
});

it('calls process restart method if instance is running', async function () {
const runStub = sinon.stub().resolves();
const restartStub = sinon.stub().resolves();
const lreStub = sinon.stub();
const instance = {
process: {restart: restartStub},
loadRunningEnvironment: lreStub,
isRunning: () => Promise.resolve(true)
isRunning: sinon.stub().resolves(true),
loadRunningEnvironment: sinon.stub(),
restart: sinon.stub().resolves()
};
const ui = {
log: sinon.stub(),
run: sinon.stub().callsFake(fn => fn())
};
const system = {
getInstance: sinon.stub().returns(instance)
};

const command = new RestartCommand({run: runStub}, {
environment: 'testing',
getInstance: () => instance
});

const command = new RestartCommand(ui, system);
await command.run();

expect(lreStub.calledOnce).to.be.true;
expect(lreStub.args[0][0]).to.be.true;
expect(restartStub.calledOnce).to.be.true;
expect(restartStub.args[0]).to.deep.equal([process.cwd(), 'testing']);
expect(runStub.calledOnce).to.be.true;
expect(instance.isRunning.calledOnce).to.be.true;
expect(ui.log.called).to.be.false;
expect(instance.loadRunningEnvironment.calledOnce).to.be.true;
expect(ui.run.calledOnce).to.be.true;
expect(instance.restart.calledOnce).to.be.true;
});
});
13 changes: 13 additions & 0 deletions test/unit/instance-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,19 @@ describe('Unit: Instance', function () {
});
});

it('restart', async function () {
const restart = sinon.stub().resolves();
const instance = new Instance({}, {environment: 'testing'}, '/var/www/ghost');

instance._process = {
name: 'local',
restart
};

await instance.restart();
expect(restart.calledOnceWithExactly('/var/www/ghost', 'testing')).to.be.true;
});

describe('summary', function () {
it('returns shortened object if running is false', async function () {
const get = sinon.stub();
Expand Down

0 comments on commit 23e48b4

Please sign in to comment.