Skip to content

Commit 2eb6c43

Browse files
committed
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
1 parent b64ffca commit 2eb6c43

2 files changed

Lines changed: 25 additions & 5 deletions

File tree

__tests__/utils.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,18 @@ describe('Version from file test', () => {
126126
expect(_fn(pythonVersionFilePath)).toEqual([pythonVersion]);
127127
}
128128
);
129+
it.each([getVersionInputFromTomlFile, getVersionInputFromFile])(
130+
'Version from poetry with explicit main group pyproject.toml test',
131+
async _fn => {
132+
await io.mkdirP(tempDir);
133+
const pythonVersionFileName = 'pyproject.toml';
134+
const pythonVersionFilePath = path.join(tempDir, pythonVersionFileName);
135+
const pythonVersion = '>=3.7.0';
136+
const pythonVersionFileContent = `[tool.poetry.group.main.dependencies]\npython = "${pythonVersion}"`;
137+
fs.writeFileSync(pythonVersionFilePath, pythonVersionFileContent);
138+
expect(_fn(pythonVersionFilePath)).toEqual([pythonVersion]);
139+
}
140+
);
129141
it.each([getVersionInputFromTomlFile, getVersionInputFromFile])(
130142
'Version undefined',
131143
async _fn => {

src/utils.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -228,15 +228,23 @@ export function getVersionInputFromTomlFile(versionFile: string): string[] {
228228

229229
if ('project' in pyprojectConfig) {
230230
// standard project metadata (PEP 621)
231-
keys = ['project', 'requires-python'];
231+
keys = [['project', 'requires-python']];
232232
} else {
233233
// python poetry
234-
keys = ['tool', 'poetry', 'dependencies', 'python'];
234+
keys = [
235+
// implicit group main
236+
['tool', 'poetry', 'dependencies', 'python'],
237+
// explicit group main
238+
['tool', 'poetry', 'group', 'main', 'dependencies', 'python']
239+
];
235240
}
236241
const versions = [];
237-
const version = extractValue(pyprojectConfig, keys);
238-
if (version !== undefined) {
239-
versions.push(version);
242+
for (const key of keys) {
243+
const version = extractValue(pyprojectConfig, key);
244+
if (version !== undefined) {
245+
versions.push(version);
246+
break;
247+
}
240248
}
241249

242250
core.info(`Extracted ${versions} from ${versionFile}`);

0 commit comments

Comments
 (0)