forked from TryGhost/Ghost-CLI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run-spec.js
264 lines (231 loc) · 11.2 KB
/
run-spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
'use strict';
const expect = require('chai').expect;
const sinon = require('sinon');
const proxyquire = require('proxyquire');
const configStub = require('../../utils/config-stub');
const EventEmitter = require('events');
const modulePath = '../../../lib/commands/run';
const errors = require('../../../lib/errors');
describe('Unit: Commands > Run', function () {
describe('run', function () {
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('sets paths.contentPath if it is not already set in config, and calls useDirect if useGhostUser returns false', function () {
const logStub = sinon.stub();
const useGhostUserStub = sinon.stub().returns(false);
const config = configStub();
config.has.returns(false);
const fakeInstance = {config: config, dir: '/var/www/ghost'};
const getInstanceStub = sinon.stub().returns(fakeInstance);
const RunCommand = proxyquire(modulePath, {
'../utils/use-ghost-user': useGhostUserStub
});
const instance = new RunCommand({log: logStub}, {getInstance: getInstanceStub});
const useGhostUser = sinon.stub(instance, 'useGhostUser').resolves();
const useDirect = sinon.stub(instance, 'useDirect').resolves();
const oldIsTTY = process.stdin.isTTY;
process.stdin.isTTY = false;
return instance.run().then(() => {
process.stdin.isTTY = oldIsTTY;
expect(logStub.called).to.be.false;
expect(getInstanceStub.calledOnce).to.be.true;
expect(config.has.calledOnce).to.be.true;
expect(config.has.calledWithExactly('paths.contentPath')).to.be.true;
expect(config.set.calledOnce).to.be.true;
expect(config.set.calledWithExactly('paths.contentPath', '/var/www/ghost/content')).to.be.true;
expect(config.save.calledOnce).to.be.true;
expect(useGhostUserStub.calledOnce).to.be.true;
expect(useGhostUser.called).to.be.false;
expect(useDirect.calledOnce).to.be.true;
expect(useDirect.calledWithExactly(fakeInstance)).to.be.true;
});
});
it('doesn\'t set paths.contentPath if it is set in cofig, and calls useGhostUser if useGhostUser util returns false', function () {
const logStub = sinon.stub();
const useGhostUserStub = sinon.stub().returns(true);
const config = configStub();
config.has.returns(true);
const fakeInstance = {config: config, dir: '/var/www/ghost'};
const getInstanceStub = sinon.stub().returns(fakeInstance);
const RunCommand = proxyquire(modulePath, {
'../utils/use-ghost-user': useGhostUserStub
});
const instance = new RunCommand({log: logStub}, {getInstance: getInstanceStub});
const useGhostUser = sinon.stub(instance, 'useGhostUser').resolves();
const useDirect = sinon.stub(instance, 'useDirect').resolves();
const oldIsTTY = process.stdin.isTTY;
process.stdin.isTTY = false;
return instance.run().then(() => {
process.stdin.isTTY = oldIsTTY;
expect(logStub.called).to.be.false;
expect(getInstanceStub.calledOnce).to.be.true;
expect(config.has.called).to.be.true;
expect(config.has.calledWithExactly('paths.contentPath')).to.be.true;
expect(config.set.called).to.be.false;
expect(config.save.called).to.be.false;
expect(useGhostUserStub.calledOnce).to.be.true;
expect(useDirect.called).to.be.false;
expect(useGhostUser.calledOnce).to.be.true;
expect(useGhostUser.calledWithExactly(fakeInstance)).to.be.true;
});
});
});
it('useGhostUser spawns child process and handles events correctly', function () {
const childStub = new EventEmitter();
const spawnStub = sinon.stub().returns(childStub);
const RunCommand = proxyquire(modulePath, {
child_process: {spawn: spawnStub}
});
const failStub = sinon.stub();
const logStub = sinon.stub()
const instance = new RunCommand({fail: failStub, log: logStub}, {});
const exitStub = sinon.stub(process, 'exit');
instance.useGhostUser({dir: '/var/www/ghost'});
expect(logStub.calledOnce).to.be.true;
expect(logStub.args[0][0]).to.match(/Running sudo command/);
expect(spawnStub.calledOnce).to.be.true;
expect(spawnStub.calledWithExactly('sudo', [
'-E',
'-u',
'ghost',
process.execPath,
'current/index.js'
], {
cwd: '/var/www/ghost',
stdio: 'inherit'
})).to.be.true;
expect(instance.child).to.equal(childStub);
// Check error handler with EPERM code
failStub.reset();
exitStub.reset();
childStub.emit('error', {message: 'some error occurred', code: 'EPERM'});
expect(failStub.called).to.be.false;
expect(exitStub.called).to.be.false;
// Check error handler without EPERM code
failStub.reset();
exitStub.reset();
childStub.emit('error', {message: 'some error occurred'});
expect(failStub.calledOnce).to.be.true;
expect(failStub.calledWithExactly({message: 'some error occurred'})).to.be.true;
expect(exitStub.calledOnce).to.be.true;
expect(exitStub.calledWithExactly(1)).to.be.true;
exitStub.restore();
// Check that it does nothing on success message
childStub.emit('message', {started: true}); // nothing should happen
try {
// check message handler with error
childStub.emit('message', {error: 'oops I did it again'});
expect(false, 'error should have been thrown').to.be.true;
} catch (e) {
expect(e).to.be.an.instanceof(errors.GhostError);
expect(e.message).to.equal('oops I did it again');
}
});
it('useDirect spawns child process and handles events correctly', function () {
const childStub = new EventEmitter();
const spawnStub = sinon.stub().returns(childStub);
const RunCommand = proxyquire(modulePath, {
child_process: {spawn: spawnStub}
});
const failStub = sinon.stub();
const instance = new RunCommand({fail: failStub}, {});
const successStub = sinon.stub();
const errorStub = sinon.stub();
const exitStub = sinon.stub(process, 'exit');
instance.useDirect({dir: '/var/www/ghost', process: {success: successStub, error: errorStub}}, {delayErrorChaining: false});
expect(spawnStub.calledOnce).to.be.true;
expect(spawnStub.calledWithExactly(process.execPath, ['current/index.js'], {
cwd: '/var/www/ghost',
stdio: [0, 1, 2, 'ipc']
})).to.be.true;
expect(instance.child).to.equal(childStub);
// Check error handler
expect(failStub.called).to.be.false;
expect(exitStub.called).to.be.false;
childStub.emit('error', {message: 'some error occurred'});
expect(failStub.calledOnce).to.be.true;
expect(failStub.calledWithExactly({message: 'some error occurred'})).to.be.true;
expect(exitStub.calledOnce).to.be.true;
expect(exitStub.calledWithExactly(1)).to.be.true;
// Check message handler with success
expect(successStub.called).to.be.false;
expect(errorStub.called).to.be.false;
childStub.emit('message', {started: true});
expect(successStub.calledOnce).to.be.true;
expect(errorStub.called).to.be.false;
successStub.reset();
errorStub.reset();
// check message handler with error
childStub.emit('message', {error: 'oops I did it again'});
expect(successStub.called).to.be.false;
expect(errorStub.calledOnce).to.be.true;
expect(errorStub.calledWithExactly('oops I did it again')).to.be.true;
exitStub.restore();
});
describe('cleanup handler', function () {
const RunCommand = require(modulePath);
it('doesn\'t do anything if child process has not been set', function () {
const instance = new RunCommand({}, {});
expect(instance.child).to.not.exist;
instance.cleanup(); // If no error is thrown then it was successful
});
it('attempts to kill child process, and doesn\'t throw if error is EPERM and sudo is true', function () {
const instance = new RunCommand({}, {});
const err = new Error();
err.code = 'EPERM';
const killStub = sinon.stub().throws(err);
instance.child = {kill: killStub};
instance.sudo = true;
instance.cleanup();
expect(killStub.calledOnce).to.be.true;
});
it('attempts to kill child process and throws if kill fails and not sudo', function () {
const instance = new RunCommand({}, {});
const err = new Error();
err.code = 'EPERM';
const killStub = sinon.stub().throws(err);
instance.child = {kill: killStub};
instance.sudo = false;
try {
instance.cleanup();
expect(false, 'error should have been thrown').to.be.true;
} catch (e) {
expect(e).to.be.an.instanceof(Error);
expect(e.code).to.equal('EPERM');
expect(killStub.calledOnce).to.be.true;
}
});
it('attempts to kill child process and throws if kill fails and error code is not EPERM', function () {
const instance = new RunCommand({}, {});
const err = new Error('yikes');
err.code = 'ENOTFOUND';
const killStub = sinon.stub().throws(err);
instance.child = {kill: killStub};
instance.sudo = true;
try {
instance.cleanup();
expect(false, 'error should have been thrown').to.be.true;
} catch (e) {
expect(e).to.be.an.instanceof(Error);
expect(e.message).to.equal('yikes');
expect(e.code).to.equal('ENOTFOUND');
expect(killStub.calledOnce).to.be.true;
}
});
});
});