-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(manager/poetry): use zod schema validation (#23830)
- Loading branch information
Showing
7 changed files
with
184 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { z } from 'zod'; | ||
import { LooseRecord, Toml } from '../../../util/schema-utils'; | ||
|
||
const PoetryDependencySchema = z.object({ | ||
path: z.string().optional(), | ||
git: z.string().optional(), | ||
tag: z.string().optional(), | ||
version: z.string().optional(), | ||
}); | ||
|
||
export const PoetryDependencyRecord = LooseRecord( | ||
z.string(), | ||
z.union([PoetryDependencySchema, z.array(PoetryDependencySchema), z.string()]) | ||
); | ||
|
||
export type PoetryDependencyRecord = z.infer<typeof PoetryDependencyRecord>; | ||
|
||
export const PoetryGroupRecord = LooseRecord( | ||
z.string(), | ||
z.object({ | ||
dependencies: PoetryDependencyRecord.optional(), | ||
}) | ||
); | ||
|
||
export type PoetryGroupRecord = z.infer<typeof PoetryGroupRecord>; | ||
|
||
export const PoetrySectionSchema = z.object({ | ||
dependencies: PoetryDependencyRecord.optional(), | ||
'dev-dependencies': PoetryDependencyRecord.optional(), | ||
extras: PoetryDependencyRecord.optional(), | ||
group: PoetryGroupRecord.optional(), | ||
source: z | ||
.array(z.object({ name: z.string(), url: z.string().optional() })) | ||
.optional(), | ||
}); | ||
|
||
export type PoetrySectionSchema = z.infer<typeof PoetrySectionSchema>; | ||
|
||
export const PoetrySchema = z.object({ | ||
tool: z | ||
.object({ | ||
poetry: PoetrySectionSchema.optional(), | ||
}) | ||
.optional(), | ||
'build-system': z | ||
.object({ | ||
requires: z.array(z.string()), | ||
'build-backend': z.string().optional(), | ||
}) | ||
.optional(), | ||
}); | ||
|
||
export type PoetrySchema = z.infer<typeof PoetrySchema>; | ||
|
||
export const PoetrySchemaToml = Toml.pipe(PoetrySchema); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { codeBlock } from 'common-tags'; | ||
import { parsePoetry } from './utils'; | ||
|
||
describe('modules/manager/poetry/utils', () => { | ||
const fileName = 'fileName'; | ||
|
||
describe('parsePoetry', () => { | ||
it('load and parse successfully', () => { | ||
const fileContent = codeBlock` | ||
[tool.poetry.dependencies] | ||
dep1 = "1.0.0" | ||
[tool.poetry.group.dev.dependencies] | ||
dep2 = "1.0.1" | ||
`; | ||
const actual = parsePoetry(fileName, fileContent); | ||
expect(actual).toMatchObject({ | ||
tool: { | ||
poetry: { | ||
dependencies: { dep1: '1.0.0' }, | ||
group: { dev: { dependencies: { dep2: '1.0.1' } } }, | ||
}, | ||
}, | ||
}); | ||
}); | ||
|
||
it('invalid toml', () => { | ||
const actual = parsePoetry(fileName, 'clearly_invalid'); | ||
expect(actual).toBeNull(); | ||
}); | ||
|
||
it('invalid schema', () => { | ||
const fileContent = codeBlock` | ||
[tool.poetry.dependencies]: | ||
dep1 = 1 | ||
`; | ||
const actual = parsePoetry(fileName, fileContent); | ||
expect(actual).toBeNull(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { logger } from '../../../logger'; | ||
import { type PoetrySchema, PoetrySchemaToml } from './schema'; | ||
|
||
export function parsePoetry( | ||
fileName: string, | ||
fileContent: string | ||
): PoetrySchema | null { | ||
const res = PoetrySchemaToml.safeParse(fileContent); | ||
if (res.success) { | ||
return res.data; | ||
} | ||
logger.debug({ err: res.error, fileName }, 'Error parsing poetry lockfile.'); | ||
return null; | ||
} |