From e563e22e2b5d025a1f19cdeb05d1343fbbed746f Mon Sep 17 00:00:00 2001 From: scemily13 <68038396+scemily13@users.noreply.github.com> Date: Sat, 28 May 2022 00:39:21 -0400 Subject: [PATCH] fix(pep440): Handle widening ranges with '~=' operator (#15704) --- lib/modules/versioning/pep440/index.spec.ts | 3 +++ lib/modules/versioning/pep440/range.ts | 29 +++++++++++++++++++-- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/lib/modules/versioning/pep440/index.spec.ts b/lib/modules/versioning/pep440/index.spec.ts index eabcdf676d..84fe37c509 100644 --- a/lib/modules/versioning/pep440/index.spec.ts +++ b/lib/modules/versioning/pep440/index.spec.ts @@ -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'} diff --git a/lib/modules/versioning/pep440/range.ts b/lib/modules/versioning/pep440/range.ts index 56920e731a..ca7efbad6c 100644 --- a/lib/modules/versioning/pep440/range.ts +++ b/lib/modules/versioning/pep440/range.ts @@ -85,7 +85,7 @@ function getFutureVersion( interface Range { operator: string; - prefix: string; + prefix?: string; version: string; } @@ -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[] @@ -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 ?? []; -- GitLab