Skip to content

Commit c93124b

Browse files
acburdineaileen
authored andcommitted
feat(process): allow process.isEnabled to return a promise (#674)
refs #672 - support isEnabled returning a promise
1 parent 29b4a0b commit c93124b

File tree

4 files changed

+163
-45
lines changed

4 files changed

+163
-45
lines changed

lib/commands/start.js

+8-7
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,17 @@ class StartCommand extends Command {
4545
};
4646

4747
return this.ui.run(start, 'Starting Ghost', runOptions).then(() => {
48-
// If process manager doesn't support enable behavior OR it's already enabled, don't try to enable
49-
if (!ProcessManager.supportsEnableBehavior(processInstance) || processInstance.isEnabled()) {
50-
argv.enable = false;
51-
}
52-
53-
if (!argv.enable) {
48+
if (!argv.enable || !ProcessManager.supportsEnableBehavior(processInstance)) {
5449
return Promise.resolve();
5550
}
5651

57-
return this.ui.run(processInstance.enable(), 'Enabling Ghost instance startup on server boot', runOptions);
52+
return Promise.resolve(processInstance.isEnabled()).then((isEnabled) => {
53+
if (isEnabled) {
54+
return Promise.resolve();
55+
}
56+
57+
return this.ui.run(() => processInstance.enable(), 'Enabling Ghost instance startup on server boot', runOptions);
58+
});
5859
}).then(() => {
5960
if (!argv.quiet) {
6061
this.ui.log(`You can access your blog at ${instance.config.get('url')}`, 'cyan');

lib/commands/stop.js

+11-7
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,21 @@ class StopCommand extends Command {
5353

5454
return this.ui.run(stop, 'Stopping Ghost', runOptions);
5555
}).then(() => {
56-
if (
57-
argv.disable &&
58-
ProcessManager.supportsEnableBehavior(instance.process) &&
59-
instance.process.isEnabled()
60-
) {
56+
if (!argv.disable || !ProcessManager.supportsEnableBehavior(instance.process)) {
57+
return Promise.resolve();
58+
}
59+
60+
return Promise.resolve(instance.process.isEnabled()).then((isEnabled) => {
61+
if (!isEnabled) {
62+
return Promise.resolve();
63+
}
64+
6165
return this.ui.run(
62-
instance.process.disable(),
66+
() => instance.process.disable(),
6367
'Disabling Ghost instance startup on server boot',
6468
runOptions
6569
);
66-
}
70+
});
6771
});
6872
}
6973

test/unit/commands/start-spec.js

+32-7
Original file line numberDiff line numberDiff line change
@@ -94,16 +94,19 @@ describe('Unit: Commands > Start', function () {
9494
describe('enables instance if needed', function () {
9595
it('normal conditions', function () {
9696
const ui = {
97-
run: sinon.stub().resolves(),
97+
run: sinon.stub(),
9898
listr: () => Promise.resolve()
9999
};
100+
ui.run.onFirstCall().resolves();
101+
ui.run.onSecondCall().callsFake((fn) => Promise.resolve(fn()));
100102
myInstance.process = {
101-
isEnabled: sinon.stub().returns(false),
102-
enable: sinon.stub(),
103+
isEnabled: sinon.stub().resolves(false),
104+
enable: sinon.stub().resolves(),
103105
disable: 'yes'
104106
};
105107
const start = new StartCommand(ui, mySystem);
106108
const runCommandStub = sinon.stub(start, 'runCommand').resolves();
109+
107110
return start.run({quiet: true, enable: true}).then(() => {
108111
const ie = myInstance.process.isEnabled;
109112
const enable = myInstance.process.enable;
@@ -120,8 +123,8 @@ describe('Unit: Commands > Start', function () {
120123
listr: () => Promise.resolve()
121124
};
122125
myInstance.process = {
123-
isEnabled: sinon.stub().returns(true),
124-
enable: sinon.stub(),
126+
isEnabled: sinon.stub().resolves(true),
127+
enable: sinon.stub().resolves(),
125128
disable: 'yes'
126129
};
127130
const start = new StartCommand(ui, mySystem);
@@ -142,16 +145,38 @@ describe('Unit: Commands > Start', function () {
142145
listr: () => Promise.resolve()
143146
};
144147
myInstance.process = {
145-
isEnabled: sinon.stub().returns(true),
146-
enable: sinon.stub()
148+
isEnabled: sinon.stub().resolves(true),
149+
enable: sinon.stub().resolves()
147150
};
148151
const start = new StartCommand(ui, mySystem);
149152
const runCommandStub = sinon.stub(start, 'runCommand').resolves();
150153
return start.run({quiet: true, enable: true}).then(() => {
151154
const ie = myInstance.process.isEnabled;
152155
const enable = myInstance.process.enable;
156+
expect(runCommandStub.calledOnce).to.be.true;
153157
expect(ie.called).to.be.false;
158+
expect(enable.called).to.be.false;
159+
expect(ui.run.calledOnce).to.be.true;
160+
});
161+
});
162+
163+
it('not when enabled flag is false', function () {
164+
const ui = {
165+
run: sinon.stub().resolves(),
166+
listr: () => Promise.resolve()
167+
};
168+
myInstance.process = {
169+
isEnabled: sinon.stub().resolves(true),
170+
enable: sinon.stub().resolves(),
171+
disable: sinon.stub().resolves()
172+
};
173+
const start = new StartCommand(ui, mySystem);
174+
const runCommandStub = sinon.stub(start, 'runCommand').resolves();
175+
return start.run({quiet: true, enable: false}).then(() => {
176+
const ie = myInstance.process.isEnabled;
177+
const enable = myInstance.process.enable;
154178
expect(runCommandStub.calledOnce).to.be.true;
179+
expect(ie.called).to.be.false;
155180
expect(enable.called).to.be.false;
156181
expect(ui.run.calledOnce).to.be.true;
157182
});

test/unit/commands/stop-spec.js

+112-24
Original file line numberDiff line numberDiff line change
@@ -100,33 +100,121 @@ describe('Unit: Commands > Stop', function () {
100100
return stop.run.call(context, {});
101101
});
102102

103-
it('disables extensions if it needs to', function () {
104-
class ProcessManager {}
105-
const sEBstub = sinon.stub().returns(true);
106-
const disableStub = sinon.stub().resolves();
107-
const gIstub = sinon.stub().returns({
108-
running: () => Promise.resolve(true),
109-
process: {
110-
disable: disableStub,
111-
stop: () => true,
112-
isEnabled: () => true
113-
},
114-
loadRunningEnvironment: () => true
103+
describe('handles disabling', function () {
104+
it('skips disabling if disable flag is not set', function () {
105+
const instance = {
106+
running: () => Promise.resolve(true),
107+
process: {
108+
enable: () => Promise.resolve(),
109+
disable: sinon.stub().resolves(),
110+
stop: sinon.stub().resolves(),
111+
isEnabled: sinon.stub().resolves(true)
112+
},
113+
loadRunningEnvironment: () => true
114+
};
115+
const system = {
116+
getInstance: () => instance
117+
};
118+
const ui = {
119+
run: sinon.stub().resolves()
120+
};
121+
122+
const StopCommand = proxyquire(modulePath, {
123+
'../utils/check-valid-install': () => true
124+
});
125+
const stop = new StopCommand(ui, system);
126+
127+
return stop.run({disable: false}).then(() => {
128+
expect(instance.process.isEnabled.called).to.be.false;
129+
expect(ui.run.calledOnce).to.be.true;
130+
});
115131
});
116-
const context = {
117-
system: {getInstance: gIstub},
118-
ui: {run: () => Promise.resolve()}
119-
};
120-
ProcessManager.supportsEnableBehavior = sEBstub;
121-
const StopCommand = proxyquire(modulePath, {
122-
'../utils/check-valid-install': () => true,
123-
'../process-manager': ProcessManager
132+
133+
it('skips disabling if process manager doesn\'t support enable behavior', function () {
134+
const instance = {
135+
running: () => Promise.resolve(true),
136+
process: {
137+
stop: sinon.stub().resolves(),
138+
isEnabled: sinon.stub().resolves(false)
139+
},
140+
loadRunningEnvironment: () => true
141+
};
142+
const system = {
143+
getInstance: () => instance
144+
};
145+
const ui = {
146+
run: sinon.stub().resolves()
147+
};
148+
149+
const StopCommand = proxyquire(modulePath, {
150+
'../utils/check-valid-install': () => true
151+
});
152+
const stop = new StopCommand(ui, system);
153+
154+
return stop.run({disable: true}).then(() => {
155+
expect(ui.run.calledOnce).to.be.true;
156+
expect(instance.process.isEnabled.called).to.be.false;
157+
});
124158
});
125-
const stop = new StopCommand();
126159

127-
return stop.run.call(context, {disable: true}).then(() => {
128-
expect(sEBstub.calledOnce).to.be.true;
129-
expect(disableStub.calledOnce).to.be.true;
160+
it('skips disabling if isEnabled returns false', function () {
161+
const instance = {
162+
running: () => Promise.resolve(true),
163+
process: {
164+
enable: () => Promise.resolve(),
165+
disable: sinon.stub().resolves(),
166+
stop: sinon.stub().resolves(),
167+
isEnabled: sinon.stub().resolves(false)
168+
},
169+
loadRunningEnvironment: () => true
170+
};
171+
const system = {
172+
getInstance: () => instance
173+
};
174+
const ui = {
175+
run: sinon.stub().resolves()
176+
};
177+
178+
const StopCommand = proxyquire(modulePath, {
179+
'../utils/check-valid-install': () => true
180+
});
181+
const stop = new StopCommand(ui, system);
182+
183+
return stop.run({disable: true}).then(() => {
184+
expect(instance.process.isEnabled.calledOnce).to.be.true;
185+
expect(instance.process.disable.called).to.be.false;
186+
expect(ui.run.calledOnce).to.be.true;
187+
});
188+
});
189+
190+
it('disables if necessary', function () {
191+
const instance = {
192+
running: () => Promise.resolve(true),
193+
process: {
194+
enable: () => Promise.resolve(),
195+
disable: sinon.stub().resolves(),
196+
stop: sinon.stub().resolves(),
197+
isEnabled: sinon.stub().resolves(true)
198+
},
199+
loadRunningEnvironment: () => true
200+
};
201+
const system = {
202+
getInstance: () => instance
203+
};
204+
const ui = {
205+
run: sinon.stub().callsFake(fn => Promise.resolve(fn()))
206+
};
207+
208+
const StopCommand = proxyquire(modulePath, {
209+
'../utils/check-valid-install': () => true
210+
});
211+
const stop = new StopCommand(ui, system);
212+
213+
return stop.run({disable: true}).then(() => {
214+
expect(instance.process.isEnabled.calledOnce).to.be.true;
215+
expect(instance.process.disable.calledOnce).to.be.true;
216+
expect(ui.run.calledTwice).to.be.true;
217+
});
130218
});
131219
});
132220
});

0 commit comments

Comments
 (0)