From 91a39cc06f2d349811d1ac2655f8a2a6f940501d Mon Sep 17 00:00:00 2001 From: Elias Date: Wed, 25 Oct 2023 15:09:33 +0200 Subject: [PATCH] Ensure all content is restored from dumps in tests. --- tests/conftest.py | 38 +++++++++++++++++++++++++++----------- tests/test_install.py | 13 +++++++------ tests/util.py | 13 ++++++++----- 3 files changed, 42 insertions(+), 22 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 4759a10..d5fbab9 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -12,19 +12,26 @@ # See the License for the specific language governing permissions and # limitations under the License. -from gitbark.cli.__main__ import cli +from gitbark.cli.__main__ import cli, _DefaultFormatter +from gitbark.cli.util import CliFail from gitbark.objects import BranchRule, BarkRules from gitbark.core import BARK_RULES_BRANCH -from .util import Environment, get_test_bark_module, disable_bark +from .util import Environment, disable_bark from click.testing import CliRunner -from unittest.mock import patch +import logging import pytest import os +@pytest.fixture(autouse=True, scope="session") +def test_bark_module(): + cwd = os.getcwd() + return os.path.join(cwd, "tests", "test_bark_module") + + @pytest.fixture(scope="session") def _env_clean_state(tmp_path_factory): env_path = tmp_path_factory.mktemp("env") @@ -35,7 +42,9 @@ def _env_clean_state(tmp_path_factory): @pytest.fixture(scope="session") -def _env_initialized_state(_env_clean_state: tuple[Environment, str], tmp_path_factory): +def _env_initialized_state( + _env_clean_state: tuple[Environment, str], tmp_path_factory, test_bark_module +): env, _ = _env_clean_state bootstrap_main = env.repo.commit("Initial commit.") @@ -43,7 +52,7 @@ def _env_initialized_state(_env_clean_state: tuple[Environment, str], tmp_path_f branch_rule = BranchRule( pattern="main", bootstrap=bootstrap_main.hash, ff_only=False ) - bark_rules = BarkRules(branches=[branch_rule], modules=[get_test_bark_module()]) + bark_rules = BarkRules(branches=[branch_rule], modules=[test_bark_module]) env.repo.add_bark_rules(bark_rules) dump_path = tmp_path_factory.mktemp("dump") @@ -59,9 +68,7 @@ def _env_installed_state( cwd = os.getcwd() os.chdir(env.repo.repo_dir) - - with patch("click.confirm", return_value="y"): - bark_cli("install") + bark_cli("install", input="y") os.chdir(cwd) dump_path = tmp_path_factory.mktemp("dump") @@ -119,7 +126,16 @@ def bark_cli(): return _bark_cli -def _bark_cli(*args): - runner = CliRunner() - result = runner.invoke(cli, args) +def _bark_cli(*argv, **kwargs): + handler = logging.StreamHandler() + handler.setLevel(logging.WARNING) + handler.setFormatter(_DefaultFormatter()) + logging.getLogger().addHandler(handler) + + runner = CliRunner(mix_stderr=True) + result = runner.invoke(cli, argv, obj={}, **kwargs) + if result.exit_code != 0: + if isinstance(result.exception, CliFail): + raise SystemExit() + raise result.exception return result diff --git a/tests/test_install.py b/tests/test_install.py index c382e02..8d4e678 100644 --- a/tests/test_install.py +++ b/tests/test_install.py @@ -14,21 +14,22 @@ from .util import Environment -from unittest.mock import patch import os +import pytest class TestInstall: def verify_install(self, env: Environment, bark_cli, passes: bool): cwd = os.getcwd() os.chdir(env.repo.repo_dir) - with patch("click.confirm", return_value="y"): - result = bark_cli("install") - if passes: - assert result.exit_code == 0 + print(env.repo.repo_dir) + + if not passes: + with pytest.raises(SystemExit): + bark_cli("install", input="y") else: - assert result.exit_code != 0 + bark_cli("install", input="y") os.chdir(cwd) def test_install_without_bark_rules(self, env_clean: Environment, bark_cli): diff --git a/tests/util.py b/tests/util.py index 22c8484..6b67845 100644 --- a/tests/util.py +++ b/tests/util.py @@ -30,11 +30,6 @@ import os -def get_test_bark_module(): - cwd = os.getcwd() - return os.path.join(cwd, "tests", "test_bark_module") - - def random_string(length: int = 10): # choose from all lowercase letter letters = string.ascii_lowercase @@ -192,6 +187,14 @@ def __init__(self, path: str) -> None: def restore_from_dump(self, dump: str) -> None: dump_repo_path = os.path.join(dump, "repo") dump_remote_path = os.path.join(dump, "remote") + + # Recreating the folders to ensure all files and folders + # are copied. + shutil.rmtree(self.repo_dir) + shutil.rmtree(self.remote_dir) + os.mkdir(self.repo_dir) + os.mkdir(self.remote_dir) + cmd("cp", "-fr", f"{dump_repo_path}/.", f"{self.repo_dir}/") cmd("cp", "-fr", f"{dump_remote_path}/.", f"{self.remote_dir}/")