diff --git a/pontos/version/javascript.py b/pontos/version/javascript.py index ba3b3fddf..99b77133d 100644 --- a/pontos/version/javascript.py +++ b/pontos/version/javascript.py @@ -27,7 +27,7 @@ # This class is used for JavaScript Version command(s) class JavaScriptVersionCommand(VersionCommand): project_file_name = "package.json" - version_file_path = Path("src", "version.js") + version_file_paths = (Path("src", "version.js"), Path("src", "version.ts")) _package = None @property @@ -59,16 +59,16 @@ def package(self) -> Dict[str, Any]: return self._package - def _get_current_js_file_version(self) -> Optional[Version]: - if not self.version_file_path.exists(): + def _get_current_file_version( + self, version_file: Path + ) -> Optional[Version]: + if not version_file.exists(): return None - content = self.version_file_path.read_text(encoding="utf-8") + content = version_file.read_text(encoding="utf-8") match = re.search(r'VERSION = "(?P.*)";', content) if not match: - raise VersionError( - f"VERSION variable not found in {self.version_file_path}" - ) + raise VersionError(f"VERSION variable not found in {version_file}") return Version(match.group("version")) @@ -85,13 +85,14 @@ def verify_version( current_version = self.get_current_version() if version == "current": - js_version = self._get_current_js_file_version() - if js_version and js_version != current_version: - raise VersionError( - f"The version {js_version} in " - f"{self.version_file_path} doesn't match the current " - f"version {current_version}." - ) + for version_file in self.version_file_paths: + file_version = self._get_current_file_version(version_file) + if file_version and file_version != current_version: + raise VersionError( + f"The version {file_version} in " + f"{version_file} doesn't match the current " + f"version {current_version}." + ) return if current_version != version: @@ -101,13 +102,13 @@ def verify_version( f"{self.project_file_path}." ) - js_version = self._get_current_js_file_version() - if js_version and js_version != version: - raise VersionError( - f"Provided version {version} does not match the " - f"current version {js_version} in " - f"{self.version_file_path}." - ) + for version_file in self.version_file_paths: + file_version = self._get_current_file_version(version_file) + if file_version and file_version != version: + raise VersionError( + f"Provided version {version} does not match the " + f"current version {file_version} in {version_file}." + ) def _update_package_json(self, new_version: Version) -> None: """ @@ -127,20 +128,22 @@ def _update_package_json(self, new_version: Version) -> None: except json.JSONDecodeError as e: raise VersionError("Couldn't load JSON") from e - def _update_version_file(self, new_version: Version) -> bool: + def _update_version_file( + self, version_file: Path, new_version: Version + ) -> bool: """ Update the version file with the new version """ - if not self.version_file_path.exists(): + if not version_file.exists(): return False - content = self.version_file_path.read_text(encoding="utf-8") + content = version_file.read_text(encoding="utf-8") content = re.sub( pattern=r'VERSION = "(?P.*)";', repl=f'VERSION = "{new_version}";', string=content, ) - self.version_file_path.write_text(content, encoding="utf-8") + version_file.write_text(content, encoding="utf-8") return True def update_version( @@ -153,9 +156,12 @@ def update_version( changed_files = [self.project_file_path] self._update_package_json(new_version=new_version) - updated = self._update_version_file(new_version=new_version) - if updated: - changed_files.append(self.version_file_path) + for version_file in self.version_file_paths: + updated = self._update_version_file( + version_file, new_version=new_version + ) + if updated: + changed_files.append(version_file) return VersionUpdate( previous=package_version, diff --git a/tests/version/test_javascript_version.py b/tests/version/test_javascript_version.py index da9b8fd8d..7f23813ae 100644 --- a/tests/version/test_javascript_version.py +++ b/tests/version/test_javascript_version.py @@ -88,7 +88,7 @@ def test_update_js_version_file(self): package_json = temp_dir / "package.json" package_json.write_text(content, encoding="utf8") js_version_file = ( - temp_dir / JavaScriptVersionCommand.version_file_path + temp_dir / JavaScriptVersionCommand.version_file_paths[0] ) js_version_file.parent.mkdir() js_version_file.write_text(js_content, encoding="utf8") @@ -100,7 +100,54 @@ def test_update_js_version_file(self): self.assertEqual(updated.new, Version("22.4.0")) self.assertEqual( updated.changed_files, - [package_json, JavaScriptVersionCommand.version_file_path], + [package_json, JavaScriptVersionCommand.version_file_paths[0]], + ) + + with package_json.open(mode="r", encoding="utf-8") as fp: + fake_package = json.load(fp) + + self.assertEqual(fake_package["version"], "22.4.0") + + self.assertEqual( + js_version_file.read_text(encoding="utf8"), + 'const foo = "bar";\nconst VERSION = "22.4.0";\n' + "const func = () => ();\n", + ) + + def test_update_version_files(self): + content = '{"name":"foo", "version":"1.2.3"}' + file_content = """const foo = "bar"; +const VERSION = "1.2.3"; +const func = () => (); +""" + + with temp_directory(change_into=True) as temp_dir: + package_json = temp_dir / "package.json" + package_json.write_text(content, encoding="utf8") + + js_version_file = ( + temp_dir / JavaScriptVersionCommand.version_file_paths[0] + ) + js_version_file.parent.mkdir() + js_version_file.write_text(file_content, encoding="utf8") + + ts_version_file = ( + temp_dir / JavaScriptVersionCommand.version_file_paths[1] + ) + ts_version_file.write_text(file_content, encoding="utf8") + + cmd = JavaScriptVersionCommand() + updated = cmd.update_version(Version("22.4.0")) + + self.assertEqual(updated.previous, Version("1.2.3")) + self.assertEqual(updated.new, Version("22.4.0")) + self.assertEqual( + updated.changed_files, + [ + package_json, + JavaScriptVersionCommand.version_file_paths[0], + JavaScriptVersionCommand.version_file_paths[1], + ], ) with package_json.open(mode="r", encoding="utf-8") as fp: @@ -192,7 +239,7 @@ def test_verify_js_mismatch(self): MagicMock(return_value=Version("22.4")), ), patch.object( JavaScriptVersionCommand, - "_get_current_js_file_version", + "_get_current_file_version", MagicMock(return_value=Version("22.5")), ), self.assertRaisesRegex( VersionError, @@ -202,6 +249,23 @@ def test_verify_js_mismatch(self): cmd = JavaScriptVersionCommand() cmd.verify_version(Version("22.4")) + def test_verify_ts_mismatch(self): + with patch.object( + JavaScriptVersionCommand, + "get_current_version", + MagicMock(return_value=Version("22.4")), + ), patch.object( + JavaScriptVersionCommand, + "_get_current_file_version", + MagicMock(side_effect=[Version("22.4"), Version("22.5")]), + ), self.assertRaisesRegex( + VersionError, + "Provided version 22.4 does not match the current version 22.5 in " + "src/version.ts.", + ): + cmd = JavaScriptVersionCommand() + cmd.verify_version(Version("22.4")) + def test_verify_current(self): with patch.object( JavaScriptVersionCommand, @@ -226,7 +290,7 @@ def test_verify_current_js_version_matches(self): package_json = temp_dir / "package.json" package_json.write_text(content, encoding="utf8") js_version_file = ( - temp_dir / JavaScriptVersionCommand.version_file_path + temp_dir / JavaScriptVersionCommand.version_file_paths[0] ) js_version_file.parent.mkdir() js_version_file.write_text(js_content, encoding="utf8") @@ -248,7 +312,7 @@ def test_verify_current_js_mismatch(self): package_json = temp_dir / "package.json" package_json.write_text(content, encoding="utf8") js_version_file = ( - temp_dir / JavaScriptVersionCommand.version_file_path + temp_dir / JavaScriptVersionCommand.version_file_paths[0] ) js_version_file.parent.mkdir() js_version_file.write_text(js_content, encoding="utf8")