Skip to content

Commit

Permalink
Change: Improve helper module tests
Browse files Browse the repository at this point in the history
Use new testing functions for helper module tests. This ensures that the
temporary things are always cleaned up.
  • Loading branch information
bjoernricks committed Sep 1, 2022
1 parent a7c9b25 commit 9263399
Showing 1 changed file with 68 additions and 76 deletions.
144 changes: 68 additions & 76 deletions tests/release/test_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,16 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

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,
Expand All @@ -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):
Expand All @@ -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()
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 9263399

Please sign in to comment.