Skip to content

Commit

Permalink
Add --dry-run option to version command
Browse files Browse the repository at this point in the history
Resolves: #5074
  • Loading branch information
estyxx authored May 16, 2022
1 parent f0f5ae2 commit c4e1b21
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
1 change: 1 addition & 0 deletions docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,7 @@ The table below illustrates the effect of these rules with concrete examples.
### Options

* `--short (-s)`: Output the version number only.
* `--dry-run`: Do not update pyproject.toml file.

## export

Expand Down
20 changes: 14 additions & 6 deletions src/poetry/console/commands/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,14 @@ class VersionCommand(Command):
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 = """\
The version command shows the current version of the project or bumps the version of
Expand Down Expand Up @@ -66,12 +73,13 @@ def handle(self) -> None:
f" to <fg=green>{version}</>"
)

content: dict[str, Any] = self.poetry.file.read()
poetry_content = content["tool"]["poetry"]
poetry_content["version"] = version.text
if not self.option("dry-run"):
content: dict[str, Any] = self.poetry.file.read()
poetry_content = content["tool"]["poetry"]
poetry_content["version"] = version.text

assert isinstance(content, TOMLDocument)
self.poetry.file.write(content)
assert isinstance(content, TOMLDocument)
self.poetry.file.write(content)
else:
if self.option("short"):
self.line(self.poetry.package.pretty_version)
Expand Down
9 changes: 9 additions & 0 deletions tests/console/commands/test_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,12 @@ def test_version_update(tester: CommandTester):
def test_short_version_update(tester: CommandTester):
tester.execute("--short 2.0.0")
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

0 comments on commit c4e1b21

Please sign in to comment.