Skip to content

Commit

Permalink
fix(poetry): Provide better feedback for negation in the ranges (#27193)
Browse files Browse the repository at this point in the history
  • Loading branch information
zharinov committed Feb 9, 2024
1 parent 82e621f commit a62cfa0
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
3 changes: 3 additions & 0 deletions lib/modules/versioning/poetry/index.spec.ts
Expand Up @@ -83,6 +83,8 @@ describe('modules/versioning/poetry/index', () => {

it.each`
version | expected
${null} | ${false}
${undefined} | ${false}
${'17.04.00'} | ${true}
${'17.b4.0'} | ${false}
${'1.2.3'} | ${true}
Expand All @@ -101,6 +103,7 @@ describe('modules/versioning/poetry/index', () => {
${'renovatebot/renovate'} | ${false}
${'renovatebot/renovate#master'} | ${false}
${'https://github.com/renovatebot/renovate.git'} | ${false}
${'>=2.6, !=3.0.*, !=3.1.*, !=3.2.*, <4'} | ${false}
`('isValid("$version") === $expected', ({ version, expected }) => {
expect(!!versioning.isValid(version)).toBe(expected);
});
Expand Down
14 changes: 13 additions & 1 deletion lib/modules/versioning/poetry/index.ts
Expand Up @@ -64,7 +64,19 @@ function isLessThanRange(version: string, range: string): boolean {
}

export function isValid(input: string): boolean {
return npm.isValid(poetry2npm(input));
if (!input) {
return false;
}

try {
return npm.isValid(poetry2npm(input, true));
} catch (err) {
logger.once.debug(
{ version: input },
'Poetry version or range is not supported by current implementation',
);
return false;
}
}

function isStable(version: string): boolean {
Expand Down
11 changes: 10 additions & 1 deletion lib/modules/versioning/poetry/transform.ts
@@ -1,4 +1,5 @@
import semver from 'semver';
import { regEx } from '../../../util/regex';
import { RANGE_COMPARATOR_PATTERN, VERSION_PATTERN } from './patterns';

interface LetterTag {
Expand Down Expand Up @@ -100,7 +101,7 @@ export function semver2poetry(version?: string): string | null {
* add a '^', because poetry treats versions without operators as
* exact versions.
*/
export function poetry2npm(input: string): string {
export function poetry2npm(input: string, throwOnUnsupported = false): string {
// replace commas with spaces, then split at valid semver comparators
const chunks = input
.split(',')
Expand All @@ -113,6 +114,14 @@ export function poetry2npm(input: string): string {
.map((chunk) => poetry2semver(chunk, false) ?? chunk)
.join('')
.replace(/===/, '=');
if (throwOnUnsupported) {
const isUnsupported = transformed
.split(regEx(/\s+/))
.some((part) => part.startsWith('!=')); // Patterns like `!=1.2.3` can't be easily translated between poetry/npm
if (isUnsupported) {
throw new Error('Unsupported by Poetry versioning implementation');
}
}
return transformed;
}

Expand Down

0 comments on commit a62cfa0

Please sign in to comment.