From 9263399d325e4c0462b4d2b9e7813b27177ebe79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Thu, 1 Sep 2022 17:12:50 +0200 Subject: [PATCH] Change: Improve helper module tests Use new testing functions for helper module tests. This ensures that the temporary things are always cleaned up. --- tests/release/test_helper.py | 144 +++++++++++++++++------------------ 1 file changed, 68 insertions(+), 76 deletions(-) diff --git a/tests/release/test_helper.py b/tests/release/test_helper.py index 3d673b639..8e723c36b 100644 --- a/tests/release/test_helper.py +++ b/tests/release/test_helper.py @@ -16,17 +16,16 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . +import contextlib import datetime import os -import shutil import subprocess -import sys -import tempfile import unittest from pathlib import Path +from typing import Generator from unittest.mock import MagicMock, patch -from pontos.git.git import Git +from pontos.git.git import Git, exec_git from pontos.release.helper import ( calculate_calendar_version, find_signing_key, @@ -36,7 +35,15 @@ get_project_name, update_version, ) -from pontos.testing import temp_git_repository +from pontos.testing import temp_directory, temp_git_repository + + +@contextlib.contextmanager +def init_test_git_repo() -> Generator[Path, None, None]: + with temp_git_repository() as repo_path: + exec_git("remote", "add", "foo", "https://foo.bar/bla.git") + exec_git("remote", "add", "origin", "https://foo.bar/testrepo.git") + yield repo_path class TestHelperFunctions(unittest.TestCase): @@ -50,31 +57,16 @@ def setUp(self): stderr=subprocess.PIPE, cwd=cwd, ) - self.tmpdir = Path(tempfile.gettempdir()) / "testrepo" - self.tmpdir.mkdir(parents=True, exist_ok=True) - self.shell_cmd_runner(f"git -C {self.tmpdir} init") - self.shell_cmd_runner( - f"git -C {self.tmpdir} remote add foo https://foo.bar/bla.git" - ) - self.shell_cmd_runner( - f"git -C {self.tmpdir} remote add " - "origin https://foo.bar/testrepo.git" - ) - - def tearDown(self) -> None: - shutil.rmtree(self.tmpdir) def test_get_project_name(self): - proj_path = Path.cwd() - os.chdir(self.tmpdir) - project = get_project_name( - shell_cmd_runner=self.shell_cmd_runner, remote="foo" - ) - self.assertEqual(project, "bla") + with init_test_git_repo(): + project = get_project_name( + shell_cmd_runner=self.shell_cmd_runner, remote="foo" + ) + self.assertEqual(project, "bla") - project = get_project_name(shell_cmd_runner=self.shell_cmd_runner) - self.assertEqual(project, "testrepo") - os.chdir(proj_path) + project = get_project_name(shell_cmd_runner=self.shell_cmd_runner) + self.assertEqual(project, "testrepo") def test_find_signing_key(self): terminal = MagicMock() @@ -130,61 +122,61 @@ def test_find_no_signing_key(self): def test_update_version_not_found(self): terminal = MagicMock() - proj_path = Path.cwd() - os.chdir(self.tmpdir) - executed, filename = update_version(terminal, to="21.4.4", develop=True) + + with temp_directory(change_into=True): + executed, filename = update_version( + terminal, to="21.4.4", develop=True + ) + self.assertFalse(executed) self.assertEqual(filename, "") - os.chdir(proj_path) - def test_update_version_no_version_file(self): terminal = MagicMock() - proj_path = Path.cwd() - sys.path.append(self.tmpdir) - os.chdir(self.tmpdir) - - module_path = self.tmpdir / "testrepo" - module_path.mkdir(parents=True, exist_ok=True) - init_file = module_path / "__init__.py" - init_file.touch() - version_file = module_path / "__version__.py" - toml = self.tmpdir / "pyproject.toml" - toml.write_text( - "[tool.poetry]\n" - 'name = "testrepo"\n' - 'version = "21.6.2.dev1"\n\n' - "[tool.pontos.version]\n" - 'version-module-file = "testrepo/__version__.py"\n', - encoding="utf-8", - ) - executed, filename = update_version( - terminal, to="21.4.4", develop=False - ) - toml_text = toml.read_text(encoding="utf-8") - self.assertEqual(filename, "pyproject.toml") - self.assertTrue(executed) - self.assertEqual( - toml_text, - "[tool.poetry]\n" - 'name = "testrepo"\n' - 'version = "21.4.4"\n\n' - "[tool.pontos.version]\n" - 'version-module-file = "testrepo/__version__.py"\n', - ) - self.assertTrue(version_file.exists()) - version_text = version_file.read_text(encoding="utf-8") - self.assertEqual( - version_text, - "# pylint: disable=invalid-name\n\n" - "# THIS IS AN AUTOGENERATED FILE. DO NOT TOUCH!\n\n" - '__version__ = "21.4.4"\n', - ) - toml.unlink() - shutil.rmtree(module_path) - sys.path.remove(self.tmpdir) - os.chdir(proj_path) + with temp_directory(change_into=True, add_to_sys_path=True) as tmp_dir: + module_path = tmp_dir / "testrepo" + module_path.mkdir(parents=True, exist_ok=True) + + init_file = module_path / "__init__.py" + init_file.touch() + + version_file = module_path / "__version__.py" + + toml = tmp_dir / "pyproject.toml" + toml.write_text( + "[tool.poetry]\n" + 'name = "testrepo"\n' + 'version = "21.6.2.dev1"\n\n' + "[tool.pontos.version]\n" + 'version-module-file = "testrepo/__version__.py"\n', + encoding="utf-8", + ) + + executed, filename = update_version( + terminal, to="21.4.4", develop=False + ) + + toml_text = toml.read_text(encoding="utf-8") + + self.assertEqual(filename, "pyproject.toml") + self.assertTrue(executed) + self.assertEqual( + toml_text, + "[tool.poetry]\n" + 'name = "testrepo"\n' + 'version = "21.4.4"\n\n' + "[tool.pontos.version]\n" + 'version-module-file = "testrepo/__version__.py"\n', + ) + self.assertTrue(version_file.exists()) + version_text = version_file.read_text(encoding="utf-8") + self.assertEqual( + version_text, + "# pylint: disable=invalid-name\n\n" + "# THIS IS AN AUTOGENERATED FILE. DO NOT TOUCH!\n\n" + '__version__ = "21.4.4"\n', + ) @patch("pontos.release.helper.Git", spec=Git) def test_get_last_release_version_git(self, _git_interface_mock):