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 Oct 18, 2023
1 parent a00ea43 commit 2039a9c
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 (let key of keys) {

Check failure on line 242 in src/utils.ts

View workflow job for this annotation

GitHub Actions / Basic validation / build (ubuntu-latest)

'key' is never reassigned. Use 'const' instead

Check failure on line 242 in src/utils.ts

View workflow job for this annotation

GitHub Actions / Basic validation / build (windows-latest)

'key' is never reassigned. Use 'const' instead

Check failure on line 242 in src/utils.ts

View workflow job for this annotation

GitHub Actions / Basic validation / build (macos-latest)

'key' is never reassigned. Use 'const' instead
const version = extractValue(pyprojectConfig, key);
if (version !== undefined) {
versions.push(version);
break;
}
}

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

0 comments on commit 2039a9c

Please sign in to comment.