Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(versioning): add PVP versioning scheme #32298

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 11 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
2 changes: 2 additions & 0 deletions lib/modules/versioning/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import * as nuget from './nuget';
import * as pep440 from './pep440';
import * as perl from './perl';
import * as poetry from './poetry';
import * as pvp from './pvp';
import * as python from './python';
import * as redhat from './redhat';
import * as regex from './regex';
Expand Down Expand Up @@ -71,6 +72,7 @@ api.set(nuget.id, nuget.api);
api.set(pep440.id, pep440.api);
api.set(perl.id, perl.api);
api.set(poetry.id, poetry.api);
api.set(pvp.id, pvp.api);
api.set(python.id, python.api);
api.set(redhat.id, redhat.api);
api.set(regex.id, regex.api);
Expand Down
294 changes: 294 additions & 0 deletions lib/modules/versioning/pvp/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,294 @@
import pvp, { extractAllComponents, getComponents, parseRange } from '.';

describe('modules/versioning/pvp/index', () => {
describe('.isGreaterThan(version, other)', () => {
secustor marked this conversation as resolved.
Show resolved Hide resolved
it.each`
first | second | expected
${'1.23.1'} | ${'1.9.6'} | ${true}
${'4.0.0'} | ${'3.0.0'} | ${true}
${'3.0.1'} | ${'3.0.0'} | ${true}
${'4.10'} | ${'4.1'} | ${true}
${'1.0.0'} | ${'1.0'} | ${true}
${'2.0.2'} | ${'3.1.0'} | ${false}
${'3.0.0'} | ${'3.0.0'} | ${false}
${'4.1'} | ${'4.10'} | ${false}
${'1.0'} | ${'1.0.0'} | ${false}
${''} | ${'1.0'} | ${false}
${'1.0'} | ${''} | ${false}
`('pvp.isGreaterThan($first, $second)', ({ first, second, expected }) => {
expect(pvp.isGreaterThan(first, second)).toBe(expected);
});
});

describe('.parseRange(range)', () => {
it('should parse >=1.0 && <1.1', () => {
const parsed = parseRange('>=1.0 && <1.1');
expect(parsed).not.toBeNull();
expect(parsed!.lower).toBe('1.0');
expect(parsed!.upper).toBe('1.1');
});
});

describe('.getMajor(version)', () => {
it.each`
version | expected
${'1.0.0'} | ${1.0}
${'1.0.1'} | ${1.0}
${'1.1.1'} | ${1.1}
${''} | ${null}
`('pvp.getMajor("$version") === $expected', ({ version, expected }) => {
expect(pvp.getMajor(version)).toBe(expected);
});
});

describe('.getMinor(version)', () => {
it.each`
version | expected
${'1.0'} | ${null}
${'1.0.0'} | ${0}
${'1.0.1'} | ${1}
${'1.1.2'} | ${2}
`('pvp.getMinor("$version") === $expected', ({ version, expected }) => {
expect(pvp.getMinor(version)).toBe(expected);
});
});

describe('.getPatch(version)', () => {
it.each`
version | expected
${'1.0.0'} | ${null}
${'1.0.0.5.1'} | ${5.1}
${'1.0.1.6'} | ${6}
${'1.1.2.7'} | ${7}
${'0.0.0.0.1'} | ${0.1}
${'0.0.0.0.10'} | ${0.1}
`('pvp.getPatch("$version") === $expected', ({ version, expected }) => {
expect(pvp.getPatch(version)).toBe(expected);
});
});

describe('.matches(version, range)', () => {
it.each`
version | range | expected
${'1.0.1'} | ${'>=1.0 && <1.1'} | ${true}
${'4.1'} | ${'>=4.0 && <4.10'} | ${true}
${'4.1'} | ${'>=4.1 && <4.10'} | ${true}
${'4.1.0'} | ${'>=4.1 && <4.10'} | ${true}
ysangkok marked this conversation as resolved.
Show resolved Hide resolved
${'4.1.0'} | ${'<4.10 && >=4.1'} | ${true}
${'4.10'} | ${'>=4.1 && <4.10.0'} | ${true}
${'4.10'} | ${'>=4.0 && <4.10.1'} | ${true}
${'1.0.0'} | ${'>=2.0 && <2.1'} | ${false}
${'4'} | ${'>=4.0 && <4.10'} | ${false}
${'4.10'} | ${'>=4.1 && <4.10'} | ${false}
${'4'} | ${'gibberish'} | ${false}
${''} | ${'>=1.0 && <1.1'} | ${false}
`(
'pvp.matches("$version", "$range") === $expected',
({ version, range, expected }) => {
expect(pvp.matches(version, range)).toBe(expected);
},
);
});

describe('.getSatisfyingVersion(versions, range)', () => {
it.each`
versions | range | expected
${['1.0.0', '1.0.4', '1.3.0', '2.0.0']} | ${'>=1.0 && <1.1'} | ${'1.0.4'}
${['2.0.0', '1.0.0', '1.0.4', '1.3.0']} | ${'>=1.0 && <1.1'} | ${'1.0.4'}
${['1.0.0', '1.0.4', '1.3.0', '2.0.0']} | ${'>=3.0 && <4.0'} | ${null}
`(
'pvp.getSatisfyingVersion($versions, "$range") === $expected',
({ versions, range, expected }) => {
expect(pvp.getSatisfyingVersion(versions, range)).toBe(expected);
},
);
});

describe('.minSatisfyingVersion(versions, range)', () => {
it('should return min satisfying version in range', () => {
expect(
pvp.minSatisfyingVersion(
['0.9', '1.0.0', '1.0.4', '1.3.0', '2.0.0'],
'>=1.0 && <1.1',
),
).toBe('1.0.0');
});
});

describe('.isLessThanRange(version, range)', () => {
it.each`
version | range | expected
${'2.0.2'} | ${'>=3.0 && <3.1'} | ${true}
${'3'} | ${'>=3.0 && <3.1'} | ${true}
${'3'} | ${'>=3 && <3.1'} | ${false}
${'3.0'} | ${'>=3.0 && <3.1'} | ${false}
${'3.0.0'} | ${'>=3.0 && <3.1'} | ${false}
${'4.0.0'} | ${'>=3.0 && <3.1'} | ${false}
${'3.1.0'} | ${'>=3.0 && <3.1'} | ${false}
${'3'} | ${'gibberish'} | ${false}
${''} | ${'>=3.0 && <3.1'} | ${false}
`(
'pvp.isLessThanRange?.("$version", "$range") === $expected',
({ version, range, expected }) => {
expect(pvp.isLessThanRange?.(version, range)).toBe(expected);
},
);
});

describe('.extractAllComponents(version)', () => {
it('should return null when there are no numbers', () => {
expect(extractAllComponents('')).toBeNull();
});

it('should parse 3.0', () => {
expect(extractAllComponents('3.0')).toEqual([3, 0]);
});
});

describe('.isValid(version)', () => {
it.each`
version | expected
${''} | ${false}
${'1.0.0.0'} | ${true}
${'1.0'} | ${true}
${'>=1.0 && <1.1'} | ${true}
`('pvp.isValid("$version") === $expected', ({ version, expected }) => {
expect(pvp.isValid(version)).toBe(expected);
});
});

describe('.getNewValue(newValueConfig)', () => {
it.each`
currentValue | newVersion | rangeStrategy | expected
${'>=1.0 && <1.1'} | ${'1.1'} | ${'auto'} | ${'>=1.0 && <1.2'}
${'>=1.2 && <1.3'} | ${'1.2.3'} | ${'auto'} | ${null}
${'>=1.0 && <1.1'} | ${'1.2.3'} | ${'update-lockfile'} | ${null}
${'gibberish'} | ${'1.2.3'} | ${'auto'} | ${null}
${'>=1.0 && <1.1'} | ${'0.9'} | ${'auto'} | ${null}
${'>=1.0 && <1.1'} | ${''} | ${'auto'} | ${null}
`(
'pvp.getNewValue({currentValue: "$currentValue", newVersion: "$newVersion", rangeStrategy: "$rangeStrategy"}) === $expected',
({ currentValue, newVersion, rangeStrategy, expected }) => {
expect(
pvp.getNewValue({ currentValue, newVersion, rangeStrategy }),
).toBe(expected);
},
);
});

describe('.getComponents(...)', () => {
it('"0" is valid major version', () => {
expect(getComponents('0')?.major).toEqual([0]);
});

it('returns null when no components could be extracted', () => {
expect(getComponents('')).toBeNull();
});
});

describe('.isSame(...)', () => {
it.each`
type | a | b | expected
${'major'} | ${'4.10'} | ${'4.1'} | ${false}
${'major'} | ${'4.1.0'} | ${'5.1.0'} | ${false}
${'major'} | ${'4.1'} | ${'5.1'} | ${false}
${'major'} | ${'0'} | ${'1'} | ${false}
${'major'} | ${'4.1'} | ${'4.1.0'} | ${true}
${'major'} | ${'4.1.1'} | ${'4.1.2'} | ${true}
${'major'} | ${'0'} | ${'0'} | ${true}
${'minor'} | ${'4.1.0'} | ${'5.1.0'} | ${true}
${'minor'} | ${'4.1'} | ${'4.1'} | ${true}
${'minor'} | ${'4.1'} | ${'5.1'} | ${true}
${'minor'} | ${'4.1.0'} | ${'4.1.1'} | ${false}
${'minor'} | ${''} | ${'0'} | ${false}
${'patch'} | ${'1.0.0.0'} | ${'1.0.0.0'} | ${true}
${'patch'} | ${'1.0.0.0'} | ${'2.0.0.0'} | ${true}
${'patch'} | ${'1.0.0.0'} | ${'1.0.0.1'} | ${false}
${'patch'} | ${'0.0.0.0.1'} | ${'0.0.0.0.10'} | ${false}
`(
'pvp.isSame("$type", "$a", "$b") === $expected',
({ type, a, b, expected }) => {
expect(pvp.isSame?.(type, a, b)).toBe(expected);
},
);
});

describe('.isVersion(maybeRange)', () => {
it.each`
version | expected
${'1.0'} | ${true}
${'>=1.0 && <1.1'} | ${false}
`('pvp.isVersion("$version") === $expected', ({ version, expected }) => {
expect(pvp.isVersion(version)).toBe(expected);
});
});

describe('.equals(a, b)', () => {
it.each`
a | b | expected
${'1.01'} | ${'1.1'} | ${true}
${'1.01'} | ${'1.0'} | ${false}
${''} | ${'1.0'} | ${false}
${'1.0'} | ${''} | ${false}
`('pvp.equals("$a", "$b") === $expected', ({ a, b, expected }) => {
expect(pvp.equals(a, b)).toBe(expected);
});
});

describe('.isSingleVersion(range)', () => {
it.each`
version | expected
${'==1.0'} | ${true}
${'>=1.0 && <1.1'} | ${false}
`(
'pvp.isSingleVersion("$version") === $expected',
({ version, expected }) => {
expect(pvp.isSingleVersion(version)).toBe(expected);
},
);
});

describe('.subset(subRange, superRange)', () => {
it.each`
subRange | superRange | expected
${'>=1.0 && <1.1'} | ${'>=1.0 && <2.0'} | ${true}
${'>=1.0 && <2.0'} | ${'>=1.0 && <2.0'} | ${true}
${'>=1.0 && <2.1'} | ${'>=1.0 && <2.0'} | ${false}
${'>=0.9 && <2.1'} | ${'>=1.0 && <2.0'} | ${false}
${'gibberish'} | ${''} | ${undefined}
${'>=. && <.'} | ${'>=. && <.'} | ${undefined}
`(
'pvp.subbet("$subRange", "$superRange") === $expected',
({ subRange, superRange, expected }) => {
expect(pvp.subset?.(subRange, superRange)).toBe(expected);
},
);
});

describe('.sortVersions()', () => {
it.each`
a | b | expected
${'1.0'} | ${'1.1'} | ${-1}
${'1.1'} | ${'1.0'} | ${1}
${'1.0'} | ${'1.0'} | ${0}
`('pvp.sortVersions("$a", "$b") === $expected', ({ a, b, expected }) => {
expect(pvp.sortVersions(a, b)).toBe(expected);
});
});

describe('.isStable()', () => {
it('should consider 0.0.0 stable', () => {
// in PVP, stability is not conveyed in the version number
// so we consider all versions stable
expect(pvp.isStable('0.0.0')).toBeTrue();
});
});

describe('.isCompatible()', () => {
it('should consider 0.0.0 compatible', () => {
// in PVP, there is no extra information besides the numbers
// so we consider all versions compatible
expect(pvp.isCompatible('0.0.0')).toBeTrue();
});
});
});
Loading