Skip to content

Commit

Permalink
Extract python version from explicit main group (poetry)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ticosax committed Nov 8, 2023
1 parent b64ffca commit 2eb6c43
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
12 changes: 12 additions & 0 deletions __tests__/utils.test.ts
Expand Up @@ -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 => {
Expand Down
18 changes: 13 additions & 5 deletions src/utils.ts
Expand Up @@ -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}`);
Expand Down

0 comments on commit 2eb6c43

Please sign in to comment.