'use strict'; const sinon = require('sinon'); const proxyquire = require('proxyquire'); const errors = require('../../../lib/errors'); const EventEmitter = require('events').EventEmitter; const fs = require('node:fs'); const os = require('node:os'); const childProcess = require('child_process'); const modulePath = '../../../lib/utils/local-process'; describe('Unit: Utils > local-process', function () { beforeAll(() => { process.send = process.send || (() => {}); }); afterEach(() => { // This is here so we at least have a function to stub out sinon.restore(); }); it('willRun returns true', function () { const LocalProcess = require(modulePath); expect(LocalProcess.willRun()).to.be.true; }); it('success works', function () { const successStub = sinon.stub(process, 'send'); const LocalProcess = require(modulePath); const instance = new LocalProcess({}, {}, {}); instance.success(); expect(successStub.calledOnce).to.be.true; expect(successStub.calledWithExactly({started: true})).to.be.true; }); it('error works', function () { const errorStub = sinon.stub(process, 'send'); const LocalProcess = require(modulePath); const instance = new LocalProcess({}, {}, {}); instance.error({message: 'Test Error'}); expect(errorStub.calledOnce).to.be.true; expect(errorStub.calledWithExactly({error: true, message: 'Test Error'})).to.be.true; }); describe('isRunning', function () { it("returns false if pidfile doesn't exist", async function () { const existsStub = sinon.stub(fs, 'existsSync').returns(false); const LocalProcess = require(modulePath); const instance = new LocalProcess({}, {}, {}); const result = await instance.isRunning('/var/www/ghost'); expect(result).to.be.false; expect(existsStub.calledWithExactly('/var/www/ghost/.ghostpid')).to.be.true; }); it('fetches pid from file, removes pidfile and returns false if not a ghost process', async function () { const existsStub = sinon.stub(fs, 'existsSync').returns(true); const readFileStub = sinon.stub(fs, 'readFileSync').callThrough(); readFileStub.withArgs('/var/www/ghost/.ghostpid').returns('42'); const removeStub = sinon.stub(fs, 'rmSync'); const LocalProcess = require(modulePath); const instance = new LocalProcess({}, {}, {}); const ghostProcessStub = sinon.stub(instance, '_isGhostProcess').resolves(false); const result = await instance.isRunning('/var/www/ghost'); expect(result).to.be.false; expect(existsStub.calledWithExactly('/var/www/ghost/.ghostpid')).to.be.true; expect(readFileStub.calledOnce).to.be.true; expect(ghostProcessStub.calledWithExactly(42)).to.be.true; expect(removeStub.calledWithExactly('/var/www/ghost/.ghostpid', {recursive: true, force: true})).to.be.true; }); it('fetches pid from file, returns true if running ghost process', async function () { const existsStub = sinon.stub(fs, 'existsSync').returns(true); const readFileStub = sinon.stub(fs, 'readFileSync').callThrough(); readFileStub.withArgs('/var/www/ghost/.ghostpid').returns('42'); const removeStub = sinon.stub(fs, 'rmSync'); const LocalProcess = require(modulePath); const instance = new LocalProcess({}, {}, {}); const ghostProcessStub = sinon.stub(instance, '_isGhostProcess').resolves(true); const result = await instance.isRunning('/var/www/ghost'); expect(result).to.be.true; expect(existsStub.calledWithExactly('/var/www/ghost/.ghostpid')).to.be.true; expect(readFileStub.calledOnce).to.be.true; expect(ghostProcessStub.calledWithExactly(42)).to.be.true; expect(removeStub.called).to.be.false; }); }); describe('start', function () { it('errors if _checkContentFolder returns false', async function () { const LocalProcess = require(modulePath); const instance = new LocalProcess({}, {}, {}); const checkStub = sinon.stub(instance, '_checkContentFolder').returns(false); const error = await instance.start('/var/www/ghost', 'development').catch(e => e); expect(error).to.be.an.instanceof(errors.SystemError); expect(error.message).to.match(/content folder is not owned by the current user/); expect(checkStub.calledWithExactly('/var/www/ghost')).to.be.true; }); it('writes pid to file, rejects on error event', async function () { const cp = new EventEmitter(); cp.stderr = { on: sinon.stub(), destroy: sinon.stub() }; cp.pid = 42; const spawnStub = sinon.stub(childProcess, 'spawn').returns(cp); const writeFileStub = sinon.stub(fs, 'writeFileSync'); const LocalProcess = require(modulePath); const instance = new LocalProcess({}, {}, {}); const checkStub = sinon.stub(instance, '_checkContentFolder').returns(true); const startPromise = instance.start('/var/www/ghost', 'production'); expect(checkStub.calledWithExactly('/var/www/ghost')).to.be.true; cp.emit('error', {message: 'something happened'}); const error = await startPromise.catch(e => e); expect(error).to.be.an.instanceof(errors.CliError); expect(error.message).to.equal('An error occurred while starting Ghost.'); expect(spawnStub.calledOnce).to.be.true; expect(cp.stderr.on.calledOnce).to.be.true; expect(writeFileStub.calledWithExactly('/var/www/ghost/.ghostpid', '42')).to.be.true; }); it('writes pid to file, rejects on exit event', async function () { const cp = new EventEmitter(); cp.stderr = { on: sinon.stub(), destroy: sinon.stub() }; cp.pid = 42; const spawnStub = sinon.stub(childProcess, 'spawn').returns(cp); const writeFileStub = sinon.stub(fs, 'writeFileSync'); const removeStub = sinon.stub(fs, 'rmSync'); const LocalProcess = require(modulePath); const instance = new LocalProcess({}, {}, {}); const checkStub = sinon.stub(instance, '_checkContentFolder').returns(true); const startPromise = instance.start('/var/www/ghost', 'production'); expect(checkStub.calledWithExactly('/var/www/ghost')).to.be.true; cp.emit('exit', 1); const error = await startPromise.catch(e => e); expect(error).to.be.an.instanceof(errors.GhostError); expect(error.message).to.equal('Ghost process exited with code: 1'); expect(spawnStub.calledOnce).to.be.true; expect(cp.stderr.on.calledOnce).to.be.true; expect(writeFileStub.calledWithExactly('/var/www/ghost/.ghostpid', '42')).to.be.true; expect(removeStub.calledWithExactly('/var/www/ghost/.ghostpid', {recursive: true, force: true})).to.be.true; }); it('writes pid to file, rejects on message event with error', async function () { const cp = new EventEmitter(); cp.stderr = { on: sinon.stub(), destroy: sinon.stub() }; cp.pid = 42; const spawnStub = sinon.stub(childProcess, 'spawn').returns(cp); const writeFileStub = sinon.stub(fs, 'writeFileSync'); const removeStub = sinon.stub(fs, 'rmSync'); const LocalProcess = require(modulePath); const instance = new LocalProcess({}, {}, {}); const checkStub = sinon.stub(instance, '_checkContentFolder').returns(true); const startPromise = instance.start('/var/www/ghost', 'production'); expect(checkStub.calledWithExactly('/var/www/ghost')).to.be.true; cp.emit('message', {error: true, message: 'Test Error Message'}); const error = await startPromise.catch(e => e); expect(error).to.be.an.instanceof(errors.GhostError); expect(error.message).to.equal('Test Error Message'); expect(spawnStub.called).to.be.true; expect(cp.stderr.on.calledOnce).to.be.true; expect(writeFileStub.calledWithExactly('/var/www/ghost/.ghostpid', '42')).to.be.true; expect(removeStub.calledWithExactly('/var/www/ghost/.ghostpid', {recursive: true, force: true})).to.be.true; }); it('writes pid to file, resolves on start message', async function () { const cp = new EventEmitter(); cp.stderr = { on: sinon.stub(), destroy: sinon.stub() }; cp.pid = 42; cp.unref = sinon.stub(); cp.disconnect = sinon.stub(); const spawnStub = sinon.stub(childProcess, 'spawn').returns(cp); const writeFileStub = sinon.stub(fs, 'writeFileSync'); const LocalProcess = require(modulePath); const instance = new LocalProcess({}, {}, {}); const checkStub = sinon.stub(instance, '_checkContentFolder').returns(true); const startPromise = instance.start('/var/www/ghost', 'production'); expect(checkStub.calledWithExactly('/var/www/ghost')).to.be.true; cp.emit('message', {started: true}); await startPromise; expect(spawnStub.calledOnce).to.be.true; expect(writeFileStub.calledWithExactly('/var/www/ghost/.ghostpid', '42')).to.be.true; expect(cp.stderr.on.calledOnce).to.be.true; expect(cp.stderr.destroy.calledOnce).to.be.true; expect(cp.disconnect.calledOnce).to.be.true; expect(cp.unref.calledOnce).to.be.true; }); }); describe('stop', function () { it('returns if pidfile not found', function () { const fkillStub = sinon.stub(); const LocalProcess = proxyquire(modulePath, { fkill: {default: fkillStub} }); // only throw for the pidfile - the CJS loader reads source via readFileSync too const readFileStub = sinon.stub(fs, 'readFileSync').callThrough(); readFileStub.withArgs('/var/www/ghost/.ghostpid').throws({code: 'ENOENT'}); const instance = new LocalProcess({}, {}, {}); return instance.stop('/var/www/ghost').then(() => { expect(readFileStub.calledWithExactly('/var/www/ghost/.ghostpid')).to.be.true; expect(fkillStub.called).to.be.false; }); }); it('rejects if any unexpected error occurs during reading of pidfile', async function () { const fkillStub = sinon.stub(); const LocalProcess = proxyquire(modulePath, { fkill: {default: fkillStub} }); const readFileStub = sinon.stub(fs, 'readFileSync').callThrough(); readFileStub.withArgs('/var/www/ghost/.ghostpid').throws(new Error('test error')); const instance = new LocalProcess({}, {}, {}); const error = await instance.stop('/var/www/ghost').catch(e => e); expect(error).to.be.an.instanceof(errors.CliError); expect(error.message).to.equal('An unexpected error occurred when reading the pidfile.'); expect(readFileStub.calledWithExactly('/var/www/ghost/.ghostpid')).to.be.true; expect(fkillStub.called).to.be.false; }); it('does not kill the process but removes the pidfile if not a ghost process', async function () { const fkillStub = sinon.stub(); const LocalProcess = proxyquire(modulePath, { fkill: {default: fkillStub} }); const readFileStub = sinon.stub(fs, 'readFileSync').callThrough(); readFileStub.withArgs('/var/www/ghost/.ghostpid').returns('42'); const removeStub = sinon.stub(fs, 'rmSync'); const instance = new LocalProcess( {}, { platform: {macos: true, windows: false} }, {} ); const ghostProcessStub = sinon.stub(instance, '_isGhostProcess').resolves(false); await instance.stop('/var/www/ghost'); expect(readFileStub.calledWithExactly('/var/www/ghost/.ghostpid')).to.be.true; expect(ghostProcessStub.calledWithExactly(42)).to.be.true; expect(fkillStub.called).to.be.false; expect(removeStub.calledWithExactly('/var/www/ghost/.ghostpid', {recursive: true, force: true})).to.be.true; }); it('kills the process and removes the pidfile', async function () { let isChildRunning = true; const program = os.platform() === 'win32' ? 'timeout' : 'sleep'; const child = childProcess.spawn(program, ['10000']); child.once('close', () => { isChildRunning = false; }); const LocalProcess = require(modulePath); const readFileStub = sinon.stub(fs, 'readFileSync').callThrough(); readFileStub.withArgs('/var/www/ghost/.ghostpid').returns(child.pid.toString()); const removeStub = sinon.stub(fs, 'rmSync'); const instance = new LocalProcess( {}, { platform: {windows: true} }, {} ); sinon.stub(instance, '_isGhostProcess').resolves(true); expect(isChildRunning).to.be.true; await instance.stop('/var/www/ghost'); expect(isChildRunning).to.be.false; expect(readFileStub.calledWithExactly('/var/www/ghost/.ghostpid')).to.be.true; expect(removeStub.calledWithExactly('/var/www/ghost/.ghostpid', {recursive: true, force: true})).to.be.true; }); it("resolves if process didn't exist", function () { const fkillStub = sinon.stub().rejects(new Error('No such process: 42')); const LocalProcess = proxyquire(modulePath, { fkill: {default: fkillStub} }); const readFileStub = sinon.stub(fs, 'readFileSync').callThrough(); readFileStub.withArgs('/var/www/ghost/.ghostpid').returns('42'); const removeStub = sinon.stub(fs, 'rmSync'); const instance = new LocalProcess( {}, { platform: {macos: true, windows: false} }, {} ); sinon.stub(instance, '_isGhostProcess').resolves(true); return instance.stop('/var/www/ghost').then(() => { expect(readFileStub.calledWithExactly('/var/www/ghost/.ghostpid')).to.be.true; expect(fkillStub.calledWithExactly(42, sinon.match({force: false}))).to.be.true; expect(removeStub.calledWithExactly('/var/www/ghost/.ghostpid', {recursive: true, force: true})).to.be .true; }); }); it('rejects with an unknown error from fkill', async function () { const fkillStub = sinon.stub().callsFake(() => Promise.reject(new Error('no idea'))); const LocalProcess = proxyquire(modulePath, { fkill: {default: fkillStub} }); const readFileStub = sinon.stub(fs, 'readFileSync').callThrough(); readFileStub.withArgs('/var/www/ghost/.ghostpid').returns('42'); const removeStub = sinon.stub(fs, 'rmSync'); const instance = new LocalProcess( {}, { platform: {macos: true, windows: false} }, {} ); sinon.stub(instance, '_isGhostProcess').resolves(true); const error = await instance.stop('/var/www/ghost').catch(e => e); 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(fkillStub.calledWithExactly(42, sinon.match({force: false}))).to.be.true; expect(removeStub.calledOnce).to.be.false; }); }); describe('_isGhostProcess', function () { it('returns false for a falsy or NaN pid', async function () { const LocalProcess = require(modulePath); const instance = new LocalProcess({}, {}, {}); expect(await instance._isGhostProcess(0)).to.be.false; expect(await instance._isGhostProcess(NaN)).to.be.false; }); it('returns false if no process matches the pid', async function () { const processesStub = sinon .stub() .resolves({list: [{pid: 99, command: 'node', params: '/usr/lib/ghost-cli/bin/ghost run'}]}); const LocalProcess = proxyquire(modulePath, { systeminformation: {processes: processesStub} }); const instance = new LocalProcess({}, {}, {}); expect(await instance._isGhostProcess(42)).to.be.false; expect(processesStub.calledOnce).to.be.true; }); it('returns false if the process is not a ghost run process', async function () { const processesStub = sinon.stub().resolves({list: [{pid: 42, command: 'sleep', params: '10000'}]}); const LocalProcess = proxyquire(modulePath, { systeminformation: {processes: processesStub} }); const instance = new LocalProcess({}, {}, {}); expect(await instance._isGhostProcess(42)).to.be.false; }); it('returns true if the process command line contains "ghost run"', async function () { const processesStub = sinon .stub() .resolves({list: [{pid: 42, command: 'node', params: '/usr/lib/ghost-cli/bin/ghost run'}]}); const LocalProcess = proxyquire(modulePath, { systeminformation: {processes: processesStub} }); const instance = new LocalProcess({}, {}, {}); expect(await instance._isGhostProcess(42)).to.be.true; }); }); describe('_checkContentFolder', function () { let LocalProcess; beforeEach(() => { LocalProcess = proxyquire(modulePath, {}); }); it('skips if windows', function () { const statStub = sinon.stub(fs, 'lstatSync'); const instance = new LocalProcess( {}, { platform: {windows: true} }, {} ); const result = instance._checkContentFolder('/var/www/ghost'); expect(result).to.be.true; expect(statStub.called).to.be.false; }); it('returns true if getuid and lstatSync match', function () { const statStub = sinon.stub(fs, 'lstatSync').returns({uid: 1, mode: 0o750}); const uidStub = sinon.stub(process, 'getuid').returns(1); const instance = new LocalProcess( {}, { platform: {linux: true} }, {} ); const result = instance._checkContentFolder('/var/www/ghost'); expect(result).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 don't match, but current user has read&write permissions", function () { const statStub = sinon.stub(fs, 'lstatSync').returns({uid: 2, mode: 0o757}); const uidStub = sinon.stub(process, 'getuid').returns(1); const instance = new LocalProcess( {}, { platform: {linux: true} }, {} ); const result = instance._checkContentFolder('/var/www/ghost'); expect(result).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 false if getuid and lstatSync don't match, and current user doesn't have read&write permissions", function () { const statStub = sinon.stub(fs, 'lstatSync').returns({uid: 2, mode: 0o755}); const uidStub = sinon.stub(process, 'getuid').returns(1); const instance = new LocalProcess( {}, { platform: {linux: true} }, {} ); const result = instance._checkContentFolder('/var/www/ghost'); expect(result).to.be.false; expect(statStub.calledOnce).to.be.true; expect(statStub.calledWithExactly('/var/www/ghost/content')).to.be.true; expect(uidStub.calledOnce).to.be.true; }); }); });