Skip to content
This repository has been archived by the owner on Nov 10, 2022. It is now read-only.

fix: enforce strict equality on prerelease versions #94

Merged
merged 5 commits into from
Jul 2, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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
16 changes: 15 additions & 1 deletion src/internal/semver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import { VERSION } from '../version';

const re = /^(\d+)\.(\d+)\.(\d+)(?:-(.*))?$/;
const re = /^(\d+)\.(\d+)\.(\d+)(-(.+))?$/;

/**
* Create a function to test an API version to see if it is compatible with the provided ownVersion.
Expand Down Expand Up @@ -50,8 +50,16 @@ export function _makeCompatibilityCheck(
major: +myVersionMatch[1],
minor: +myVersionMatch[2],
patch: +myVersionMatch[3],
prerelease: myVersionMatch[4],
};

// if ownVersion has a prerelease tag, versions must match exactly
if (ownVersionParsed.prerelease != null) {
return function isExactmatch(globalVersion: string): boolean {
return globalVersion === ownVersion;
};
}

function _reject(v: string) {
rejectedVersions.add(v);
return false;
Expand Down Expand Up @@ -82,8 +90,14 @@ export function _makeCompatibilityCheck(
major: +globalVersionMatch[1],
minor: +globalVersionMatch[2],
patch: +globalVersionMatch[3],
prerelease: globalVersionMatch[4],
};

// if globalVersion has a prerelease tag, versions must match exactly
if (globalVersionParsed.prerelease != null) {
return _reject(globalVersion);
}

// major versions must match
if (ownVersionParsed.major !== globalVersionParsed.major) {
return _reject(globalVersion);
Expand Down
25 changes: 20 additions & 5 deletions test/internal/semver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ describe('semver', () => {
['5.5.6', true],
['5.5.4', true],

// prerelease version should not be compatible
['5.5.5-rc.0', false],

// if our version has a minor version increase, we may try to call methods which don't exist on the global
['5.6.5', false],
['5.6.6', false],
Expand Down Expand Up @@ -84,6 +87,9 @@ describe('semver', () => {
// same minor/patch should be compatible
['0.5.5', true],

// prerelease version should not be compatible
['0.5.5-rc.0', false],

// if our version has a patch version increase, we may try to call methods which don't exist on the global
['0.5.6', false],

Expand Down Expand Up @@ -112,21 +118,30 @@ describe('semver', () => {

test(globalVersion, vers);
});

describe('global version is prerelease', () => {
const globalVersion = '1.0.0-rc.3';
const vers: [string, boolean][] = [
// must match exactly
['1.0.0', false],
['1.0.0-rc.2', false],
['1.0.0-rc.4', false],

['1.0.0-rc.3', true],
];

test(globalVersion, vers);
});
});

function test(globalVersion: string, vers: [string, boolean][]) {
describe(`global version is ${globalVersion}`, () => {
for (const [version, compatible] of vers) {
const alphaVersion = `${version}-alpha.1`;
it(`API version ${version} ${
compatible ? 'should' : 'should not'
} be able to access global`, () => {
const check = _makeCompatibilityCheck(version);
assert.strictEqual(check(globalVersion), compatible);

// alpha tag should have no effect different than the regular version
const alphaCheck = _makeCompatibilityCheck(alphaVersion);
assert.strictEqual(alphaCheck(globalVersion), compatible);
});
}
});
Expand Down