Skip to content

Commit 0b3f5a7

Browse files
bjoernricksy0urself
authored andcommitted
Change: Refactor Project class
* Drop static gather_project method in favor of using the constructor directly * Require a VersioningScheme to be passed to the constructor
1 parent 0a8de36 commit 0b3f5a7

File tree

2 files changed

+79
-29
lines changed

2 files changed

+79
-29
lines changed

pontos/version/project.py

Lines changed: 62 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,38 +18,73 @@
1818

1919
from typing import List, Literal, Union
2020

21-
from pontos.version.commands import get_commands
22-
from pontos.version.errors import ProjectError
23-
from pontos.version.version import Version, VersionCommand, VersionUpdate
21+
from .commands import VersionCommand, get_commands
22+
from .errors import ProjectError
23+
from .schemes import VersioningScheme
24+
from .version import Version, VersionUpdate
25+
26+
__all__ = ("Project",)
2427

2528

2629
class Project:
27-
def __init__(self, commands: List[VersionCommand]) -> None:
28-
self._commands = commands
30+
"""
31+
A project for handling versioning
32+
33+
Example:
34+
.. code-block:: python
35+
36+
from pontos.version.scheme import PEP440VersioningScheme
37+
from pontos.version.project import Project
38+
39+
project = Project(PEP440VersioningScheme)
40+
"""
41+
42+
def __init__(self, versioning_scheme: VersioningScheme) -> None:
43+
"""
44+
Creates a new project instance
45+
46+
Args:
47+
versioning_scheme: Scheme for version handling
48+
49+
Raises:
50+
ProjectError: If no fitting VersionCommand could be found
51+
"""
52+
self._versioning_scheme = versioning_scheme
53+
self._commands = self._gather_commands()
2954

30-
@classmethod
31-
def gather_project(cls) -> "Project":
55+
def _gather_commands(self) -> List[VersionCommand]:
3256
"""
33-
Get the project with the fitting VersionCommands of the current working
34-
directory
57+
Initialize the project with the fitting VersionCommands of the current
58+
working directory
3559
3660
Raises:
37-
ProjectError if no fitting VersionCommand could be found
61+
ProjectError: If no fitting VersionCommand could be found
3862
"""
3963
commands = []
4064
for cmd in get_commands():
41-
command = cmd()
65+
command = cmd(versioning_scheme=self._versioning_scheme)
4266
if command.project_found():
4367
commands.append(command)
4468

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

48-
return cls(commands)
72+
return commands
4973

5074
def update_version(
5175
self, new_version: Version, *, force: bool = False
5276
) -> VersionUpdate:
77+
"""
78+
Update the current version of this project
79+
80+
Args:
81+
new_version: Use this version in the update
82+
force: Force updating the version even if the current version is the
83+
same as the new version
84+
85+
Returns:
86+
The version update including the changed files
87+
"""
5388
update = self._commands[0].update_version(new_version, force=force)
5489
for cmd in self._commands[1:]:
5590
next_update = cmd.update_version(new_version, force=force)
@@ -58,10 +93,25 @@ def update_version(
5893
return update
5994

6095
def get_current_version(self) -> Version:
96+
"""
97+
Get the current version of the project
98+
99+
Returns:
100+
The current version
101+
"""
61102
return self._commands[0].get_current_version()
62103

63104
def verify_version(
64105
self, version: Union[Literal["current"], Version]
65106
) -> None:
107+
"""
108+
Verify the current version of this project
109+
110+
Args:
111+
version: Version to check against the current applied version of
112+
this project. If version is "current" the command should verify
113+
if all version information is consistent, for example if the
114+
version information in several files is the same.
115+
"""
66116
for cmd in self._commands:
67117
cmd.verify_version(version)

tests/version/test_project.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,19 @@
2020
from pontos.testing import temp_directory, temp_python_module
2121
from pontos.version.errors import ProjectError
2222
from pontos.version.project import Project
23-
from pontos.version.version import Version
23+
from pontos.version.schemes import PEP440VersioningScheme
2424

2525

2626
class ProjectTestCase(unittest.TestCase):
2727
def test_no_project_found(self):
2828
with temp_directory(change_into=True), self.assertRaisesRegex(
2929
ProjectError, "No project settings file found"
3030
):
31-
Project.gather_project()
31+
Project(PEP440VersioningScheme)
3232

3333
def test_python_project(self):
34-
current_version = Version("1.2.3")
35-
new_version = Version("1.2.4")
34+
current_version = PEP440VersioningScheme.parse_version("1.2.3")
35+
new_version = PEP440VersioningScheme.parse_version("1.2.4")
3636

3737
content = f"__version__ = '{current_version}'"
3838
with temp_python_module(
@@ -45,7 +45,7 @@ def test_python_project(self):
4545
encoding="utf8",
4646
)
4747

48-
project = Project.gather_project()
48+
project = Project(PEP440VersioningScheme)
4949
self.assertEqual(project.get_current_version(), current_version)
5050

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

5858
def test_go_project(self):
59-
current_version = Version("1.2.3")
60-
new_version = Version("1.2.4")
59+
current_version = PEP440VersioningScheme.parse_version("1.2.3")
60+
new_version = PEP440VersioningScheme.parse_version("1.2.4")
6161

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

68-
project = Project.gather_project()
68+
project = Project(PEP440VersioningScheme)
6969
self.assertEqual(project.get_current_version(), current_version)
7070

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

7878
def test_javascript_project(self):
79-
current_version = Version("1.2.3")
80-
new_version = Version("1.2.4")
79+
current_version = PEP440VersioningScheme.parse_version("1.2.3")
80+
new_version = PEP440VersioningScheme.parse_version("1.2.4")
8181

8282
with temp_directory(change_into=True) as temp_dir:
8383
version_file = temp_dir / "package.json"
@@ -86,7 +86,7 @@ def test_javascript_project(self):
8686
encoding="utf8",
8787
)
8888

89-
project = Project.gather_project()
89+
project = Project(PEP440VersioningScheme)
9090
self.assertEqual(project.get_current_version(), current_version)
9191

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

9999
def test_cmake_project_version(self):
100-
current_version = Version("1.2.3")
101-
new_version = Version("1.2.4")
100+
current_version = PEP440VersioningScheme.parse_version("1.2.3")
101+
new_version = PEP440VersioningScheme.parse_version("1.2.4")
102102

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

107-
project = Project.gather_project()
107+
project = Project(PEP440VersioningScheme)
108108
self.assertEqual(project.get_current_version(), current_version)
109109

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

117117
def test_all(self):
118-
current_version = Version("1.2.3")
119-
new_version = Version("1.2.4")
118+
current_version = PEP440VersioningScheme.parse_version("1.2.3")
119+
new_version = PEP440VersioningScheme.parse_version("1.2.4")
120120

121121
content = f"__version__ = '{current_version}'"
122122
with temp_python_module(
@@ -146,7 +146,7 @@ def test_all(self):
146146
"project(VERSION 1.2.3)", encoding="utf8"
147147
)
148148

149-
project = Project.gather_project()
149+
project = Project(PEP440VersioningScheme)
150150
self.assertEqual(project.get_current_version(), current_version)
151151

152152
update = project.update_version(new_version)

0 commit comments

Comments
 (0)