Skip to content

feat: add --next-phase command to poetry version #8089

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

Merged
merged 11 commits into from
Sep 10, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
9 changes: 9 additions & 0 deletions docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -693,8 +693,17 @@ The table below illustrates the effect of these rules with concrete examples.
| prerelease | 1.0.3a0 | 1.0.3a1 |
| prerelease | 1.0.3b0 | 1.0.3b1 |

The option `--next-phase` allows the increment of prerelease phase versions.

| rule | before | after |
| ---------- |---------|---------|
| prerelease --next-phase | 1.0.3a0 | 1.0.3b0 |
| prerelease --next-phase | 1.0.3b0 | 1.0.3rc0 |
| prerelease --next-phase | 1.0.3rc0 | 1.0.3 |

### Options

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

Expand Down
12 changes: 8 additions & 4 deletions src/poetry/console/commands/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class VersionCommand(Command):
"version",
"The version number or the rule to update the version.",
optional=True,
)
),
]
options = [
option("short", "s", "Output the version number only"),
Expand All @@ -36,6 +36,7 @@ class VersionCommand(Command):
None,
"Do not update pyproject.toml file",
),
option("next-phase", None, "Increment the phase of the current version"),
]

help = """\
Expand All @@ -62,7 +63,7 @@ def handle(self) -> int:

if version:
version = self.increment_version(
self.poetry.package.pretty_version, version
self.poetry.package.pretty_version, version, self.option("next-phase")
)

if self.option("short"):
Expand Down Expand Up @@ -91,7 +92,9 @@ def handle(self) -> int:

return 0

def increment_version(self, version: str, rule: str) -> Version:
def increment_version(
self, version: str, rule: str, next_phase: bool = False
) -> Version:
from poetry.core.constraints.version import Version

try:
Expand All @@ -115,7 +118,8 @@ def increment_version(self, version: str, rule: str) -> Version:
if parsed.is_unstable():
pre = parsed.pre
assert pre is not None
new = Version(parsed.epoch, parsed.release, pre.next())
pre = pre.next_phase() if next_phase else pre.next()
new = Version(parsed.epoch, parsed.release, pre)
else:
new = parsed.next_patch().first_prerelease()
else:
Expand Down
19 changes: 19 additions & 0 deletions tests/console/commands/test_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,25 @@ def test_increment_version(
assert command.increment_version(version, rule).text == expected


@pytest.mark.parametrize(
"version, rule, expected",
[
("1.2.3", "prerelease", "1.2.4a0"),
("1.2.3a0", "prerelease", "1.2.3b0"),
("1.2.3a1", "prerelease", "1.2.3b0"),
("1.2.3b1", "prerelease", "1.2.3rc0"),
("1.2.3rc0", "prerelease", "1.2.3"),
("1.2.3-beta.1", "prerelease", "1.2.3rc0"),
("1.2.3-beta1", "prerelease", "1.2.3rc0"),
("1.2.3beta1", "prerelease", "1.2.3rc0"),
],
)
def test_next_phase_version(
version: str, rule: str, expected: str, command: VersionCommand
) -> None:
assert command.increment_version(version, rule, True).text == expected


def test_version_show(tester: CommandTester) -> None:
tester.execute()
assert tester.io.fetch_output() == "simple-project 1.2.3\n"
Expand Down