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,93 +2,80 @@ import semver from '../semver';
import docker from '.'; import docker from '.';
describe('versioning/docker/index', () => { describe('versioning/docker/index', () => {
describe('isValid(version)', () => { test.each`
it('should support all versions length', () => { version | expected
expect(docker.isValid(null)).toBeNull(); ${null} | ${false}
expect(docker.isValid('1.2.3')).toBe('1.2.3'); ${'1.2.3'} | ${true}
expect(docker.isValid('18.04')).toBe('18.04'); ${'18.04'} | ${true}
expect(docker.isValid('10.1')).toBe('10.1'); ${'10.1'} | ${true}
expect(docker.isValid('3')).toBe('3'); ${'3'} | ${true}
expect(docker.isValid('foo')).toBeNull(); ${'foo'} | ${false}
}); ${'0a1b2c3'} | ${false}
it('should return null if the version string looks like a git commit hash', () => { ${'0a1b2c3d'} | ${false}
[ ${'0a1b2c3d4e5f6a7b8c9d0a1b2c3d4e5f6a7b8c9d'} | ${false}
'0a1b2c3', ${'0a1b2c3d4e5f6a7b8c9d0a1b2c3d4e5f6a7b8c9d0'} | ${true}
'0a1b2c3d', ${'0a1b2C3'} | ${true}
'0a1b2c3d4e5f6a7b8c9d0a1b2c3d4e5f6a7b8c9d', ${'0z1b2c3'} | ${true}
].forEach((version) => { ${'0A1b2c3d4e5f6a7b8c9d0a1b2c3d4e5f6a7b8c9d'} | ${true}
expect(docker.isValid(version)).toBeNull(); ${'123098140293'} | ${true}
}); `('isValid("$version") === $expected', ({ version, expected }) => {
[ const res = docker.isValid(version);
'0a1b2c3d4e5f6a7b8c9d0a1b2c3d4e5f6a7b8c9d0', expect(!!res).toBe(expected);
'0a1b2C3',
'0z1b2c3',
'0A1b2c3d4e5f6a7b8c9d0a1b2c3d4e5f6a7b8c9d',
'123098140293',
].forEach((version) => {
expect(docker.isValid(version)).toBe(version);
});
});
});
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();
});
});
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();
});
});
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('isGreaterThan(version, other)', () => { test.each`
it('should support all versions length', () => { version | major | minor | patch
expect(docker.isGreaterThan('1.2.3', '1.2')).toBe(false); ${'1.2.3'} | ${1} | ${2} | ${3}
expect(docker.isGreaterThan('18.04', '18.1')).toBe(true); ${'18.04'} | ${18} | ${4} | ${null}
expect(docker.isGreaterThan('10.1', '10.1.2')).toBe(true); ${'10.1'} | ${10} | ${1} | ${null}
expect(docker.isGreaterThan('3', '2')).toBe(true); ${'3'} | ${3} | ${null} | ${null}
expect(docker.isGreaterThan('1.2.3', '1.2.3')).toBe(false); ${'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);
}); });
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('isLessThanRange(version, range)', () => {
it('should support all versions length', () => { describe('Satisfying versions', () => {
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 = [ const versions = [
'0.9.8', '0.9.8',
'1.1.1', '1.1.1',
@ -100,30 +87,37 @@ describe('versioning/docker/index', () => {
'2.2', '2.2',
'2', '2',
]; ];
// returns range if found
expect(max(versions, '1.2.3')).toBe('1.2.3'); test.each`
expect(max(versions, '1.2')).toBe('1.2'); version | expected
expect(max(versions, '1')).toBe('1'); ${'1.2.3'} | ${'1.2.3'}
// return null if not found ${'1.2'} | ${'1.2'}
expect(max(versions, '1.3')).toBeNull(); ${'1'} | ${'1'}
expect(max(versions, '0.9')).toBeNull(); ${'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('sortVersions(v1, v2)', () => {
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);
} }
); );
});
});
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])
);
});
});
it('sorts unstable', () => { it('sorts unstable', () => {
const versions = [ const versions = [
@ -149,77 +143,69 @@ describe('versioning/docker/index', () => {
]); ]);
}); });
}); });
describe('getNewValue(', () => {
it('returns newVersion', () => { test.each`
expect( currentValue | rangeStrategy | currentVersion | newVersion | expected
docker.getNewValue({ ${null} | ${null} | ${null} | ${'1.2.3'} | ${'1.2.3'}
currentValue: null, `(
rangeStrategy: null, 'getNewValue($currentValue, $rangeStrategy, $currentVersion, $newVersion, $expected) === $expected',
currentVersion: null, ({ currentValue, rangeStrategy, currentVersion, newVersion, expected }) => {
newVersion: '1.2.3', const res = docker.getNewValue({
}) currentValue,
).toBe('1.2.3'); 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)', () => { test.each`
const versions = [ version | range | expected
'3.7.0', ${'3.7.0'} | ${'3.7.0'} | ${true}
'3.7.0b1', ${'3.7.0b1'} | ${'3.7.0'} | ${true}
'3.7-alpine', ${'3.7-alpine'} | ${'3.7.0'} | ${false}
'3.8.0-alpine', ${'3.8.0-alpine'} | ${'3.7.0'} | ${false}
'3.8.0b1-alpine', ${'3.8.0b1-alpine'} | ${'3.7.0'} | ${false}
'3.8.2', ${'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([ test.each`
'3.7.0', value | expected
'3.7-alpine', ${'3.7.0'} | ${'3.7.0'}
'3.8.0-alpine', ${'3.7.0b1'} | ${'3.7.0b1'}
'3.8.2', ${'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'}
it('isCompatible(version)', () => { ${undefined} | ${undefined}
const versions = [ `('valueToVersion("$value") === $expected', ({ value, expected }) => {
'3.7.0', const res = docker.valueToVersion(value);
'3.7.0b1', expect(res).toBe(expected);
'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,
]);
}); });
}); });