Commit f43ef050 by Maria Soulountsi Committed by Steph Samson

Show project version if no argument provided for version command (#1191)

This changes the behavior of version command to show the current version
of the project if no arguments are provided.
parent 159123f3
...@@ -384,8 +384,9 @@ poetry lock ...@@ -384,8 +384,9 @@ poetry lock
## version ## version
This command bumps the version of the project This command shows the current version of the project or bumps the version of
and writes the new version back to `pyproject.toml` the project and writes the new version back to `pyproject.toml` if a valid
bump rule is provided.
The new version should ideally be a valid semver string or a valid bump rule: The new version should ideally be a valid semver string or a valid bump rule:
`patch`, `minor`, `major`, `prepatch`, `preminor`, `premajor`, `prerelease`. `patch`, `minor`, `major`, `prepatch`, `preminor`, `premajor`, `prerelease`.
......
...@@ -6,20 +6,23 @@ from .command import Command ...@@ -6,20 +6,23 @@ from .command import Command
class VersionCommand(Command): class VersionCommand(Command):
name = "version" name = "version"
description = "Bumps the version of the project." description = (
"Shows the version of the project or bumps it when a valid"
"bump rule is provided."
)
arguments = [ arguments = [
argument( argument(
"version", "version",
"The version number or the rule to update the version.", "The version number or the rule to update the version.",
optional=True, optional=True,
default="patch",
) )
] ]
help = """\ help = """\
The version command bumps the version of the project The version command shows the current version of the project or bumps the version of
and writes the new version back to <comment>pyproject.toml</>. the project and writes the new version back to <comment>pyproject.toml</> if a valid
bump rule is provided.
The new version should ideally be a valid semver string or a valid bump rule: The new version should ideally be a valid semver string or a valid bump rule:
patch, minor, major, prepatch, preminor, premajor, prerelease. patch, minor, major, prepatch, preminor, premajor, prerelease.
...@@ -38,19 +41,28 @@ patch, minor, major, prepatch, preminor, premajor, prerelease. ...@@ -38,19 +41,28 @@ patch, minor, major, prepatch, preminor, premajor, prerelease.
def handle(self): def handle(self):
version = self.argument("version") version = self.argument("version")
version = self.increment_version(self.poetry.package.pretty_version, version) if version:
version = self.increment_version(
self.line(
"Bumping version from <comment>{}</> to <info>{}</>".format(
self.poetry.package.pretty_version, version self.poetry.package.pretty_version, version
) )
)
content = self.poetry.file.read() self.line(
poetry_content = content["tool"]["poetry"] "Bumping version from <comment>{}</> to <info>{}</>".format(
poetry_content["version"] = version.text self.poetry.package.pretty_version, version
)
)
content = self.poetry.file.read()
poetry_content = content["tool"]["poetry"]
poetry_content["version"] = version.text
self.poetry.file.write(content) self.poetry.file.write(content)
else:
self.line(
"Project (<comment>{}</>) version is <info>{}</>".format(
self.poetry.package.name, self.poetry.package.pretty_version
)
)
def increment_version(self, version, rule): def increment_version(self, version, rule):
from poetry.semver import Version from poetry.semver import Version
......
import pytest import pytest
from cleo import CommandTester
from poetry.console.commands import VersionCommand from poetry.console.commands import VersionCommand
...@@ -36,3 +37,10 @@ def command(): ...@@ -36,3 +37,10 @@ def command():
) )
def test_increment_version(version, rule, expected, command): def test_increment_version(version, rule, expected, command):
assert expected == command.increment_version(version, rule).text assert expected == command.increment_version(version, rule).text
def test_version_show(app):
command = app.find("version")
tester = CommandTester(command)
tester.execute()
assert "Project (simple-project) version is 1.2.3\n" == tester.io.fetch_output()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment