Skip to content

Commit

Permalink
feat(poetry): handle dependency groups (#17964)
Browse files Browse the repository at this point in the history
  • Loading branch information
mkniewallner authored Oct 6, 2022
1 parent 72371cb commit 4c1b3e5
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 9 deletions.
26 changes: 26 additions & 0 deletions lib/modules/manager/poetry/extract.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,32 @@ describe('modules/manager/poetry/extract', () => {
});
});

it('extracts dependencies from dependency groups', async () => {
const content =
'[tool.poetry.dependencies]\ndep = "^2.0"\n\n[tool.poetry.group.dev.dependencies]\ndev_dep = "^3.0"\n\n[tool.poetry.group.typing.dependencies]\ntyping_dep = "^4.0"';
const res = await extractPackageFile(content, filename);
expect(res?.deps).toMatchObject([
{
currentValue: '^2.0',
datasource: 'pypi',
depName: 'dep',
depType: 'dependencies',
},
{
currentValue: '^3.0',
datasource: 'pypi',
depName: 'dev_dep',
depType: 'dev',
},
{
currentValue: '^4.0',
datasource: 'pypi',
depName: 'typing_dep',
depType: 'typing',
},
]);
});

it('resolves lockedVersions from the lockfile', async () => {
fs.readLocalFile.mockResolvedValue(pyproject11tomlLock);
const res = await extractPackageFile(pyproject11toml, filename);
Expand Down
54 changes: 45 additions & 9 deletions lib/modules/manager/poetry/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,43 @@ import * as pep440Versioning from '../../versioning/pep440';
import * as poetryVersioning from '../../versioning/poetry';
import type { PackageDependency, PackageFile } from '../types';
import { extractLockFileEntries } from './locked-version';
import type { PoetryFile, PoetrySection } from './types';
import type { PoetryDependency, PoetryFile, PoetrySection } from './types';

function extractFromSection(
function extractFromDependenciesSection(
parsedFile: PoetryFile,
section: keyof Omit<PoetrySection, 'source'>,
section: keyof Omit<PoetrySection, 'source' | 'group'>,
poetryLockfile: Record<string, string>
): PackageDependency[] {
return extractFromSection(
parsedFile.tool?.poetry?.[section],
section,
poetryLockfile
);
}

function extractFromDependenciesGroupSection(
parsedFile: PoetryFile,
group: string,
poetryLockfile: Record<string, string>
): PackageDependency[] {
return extractFromSection(
parsedFile.tool?.poetry?.group[group]?.dependencies,
group,
poetryLockfile
);
}

function extractFromSection(
sectionContent: Record<string, PoetryDependency | string> | undefined,
depType: string,
poetryLockfile: Record<string, string>
): PackageDependency[] {
const deps: PackageDependency[] = [];
const sectionContent = parsedFile.tool?.poetry?.[section];
if (!sectionContent) {
return [];
}

const deps: PackageDependency[] = [];

for (const depName of Object.keys(sectionContent)) {
if (depName === 'python' || depName === 'source') {
continue;
Expand Down Expand Up @@ -56,7 +80,7 @@ function extractFromSection(
}
const dep: PackageDependency = {
depName,
depType: section,
depType,
currentValue,
managerData: { nestedVersion },
datasource: PypiDatasource.id,
Expand Down Expand Up @@ -121,10 +145,22 @@ export async function extractPackageFile(
const lockfileMapping = extractLockFileEntries(lockContents);

const deps = [
...extractFromSection(pyprojectfile, 'dependencies', lockfileMapping),
...extractFromSection(pyprojectfile, 'dev-dependencies', lockfileMapping),
...extractFromSection(pyprojectfile, 'extras', lockfileMapping),
...extractFromDependenciesSection(
pyprojectfile,
'dependencies',
lockfileMapping
),
...extractFromDependenciesSection(
pyprojectfile,
'dev-dependencies',
lockfileMapping
),
...extractFromDependenciesSection(pyprojectfile, 'extras', lockfileMapping),
...Object.keys(pyprojectfile.tool?.poetry?.group ?? []).flatMap((group) =>
extractFromDependenciesGroupSection(pyprojectfile, group, lockfileMapping)
),
];

if (!deps.length) {
return null;
}
Expand Down
1 change: 1 addition & 0 deletions lib/modules/manager/poetry/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ The following `depTypes` are supported by the Poetry manager:
- `dependencies`
- `dev-dependencies`
- `extras`
- `<group-name>` (dynamic, based on the group name, per [dependency groups documentation](https://python-poetry.org/docs/managing-dependencies/#dependency-groups))
5 changes: 5 additions & 0 deletions lib/modules/manager/poetry/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export interface PoetrySection {
dependencies: Record<string, PoetryDependency | string>;
'dev-dependencies': Record<string, PoetryDependency | string>;
extras: Record<string, PoetryDependency | string>;
group: Record<string, PoetryGroup>;
source?: PoetrySource[];
}

Expand All @@ -27,6 +28,10 @@ export interface PoetrySource {
url?: string;
}

export interface PoetryGroup {
dependencies: Record<string, PoetryDependency | string>;
}

export interface PoetryLockSection {
name?: string;
version?: string;
Expand Down

0 comments on commit 4c1b3e5

Please sign in to comment.