Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add check-update command #881

Merged
merged 1 commit into from
May 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
});
});
});