Skip to content

Commit

Permalink
fix(pep440): Handle widening ranges with '~=' operator (#15704)
Browse files Browse the repository at this point in the history
  • Loading branch information
scemily13 committed May 28, 2022
1 parent 006b025 commit e563e22
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
3 changes: 3 additions & 0 deletions lib/modules/versioning/pep440/index.spec.ts
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
Expand Up @@ -85,7 +85,7 @@ function getFutureVersion(

interface Range {
operator: string;
prefix: string;
prefix?: string;
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

0 comments on commit e563e22

Please sign in to comment.