Skip to content

Commit

Permalink
feat: add check-update command (#881)
Browse files Browse the repository at this point in the history
closes #876
- add check-update command to check for available updates
  • Loading branch information
acburdine authored May 29, 2019
1 parent 8a17c8b commit 527104b
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 0 deletions.
34 changes: 34 additions & 0 deletions lib/commands/check-update.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const Command = require('../command');

class CheckUpdateCommand extends Command {
run({v1}) {
const semver = require('semver');
const resolveVersion = require('../utils/resolve-version');
const {CliError} = require('../errors');

const instance = this.system.getInstance();

if (instance.version) {
return resolveVersion(null, instance.version, v1).then((version) => {
const diff = semver.diff(instance.version, version);
this.ui.log(`New ${diff} version available: ${version}`);
}).catch((error) => {
if (!(error instanceof CliError) || !error.message.match(/No valid versions/)) {
return Promise.reject(error);
}
});
}
}
}

CheckUpdateCommand.description = 'Check if an update is available for a Ghost installation';
CheckUpdateCommand.allowRoot = true;
CheckUpdateCommand.options = {
v1: {
describe: 'Limit check to Ghost 1.x releases',
type: 'boolean',
default: false
}
};

module.exports = CheckUpdateCommand;
84 changes: 84 additions & 0 deletions test/unit/commands/check-update-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
const sinon = require('sinon');
const proxyquire = require('proxyquire');
const {expect} = require('chai');
const {CliError} = require('../../../lib/errors');

const modulePath = '../../../lib/commands/check-update';

describe('Unit: Commands > check-update', function () {
it('doesn\'t output anything if instance doesn\'t exist', function () {
const CheckUpdateCommand = require(modulePath);
const log = sinon.stub();
const getInstance = sinon.stub().returns({version: null});

const cmd = new CheckUpdateCommand({log}, {getInstance});

cmd.run({v1: false});

expect(getInstance.calledOnce).to.be.true;
expect(log.called).to.be.false;
});

it('doesn\'t output anything if no new versions are available', function () {
const resolveVersion = sinon.stub().rejects(new CliError('No valid versions found.'));

const CheckUpdateCommand = proxyquire(modulePath, {
'../utils/resolve-version': resolveVersion
});

const log = sinon.stub();
const getInstance = sinon.stub().returns({version: '2.0.0'});

const cmd = new CheckUpdateCommand({log}, {getInstance});

return cmd.run({v1: false}).then(() => {
expect(getInstance.calledOnce).to.be.true;
expect(resolveVersion.calledOnce).to.be.true;
expect(resolveVersion.calledWithExactly(null, '2.0.0', false)).to.be.true;
expect(log.called).to.be.false;
});
});

it('rejects errors from resolveVersion', function () {
const resolveVersion = sinon.stub().rejects(new CliError('An error occurred'));

const CheckUpdateCommand = proxyquire(modulePath, {
'../utils/resolve-version': resolveVersion
});

const log = sinon.stub();
const getInstance = sinon.stub().returns({version: '2.0.0'});

const cmd = new CheckUpdateCommand({log}, {getInstance});

return cmd.run({v1: true}).then(() => {
expect(false, 'error should have been thrown').to.be.false;
}).catch((error) => {
expect(error).to.be.an.instanceof(CliError);
expect(getInstance.calledOnce).to.be.true;
expect(resolveVersion.calledOnce).to.be.true;
expect(resolveVersion.calledWithExactly(null, '2.0.0', true)).to.be.true;
expect(log.called).to.be.false;
});
});

it('logs out available new version', function () {
const resolveVersion = sinon.stub().resolves('2.1.0');

const CheckUpdateCommand = proxyquire(modulePath, {
'../utils/resolve-version': resolveVersion
});

const log = sinon.stub();
const getInstance = sinon.stub().returns({version: '2.0.0'});

const cmd = new CheckUpdateCommand({log}, {getInstance});

return cmd.run({v1: true}).then(() => {
expect(getInstance.calledOnce).to.be.true;
expect(resolveVersion.calledOnce).to.be.true;
expect(resolveVersion.calledWithExactly(null, '2.0.0', true)).to.be.true;
expect(log.calledOnce).to.be.true;
});
});
});

0 comments on commit 527104b

Please sign in to comment.