test: Refactor docker versioning tests (#11516)

This commit is contained in:
Sergei Zharinov 2021-09-01 11:12:00 +03:00 committed by GitHub
parent 3f5d13ee47
commit f1d22c98f4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -2,128 +2,122 @@ import semver from '../semver';
import docker from '.';
describe('versioning/docker/index', () => {
describe('isValid(version)', () => {
it('should support all versions length', () => {
expect(docker.isValid(null)).toBeNull();
expect(docker.isValid('1.2.3')).toBe('1.2.3');
expect(docker.isValid('18.04')).toBe('18.04');
expect(docker.isValid('10.1')).toBe('10.1');
expect(docker.isValid('3')).toBe('3');
expect(docker.isValid('foo')).toBeNull();
});
it('should return null if the version string looks like a git commit hash', () => {
[
'0a1b2c3',
'0a1b2c3d',
'0a1b2c3d4e5f6a7b8c9d0a1b2c3d4e5f6a7b8c9d',
].forEach((version) => {
expect(docker.isValid(version)).toBeNull();
});
[
'0a1b2c3d4e5f6a7b8c9d0a1b2c3d4e5f6a7b8c9d0',
'0a1b2C3',
'0z1b2c3',
'0A1b2c3d4e5f6a7b8c9d0a1b2c3d4e5f6a7b8c9d',
'123098140293',
].forEach((version) => {
expect(docker.isValid(version)).toBe(version);
});
});
test.each`
version | expected
${null} | ${false}
${'1.2.3'} | ${true}
${'18.04'} | ${true}
${'10.1'} | ${true}
${'3'} | ${true}
${'foo'} | ${false}
${'0a1b2c3'} | ${false}
${'0a1b2c3d'} | ${false}
${'0a1b2c3d4e5f6a7b8c9d0a1b2c3d4e5f6a7b8c9d'} | ${false}
${'0a1b2c3d4e5f6a7b8c9d0a1b2c3d4e5f6a7b8c9d0'} | ${true}
${'0a1b2C3'} | ${true}
${'0z1b2c3'} | ${true}
${'0A1b2c3d4e5f6a7b8c9d0a1b2c3d4e5f6a7b8c9d'} | ${true}
${'123098140293'} | ${true}
`('isValid("$version") === $expected', ({ version, expected }) => {
const res = docker.isValid(version);
expect(!!res).toBe(expected);
});
describe('getMajor(version)', () => {
it('should support all versions length', () => {
expect(docker.getMajor('1.2.3')).toBe(1);
expect(docker.getMajor('18.04')).toBe(18);
expect(docker.getMajor('10.1')).toBe(10);
expect(docker.getMajor('3')).toBe(3);
expect(docker.getMajor('foo')).toBeNull();
});
test.each`
version | major | minor | patch
${'1.2.3'} | ${1} | ${2} | ${3}
${'18.04'} | ${18} | ${4} | ${null}
${'10.1'} | ${10} | ${1} | ${null}
${'3'} | ${3} | ${null} | ${null}
${'foo'} | ${null} | ${null} | ${null}
`(
'getMajor, getMinor, getPatch for "$version"',
({ version, major, minor, patch }) => {
expect(docker.getMajor(version)).toBe(major);
expect(docker.getMinor(version)).toBe(minor);
expect(docker.getPatch(version)).toBe(patch);
}
);
test.each`
a | b | expected
${'1.2.3'} | ${'1.2'} | ${false}
${'18.04'} | ${'18.1'} | ${true}
${'10.1'} | ${'10.1.2'} | ${true}
${'3'} | ${'2'} | ${true}
${'1.2.3'} | ${'1.2.3'} | ${false}
`('isGreaterThan($a, $b) === $expected', ({ a, b, expected }) => {
expect(docker.isGreaterThan(a, b)).toBe(expected);
});
describe('getMinor(version)', () => {
it('should support all versions length', () => {
expect(docker.getMinor('1.2.3')).toBe(2);
expect(docker.getMinor('18.04')).toBe(4);
expect(docker.getMinor('10.1')).toBe(1);
expect(docker.getMinor('3')).toBeNull();
expect(docker.getMinor('foo')).toBeNull();
});
test.each`
version | range | expected
${'1.2.3'} | ${'2.0'} | ${true}
${'18.04'} | ${'18.1'} | ${false}
${'10.1'} | ${'10.0.4'} | ${false}
${'3'} | ${'4.0'} | ${true}
${'1.2'} | ${'1.3.4'} | ${true}
`(
'isLessThanRange($version, $range) === $expected',
({ version, range, expected }) => {
expect(docker.isLessThanRange(version, range)).toBe(expected);
}
);
test.each`
a | b | expected
${'1.2.3'} | ${'1.2.3'} | ${true}
${'18.04'} | ${'18.4'} | ${true}
${'10.0'} | ${'10.0.4'} | ${false}
${'3'} | ${'4.0'} | ${false}
${'1.2'} | ${'1.2.3'} | ${false}
`('equals($a, $b) === $expected', ({ a, b, expected }) => {
expect(docker.equals(a, b)).toBe(expected);
});
describe('getPatch(version)', () => {
it('should support all versions length', () => {
expect(docker.getPatch('1.2.3')).toBe(3);
expect(docker.getPatch('18.04')).toBeNull();
expect(docker.getPatch('10.1')).toBeNull();
expect(docker.getPatch('3')).toBeNull();
expect(docker.getPatch('foo')).toBeNull();
describe('Satisfying versions', () => {
const versions = [
'0.9.8',
'1.1.1',
'1.1',
'1.2.3',
'1.2',
'1',
'2.2.2',
'2.2',
'2',
];
test.each`
version | expected
${'1.2.3'} | ${'1.2.3'}
${'1.2'} | ${'1.2'}
${'1'} | ${'1'}
${'1.3'} | ${null}
${'0.9'} | ${null}
`(`satisfying for $version -> $expected`, ({ version, expected }) => {
const satisfying = docker.getSatisfyingVersion(versions, version);
const minSatisfying = docker.minSatisfyingVersion(versions, version);
expect(satisfying).toBe(expected);
expect(minSatisfying).toBe(expected);
});
});
describe('isGreaterThan(version, other)', () => {
it('should support all versions length', () => {
expect(docker.isGreaterThan('1.2.3', '1.2')).toBe(false);
expect(docker.isGreaterThan('18.04', '18.1')).toBe(true);
expect(docker.isGreaterThan('10.1', '10.1.2')).toBe(true);
expect(docker.isGreaterThan('3', '2')).toBe(true);
expect(docker.isGreaterThan('1.2.3', '1.2.3')).toBe(false);
});
});
describe('isLessThanRange(version, range)', () => {
it('should support all versions length', () => {
expect(docker.isLessThanRange('1.2.3', '2.0')).toBe(true);
expect(docker.isLessThanRange('18.04', '18.1')).toBe(false);
expect(docker.isLessThanRange('10.1', '10.0.4')).toBe(false);
expect(docker.isLessThanRange('3', '4.0')).toBe(true);
expect(docker.isLessThanRange('1.2', '1.3.4')).toBe(true);
});
});
describe('equals(version, other)', () => {
it('should support all versions length', () => {
expect(docker.equals('1.2.3', '1.2.3')).toBe(true);
expect(docker.equals('18.04', '18.4')).toBe(true);
expect(docker.equals('10.0', '10.0.4')).toBe(false);
expect(docker.equals('3', '4.0')).toBe(false);
expect(docker.equals('1.2', '1.2.3')).toBe(false);
});
});
describe('getSatisfyingVersion(versions, range)', () => {
it('should support all versions length', () => {
[docker.minSatisfyingVersion, docker.getSatisfyingVersion].forEach(
(max) => {
const versions = [
'0.9.8',
'1.1.1',
'1.1',
'1.2.3',
'1.2',
'1',
'2.2.2',
'2.2',
'2',
];
// returns range if found
expect(max(versions, '1.2.3')).toBe('1.2.3');
expect(max(versions, '1.2')).toBe('1.2');
expect(max(versions, '1')).toBe('1');
// return null if not found
expect(max(versions, '1.3')).toBeNull();
expect(max(versions, '0.9')).toBeNull();
}
);
});
});
describe('sortVersions(v1, v2)', () => {
it('behaves like semver.sortVersions', () => {
[
['1.1.1', '1.2.3'],
['1.2.3', '1.3.4'],
['2.0.1', '1.2.3'],
['1.2.3', '0.9.5'],
].forEach((pair) => {
expect(docker.sortVersions(pair[0], pair[1])).toBe(
semver.sortVersions(pair[0], pair[1])
);
});
});
test.each`
a | b
${'1.1.1'} | ${'1.2.3'}
${'1.2.3'} | ${'1.3.4'}
${'2.0.1'} | ${'1.2.3'}
${'1.2.3'} | ${'0.9.5'}
`(
'docker.sortVersions("$a", "$b") === semver.sortVersions("$a", "$b")',
({ a, b }) => {
const dockerSorted = docker.sortVersions(a, b);
const semverSorted = semver.sortVersions(a, b);
expect(dockerSorted).toBe(semverSorted);
}
);
it('sorts unstable', () => {
const versions = [
@ -149,77 +143,69 @@ describe('versioning/docker/index', () => {
]);
});
});
describe('getNewValue(', () => {
it('returns newVersion', () => {
expect(
docker.getNewValue({
currentValue: null,
rangeStrategy: null,
currentVersion: null,
newVersion: '1.2.3',
})
).toBe('1.2.3');
});
test.each`
currentValue | rangeStrategy | currentVersion | newVersion | expected
${null} | ${null} | ${null} | ${'1.2.3'} | ${'1.2.3'}
`(
'getNewValue($currentValue, $rangeStrategy, $currentVersion, $newVersion, $expected) === $expected',
({ currentValue, rangeStrategy, currentVersion, newVersion, expected }) => {
const res = docker.getNewValue({
currentValue,
rangeStrategy,
currentVersion,
newVersion,
});
expect(res).toBe(expected);
}
);
test.each`
version | expected
${'3.7.0'} | ${true}
${'3.7.0b1'} | ${false}
${'3.7-alpine'} | ${true}
${'3.8.0-alpine'} | ${true}
${'3.8.0b1-alpine'} | ${false}
${'3.8.2'} | ${true}
`('isStable("$version") === $expected', ({ version, expected }) => {
const res = docker.isStable(version);
expect(!!res).toBe(expected);
});
it('isStable(version)', () => {
const versions = [
'3.7.0',
'3.7.0b1',
'3.7-alpine',
'3.8.0-alpine',
'3.8.0b1-alpine',
'3.8.2',
];
test.each`
version | range | expected
${'3.7.0'} | ${'3.7.0'} | ${true}
${'3.7.0b1'} | ${'3.7.0'} | ${true}
${'3.7-alpine'} | ${'3.7.0'} | ${false}
${'3.8.0-alpine'} | ${'3.7.0'} | ${false}
${'3.8.0b1-alpine'} | ${'3.7.0'} | ${false}
${'3.8.2'} | ${'3.7.0'} | ${true}
${'3.7.0'} | ${'3.7.0-alpine'} | ${false}
${'3.7.0b1'} | ${'3.7.0-alpine'} | ${false}
${'3.7-alpine'} | ${'3.7.0-alpine'} | ${false}
${'3.8.0-alpine'} | ${'3.7.0-alpine'} | ${true}
${'3.8.0b1-alpine'} | ${'3.7.0-alpine'} | ${true}
${'3.8.2'} | ${'3.7.0-alpine'} | ${false}
`(
'isCompatible("$version") === $expected',
({ version, range, expected }) => {
const res = docker.isCompatible(version, range);
expect(!!res).toBe(expected);
}
);
expect(versions.filter(docker.isStable)).toEqual([
'3.7.0',
'3.7-alpine',
'3.8.0-alpine',
'3.8.2',
]);
});
it('isCompatible(version)', () => {
const versions = [
'3.7.0',
'3.7.0b1',
'3.7-alpine',
'3.8.0-alpine',
'3.8.0b1-alpine',
'3.8.2',
];
expect(versions.filter((v) => docker.isCompatible(v, '3.7.0'))).toEqual([
'3.7.0',
'3.7.0b1',
'3.8.2',
]);
expect(
versions.filter((v) => docker.isCompatible(v, '3.7.0-alpine'))
).toEqual(['3.8.0-alpine', '3.8.0b1-alpine']);
});
it('valueToVersion(version)', () => {
const versions = [
'3.7.0',
'3.7.0b1',
'3.7-alpine',
'3.8.0-alpine',
'3.8.0b1-alpine',
'3.8.2',
undefined,
];
expect(versions.map(docker.valueToVersion)).toEqual([
'3.7.0',
'3.7.0b1',
'3.7',
'3.8.0',
'3.8.0b1',
'3.8.2',
undefined,
]);
test.each`
value | expected
${'3.7.0'} | ${'3.7.0'}
${'3.7.0b1'} | ${'3.7.0b1'}
${'3.7-alpine'} | ${'3.7'}
${'3.8.0-alpine'} | ${'3.8.0'}
${'3.8.0b1-alpine'} | ${'3.8.0b1'}
${'3.8.2'} | ${'3.8.2'}
${undefined} | ${undefined}
`('valueToVersion("$value") === $expected', ({ value, expected }) => {
const res = docker.valueToVersion(value);
expect(res).toBe(expected);
});
});