Skip to content

Commit

Permalink
chore(local-process): defer some requires & cleanup tests
Browse files Browse the repository at this point in the history
  • Loading branch information
acburdine committed Feb 6, 2018
1 parent 378d4ed commit d791101
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 28 deletions.
19 changes: 10 additions & 9 deletions lib/utils/local-process.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
'use strict';
const fs = require('fs-extra');
const os = require('os');
const path = require('path');
const fkill = require('fkill');
const childProcess = require('child_process');
const isRunning = require('is-running');

const errors = require('../errors');
const ProcessManager = require('../process-manager');

const PID_FILE = '.ghostpid';
Expand All @@ -29,6 +24,9 @@ class LocalProcess extends ProcessManager {
* @public
*/
start(cwd, environment) {
const childProcess = require('child_process');
const errors = require('../errors');

// Check that content folder is owned by the current user
if (!this._checkContentFolder(cwd)) {
return Promise.reject(new errors.SystemError(`The content folder is not owned by the current user.
Expand Down Expand Up @@ -85,6 +83,9 @@ Please ensure the content folder has correct permissions and try again.`));
* @public
*/
stop(cwd) {
const fkill = require('fkill');
const errors = require('../errors');

let pid;

try {
Expand All @@ -101,9 +102,7 @@ Please ensure the content folder has correct permissions and try again.`));
}));
}

const isWindows = os.platform() === 'win32';

return fkill(pid, {force: isWindows}).catch((error) => {
return fkill(pid, {force: this.system.platform.windows}).catch((error) => {
// TODO: verify windows outputs same error message as mac/linux
if (!error.message.match(/No such process/)) {
return Promise.reject(new errors.CliError({
Expand Down Expand Up @@ -152,6 +151,8 @@ Please ensure the content folder has correct permissions and try again.`));
* @public
*/
isRunning(cwd) {
const isRunning = require('is-running');

const pidfile = path.join(cwd, PID_FILE);

if (!fs.existsSync(pidfile)) {
Expand All @@ -178,7 +179,7 @@ Please ensure the content folder has correct permissions and try again.`));
* @return {Boolean} true if ownership is correct, otherwise false
*/
_checkContentFolder(cwd) {
if (os.platform() === 'win32') {
if (this.system.platform.windows) {
return true;
}

Expand Down
37 changes: 18 additions & 19 deletions test/unit/utils/local-process-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const EventEmitter = require('events').EventEmitter;

const fs = require('fs-extra');
const childProcess = require('child_process');
const os = require('os');

const modulePath = '../../../lib/utils/local-process';

Expand Down Expand Up @@ -269,17 +268,17 @@ describe('Unit: Utils > local-process', function () {
it('calls fkill and removes pidfile', function () {
const readFileStub = sandbox.stub(fs, 'readFileSync').returns('42');
const removeStub = sandbox.stub(fs, 'removeSync');
const platformStub = sandbox.stub(os, 'platform').returns('win32');
const fkillStub = sandbox.stub().resolves();

const LocalProcess = proxyquire(modulePath, {
fkill: fkillStub
});
const instance = new LocalProcess({}, {}, {});
const instance = new LocalProcess({}, {
platform: {windows: true}
}, {});

return instance.stop('/var/www/ghost').then(() => {
expect(readFileStub.calledWithExactly('/var/www/ghost/.ghostpid')).to.be.true;
expect(platformStub.calledOnce).to.be.true;
expect(fkillStub.calledWithExactly(42, {force: true})).to.be.true;
expect(removeStub.calledWithExactly('/var/www/ghost/.ghostpid')).to.be.true;
});
Expand All @@ -288,17 +287,17 @@ describe('Unit: Utils > local-process', function () {
it('resolves if process didn\'t exist', function () {
const readFileStub = sandbox.stub(fs, 'readFileSync').returns('42');
const removeStub = sandbox.stub(fs, 'removeSync');
const platformStub = sandbox.stub(os, 'platform').returns('darwin');
const fkillStub = sandbox.stub().rejects(new Error('No such process: 42'));

const LocalProcess = proxyquire(modulePath, {
fkill: fkillStub
});
const instance = new LocalProcess({}, {}, {});
const instance = new LocalProcess({}, {
platform: {macos: true, windows: false}
}, {});

return instance.stop('/var/www/ghost').then(() => {
expect(readFileStub.calledWithExactly('/var/www/ghost/.ghostpid')).to.be.true;
expect(platformStub.calledOnce).to.be.true;
expect(fkillStub.calledWithExactly(42, {force: false})).to.be.true;
expect(removeStub.calledWithExactly('/var/www/ghost/.ghostpid')).to.be.true;
});
Expand All @@ -307,21 +306,21 @@ describe('Unit: Utils > local-process', function () {
it('rejects with an unknown error from fkill', function (done) {
const readFileStub = sandbox.stub(fs, 'readFileSync').returns('42');
const removeStub = sandbox.stub(fs, 'removeSync');
const platformStub = sandbox.stub(os, 'platform').returns('darwin');
const fkillStub = sandbox.stub().callsFake(() => Promise.reject(new Error('no idea')));

const LocalProcess = proxyquire(modulePath, {
fkill: fkillStub
});
const instance = new LocalProcess({}, {}, {});
const instance = new LocalProcess({}, {
platform: {macos: true, windows: false}
}, {});

instance.stop('/var/www/ghost').then(() => {
done(new Error('stop should have rejected'))
}).catch((error) => {
expect(error).to.be.an.instanceof(errors.CliError);
expect(error.message).to.equal('An unexpected error occurred while stopping Ghost.');
expect(readFileStub.calledWithExactly('/var/www/ghost/.ghostpid')).to.be.true;
expect(platformStub.calledOnce).to.be.true;
expect(fkillStub.calledWithExactly(42, {force: false})).to.be.true;
expect(removeStub.calledOnce).to.be.false;
done();
Expand All @@ -334,41 +333,41 @@ describe('Unit: Utils > local-process', function () {

it('skips if windows', function () {
const statStub = sandbox.stub(fs, 'lstatSync');
const platformStub = sandbox.stub(os, 'platform').returns('win32');
const instance = new LocalProcess({}, {}, {});
const instance = new LocalProcess({}, {
platform: {windows: true}
}, {});

const result = instance._checkContentFolder('/var/www/ghost');

expect(result).to.be.true;
expect(platformStub.calledOnce).to.be.true;
expect(statStub.called).to.be.false;
});

it('returns false if getuid and lstatSync don\'t match', function () {
const platformStub = sandbox.stub(os, 'platform').returns('linux');
const statStub = sandbox.stub(fs, 'lstatSync').returns({uid: 2});
const uidStub = sandbox.stub(process, 'getuid').returns(1);

const instance = new LocalProcess({}, {}, {});
const instance = new LocalProcess({}, {
platform: {linux: true}
}, {});
const result = instance._checkContentFolder('/var/www/ghost');

expect(result).to.be.false;
expect(platformStub.calledOnce).to.be.true;
expect(statStub.calledOnce).to.be.true;
expect(statStub.calledWithExactly('/var/www/ghost/content')).to.be.true;
expect(uidStub.calledOnce).to.be.true;
});

it('returns true if getuid and lstatSync match', function () {
const platformStub = sandbox.stub(os, 'platform').returns('linux');
const statStub = sandbox.stub(fs, 'lstatSync').returns({uid: 1});
const uidStub = sandbox.stub(process, 'getuid').returns(1);

const instance = new LocalProcess({}, {}, {});
const instance = new LocalProcess({}, {
platform: {linux: true}
}, {});
const result = instance._checkContentFolder('/var/www/ghost');

expect(result).to.be.true;
expect(platformStub.calledOnce).to.be.true;
expect(statStub.calledOnce).to.be.true;
expect(statStub.calledWithExactly('/var/www/ghost/content')).to.be.true;
expect(uidStub.calledOnce).to.be.true;
Expand Down

0 comments on commit d791101

Please sign in to comment.