Commit c4e1b218 by Etty Committed by GitHub

Add --dry-run option to version command

Resolves: #5074
parent f0f5ae22
...@@ -623,6 +623,7 @@ The table below illustrates the effect of these rules with concrete examples. ...@@ -623,6 +623,7 @@ The table below illustrates the effect of these rules with concrete examples.
### Options ### Options
* `--short (-s)`: Output the version number only. * `--short (-s)`: Output the version number only.
* `--dry-run`: Do not update pyproject.toml file.
## export ## export
......
...@@ -29,7 +29,14 @@ class VersionCommand(Command): ...@@ -29,7 +29,14 @@ class VersionCommand(Command):
optional=True, optional=True,
) )
] ]
options = [option("short", "s", "Output the version number only")] options = [
option("short", "s", "Output the version number only"),
option(
"dry-run",
None,
"Do not update pyproject.toml file",
),
]
help = """\ help = """\
The version command shows the current version of the project or bumps the version of The version command shows the current version of the project or bumps the version of
...@@ -66,12 +73,13 @@ patch, minor, major, prepatch, preminor, premajor, prerelease. ...@@ -66,12 +73,13 @@ patch, minor, major, prepatch, preminor, premajor, prerelease.
f" to <fg=green>{version}</>" f" to <fg=green>{version}</>"
) )
content: dict[str, Any] = self.poetry.file.read() if not self.option("dry-run"):
poetry_content = content["tool"]["poetry"] content: dict[str, Any] = self.poetry.file.read()
poetry_content["version"] = version.text poetry_content = content["tool"]["poetry"]
poetry_content["version"] = version.text
assert isinstance(content, TOMLDocument) assert isinstance(content, TOMLDocument)
self.poetry.file.write(content) self.poetry.file.write(content)
else: else:
if self.option("short"): if self.option("short"):
self.line(self.poetry.package.pretty_version) self.line(self.poetry.package.pretty_version)
......
...@@ -76,3 +76,12 @@ def test_version_update(tester: CommandTester): ...@@ -76,3 +76,12 @@ def test_version_update(tester: CommandTester):
def test_short_version_update(tester: CommandTester): def test_short_version_update(tester: CommandTester):
tester.execute("--short 2.0.0") tester.execute("--short 2.0.0")
assert tester.io.fetch_output() == "2.0.0\n" assert tester.io.fetch_output() == "2.0.0\n"
def test_dry_run(tester: CommandTester):
old_pyproject = tester.command.poetry.file.path.read_text()
tester.execute("--dry-run major")
new_pyproject = tester.command.poetry.file.path.read_text()
assert tester.io.fetch_output() == "Bumping version from 1.2.3 to 2.0.0\n"
assert old_pyproject == new_pyproject
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment