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(manager/poetry): fix schema for optional dependencies #32096

Merged
merged 8 commits into from
Oct 30, 2024
5 changes: 0 additions & 5 deletions lib/modules/manager/poetry/__fixtures__/pyproject.1.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ dep2 = "^0.6.0"
dep3 = "^0.33.6"
python = "~2.7 || ^3.4"

[tool.poetry.extras]
extra_dep1 = "^0.8.3"
extra_dep2 = "^0.9.4"
extra_dep3 = "^0.4.0"

[tool.poetry.dev-dependencies]
dev_dep1 = "^3.0"
dev_dep2 = "Invalid version."
Expand Down
16 changes: 13 additions & 3 deletions lib/modules/manager/poetry/__fixtures__/pyproject.2.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,21 @@ dep2 = { version = "^0.6.0" }
dep3 = { path = "/some/path/", version = '^0.33.6' }
dep4 = { path = "/some/path/" }
dep5 = {}
extra_dep1 = {version = "^0.8.3", optional = true}
extra_dep2 = {version = "^0.9.4", optional = true}
extra_dep3 = {version = "^0.4.0", optional = true}

[tool.poetry.extras]
extra_dep1 = "^0.8.3"
extra_dep2 = "^0.9.4"
extra_dep3 = '^0.4.0'
extra1 = ["extra_dep1", "extra_dep2"]
extra2 = ["extra_dep3"]

[tool.poetry.dev-dependencies]
required_dev_dep = {version = "^3.0", optional = false}
optional_dev_dep = {version = "^3.0", optional = true} # NOTE: `optional = true` is meaningless here

[tool.poetry.group.group1.dependencies]
required_group_dep = {version = "^3.0", optional = false}
optional_group_dep = {version = "^3.0", optional = true} # NOTE: `optional = true` is meaningless here

