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

fix(pep440): Handle widening ranges with '~=' operator #15704

Merged
merged 3 commits into from
May 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/modules/versioning/pep440/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@ describe('modules/versioning/pep440/index', () => {
${'~=7.2'} | ${'replace'} | ${'7.2.0'} | ${'8.0.1'} | ${'~=8.0'}
${'~=7.2'} | ${'replace'} | ${'7.2.0'} | ${'8'} | ${'~=8.0'}
${'~=7.2.0'} | ${'replace'} | ${'7.2.0'} | ${'8.2'} | ${'~=8.2.0'}
${'~=7.2'} | ${'widen'} | ${'7.2.0'} | ${'8.0.1'} | ${'>=7.2,<9'}
${'~=7.2'} | ${'widen'} | ${'7.2.0'} | ${'8'} | ${'>=7.2,<9'}
${'~=7.2.0'} | ${'widen'} | ${'7.2.0'} | ${'8.2'} | ${'>=7.2.0,<8.3'}
${'==3.2.*,>=3.2.2'} | ${'replace'} | ${'3.2.2'} | ${'4.1.1'} | ${'==4.1.*'}
${'==3.2.*,>=3.2.2'} | ${'replace'} | ${'3.2.2'} | ${'4.0.0'} | ${'==4.0.*'}
${'>=1.0.0,<1.1.0'} | ${'replace'} | ${'1.0.0'} | ${'1.2.0'} | ${'>=1.2.0,<1.3.0'}
Expand Down
29 changes: 27 additions & 2 deletions lib/modules/versioning/pep440/range.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ function getFutureVersion(

interface Range {
operator: string;
prefix: string;
prefix?: string;
viceice marked this conversation as resolved.
Show resolved Hide resolved
version: string;
}

Expand Down Expand Up @@ -365,6 +365,23 @@ function trimTrailingZeros(numbers: number[]): number[] {
return numbers.slice(0, i + 1);
}

function divideCompatibleReleaseRange(currentRange: Range): Range[] {
const currentVersionUpperBound = currentRange.version
.split('.')
.map((num) => parseInt(num));
if (currentVersionUpperBound.length > 1) {
currentVersionUpperBound.splice(-1);
}
currentVersionUpperBound[currentVersionUpperBound.length - 1] += 1;
return [
{ operator: '>=', version: currentRange.version },
{
operator: '<',
version: currentVersionUpperBound.join('.'),
},
];
}

function handleWidenStrategy(
{ currentValue, rangeStrategy, currentVersion, newVersion }: NewValueConfig,
ranges: Range[]
Expand All @@ -375,7 +392,15 @@ function handleWidenStrategy(
}
let rangePrecision = getRangePrecision(ranges);
const trimZeros = hasZeroSpecifier(ranges);
return ranges.map((range) => {
let newRanges: Range[] = [];
if (ranges.length === 1 && ranges[0].operator === '~=') {
// If range operator is '~=', divide the range into its logical equivalent.
// Example: ~=1.0 --> >=1.0,<2
newRanges = divideCompatibleReleaseRange(ranges[0]);
} else {
newRanges = ranges;
}
return newRanges.map((range) => {
// newVersion is over the upper bound
if (range.operator === '<' && gte(newVersion, range.version)) {
const upperBound = parseVersion(range.version)?.release ?? [];
Expand Down