Skip to content

Commit 3588b25

Browse files
committed
Add: Add support for pontos-version verify current for JS projects
Extend the JS version command to support validating the current versioning and also check the optional src/version.js file.
1 parent ed61a34 commit 3588b25

File tree

2 files changed

+106
-3
lines changed

2 files changed

+106
-3
lines changed

pontos/version/javascript.py

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import json
1919
import re
2020
from pathlib import Path
21-
from typing import Any, Dict, Literal, Union
21+
from typing import Any, Dict, Literal, Optional, Union
2222

2323
from .errors import VersionError
2424
from .version import Version, VersionCommand, VersionUpdate, parse_version
@@ -60,6 +60,19 @@ def package(self) -> Dict[str, Any]:
6060

6161
return self._package
6262

63+
def _get_current_js_file_version(self) -> Optional[Version]:
64+
if not GREENBONE_JS_VERSION_FILE.exists():
65+
return None
66+
67+
content = GREENBONE_JS_VERSION_FILE.read_text(encoding="utf-8")
68+
match = re.search(r'VERSION = "(?P<version>.*)";', content)
69+
if not match:
70+
raise VersionError(
71+
f"VERSION variable not found in {GREENBONE_JS_VERSION_FILE}"
72+
)
73+
74+
return Version(match.group("version"))
75+
6376
def get_current_version(self) -> Version:
6477
"""Get the current version of this project
6578
In go the version is only defined within the repository
@@ -71,10 +84,30 @@ def verify_version(
7184
) -> None:
7285
"""Verify the current version of this project"""
7386
current_version = self.get_current_version()
87+
88+
if version == "current":
89+
js_version = self._get_current_js_file_version()
90+
if js_version and js_version != current_version:
91+
raise VersionError(
92+
f"The version {js_version} in "
93+
f"{GREENBONE_JS_VERSION_FILE} doesn't match the current "
94+
f"version {current_version}."
95+
)
96+
return
97+
7498
if current_version != version:
7599
raise VersionError(
76100
f"Provided version {version} does not match the "
77-
f"current version {current_version}."
101+
f"current version {current_version} in "
102+
f"{self.project_file_path}."
103+
)
104+
105+
js_version = self._get_current_js_file_version()
106+
if js_version and js_version != version:
107+
raise VersionError(
108+
f"Provided version {version} does not match the "
109+
f"current version {js_version} in "
110+
f"{GREENBONE_JS_VERSION_FILE}."
78111
)
79112

80113
def _update_package_json(self, new_version: Version) -> None:

tests/version/test_javascript_version.py

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@
2222

2323
from pontos.testing import temp_directory, temp_file
2424
from pontos.version.errors import VersionError
25-
from pontos.version.javascript import JavaScriptVersionCommand
25+
from pontos.version.javascript import (
26+
GREENBONE_JS_VERSION_FILE,
27+
JavaScriptVersionCommand,
28+
)
2629
from pontos.version.version import Version
2730

2831

@@ -148,6 +151,73 @@ def test_verify_success(self):
148151
cmd = JavaScriptVersionCommand()
149152
cmd.verify_version(Version("22.4"))
150153

154+
def test_verify_js_mismatch(self):
155+
with patch.object(
156+
JavaScriptVersionCommand,
157+
"get_current_version",
158+
MagicMock(return_value=Version("22.4")),
159+
), patch.object(
160+
JavaScriptVersionCommand,
161+
"_get_current_js_file_version",
162+
MagicMock(return_value=Version("22.5")),
163+
), self.assertRaisesRegex(
164+
VersionError,
165+
"Provided version 22.4 does not match the current version 22.5 in "
166+
"src/version.js.",
167+
):
168+
cmd = JavaScriptVersionCommand()
169+
cmd.verify_version(Version("22.4"))
170+
171+
def test_verify_current(self):
172+
with patch.object(
173+
JavaScriptVersionCommand,
174+
"get_current_version",
175+
MagicMock(return_value=Version("22.4")),
176+
):
177+
cmd = JavaScriptVersionCommand()
178+
cmd.verify_version("current")
179+
180+
def test_verify_current_failure(self):
181+
with temp_directory(change_into=True), self.assertRaisesRegex(
182+
VersionError, "^.*package.json file not found"
183+
):
184+
cmd = JavaScriptVersionCommand()
185+
cmd.verify_version("current")
186+
187+
def test_verify_current_js_version_matches(self):
188+
content = '{"name":"foo", "version":"1.2.3"}'
189+
js_content = 'const VERSION = "1.2.3";'
190+
191+
with temp_directory(change_into=True) as temp_dir:
192+
package_json = temp_dir / "package.json"
193+
package_json.write_text(content, encoding="utf8")
194+
js_version_file = temp_dir / GREENBONE_JS_VERSION_FILE
195+
js_version_file.parent.mkdir()
196+
js_version_file.write_text(js_content, encoding="utf8")
197+
198+
cmd = JavaScriptVersionCommand()
199+
cmd.verify_version("current")
200+
201+
def test_verify_current_js_mismatch(self):
202+
content = '{"name":"foo", "version":"1.2.3"}'
203+
js_content = 'const VERSION = "1.2.4";'
204+
205+
with temp_directory(
206+
change_into=True
207+
) as temp_dir, self.assertRaisesRegex(
208+
VersionError,
209+
"The version 1.2.4 in src/version.js doesn't match the current "
210+
"version 1.2.3.",
211+
):
212+
package_json = temp_dir / "package.json"
213+
package_json.write_text(content, encoding="utf8")
214+
js_version_file = temp_dir / GREENBONE_JS_VERSION_FILE
215+
js_version_file.parent.mkdir()
216+
js_version_file.write_text(js_content, encoding="utf8")
217+
218+
cmd = JavaScriptVersionCommand()
219+
cmd.verify_version("current")
220+
151221

152222
class ProjectFileJavaScriptVersionCommandTestCase(unittest.TestCase):
153223
def test_project_file_not_found(self):

0 commit comments

Comments
 (0)