forked from TryGhost/Ghost-CLI
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(stop): abstract instance getter to its own utility
- Loading branch information
1 parent
9e27b59
commit 9a7ddc2
Showing
3 changed files
with
87 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
'use strict'; | ||
|
||
const checkValidInstall = require('./check-valid-install'); | ||
const {SystemError} = require('../errors'); | ||
|
||
function findInstance(instanceName, system, commandName) { | ||
const instance = system.getInstance(instanceName); | ||
|
||
if (instanceName) { | ||
if (!instance) { | ||
throw new SystemError(`Ghost instance "${instanceName}" does not exist`); | ||
} | ||
|
||
process.chdir(instance.dir); | ||
} | ||
|
||
checkValidInstall(commandName); | ||
|
||
return instance; | ||
} | ||
|
||
module.exports = findInstance; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
'use strict'; | ||
const {expect} = require('chai'); | ||
const sinon = require('sinon'); | ||
const proxyquire = require('proxyquire').noCallThru(); | ||
const {SystemError} = require('../../../lib/errors'); | ||
const modulePath = '../../../lib/utils/get-instance'; | ||
|
||
describe('Unit: Utils > getInstance', function () { | ||
let getInstance, stubs, system; | ||
beforeEach(function () { | ||
stubs = { | ||
checkValidInstall: sinon.stub().callsFake(a => a), | ||
chdir: sinon.stub(process, 'chdir'), | ||
getInstance: sinon.stub().returns('It\'s-a Me, Mario!') | ||
}; | ||
|
||
system = {getInstance: stubs.getInstance}; | ||
getInstance = proxyquire(modulePath, { | ||
'./check-valid-install': stubs.checkValidInstall | ||
}); | ||
}); | ||
|
||
this.afterEach(function () { | ||
sinon.restore(); | ||
}); | ||
|
||
it('Doesn\'t change directory by default', function () { | ||
const result = getInstance(undefined, system, 'test'); | ||
|
||
expect(result).to.equal('It\'s-a Me, Mario!'); | ||
expect(stubs.getInstance.calledOnce).to.be.true; | ||
expect(stubs.chdir.called).to.be.false; | ||
expect(stubs.checkValidInstall.calledOnce).to.be.true; | ||
expect(stubs.checkValidInstall.calledWithExactly('test')).to.be.true; | ||
}); | ||
|
||
it('Fails if the instance cannot be found', function () { | ||
stubs.getInstance.returns(null); | ||
|
||
try { | ||
getInstance('ghosted', system, 'test'); | ||
expect(false, 'Promise should have rejected').to.be.true; | ||
} catch (error) { | ||
expect(error).to.be.instanceof(SystemError); | ||
expect(error.message).to.equal('Ghost instance "ghosted" does not exist'); | ||
} | ||
}); | ||
|
||
it('Chdirs into instance directory when it exists', function () { | ||
const dir = '/path/to/ghost'; | ||
stubs.getInstance.returns({dir}); | ||
|
||
const result = getInstance('i', system, 'test'); | ||
|
||
expect(result.dir).to.equal(dir); | ||
expect(stubs.chdir.calledOnce).to.to.true; | ||
expect(stubs.chdir.calledWithExactly(dir)).to.be.true; | ||
}); | ||
}) |