From 2eb6c43547e331c783666da12dc296e9bec30f2e Mon Sep 17 00:00:00 2001 From: Nicolas Delaby Date: Wed, 18 Oct 2023 12:56:03 +0200 Subject: [PATCH] Extract python version from explicit main group (poetry) As documented here https://python-poetry.org/docs/managing-dependencies/#dependency-groups ```toml [tool.poetry.dependencies] ``` and ```toml [tool.poetry.group.main.dependencies] ``` Are equivalent --- __tests__/utils.test.ts | 12 ++++++++++++ src/utils.ts | 18 +++++++++++++----- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/__tests__/utils.test.ts b/__tests__/utils.test.ts index f5e3647..9087288 100644 --- a/__tests__/utils.test.ts +++ b/__tests__/utils.test.ts @@ -126,6 +126,18 @@ describe('Version from file test', () => { expect(_fn(pythonVersionFilePath)).toEqual([pythonVersion]); } ); + it.each([getVersionInputFromTomlFile, getVersionInputFromFile])( + 'Version from poetry with explicit main group pyproject.toml test', + async _fn => { + await io.mkdirP(tempDir); + const pythonVersionFileName = 'pyproject.toml'; + const pythonVersionFilePath = path.join(tempDir, pythonVersionFileName); + const pythonVersion = '>=3.7.0'; + const pythonVersionFileContent = `[tool.poetry.group.main.dependencies]\npython = "${pythonVersion}"`; + fs.writeFileSync(pythonVersionFilePath, pythonVersionFileContent); + expect(_fn(pythonVersionFilePath)).toEqual([pythonVersion]); + } + ); it.each([getVersionInputFromTomlFile, getVersionInputFromFile])( 'Version undefined', async _fn => { diff --git a/src/utils.ts b/src/utils.ts index db563d0..0d36358 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -228,15 +228,23 @@ export function getVersionInputFromTomlFile(versionFile: string): string[] { if ('project' in pyprojectConfig) { // standard project metadata (PEP 621) - keys = ['project', 'requires-python']; + keys = [['project', 'requires-python']]; } else { // python poetry - keys = ['tool', 'poetry', 'dependencies', 'python']; + keys = [ + // implicit group main + ['tool', 'poetry', 'dependencies', 'python'], + // explicit group main + ['tool', 'poetry', 'group', 'main', 'dependencies', 'python'] + ]; } const versions = []; - const version = extractValue(pyprojectConfig, keys); - if (version !== undefined) { - versions.push(version); + for (const key of keys) { + const version = extractValue(pyprojectConfig, key); + if (version !== undefined) { + versions.push(version); + break; + } } core.info(`Extracted ${versions} from ${versionFile}`);