test(pep440): Refactor versioning scheme (#11930)

Co-authored-by: Michael Kriese <michael.kriese@visualon.de>
This commit is contained in:
Sergei Zharinov 2021-09-28 17:14:51 +03:00 committed by GitHub
parent 959e3943f9
commit 63423a1729
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,67 +1,53 @@
import pep440 from '.'; import pep440 from '.';
describe('versioning/pep440/index', () => { describe('versioning/pep440/index', () => {
describe('pep440.isValid(input)', () => { test.each`
it('should support a version without equals', () => { input | expected
expect(pep440.isValid('0.750')).toBeTruthy(); ${'0.750'} | ${true}
expect(pep440.isValid('1.2.3')).toBeTruthy(); ${'1.2.3'} | ${true}
expect(pep440.isValid('1.9')).toBeTruthy(); ${'1.9'} | ${true}
}); ${'17.04.0'} | ${true}
it('should support irregular versions', () => { ${'==1.2.3'} | ${true}
expect(pep440.isValid('17.04.0')).toBeTruthy(); ${'==1.2.3rc0'} | ${true}
}); ${'~=1.2.3'} | ${true}
it('should support simple pep440', () => { ${'==1.2.*'} | ${true}
expect(pep440.isValid('==1.2.3')).toBeTruthy(); ${'>1.2.3'} | ${true}
}); ${'renovatebot/renovate'} | ${false}
it('should support pep440 with RC', () => { ${'renovatebot/renovate#master'} | ${false}
expect(pep440.isValid('==1.2.3rc0')).toBeTruthy(); ${'https://github.com/renovatebot/renovate.git'} | ${false}
}); `('isValid("$input") === $expected', ({ input, expected }) => {
it('should support ranges', () => { const res = !!pep440.isValid(input);
expect(pep440.isValid('~=1.2.3')).toBeTruthy(); expect(res).toBe(expected);
expect(pep440.isValid('==1.2.*')).toBeTruthy();
expect(pep440.isValid('>1.2.3')).toBeTruthy();
});
it('should reject github repositories', () => {
expect(pep440.isValid('renovatebot/renovate')).toBeFalsy();
expect(pep440.isValid('renovatebot/renovate#master')).toBeFalsy();
expect(
pep440.isValid('https://github.com/renovatebot/renovate.git')
).toBeFalsy();
});
}); });
describe('pep440.isStable(version)', () => { test.each`
it('returns correct value', () => { input | expected
expect(pep440.isStable('1.2.3')).toBeTruthy(); ${'1.2.3'} | ${true}
expect(pep440.isStable('1.2.3rc0')).toBeFalsy(); ${'1.2.3rc0'} | ${false}
}); ${'not_version'} | ${false}
it('returns false when version invalid', () => { `('isStable("$input") === $expected', ({ input, expected }) => {
expect(pep440.isStable('not_version')).toBeFalsy(); expect(pep440.isStable(input)).toBe(expected);
});
}); });
describe('pep440.equals(version1, version2)', () => { test.each`
it('returns correct true', () => { a | b | expected
expect(pep440.equals('1.0', '1.0.0')).toBeTruthy(); ${'1.0'} | ${'1.0.0'} | ${true}
}); ${'1.0.0'} | ${'1.0..foo'} | ${null}
it('returns false when version invalid', () => { `('equals($a, $b) === $expected', ({ a, b, expected }) => {
expect(pep440.equals('1.0.0', '1.0..foo')).toBeFalsy(); expect(pep440.equals(a, b)).toBe(expected);
});
}); });
describe('pep440.isSingleVersion()', () => { test.each`
it('returns true if naked version', () => { version | isSingle
expect(pep440.isSingleVersion('1.2.3')).toBeTruthy(); ${'1.2.3'} | ${true}
expect(pep440.isSingleVersion('1.2.3rc0')).toBeTruthy(); ${'1.2.3rc0'} | ${true}
}); ${'==1.2.3'} | ${true}
it('returns true if double equals', () => { ${'==1.2'} | ${true}
expect(pep440.isSingleVersion('==1.2.3')).toBeTruthy(); ${'== 1.2.3'} | ${true}
expect(pep440.isSingleVersion('==1.2')).toBeTruthy(); ${'==1.*'} | ${false}
expect(pep440.isSingleVersion('== 1.2.3')).toBeTruthy(); `('isSingleVersion("$version") === $isSingle', ({ version, isSingle }) => {
}); const res = !!pep440.isSingleVersion(version);
it('returns false when not version', () => { expect(res).toBe(isSingle);
expect(pep440.isSingleVersion('==1.*')).toBeFalsy();
});
}); });
const versions = [ const versions = [
@ -75,143 +61,134 @@ describe('versioning/pep440/index', () => {
'2.0.3', '2.0.3',
]; ];
describe('pep440.getSatisfyingVersion(versions, range)', () => { test.each`
it('returns correct value', () => { range | expected
expect(pep440.getSatisfyingVersion(versions, '~=1.2.1')).toBe('1.2.3'); ${'~=1.2.1'} | ${'1.2.3'}
}); ${'~=2.1'} | ${null}
it('returns null when none found', () => { `(
expect(pep440.getSatisfyingVersion(versions, '~=2.1')).toBeNull(); 'getSatisfyingVersion($versions, "$range") === $expected',
}); ({ range, expected }) => {
}); expect(pep440.getSatisfyingVersion(versions, range)).toBe(expected);
}
);
describe('pep440.minSatisfyingVersion(versions, range)', () => { test.each`
it('returns correct value', () => { range | expected
expect(pep440.minSatisfyingVersion(versions, '~=1.2.1')).toBe('1.2.1'); ${'~=1.2.1'} | ${'1.2.1'}
}); ${'~=2.1'} | ${null}
it('returns null when none found', () => { `(
expect(pep440.minSatisfyingVersion(versions, '~=2.1')).toBeNull(); 'minSatisfyingVersion($versions, "$range") === $expected',
}); ({ range, expected }) => {
}); expect(pep440.minSatisfyingVersion(versions, range)).toBe(expected);
}
);
describe('pep440.getNewValue()', () => { test.each`
const { getNewValue } = pep440; currentValue | rangeStrategy | currentVersion | newVersion | expected
${'1.0.0'} | ${'bump'} | ${'1.0.0'} | ${'1.2.3'} | ${'1.2.3'}
// cases: [currentValue, expectedBump] ${'1.0.0'} | ${'replace'} | ${'1.0.0'} | ${'1.2.3'} | ${'1.2.3'}
[ ${'1.0.0'} | ${'pin'} | ${'1.0.0'} | ${'1.2.3'} | ${'==1.2.3'}
// plain version ${'==1.0.3'} | ${'bump'} | ${'1.0.0'} | ${'1.2.3'} | ${'==1.2.3'}
['1.0.0', '1.2.3'], ${'==1.0.3'} | ${'replace'} | ${'1.0.0'} | ${'1.2.3'} | ${'==1.2.3'}
${'==1.0.3'} | ${'pin'} | ${'1.0.0'} | ${'1.2.3'} | ${'==1.2.3'}
// simple cases ${'>=1.2.0'} | ${'bump'} | ${'1.0.0'} | ${'1.2.3'} | ${'>=1.2.3'}
['==1.0.3', '==1.2.3'], ${'>=1.2.0'} | ${'replace'} | ${'1.0.0'} | ${'1.2.3'} | ${'>=1.2.0'}
['>=1.2.0', '>=1.2.3'], ${'>=1.2.0'} | ${'pin'} | ${'1.0.0'} | ${'1.2.3'} | ${'==1.2.3'}
['~=1.2.0', '~=1.2.3'], ${'~=1.2.0'} | ${'bump'} | ${'1.0.0'} | ${'1.2.3'} | ${'~=1.2.3'}
['~=1.0.3', '~=1.2.3'], ${'~=1.2.0'} | ${'replace'} | ${'1.0.0'} | ${'1.2.3'} | ${'~=1.2.0'}
${'~=1.2.0'} | ${'pin'} | ${'1.0.0'} | ${'1.2.3'} | ${'==1.2.3'}
// glob ${'~=1.0.3'} | ${'bump'} | ${'1.0.0'} | ${'1.2.3'} | ${'~=1.2.3'}
['==1.2.*', '==1.2.*'], ${'~=1.0.3'} | ${'replace'} | ${'1.0.0'} | ${'1.2.3'} | ${'~=1.2.3'}
['==1.0.*', '==1.2.*'], ${'~=1.0.3'} | ${'pin'} | ${'1.0.0'} | ${'1.2.3'} | ${'==1.2.3'}
${'==1.2.*'} | ${'bump'} | ${'1.0.0'} | ${'1.2.3'} | ${'==1.2.*'}
// future versions guard ${'==1.2.*'} | ${'replace'} | ${'1.0.0'} | ${'1.2.3'} | ${'==1.2.*'}
['<1.2.2.3', '<1.2.4.0'], ${'==1.2.*'} | ${'pin'} | ${'1.0.0'} | ${'1.2.3'} | ${'==1.2.3'}
['<1.2.3', '<1.2.4'], ${'==1.0.*'} | ${'bump'} | ${'1.0.0'} | ${'1.2.3'} | ${'==1.2.*'}
['<1.2', '<1.3'], ${'==1.0.*'} | ${'replace'} | ${'1.0.0'} | ${'1.2.3'} | ${'==1.2.*'}
['<1', '<2'], ${'==1.0.*'} | ${'pin'} | ${'1.0.0'} | ${'1.2.3'} | ${'==1.2.3'}
['<2.0.0', '<2.0.0'], ${'<1.2.2.3'} | ${'bump'} | ${'1.0.0'} | ${'1.2.3'} | ${'<1.2.4.0'}
${'<1.2.2.3'} | ${'replace'} | ${'1.0.0'} | ${'1.2.3'} | ${'<1.2.4.0'}
// minimum version guard ${'<1.2.2.3'} | ${'pin'} | ${'1.0.0'} | ${'1.2.3'} | ${'==1.2.3'}
['>0.9.8', '>0.9.8'], ${'<1.2.3'} | ${'bump'} | ${'1.0.0'} | ${'1.2.3'} | ${'<1.2.4'}
// rollback ${'<1.2.3'} | ${'replace'} | ${'1.0.0'} | ${'1.2.3'} | ${'<1.2.4'}
['>2.0.0', '>=1.2.3'], ${'<1.2.3'} | ${'pin'} | ${'1.0.0'} | ${'1.2.3'} | ${'==1.2.3'}
['>=2.0.0', '>=1.2.3'], ${'<1.2'} | ${'bump'} | ${'1.0.0'} | ${'1.2.3'} | ${'<1.3'}
${'<1.2'} | ${'replace'} | ${'1.0.0'} | ${'1.2.3'} | ${'<1.3'}
// complex ranges ${'<1.2'} | ${'pin'} | ${'1.0.0'} | ${'1.2.3'} | ${'==1.2.3'}
['~=1.1.0, !=1.1.1', '~=1.2.3, !=1.1.1'], ${'<1'} | ${'bump'} | ${'1.0.0'} | ${'1.2.3'} | ${'<2'}
['~=1.1.0,!=1.1.1', '~=1.2.3,!=1.1.1'], ${'<1'} | ${'replace'} | ${'1.0.0'} | ${'1.2.3'} | ${'<2'}
${'<1'} | ${'pin'} | ${'1.0.0'} | ${'1.2.3'} | ${'==1.2.3'}
// invalid & not supported ${'<2.0.0'} | ${'bump'} | ${'1.0.0'} | ${'1.2.3'} | ${'<2.0.0'}
[' ', ' '], ${'<2.0.0'} | ${'replace'} | ${'1.0.0'} | ${'1.2.3'} | ${'<2.0.0'}
['invalid', null], ${'<2.0.0'} | ${'pin'} | ${'1.0.0'} | ${'1.2.3'} | ${'==1.2.3'}
['===1.0.3', null], ${'>0.9.8'} | ${'bump'} | ${'1.0.0'} | ${'1.2.3'} | ${'>0.9.8'}
// impossible ${'>0.9.8'} | ${'replace'} | ${'1.0.0'} | ${'1.2.3'} | ${'>0.9.8'}
['!=1.2.3', null], ${'>0.9.8'} | ${'pin'} | ${'1.0.0'} | ${'1.2.3'} | ${'==1.2.3'}
].forEach(([currentValue, expectedBump]) => { ${'>2.0.0'} | ${'bump'} | ${'1.0.0'} | ${'1.2.3'} | ${'>=1.2.3'}
const bumped = getNewValue({ ${'>2.0.0'} | ${'replace'} | ${'1.0.0'} | ${'1.2.3'} | ${'>=1.2.3'}
${'>2.0.0'} | ${'pin'} | ${'1.0.0'} | ${'1.2.3'} | ${'==1.2.3'}
${'>=2.0.0'} | ${'bump'} | ${'1.0.0'} | ${'1.2.3'} | ${'>=1.2.3'}
${'>=2.0.0'} | ${'replace'} | ${'1.0.0'} | ${'1.2.3'} | ${'>=1.2.3'}
${'>=2.0.0'} | ${'pin'} | ${'1.0.0'} | ${'1.2.3'} | ${'==1.2.3'}
${'~=1.1.0, !=1.1.1'} | ${'bump'} | ${'1.0.0'} | ${'1.2.3'} | ${'~=1.2.3, !=1.1.1'}
${'~=1.1.0, !=1.1.1'} | ${'replace'} | ${'1.0.0'} | ${'1.2.3'} | ${'~=1.2.3, !=1.1.1'}
${'~=1.1.0, !=1.1.1'} | ${'pin'} | ${'1.0.0'} | ${'1.2.3'} | ${'==1.2.3'}
${'~=1.1.0,!=1.1.1'} | ${'bump'} | ${'1.0.0'} | ${'1.2.3'} | ${'~=1.2.3,!=1.1.1'}
${'~=1.1.0,!=1.1.1'} | ${'replace'} | ${'1.0.0'} | ${'1.2.3'} | ${'~=1.2.3,!=1.1.1'}
${'~=1.1.0,!=1.1.1'} | ${'pin'} | ${'1.0.0'} | ${'1.2.3'} | ${'==1.2.3'}
${' '} | ${'bump'} | ${'1.0.0'} | ${'1.2.3'} | ${' '}
${' '} | ${'replace'} | ${'1.0.0'} | ${'1.2.3'} | ${' '}
${' '} | ${'pin'} | ${'1.0.0'} | ${'1.2.3'} | ${'==1.2.3'}
${'invalid'} | ${'bump'} | ${'1.0.0'} | ${'1.2.3'} | ${null}
${'invalid'} | ${'replace'} | ${'1.0.0'} | ${'1.2.3'} | ${null}
${'invalid'} | ${'pin'} | ${'1.0.0'} | ${'1.2.3'} | ${'==1.2.3'}
${'===1.0.3'} | ${'bump'} | ${'1.0.0'} | ${'1.2.3'} | ${null}
${'===1.0.3'} | ${'replace'} | ${'1.0.0'} | ${'1.2.3'} | ${null}
${'===1.0.3'} | ${'pin'} | ${'1.0.0'} | ${'1.2.3'} | ${'==1.2.3'}
${'!=1.2.3'} | ${'bump'} | ${'1.0.0'} | ${'1.2.3'} | ${null}
${'!=1.2.3'} | ${'replace'} | ${'1.0.0'} | ${'1.2.3'} | ${null}
${'!=1.2.3'} | ${'pin'} | ${'1.0.0'} | ${'1.2.3'} | ${'==1.2.3'}
`(
'getNewValue("$currentValue", "$rangeStrategy", "$currentVersion", "$newVersion") === "$expected"',
({ currentValue, rangeStrategy, currentVersion, newVersion, expected }) => {
const res = pep440.getNewValue({
currentValue, currentValue,
rangeStrategy: 'bump', rangeStrategy,
currentVersion: '1.0.0', currentVersion,
newVersion: '1.2.3', newVersion,
});
it(`bumps '${currentValue}' to '${expectedBump}'`, () => {
expect(bumped).toBe(expectedBump);
}); });
expect(res).toEqual(expected);
}
);
const replaced = getNewValue({ test.each`
currentValue, version | range | expected
rangeStrategy: 'replace', ${'0.9.9.9'} | ${'>= 1.0.0, < 2.0.0'} | ${true}
currentVersion: '1.0.0', ${'1.0.0a0'} | ${'>= 1.0.0, < 2.0.0'} | ${true}
newVersion: '1.2.3', ${'1.0.0.0'} | ${'> 1.0.0, < 2.0.0'} | ${true}
}); ${'2.0.1.0'} | ${'> 1.0.0, < 2.0.0'} | ${false}
const needReplace = pep440.matches('1.2.3', currentValue); ${'2.0.0.0'} | ${'> 1.0.0, < 2.0.0'} | ${false}
const expectedReplace = needReplace ? currentValue : bumped; ${'2.0.0a0'} | ${'> 1.0.0, < 2.0.0'} | ${false}
it(`replaces '${currentValue}' to '${expectedReplace}'`, () => { ${'1.2.2.9'} | ${'== 1.2.3'} | ${true}
expect(replaced).toBe(expectedReplace); ${'1.2.3a0'} | ${'== 1.2.3'} | ${true}
}); ${'1.2.3.0'} | ${'== 1.2.3'} | ${false}
${'1.2.3.1'} | ${'== 1.2.3'} | ${false}
const pinned = getNewValue({ ${'1.2.4a0'} | ${'== 1.2.3'} | ${false}
currentValue, ${'1.2.2.9'} | ${'!= 1.2.3'} | ${false}
rangeStrategy: 'pin', ${'1.2.3.0'} | ${'!= 1.2.3'} | ${false}
currentVersion: '1.0.0', ${'1.2.3.1'} | ${'!= 1.2.3'} | ${false}
newVersion: '1.2.3', ${'0.0.1'} | ${'< 1.0.0'} | ${false}
}); ${'1.0.0'} | ${'< 1.0.0'} | ${false}
const expectedPin = '==1.2.3'; ${'2.0.0'} | ${'< 1.0.0'} | ${false}
it(`pins '${currentValue}' to '${expectedPin}'`, () => { ${'0.0.1'} | ${'<= 1.0.0'} | ${false}
expect(pinned).toBe(expectedPin); ${'1.0.0'} | ${'<= 1.0.0'} | ${false}
}); ${'2.0.0'} | ${'<= 1.0.0'} | ${false}
}); ${'0.0.1'} | ${'< 1.0.0, > 2.0.0'} | ${true}
${'3.0.0'} | ${'< 1.0.0, > 2.0.0'} | ${false}
it('guards against unsupported rangeStrategy', () => { `(
const invalid = getNewValue({ 'isLessThanRange("$version", "$range") === "$expected"',
currentValue: '==1.0.0', ({ version, range, expected }) => {
rangeStrategy: 'update-lockfile',
currentVersion: '1.0.0',
newVersion: '1.2.3',
});
expect(invalid).toEqual('==1.2.3');
});
});
describe('pep.isLessThanRange()', () => {
test.each([
['>= 1.0.0, < 2.0.0', '0.9.9.9', true],
['>= 1.0.0, < 2.0.0', '1.0.0a0', true],
['> 1.0.0, < 2.0.0', '1.0.0.0', true],
['> 1.0.0, < 2.0.0', '2.0.1.0', false],
['> 1.0.0, < 2.0.0', '2.0.0.0', false],
['> 1.0.0, < 2.0.0', '2.0.0a0', false],
['== 1.2.3', '1.2.2.9', true],
['== 1.2.3', '1.2.3a0', true],
['== 1.2.3', '1.2.3.0', false],
['== 1.2.3', '1.2.3.1', false],
['== 1.2.3', '1.2.4a0', false],
['!= 1.2.3', '1.2.2.9', false],
['!= 1.2.3', '1.2.3.0', false],
['!= 1.2.3', '1.2.3.1', false],
['< 1.0.0', '0.0.1', false],
['< 1.0.0', '1.0.0', false],
['< 1.0.0', '2.0.0', false],
['<= 1.0.0', '0.0.1', false],
['<= 1.0.0', '1.0.0', false],
['<= 1.0.0', '2.0.0', false],
['< 1.0.0, > 2.0.0', '0.0.1', true], // fixme (maybe)
['< 1.0.0, > 2.0.0', '3.0.0', false],
])(`%s\t%s\t%s`, (range, version, expected) => {
expect(pep440.isLessThanRange(version, range)).toBe(expected); expect(pep440.isLessThanRange(version, range)).toBe(expected);
}); }
}); );
}); });