[build-system]
requires = ["poetry>=1.0", "wheel"]
Expand Down
83 changes: 47 additions & 36 deletions lib/modules/manager/poetry/__snapshots__/extract.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ exports[`modules/manager/poetry/extract extractPackageFile() extracts multiple d
"depName": "extra_dep1",
"depType": "extras",
"managerData": {
"nestedVersion": false,
"nestedVersion": true,
},
"packageName": "extra-dep1",
"versioning": "poetry",
Expand All @@ -408,7 +408,7 @@ exports[`modules/manager/poetry/extract extractPackageFile() extracts multiple d
"depName": "extra_dep2",
"depType": "extras",
"managerData": {
"nestedVersion": false,
"nestedVersion": true,
},
"packageName": "extra-dep2",
"versioning": "poetry",
Expand All @@ -419,11 +419,55 @@ exports[`modules/manager/poetry/extract extractPackageFile() extracts multiple d
"depName": "extra_dep3",
"depType": "extras",
"managerData": {
"nestedVersion": false,
"nestedVersion": true,
},
"packageName": "extra-dep3",
"versioning": "poetry",
},
{
"currentValue": "^3.0",
"datasource": "pypi",
"depName": "required_dev_dep",
"depType": "dev-dependencies",
"managerData": {
"nestedVersion": true,
},
"packageName": "required-dev-dep",
"versioning": "poetry",
},
{
"currentValue": "^3.0",
"datasource": "pypi",
"depName": "optional_dev_dep",
"depType": "dev-dependencies",
"managerData": {
"nestedVersion": true,
},
"packageName": "optional-dev-dep",
"versioning": "poetry",
},
{
"currentValue": "^3.0",
"datasource": "pypi",
"depName": "required_group_dep",
"depType": "group1",
"managerData": {
"nestedVersion": true,
},
"packageName": "required-group-dep",
"versioning": "poetry",
},
{
"currentValue": "^3.0",
"datasource": "pypi",
"depName": "optional_group_dep",
"depType": "group1",
"managerData": {
"nestedVersion": true,
},
"packageName": "optional-group-dep",
"versioning": "poetry",
},
],
"extractedConstraints": {},
"packageFileVersion": "0.1.0",
Expand Down Expand Up @@ -508,39 +552,6 @@ exports[`modules/manager/poetry/extract extractPackageFile() extracts multiple d
"packageName": "dev-dep2",
"skipReason": "invalid-version",
},
{
"currentValue": "^0.8.3",
"datasource": "pypi",
"depName": "extra_dep1",
"depType": "extras",
"managerData": {
"nestedVersion": false,
},
"packageName": "extra-dep1",
"versioning": "poetry",
},
{
"currentValue": "^0.9.4",
"datasource": "pypi",
"depName": "extra_dep2",
"depType": "extras",
"managerData": {
"nestedVersion": false,
},
"packageName": "extra-dep2",
"versioning": "poetry",
},
{
"currentValue": "^0.4.0",
"datasource": "pypi",
"depName": "extra_dep3",
"depType": "extras",
"managerData": {
"nestedVersion": false,
},
"packageName": "extra-dep3",
"versioning": "poetry",
},
]
`;

Expand Down
4 changes: 2 additions & 2 deletions lib/modules/manager/poetry/extract.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ describe('modules/manager/poetry/extract', () => {
it('extracts multiple dependencies', async () => {
const res = await extractPackageFile(pyproject1toml, filename);
expect(res?.deps).toMatchSnapshot();
expect(res?.deps).toHaveLength(10);
expect(res?.deps).toHaveLength(7);
expect(res?.extractedConstraints).toEqual({
python: '~2.7 || ^3.4',
});
Expand All @@ -60,7 +60,7 @@ describe('modules/manager/poetry/extract', () => {
it('extracts multiple dependencies (with dep = {version = "1.2.3"} case)', async () => {
const res = await extractPackageFile(pyproject2toml, filename);
expect(res).toMatchSnapshot();
expect(res?.deps).toHaveLength(8);
expect(res?.deps).toHaveLength(12);
});

it('handles case with no dependencies', async () => {
Expand Down
28 changes: 20 additions & 8 deletions lib/modules/manager/poetry/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ import * as poetryVersioning from '../../versioning/poetry';
import { dependencyPattern } from '../pip_requirements/extract';
import type { PackageDependency, PackageFileContent } from '../types';

const PoetryOptionalDependencyMixin = z
.object({
optional: z.boolean().optional().catch(false),
})
.transform(
({ optional }): PackageDependency =>
optional ? { depType: 'extras' } : {},
);

const PoetryPathDependency = z
.object({
path: z.string(),
Expand All @@ -33,7 +42,8 @@ const PoetryPathDependency = z
}

return dep;
});
})
.and(PoetryOptionalDependencyMixin);

const PoetryGitDependency = z
.object({
Expand Down Expand Up @@ -84,7 +94,8 @@ const PoetryGitDependency = z
packageName: git,
skipReason: 'git-dependency',
};
});
})
.and(PoetryOptionalDependencyMixin);

const PoetryPypiDependency = z.union([
z
Expand All @@ -102,7 +113,8 @@ const PoetryPypiDependency = z.union([
},
currentValue,
};
}),
})
.and(PoetryOptionalDependencyMixin),
z.string().transform(
(version): PackageDependency => ({
datasource: PypiDatasource.id,
Expand Down Expand Up @@ -175,10 +187,12 @@ export const PoetryDependencies = LooseRecord(
function withDepType<
Output extends PackageDependency[],
Schema extends ZodType<Output, ZodTypeDef, unknown>,
>(schema: Schema, depType: string): ZodEffects<Schema> {
>(schema: Schema, depType: string, force: boolean = false): ZodEffects<Schema> {
return schema.transform((deps) => {
for (const dep of deps) {
dep.depType = depType;
if (!dep.depType || force) {
dep.depType = depType;
}
}
return deps;
});
Expand Down Expand Up @@ -271,8 +285,8 @@ export const PoetrySectionSchema = z
'dev-dependencies': withDepType(
PoetryDependencies,
'dev-dependencies',
true,
).optional(),
extras: withDepType(PoetryDependencies, 'extras').optional(),
group: PoetryGroupDependencies.optional(),
source: PoetrySources,
})
Expand All @@ -281,14 +295,12 @@ export const PoetrySectionSchema = z
version,
dependencies = [],
'dev-dependencies': devDependencies = [],
extras: extraDependencies = [],
group: groupDependencies = [],
source: sourceUrls,
}) => {
const deps: PackageDependency[] = [
...dependencies,
...devDependencies,
...extraDependencies,
...groupDependencies,
];

Expand Down