Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix restore from dump in tests. #26

Merged
merged 1 commit into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 27 additions & 11 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -35,15 +42,17 @@ 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.")

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")
Expand All @@ -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")
Expand Down Expand Up @@ -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
13 changes: 7 additions & 6 deletions tests/test_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
13 changes: 8 additions & 5 deletions tests/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}/")

Expand Down