Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --dry-run option to version command #5603

Merged
merged 8 commits into from
May 16, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,7 @@ The table below illustrates the effect of these rules with concrete examples.
### Options

* `--short (-s)`: Output the version number only.
* `--dry-run`: Output the operations but do not execute anything.
estyxx marked this conversation as resolved.
Show resolved Hide resolved

## export

Expand Down
16 changes: 10 additions & 6 deletions src/poetry/console/commands/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ 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, "Outputs the operations but will not execute anything"),
estyxx marked this conversation as resolved.
Show resolved Hide resolved
]

help = """\
The version command shows the current version of the project or bumps the version of
Expand Down Expand Up @@ -66,12 +69,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):
abn marked this conversation as resolved.
Show resolved Hide resolved
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