Skip to content

Commit 457fafe

Browse files
committed
refactor(tests): Port latest config and path changes
1 parent 75539d4 commit 457fafe

File tree

1 file changed

+75
-22
lines changed

1 file changed

+75
-22
lines changed

tests/conftest.py

Lines changed: 75 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
import getpass
12
import pathlib
3+
import shutil
4+
import textwrap
25

36
import pytest
47

@@ -7,38 +10,55 @@
710
from libvcs.util import run
811

912

13+
@pytest.fixture(autouse=True, scope="session")
14+
def home_path(tmp_path_factory: pytest.TempPathFactory):
15+
return tmp_path_factory.mktemp("home")
16+
17+
18+
@pytest.fixture(autouse=True, scope="session")
19+
def user_path(home_path: pathlib.Path):
20+
p = home_path / getpass.getuser()
21+
p.mkdir()
22+
return p
23+
24+
1025
@pytest.fixture(scope="function")
11-
def tmpdir_repoparent(tmp_path: pathlib.Path):
26+
def repos_path(user_path: pathlib.Path, request: pytest.FixtureRequest):
1227
"""Return temporary directory for repository checkout guaranteed unique."""
13-
fn = tmp_path
14-
return fn
28+
dir = user_path / "repos"
29+
dir.mkdir(exist_ok=True)
30+
31+
def clean():
32+
shutil.rmtree(dir)
33+
34+
request.addfinalizer(clean)
35+
return dir
1536

1637

1738
@pytest.fixture
18-
def git_repo_kwargs(tmpdir_repoparent: pathlib.Path, git_dummy_repo_dir):
39+
def git_repo_kwargs(repos_path: pathlib.Path, git_dummy_repo_dir):
1940
"""Return kwargs for :func:`create_repo_from_pip_url`."""
20-
repo_name = "repo_clone"
2141
return {
2242
"url": "git+file://" + git_dummy_repo_dir,
23-
"parent_dir": str(tmpdir_repoparent),
24-
"name": repo_name,
43+
"parent_dir": str(repos_path),
44+
"name": "repo_name",
2545
}
2646

2747

2848
@pytest.fixture
2949
def git_repo(git_repo_kwargs) -> GitRepo:
3050
"""Create an git repository for tests. Return repo."""
31-
git_repo = create_repo_from_pip_url(**git_repo_kwargs)
32-
git_repo.obtain(quiet=True)
33-
return git_repo
51+
repo = create_repo_from_pip_url(**git_repo_kwargs)
52+
repo.obtain(quiet=True)
53+
return repo
3454

3555

3656
@pytest.fixture
37-
def create_git_dummy_repo(tmpdir_repoparent: pathlib.Path) -> pathlib.Path:
57+
def create_git_dummy_repo(repos_path: pathlib.Path) -> pathlib.Path:
3858
def fn(repo_name, testfile_filename="testfile.test"):
39-
repo_path = str(tmpdir_repoparent / repo_name)
59+
repo_path = str(repos_path / repo_name)
4060

41-
run(["git", "init", repo_name], cwd=str(tmpdir_repoparent))
61+
run(["git", "init", repo_name], cwd=str(repos_path))
4262

4363
run(["touch", testfile_filename], cwd=repo_path)
4464
run(["git", "add", testfile_filename], cwd=repo_path)
@@ -50,20 +70,53 @@ def fn(repo_name, testfile_filename="testfile.test"):
5070

5171

5272
@pytest.fixture
53-
def git_dummy_repo_dir(tmpdir_repoparent: pathlib.Path, create_git_dummy_repo):
73+
def git_dummy_repo_dir(repos_path: pathlib.Path, create_git_dummy_repo):
5474
"""Create a git repo with 1 commit, used as a remote."""
5575
return create_git_dummy_repo("dummyrepo")
5676

5777

5878
@pytest.fixture(scope="function")
59-
def home_path(tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch) -> pathlib.Path:
60-
monkeypatch.setenv("HOME", str(tmp_path))
61-
tmp_path.mkdir(exist_ok=True)
62-
return tmp_path
79+
def config_path(home_path: pathlib.Path, request: pytest.FixtureRequest):
80+
conf_path = home_path / ".vcspull"
81+
conf_path.mkdir(exist_ok=True)
6382

83+
def clean():
84+
shutil.rmtree(conf_path)
6485

65-
@pytest.fixture(scope="function")
66-
def config_path(home_path: pathlib.Path):
67-
conf_path = home_path / ".vcspull"
68-
conf_path.mkdir()
86+
request.addfinalizer(clean)
6987
return conf_path
88+
89+
90+
@pytest.fixture(autouse=True, scope="session")
91+
def hgrc(user_path: pathlib.Path):
92+
hgrc = user_path / ".hgrc"
93+
hgrc.write_text(
94+
textwrap.dedent(
95+
f"""
96+
[ui]
97+
username = vcspull tests <vcspull@git-pull.com>
98+
merge = internal:merge
99+
100+
[trusted]
101+
users = {getpass.getuser()}
102+
"""
103+
),
104+
encoding="utf-8",
105+
)
106+
return hgrc
107+
108+
109+
@pytest.fixture(autouse=True, scope="module")
110+
def gitconfig(user_path: pathlib.Path):
111+
gitconfig = user_path / ".gitconfig"
112+
gitconfig.write_text(
113+
textwrap.dedent(
114+
f"""
115+
[user]
116+
email = libvcs@git-pull.com
117+
name = {getpass.getuser()}
118+
"""
119+
),
120+
encoding="utf-8",
121+
)
122+
return gitconfig

0 commit comments

Comments
 (0)