diff --git a/docs/cli.md b/docs/cli.md index 11cfc05751b..1a8fb764725 100644 --- a/docs/cli.md +++ b/docs/cli.md @@ -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 diff --git a/src/poetry/console/commands/version.py b/src/poetry/console/commands/version.py index 7b5744d6171..28b3c6815f3 100644 --- a/src/poetry/console/commands/version.py +++ b/src/poetry/console/commands/version.py @@ -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 @@ -66,12 +73,13 @@ def handle(self) -> None: f" to {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) diff --git a/tests/console/commands/test_version.py b/tests/console/commands/test_version.py index bd93589412b..36487a3f38a 100644 --- a/tests/console/commands/test_version.py +++ b/tests/console/commands/test_version.py @@ -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