Skip to content

Commit 534c984

Browse files
abichingerlennard28
andcommitted
fix(poetry): Set version inside the project section #45
Since poetry >= 2.0.0 it is possible to set the version under the `[project]` section. Co-authored-by: lennard28 <[email protected]>
1 parent 80d6914 commit 534c984

File tree

2 files changed

+62
-8
lines changed

2 files changed

+62
-8
lines changed

lib/py/set_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
# modifiy version (tomlkit will preserve comments)
1919
toml = parse(pyproject)
20-
if 'tool' in toml and 'poetry' in toml['tool']:
20+
if 'tool' in toml and 'poetry' in toml['tool'] and "version" in toml["tool"]["poetry"]:
2121
print('Poetry package detected')
2222
toml['tool']['poetry']['version'] = args.version
2323
else:

test/prepare.test.ts

Lines changed: 61 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import fs from 'fs';
2+
import TOML from 'smol-toml';
13
import { describe, expect, test } from 'vitest';
24
import {
35
bDistPackage,
@@ -52,13 +54,65 @@ test('prepare: setVersionPy', async () => {
5254
);
5355
});
5456

55-
test('prepare: setVersionToml', async () => {
56-
const { config, context } = genPackage({
57-
legacyInterface: false,
58-
});
59-
await expect(
60-
setVersionToml(config.srcDir, '2.0.0', pipe(context)),
61-
).resolves.toBe(undefined);
57+
describe('prepare: setVersionToml', async () => {
58+
const testCases = [
59+
{
60+
name: 'version in project',
61+
section: 'project',
62+
content: `[project]
63+
version = "0.0.0"`,
64+
},
65+
{
66+
name: 'no version',
67+
section: 'project',
68+
content: `[project]
69+
name = "test"`,
70+
},
71+
{
72+
name: 'poetry: version in tool.poetry',
73+
section: 'tool.poetry',
74+
content: `[tool.poetry]
75+
version = "0.0.0"`,
76+
},
77+
{
78+
name: 'poetry: version in project',
79+
section: 'project',
80+
content: `[project]
81+
version = "0.0.0"
82+
83+
[tool.poetry]
84+
name = "test"
85+
86+
[tool.poetry.dependencies]
87+
python = "^3.7"`,
88+
},
89+
];
90+
91+
for (const t of testCases) {
92+
test(
93+
t.name,
94+
async () => {
95+
const { config, context } = genPackage({
96+
legacyInterface: false,
97+
content: t.content,
98+
});
99+
await expect(
100+
setVersionToml(config.srcDir, '1.0.0', pipe(context)),
101+
).resolves.toBe(undefined);
102+
103+
const toml = fs.readFileSync(config.pyprojectPath, {
104+
encoding: 'utf8',
105+
});
106+
const pyproject: any = TOML.parse(toml);
107+
const version = t.section
108+
.split('.')
109+
.reduce((acc, key) => acc[key], pyproject).version;
110+
111+
await expect(version).toBe('1.0.0');
112+
},
113+
60000,
114+
);
115+
}
62116
});
63117

64118
test('prepare: versionCmd', async () => {

0 commit comments

Comments
 (0)