Skip to content

Commit 206e1fb

Browse files
committed
refactor(test): Home dir, git config fixture, hgrc default fixes
1 parent 2b5a63e commit 206e1fb

File tree

3 files changed

+36
-32
lines changed

3 files changed

+36
-32
lines changed

tests/conftest.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import getpass
22
import pathlib
3+
import shutil
34
from typing import Dict
45

56
import pytest
@@ -21,10 +22,15 @@ def user_path(home_path: pathlib.Path):
2122

2223

2324
@pytest.fixture(scope="function")
24-
def parentdir(tmp_path: pathlib.Path):
25+
def parentdir(user_path: pathlib.Path, request: pytest.FixtureRequest):
2526
"""Return temporary directory for repository checkout guaranteed unique."""
26-
dir = tmp_path / "repo"
27-
dir.mkdir()
27+
dir = user_path / "repos"
28+
dir.mkdir(exist_ok=True)
29+
30+
def clean():
31+
shutil.rmtree(dir)
32+
33+
request.addfinalizer(clean)
2834
return dir
2935

3036

tests/test_git.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Tests for libvcs git repos."""
22
import datetime
3+
import getpass
34
import os
45
import pathlib
56
import textwrap
@@ -15,6 +16,27 @@
1516
pytestmark = pytest.mark.skip(reason="git is not available")
1617

1718

19+
@pytest.fixture(autouse=True, scope="module")
20+
def gitconfig(user_path: pathlib.Path):
21+
gitconfig = user_path / ".gitconfig"
22+
gitconfig.write_text(
23+
textwrap.dedent(
24+
f"""
25+
[user]
26+
email = libvcs@git-pull.com
27+
name = {getpass.getuser()}
28+
"""
29+
),
30+
encoding="utf-8",
31+
)
32+
return gitconfig
33+
34+
35+
@pytest.fixture(autouse=True)
36+
def gitconfig_default(monkeypatch: pytest.MonkeyPatch, user_path: pathlib.Path):
37+
monkeypatch.setenv("HOME", str(user_path))
38+
39+
1840
def test_repo_git_obtain_initial_commit_repo(tmp_path: pathlib.Path):
1941
"""initial commit repos return 'initial'.
2042

tests/test_hg.py

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
"""Tests for libvcs hg repos."""
22
import getpass
3-
import os
43
import pathlib
54
import textwrap
65

@@ -14,33 +13,11 @@
1413

1514

1615
@pytest.fixture(autouse=True, scope="session")
17-
def home_path(tmp_path_factory: pytest.TempPathFactory):
18-
return tmp_path_factory.mktemp("home")
19-
20-
21-
@pytest.fixture(autouse=True, scope="session")
22-
def user_path(home_path: pathlib.Path):
23-
p = home_path / getpass.getuser()
24-
p.mkdir()
25-
return p
26-
27-
28-
@pytest.fixture(autouse=True, scope="session")
29-
def hg_user_path(user_path: pathlib.Path):
30-
hg_config = user_path / ".hg"
31-
hg_config.mkdir()
32-
return hg_config
33-
34-
35-
@pytest.fixture(autouse=True, scope="session")
36-
def hgrc(hg_user_path: pathlib.Path):
37-
hgrc = hg_user_path / "hgrc"
16+
def hgrc(user_path: pathlib.Path):
17+
hgrc = user_path / ".hgrc"
3818
hgrc.write_text(
3919
textwrap.dedent(
4020
f"""
41-
[paths]
42-
default = {hg_remote}
43-
4421
[ui]
4522
username = libvcs tests <libvcs@git-pull.com>
4623
merge = internal:merge
@@ -55,14 +32,14 @@ def hgrc(hg_user_path: pathlib.Path):
5532

5633

5734
@pytest.fixture(autouse=True)
58-
def hgrc_default(monkeypatch: pytest.MonkeyPatch, hgrc: pathlib.Path):
59-
monkeypatch.chdir(hgrc.parent)
35+
def hgrc_default(monkeypatch: pytest.MonkeyPatch, user_path: pathlib.Path):
36+
monkeypatch.setenv("HOME", str(user_path))
6037

6138

6239
@pytest.fixture
6340
def hg_remote(parentdir):
6441
"""Create a git repo with 1 commit, used as a remote."""
65-
name = "dummyrepo"
42+
name = "test_hg_repo"
6643
repo_path = parentdir / name
6744

6845
run(["hg", "init", name], cwd=parentdir)
@@ -95,4 +72,3 @@ def test_repo_mercurial(tmp_path: pathlib.Path, parentdir, hg_remote):
9572
)
9673

9774
assert mercurial_repo.get_revision() == test_repo_revision
98-
assert os.path.exists(tmp_path / repo_name)

0 commit comments

Comments
 (0)