Skip to content

Commit af15e3f

Browse files
committed
refactor!(pytest plugin[hg]) Session-scoped, cached VCS fixtures
1 parent 51a6c3a commit af15e3f

File tree

1 file changed

+51
-18
lines changed

1 file changed

+51
-18
lines changed

src/libvcs/pytest_plugin.py

+51-18
Original file line numberDiff line numberDiff line change
@@ -552,12 +552,34 @@ def hg_remote_repo_single_commit_post_init(remote_repo_path: pathlib.Path) -> No
552552
run(["hg", "commit", "-m", "test file for hg repo"], cwd=remote_repo_path)
553553

554554

555-
@pytest.fixture
555+
@pytest.fixture(scope="session")
556+
def empty_hg_repo_path(libvcs_test_cache_path: pathlib.Path) -> pathlib.Path:
557+
"""Return temporary directory to use for master-copy of a hg repo."""
558+
return libvcs_test_cache_path / "empty_hg_repo"
559+
560+
561+
@pytest.fixture(scope="session")
562+
@skip_if_hg_missing
563+
def empty_hg_repo(
564+
empty_hg_repo_path: pathlib.Path,
565+
) -> pathlib.Path:
566+
"""Return factory to create hg remote repo to for clone / push purposes."""
567+
if empty_hg_repo_path.exists() and (empty_hg_repo_path / ".hg").exists():
568+
return empty_hg_repo_path
569+
570+
return _create_hg_remote_repo(
571+
remote_repos_path=empty_hg_repo_path.parent,
572+
remote_repo_name=empty_hg_repo_path.stem,
573+
remote_repo_post_init=None,
574+
init_cmd_args=None,
575+
)
576+
577+
578+
@pytest.fixture(scope="session")
556579
@skip_if_hg_missing
557580
def create_hg_remote_repo(
558581
remote_repos_path: pathlib.Path,
559-
hgconfig: pathlib.Path,
560-
set_home: pathlib.Path,
582+
empty_hg_repo: pathlib.Path,
561583
) -> CreateRepoPytestFixtureFn:
562584
"""Pre-made hg repo, bare, used as a file:// remote to checkout and commit to."""
563585

@@ -567,30 +589,39 @@ def fn(
567589
remote_repo_post_init: Optional[CreateRepoPostInitFn] = None,
568590
init_cmd_args: InitCmdArgs = None,
569591
) -> pathlib.Path:
570-
return _create_hg_remote_repo(
571-
remote_repos_path=remote_repos_path,
572-
remote_repo_name=remote_repo_name
573-
if remote_repo_name is not None
574-
else unique_repo_name(remote_repos_path=remote_repos_path),
575-
remote_repo_post_init=remote_repo_post_init,
576-
init_cmd_args=init_cmd_args,
577-
)
592+
if remote_repo_name is None:
593+
remote_repo_name = unique_repo_name(remote_repos_path=remote_repos_path)
594+
remote_repo_path = remote_repos_path / remote_repo_name
595+
596+
shutil.copytree(empty_hg_repo, remote_repo_path)
597+
598+
if remote_repo_post_init is not None and callable(remote_repo_post_init):
599+
remote_repo_post_init(remote_repo_path=remote_repo_path)
600+
601+
assert empty_hg_repo.exists()
602+
603+
assert remote_repo_path.exists()
604+
605+
return remote_repo_path
578606

579607
return fn
580608

581609

582-
@pytest.fixture
610+
@pytest.fixture(scope="session")
583611
@skip_if_hg_missing
584612
def hg_remote_repo(
585613
remote_repos_path: pathlib.Path,
586-
hgconfig: pathlib.Path,
614+
create_hg_remote_repo: CreateRepoPytestFixtureFn,
587615
) -> pathlib.Path:
588616
"""Pre-made, file-based repo for push and pull."""
589-
return _create_hg_remote_repo(
590-
remote_repos_path=remote_repos_path,
591-
remote_repo_name="dummyrepo",
592-
remote_repo_post_init=hg_remote_repo_single_commit_post_init,
593-
)
617+
# return _create_hg_remote_repo(
618+
# remote_repos_path=remote_repos_path,
619+
# remote_repo_name="dummyrepo",
620+
# remote_repo_post_init=hg_remote_repo_single_commit_post_init,
621+
# )
622+
repo_path = create_hg_remote_repo()
623+
hg_remote_repo_single_commit_post_init(remote_repo_path=repo_path)
624+
return repo_path
594625

595626

596627
@pytest.fixture
@@ -640,6 +671,7 @@ def add_doctest_fixtures(
640671
tmp_path: pathlib.Path,
641672
set_home: pathlib.Path,
642673
gitconfig: pathlib.Path,
674+
hgconfig: pathlib.Path,
643675
create_git_remote_repo: CreateRepoPytestFixtureFn,
644676
create_svn_remote_repo: CreateRepoPytestFixtureFn,
645677
create_hg_remote_repo: CreateRepoPytestFixtureFn,
@@ -667,6 +699,7 @@ def add_doctest_fixtures(
667699
remote_repo_post_init=svn_remote_repo_single_commit_post_init,
668700
)
669701
if shutil.which("hg"):
702+
doctest_namespace["hgconfig"] = hgconfig
670703
doctest_namespace["create_hg_remote_repo_bare"] = create_hg_remote_repo
671704
doctest_namespace["create_hg_remote_repo"] = functools.partial(
672705
create_hg_remote_repo,

0 commit comments

Comments
 (0)