Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(process): allow process.isEnabled to return a promise #674

Merged
merged 1 commit into from
Mar 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions lib/commands/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,17 @@ class StartCommand extends Command {
};

return this.ui.run(start, 'Starting Ghost', runOptions).then(() => {
// If process manager doesn't support enable behavior OR it's already enabled, don't try to enable
if (!ProcessManager.supportsEnableBehavior(processInstance) || processInstance.isEnabled()) {
argv.enable = false;
}

if (!argv.enable) {
if (!argv.enable || !ProcessManager.supportsEnableBehavior(processInstance)) {
return Promise.resolve();
}

return this.ui.run(processInstance.enable(), 'Enabling Ghost instance startup on server boot', runOptions);
return Promise.resolve(processInstance.isEnabled()).then((isEnabled) => {
if (isEnabled) {
return Promise.resolve();
}

return this.ui.run(() => processInstance.enable(), 'Enabling Ghost instance startup on server boot', runOptions);
});
}).then(() => {
if (!argv.quiet) {
this.ui.log(`You can access your blog at ${instance.config.get('url')}`, 'cyan');
Expand Down
18 changes: 11 additions & 7 deletions lib/commands/stop.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,21 @@ class StopCommand extends Command {

return this.ui.run(stop, 'Stopping Ghost', runOptions);
}).then(() => {
if (
argv.disable &&
ProcessManager.supportsEnableBehavior(instance.process) &&
instance.process.isEnabled()
) {
if (!argv.disable || !ProcessManager.supportsEnableBehavior(instance.process)) {
return Promise.resolve();
}

return Promise.resolve(instance.process.isEnabled()).then((isEnabled) => {
if (!isEnabled) {
return Promise.resolve();
}

return this.ui.run(
instance.process.disable(),
() => instance.process.disable(),
'Disabling Ghost instance startup on server boot',
runOptions
);
}
});
});
}

Expand Down
39 changes: 32 additions & 7 deletions test/unit/commands/start-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,19 @@ describe('Unit: Commands > Start', function () {
describe('enables instance if needed', function () {
it('normal conditions', function () {
const ui = {
run: sinon.stub().resolves(),
run: sinon.stub(),
listr: () => Promise.resolve()
};
ui.run.onFirstCall().resolves();
ui.run.onSecondCall().callsFake((fn) => Promise.resolve(fn()));
myInstance.process = {
isEnabled: sinon.stub().returns(false),
enable: sinon.stub(),
isEnabled: sinon.stub().resolves(false),
enable: sinon.stub().resolves(),
disable: 'yes'
};
const start = new StartCommand(ui, mySystem);
const runCommandStub = sinon.stub(start, 'runCommand').resolves();

return start.run({quiet: true, enable: true}).then(() => {
const ie = myInstance.process.isEnabled;
const enable = myInstance.process.enable;
Expand All @@ -120,8 +123,8 @@ describe('Unit: Commands > Start', function () {
listr: () => Promise.resolve()
};
myInstance.process = {
isEnabled: sinon.stub().returns(true),
enable: sinon.stub(),
isEnabled: sinon.stub().resolves(true),
enable: sinon.stub().resolves(),
disable: 'yes'
};
const start = new StartCommand(ui, mySystem);
Expand All @@ -142,16 +145,38 @@ describe('Unit: Commands > Start', function () {
listr: () => Promise.resolve()
};
myInstance.process = {
isEnabled: sinon.stub().returns(true),
enable: sinon.stub()
isEnabled: sinon.stub().resolves(true),
enable: sinon.stub().resolves()
};
const start = new StartCommand(ui, mySystem);
const runCommandStub = sinon.stub(start, 'runCommand').resolves();
return start.run({quiet: true, enable: true}).then(() => {
const ie = myInstance.process.isEnabled;
const enable = myInstance.process.enable;
expect(runCommandStub.calledOnce).to.be.true;
expect(ie.called).to.be.false;
expect(enable.called).to.be.false;
expect(ui.run.calledOnce).to.be.true;
});
});

it('not when enabled flag is false', function () {
const ui = {
run: sinon.stub().resolves(),
listr: () => Promise.resolve()
};
myInstance.process = {
isEnabled: sinon.stub().resolves(true),
enable: sinon.stub().resolves(),
disable: sinon.stub().resolves()
};
const start = new StartCommand(ui, mySystem);
const runCommandStub = sinon.stub(start, 'runCommand').resolves();
return start.run({quiet: true, enable: false}).then(() => {
const ie = myInstance.process.isEnabled;
const enable = myInstance.process.enable;
expect(runCommandStub.calledOnce).to.be.true;
expect(ie.called).to.be.false;
expect(enable.called).to.be.false;
expect(ui.run.calledOnce).to.be.true;
});
Expand Down
136 changes: 112 additions & 24 deletions test/unit/commands/stop-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,33 +100,121 @@ describe('Unit: Commands > Stop', function () {
return stop.run.call(context, {});
});

it('disables extensions if it needs to', function () {
class ProcessManager {}
const sEBstub = sinon.stub().returns(true);
const disableStub = sinon.stub().resolves();
const gIstub = sinon.stub().returns({
running: () => Promise.resolve(true),
process: {
disable: disableStub,
stop: () => true,
isEnabled: () => true
},
loadRunningEnvironment: () => true
describe('handles disabling', function () {
it('skips disabling if disable flag is not set', function () {
const instance = {
running: () => Promise.resolve(true),
process: {
enable: () => Promise.resolve(),
disable: sinon.stub().resolves(),
stop: sinon.stub().resolves(),
isEnabled: sinon.stub().resolves(true)
},
loadRunningEnvironment: () => true
};
const system = {
getInstance: () => instance
};
const ui = {
run: sinon.stub().resolves()
};

const StopCommand = proxyquire(modulePath, {
'../utils/check-valid-install': () => true
});
const stop = new StopCommand(ui, system);

return stop.run({disable: false}).then(() => {
expect(instance.process.isEnabled.called).to.be.false;
expect(ui.run.calledOnce).to.be.true;
});
});
const context = {
system: {getInstance: gIstub},
ui: {run: () => Promise.resolve()}
};
ProcessManager.supportsEnableBehavior = sEBstub;
const StopCommand = proxyquire(modulePath, {
'../utils/check-valid-install': () => true,
'../process-manager': ProcessManager

it('skips disabling if process manager doesn\'t support enable behavior', function () {
const instance = {
running: () => Promise.resolve(true),
process: {
stop: sinon.stub().resolves(),
isEnabled: sinon.stub().resolves(false)
},
loadRunningEnvironment: () => true
};
const system = {
getInstance: () => instance
};
const ui = {
run: sinon.stub().resolves()
};

const StopCommand = proxyquire(modulePath, {
'../utils/check-valid-install': () => true
});
const stop = new StopCommand(ui, system);

return stop.run({disable: true}).then(() => {
expect(ui.run.calledOnce).to.be.true;
expect(instance.process.isEnabled.called).to.be.false;
});
});
const stop = new StopCommand();

return stop.run.call(context, {disable: true}).then(() => {
expect(sEBstub.calledOnce).to.be.true;
expect(disableStub.calledOnce).to.be.true;
it('skips disabling if isEnabled returns false', function () {
const instance = {
running: () => Promise.resolve(true),
process: {
enable: () => Promise.resolve(),
disable: sinon.stub().resolves(),
stop: sinon.stub().resolves(),
isEnabled: sinon.stub().resolves(false)
},
loadRunningEnvironment: () => true
};
const system = {
getInstance: () => instance
};
const ui = {
run: sinon.stub().resolves()
};

const StopCommand = proxyquire(modulePath, {
'../utils/check-valid-install': () => true
});
const stop = new StopCommand(ui, system);

return stop.run({disable: true}).then(() => {
expect(instance.process.isEnabled.calledOnce).to.be.true;
expect(instance.process.disable.called).to.be.false;
expect(ui.run.calledOnce).to.be.true;
});
});

it('disables if necessary', function () {
const instance = {
running: () => Promise.resolve(true),
process: {
enable: () => Promise.resolve(),
disable: sinon.stub().resolves(),
stop: sinon.stub().resolves(),
isEnabled: sinon.stub().resolves(true)
},
loadRunningEnvironment: () => true
};
const system = {
getInstance: () => instance
};
const ui = {
run: sinon.stub().callsFake(fn => Promise.resolve(fn()))
};

const StopCommand = proxyquire(modulePath, {
'../utils/check-valid-install': () => true
});
const stop = new StopCommand(ui, system);

return stop.run({disable: true}).then(() => {
expect(instance.process.isEnabled.calledOnce).to.be.true;
expect(instance.process.disable.calledOnce).to.be.true;
expect(ui.run.calledTwice).to.be.true;
});
});
});
});
Expand Down