Skip to content

Commit

Permalink
chore: enforce strict equality on prerelease versions (open-telemetry#94
Browse files Browse the repository at this point in the history
)
  • Loading branch information
dyladan committed Jul 2, 2021
1 parent 4aa44e5 commit 3ed28ac
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
16 changes: 15 additions & 1 deletion api/src/internal/semver.ts
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 api/test/internal/semver.test.ts
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

0 comments on commit 3ed28ac

Please sign in to comment.