Skip to content

Commit

Permalink
feat: add --next-phase command to poetry version (#8089)
Browse files Browse the repository at this point in the history
  • Loading branch information
nfnvsc authored Sep 10, 2023
1 parent fc57323 commit 398523c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
9 changes: 9 additions & 0 deletions docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -698,8 +698,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
26 changes: 26 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 All @@ -75,6 +94,13 @@ def test_short_version_update(tester: CommandTester) -> None:
assert tester.io.fetch_output() == "2.0.0\n"


def test_phase_version_update(tester: CommandTester) -> None:
assert isinstance(tester.command, VersionCommand)
tester.command.poetry.package._set_version("1.2.4a0")
tester.execute("prerelease --next-phase")
assert tester.io.fetch_output() == "Bumping version from 1.2.4a0 to 1.2.4b0\n"


def test_dry_run(tester: CommandTester) -> None:
assert isinstance(tester.command, VersionCommand)
old_pyproject = tester.command.poetry.file.path.read_text()
Expand Down

0 comments on commit 398523c

Please sign in to comment.