Skip to content

Commit

Permalink
Change: Refactor Project class
Browse files Browse the repository at this point in the history
* Drop static gather_project method in favor of using the constructor
  directly
* Require a VersioningScheme to be passed to the constructor
  • Loading branch information
bjoernricks authored and y0urself committed Mar 14, 2023
1 parent 0a8de36 commit 0b3f5a7
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 29 deletions.
74 changes: 62 additions & 12 deletions pontos/version/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,38 +18,73 @@

from typing import List, Literal, Union

from pontos.version.commands import get_commands
from pontos.version.errors import ProjectError
from pontos.version.version import Version, VersionCommand, VersionUpdate
from .commands import VersionCommand, get_commands
from .errors import ProjectError
from .schemes import VersioningScheme
from .version import Version, VersionUpdate

__all__ = ("Project",)


class Project:
def __init__(self, commands: List[VersionCommand]) -> None:
self._commands = commands
"""
A project for handling versioning
Example:
.. code-block:: python
from pontos.version.scheme import PEP440VersioningScheme
from pontos.version.project import Project
project = Project(PEP440VersioningScheme)
"""

def __init__(self, versioning_scheme: VersioningScheme) -> None:
"""
Creates a new project instance
Args:
versioning_scheme: Scheme for version handling
Raises:
ProjectError: If no fitting VersionCommand could be found
"""
self._versioning_scheme = versioning_scheme
self._commands = self._gather_commands()

@classmethod
def gather_project(cls) -> "Project":
def _gather_commands(self) -> List[VersionCommand]:
"""
Get the project with the fitting VersionCommands of the current working
directory
Initialize the project with the fitting VersionCommands of the current
working directory
Raises:
ProjectError if no fitting VersionCommand could be found
ProjectError: If no fitting VersionCommand could be found
"""
commands = []
for cmd in get_commands():
command = cmd()
command = cmd(versioning_scheme=self._versioning_scheme)
if command.project_found():
commands.append(command)

if not commands:
raise ProjectError("No project settings file found")

return cls(commands)
return commands

def update_version(
self, new_version: Version, *, force: bool = False
) -> VersionUpdate:
"""
Update the current version of this project
Args:
new_version: Use this version in the update
force: Force updating the version even if the current version is the
same as the new version
Returns:
The version update including the changed files
"""
update = self._commands[0].update_version(new_version, force=force)
for cmd in self._commands[1:]:
next_update = cmd.update_version(new_version, force=force)
Expand All @@ -58,10 +93,25 @@ def update_version(
return update

def get_current_version(self) -> Version:
"""
Get the current version of the project
Returns:
The current version
"""
return self._commands[0].get_current_version()

def verify_version(
self, version: Union[Literal["current"], Version]
) -> None:
"""
Verify the current version of this project
Args:
version: Version to check against the current applied version of
this project. If version is "current" the command should verify
if all version information is consistent, for example if the
version information in several files is the same.
"""
for cmd in self._commands:
cmd.verify_version(version)
34 changes: 17 additions & 17 deletions tests/version/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@
from pontos.testing import temp_directory, temp_python_module
from pontos.version.errors import ProjectError
from pontos.version.project import Project
from pontos.version.version import Version
from pontos.version.schemes import PEP440VersioningScheme


class ProjectTestCase(unittest.TestCase):
def test_no_project_found(self):
with temp_directory(change_into=True), self.assertRaisesRegex(
ProjectError, "No project settings file found"
):
Project.gather_project()
Project(PEP440VersioningScheme)

def test_python_project(self):
current_version = Version("1.2.3")
new_version = Version("1.2.4")
current_version = PEP440VersioningScheme.parse_version("1.2.3")
new_version = PEP440VersioningScheme.parse_version("1.2.4")

content = f"__version__ = '{current_version}'"
with temp_python_module(
Expand All @@ -45,7 +45,7 @@ def test_python_project(self):
encoding="utf8",
)

project = Project.gather_project()
project = Project(PEP440VersioningScheme)
self.assertEqual(project.get_current_version(), current_version)

update = project.update_version(new_version)
Expand All @@ -56,16 +56,16 @@ def test_python_project(self):
self.assertEqual(len(update.changed_files), 2)

def test_go_project(self):
current_version = Version("1.2.3")
new_version = Version("1.2.4")
current_version = PEP440VersioningScheme.parse_version("1.2.3")
new_version = PEP440VersioningScheme.parse_version("1.2.4")

with temp_directory(change_into=True) as temp_dir:
project_file = temp_dir / "go.mod"
project_file.touch()
version_file = temp_dir / "version.go"
version_file.write_text(f'var version = "{current_version}"')

project = Project.gather_project()
project = Project(PEP440VersioningScheme)
self.assertEqual(project.get_current_version(), current_version)

update = project.update_version(new_version)
Expand All @@ -76,8 +76,8 @@ def test_go_project(self):
self.assertEqual(len(update.changed_files), 1)

def test_javascript_project(self):
current_version = Version("1.2.3")
new_version = Version("1.2.4")
current_version = PEP440VersioningScheme.parse_version("1.2.3")
new_version = PEP440VersioningScheme.parse_version("1.2.4")

with temp_directory(change_into=True) as temp_dir:
version_file = temp_dir / "package.json"
Expand All @@ -86,7 +86,7 @@ def test_javascript_project(self):
encoding="utf8",
)

project = Project.gather_project()
project = Project(PEP440VersioningScheme)
self.assertEqual(project.get_current_version(), current_version)

update = project.update_version(new_version)
Expand All @@ -97,14 +97,14 @@ def test_javascript_project(self):
self.assertEqual(len(update.changed_files), 1)

def test_cmake_project_version(self):
current_version = Version("1.2.3")
new_version = Version("1.2.4")
current_version = PEP440VersioningScheme.parse_version("1.2.3")
new_version = PEP440VersioningScheme.parse_version("1.2.4")

with temp_directory(change_into=True) as temp_dir:
version_file = temp_dir / "CMakeLists.txt"
version_file.write_text("project(VERSION 1.2.3)", encoding="utf8")

project = Project.gather_project()
project = Project(PEP440VersioningScheme)
self.assertEqual(project.get_current_version(), current_version)

update = project.update_version(new_version)
Expand All @@ -115,8 +115,8 @@ def test_cmake_project_version(self):
self.assertEqual(len(update.changed_files), 1)

def test_all(self):
current_version = Version("1.2.3")
new_version = Version("1.2.4")
current_version = PEP440VersioningScheme.parse_version("1.2.3")
new_version = PEP440VersioningScheme.parse_version("1.2.4")

content = f"__version__ = '{current_version}'"
with temp_python_module(
Expand Down Expand Up @@ -146,7 +146,7 @@ def test_all(self):
"project(VERSION 1.2.3)", encoding="utf8"
)

project = Project.gather_project()
project = Project(PEP440VersioningScheme)
self.assertEqual(project.get_current_version(), current_version)

update = project.update_version(new_version)
Expand Down

0 comments on commit 0b3f5a7

Please sign in to comment.