Extended ghost version utility (#9278)

refs https://github.com/TryGhost/Ghost-Release/issues/24

- differentiate between
  1. original package.json version (can contain pre and build suffix)
  2. full package.json version X.X.X-{pre} (optional)
  3. safe package.json version X.X (major+minor)
This commit is contained in:
Katharina Irrgang 2018-01-03 20:20:18 +01:00 committed by GitHub
parent 455a1879d9
commit 341f719d92
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 123 additions and 6 deletions

View File

@ -1,8 +1,27 @@
var packageInfo = require('../../../package.json'),
version = packageInfo.version;
'use strict';
module.exports = {
full: version,
safe: version.match(/^(\d+\.)?(\d+)/)[0]
const semver = require('semver'),
packageInfo = require('../../../package.json'),
version = packageInfo.version,
plainVersion = version.match(/^(\d+\.)?(\d+\.)?(\d+)/)[0];
let _private = {};
_private.compose = function compose(type) {
switch (type) {
case 'pre':
return plainVersion + '-' + semver.prerelease(version)[0] + (semver.prerelease(version)[1] ? '.' + semver.prerelease(version)[1] : '');
default:
return version;
}
};
// major.minor
module.exports.safe = version.match(/^(\d+\.)?(\d+)/)[0];
// major.minor.patch-{prerelease}
module.exports.full = semver.prerelease(version) ? _private.compose('pre') : plainVersion;
// original string in package.json (can contain pre-release and build suffix)
module.exports.original = version;

View File

@ -3,7 +3,8 @@ var semver = require('semver'),
function checkVersionMatch(req, res, next) {
var clientVersion = req.get('X-Ghost-Version'),
serverVersion = res.locals.version,
// can contain pre-release suffix, you should be able to use e.g. 1.19.0-pre [server] with 1.18.0 [client]
serverVersion = res.locals.version.match(/^(\d+\.)?(\d+\.)?(\d+)/)[0],
constraint = '^' + clientVersion + '.0';
// no error when client is on an earlier minor version than server

View File

@ -0,0 +1,75 @@
'use strict';
// jshint unused: false
const should = require('should'),
rewire = require('rewire'),
testUtils = require('../../utils');
let ghostVersionUtils,
version;
describe('Utils: Ghost Version', function () {
const beforeEachIt = function be() {
testUtils.mockNotExistingModule(/package\.json/, {version: version});
ghostVersionUtils = rewire('../../../server/lib/ghost-version');
};
afterEach(function () {
testUtils.unmockNotExistingModule(/package\.json/);
});
it('default', function () {
version = '1.10.0';
beforeEachIt();
ghostVersionUtils.full.should.eql(version);
ghostVersionUtils.original.should.eql(version);
ghostVersionUtils.safe.should.eql('1.10');
});
it('pre-release', function () {
version = '1.11.1-beta';
beforeEachIt();
ghostVersionUtils.full.should.eql(version);
ghostVersionUtils.original.should.eql(version);
ghostVersionUtils.safe.should.eql('1.11');
});
it('pre-release .1', function () {
version = '1.11.1-alpha.1';
beforeEachIt();
ghostVersionUtils.full.should.eql(version);
ghostVersionUtils.original.should.eql(version);
ghostVersionUtils.safe.should.eql('1.11');
});
it('build', function () {
version = '1.11.1+build';
beforeEachIt();
ghostVersionUtils.full.should.eql('1.11.1');
ghostVersionUtils.original.should.eql(version);
ghostVersionUtils.safe.should.eql('1.11');
});
it('mixed', function () {
version = '1.11.1-pre+build.1';
beforeEachIt();
ghostVersionUtils.full.should.eql('1.11.1-pre');
ghostVersionUtils.original.should.eql(version);
ghostVersionUtils.safe.should.eql('1.11');
});
it('mixed 1', function () {
version = '1.11.1-beta.12+build.2';
beforeEachIt();
ghostVersionUtils.full.should.eql('1.11.1-beta.12');
ghostVersionUtils.original.should.eql(version);
ghostVersionUtils.safe.should.eql('1.11');
});
});

View File

@ -99,4 +99,26 @@ describe('Version Mismatch', function () {
nextStub.firstCall.args[0].should.have.property('errorType', 'VersionMismatchError');
nextStub.firstCall.args[0].should.have.property('statusCode', 400);
});
it('should call next if pre-release is allowed', function () {
var server = '1.5.0-pre',
client = '1.4';
testVersionMatch(server, client);
nextStub.calledOnce.should.be.true();
nextStub.firstCall.args.should.be.empty();
});
it('throws error if server is a pre-release, but later by major version', function () {
var server = '2.0.0-alpha',
client = '1.5';
testVersionMatch(server, client);
nextStub.calledOnce.should.be.true();
nextStub.firstCall.args.should.have.lengthOf(1);
nextStub.firstCall.args[0].should.have.property('errorType', 'VersionMismatchError');
nextStub.firstCall.args[0].should.have.property('statusCode', 400);
});
});