Skip to content

Commit 4e41f64

Browse files
authored
refactor(tests): Deprecate local fixtures for libvcs pytest plugin (#398)
2 parents b9c0aa1 + f33e1ae commit 4e41f64

File tree

3 files changed

+35
-114
lines changed

3 files changed

+35
-114
lines changed

CHANGES

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ $ pipx install --suffix=@next 'vcspull' --pip-args '\--pre' --force
1919

2020
- _Add your latest changes from PRs here_
2121

22+
### Internal
23+
24+
- Move test fixtures over to libvcs's pytest plugin (#398)
25+
2226
## vcspull v1.13.0 (2022-09-25)
2327

2428
### What's new

tests/conftest.py

Lines changed: 23 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,39 @@
1-
import getpass
21
import pathlib
32
import shutil
4-
import textwrap
53
import typing as t
64

75
import pytest
86

9-
from libvcs._internal.run import run
10-
from libvcs._internal.shortcuts import create_project
11-
from libvcs.sync.git import GitSync
127

8+
@pytest.fixture(autouse=True)
9+
def add_doctest_fixtures(
10+
request: pytest.FixtureRequest,
11+
doctest_namespace: t.Dict[str, t.Any],
12+
) -> None:
13+
from _pytest.doctest import DoctestItem
1314

14-
@pytest.fixture(autouse=True, scope="session")
15-
def home_path(tmp_path_factory: pytest.TempPathFactory):
16-
return tmp_path_factory.mktemp("home")
15+
if isinstance(request._pyfuncitem, DoctestItem):
16+
request.getfixturevalue("add_doctest_fixtures")
17+
request.getfixturevalue("set_home")
1718

1819

19-
@pytest.fixture(autouse=True, scope="session")
20-
def user_path(home_path: pathlib.Path):
21-
p = home_path / getpass.getuser()
22-
p.mkdir()
23-
return p
20+
@pytest.fixture(autouse=True)
21+
def setup(
22+
request: pytest.FixtureRequest,
23+
gitconfig: pathlib.Path,
24+
set_home: pathlib.Path,
25+
xdg_config_path: pathlib.Path,
26+
) -> None:
27+
pass
28+
29+
30+
@pytest.fixture(autouse=True)
31+
def cwd_default(monkeypatch: pytest.MonkeyPatch, tmp_path: pathlib.Path) -> None:
32+
monkeypatch.chdir(tmp_path)
2433

2534

2635
@pytest.fixture(autouse=True, scope="session")
27-
@pytest.mark.usefixtures("set_user_path")
36+
@pytest.mark.usefixtures("set_home")
2837
def xdg_config_path(user_path: pathlib.Path):
2938
p = user_path / ".config"
3039
p.mkdir()
@@ -43,11 +52,6 @@ def clean():
4352
return conf_path
4453

4554

46-
@pytest.fixture(autouse=True)
47-
def set_user_path(monkeypatch: pytest.MonkeyPatch, user_path: pathlib.Path):
48-
monkeypatch.setenv("HOME", str(user_path))
49-
50-
5155
@pytest.fixture(autouse=True)
5256
def set_xdg_config_path(monkeypatch: pytest.MonkeyPatch, xdg_config_path: pathlib.Path):
5357
monkeypatch.setenv("XDG_CONFIG_HOME", str(xdg_config_path))
@@ -64,90 +68,3 @@ def clean():
6468

6569
request.addfinalizer(clean)
6670
return dir
67-
68-
69-
@pytest.fixture
70-
def git_repo_kwargs(repos_path: pathlib.Path, git_dummy_repo_dir):
71-
"""Return kwargs for :func:`create_project`."""
72-
return {
73-
"url": "git+file://" + git_dummy_repo_dir,
74-
"dir": str(repos_path / "repo_name"),
75-
"name": "repo_name",
76-
}
77-
78-
79-
@pytest.fixture
80-
def git_repo(git_repo_kwargs) -> GitSync:
81-
"""Create an git repository for tests. Return repo."""
82-
repo = create_project(vcs="git", **git_repo_kwargs)
83-
repo.obtain(quiet=True)
84-
return repo
85-
86-
87-
class DummyRepoProtocol(t.Protocol):
88-
"""Callback for repo fixture factory."""
89-
90-
def __call__(self, repo_name: str, testfile_filename: str = ...) -> str:
91-
"""Callback signature for subprocess communication."""
92-
...
93-
94-
95-
@pytest.fixture
96-
def create_git_dummy_repo(
97-
repos_path: pathlib.Path,
98-
) -> t.Generator[DummyRepoProtocol, None, None]:
99-
def fn(repo_name: str, testfile_filename: str = "testfile.test"):
100-
repo_path = str(repos_path / repo_name)
101-
102-
run(["git", "init", repo_name], cwd=str(repos_path))
103-
104-
run(["touch", testfile_filename], cwd=repo_path)
105-
run(["git", "add", testfile_filename], cwd=repo_path)
106-
run(["git", "commit", "-m", "test file for %s" % repo_name], cwd=repo_path)
107-
108-
return repo_path
109-
110-
yield fn
111-
112-
113-
@pytest.fixture
114-
def git_dummy_repo_dir(
115-
repos_path: pathlib.Path, create_git_dummy_repo: DummyRepoProtocol
116-
):
117-
"""Create a git repo with 1 commit, used as a remote."""
118-
return create_git_dummy_repo("dummyrepo")
119-
120-
121-
@pytest.fixture(autouse=True, scope="session")
122-
def hgrc(user_path: pathlib.Path):
123-
hgrc = user_path / ".hgrc"
124-
hgrc.write_text(
125-
textwrap.dedent(
126-
f"""
127-
[ui]
128-
username = vcspull tests <vcspull@git-pull.com>
129-
merge = internal:merge
130-
131-
[trusted]
132-
users = {getpass.getuser()}
133-
"""
134-
),
135-
encoding="utf-8",
136-
)
137-
return hgrc
138-
139-
140-
@pytest.fixture(autouse=True, scope="module")
141-
def gitconfig(user_path: pathlib.Path):
142-
gitconfig = user_path / ".gitconfig"
143-
gitconfig.write_text(
144-
textwrap.dedent(
145-
f"""
146-
[user]
147-
email = libvcs@git-pull.com
148-
name = {getpass.getuser()}
149-
"""
150-
),
151-
encoding="utf-8",
152-
)
153-
return gitconfig

tests/test_sync.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
import kaptan
88

99
from libvcs._internal.shortcuts import create_project
10+
from libvcs.pytest_plugin import CreateProjectCallbackFixtureProtocol
1011
from libvcs.sync.git import GitRemote, GitSync
11-
from tests.conftest import DummyRepoProtocol
1212
from vcspull.cli.sync import update_repo
1313
from vcspull.config import extract_repos, filter_repos, load_configs
1414
from vcspull.types import ConfigDict
@@ -18,15 +18,15 @@
1818

1919
def test_makes_recursive(
2020
tmp_path: pathlib.Path,
21-
git_dummy_repo_dir: pathlib.Path,
21+
git_remote_repo: pathlib.Path,
2222
):
2323
"""Ensure that directories in pull are made recursively."""
2424
conf = kaptan.Kaptan(handler="yaml")
2525
conf.import_config(
2626
textwrap.dedent(
2727
f"""
2828
{tmp_path}/study/myrepo:
29-
my_url: git+file://{git_dummy_repo_dir}
29+
my_url: git+file://{git_remote_repo}
3030
"""
3131
)
3232
)
@@ -88,14 +88,14 @@ def write_config_remote(
8888
)
8989
def test_config_variations(
9090
tmp_path: pathlib.Path,
91-
create_git_dummy_repo: DummyRepoProtocol,
91+
create_git_remote_repo: CreateProjectCallbackFixtureProtocol,
9292
config_tpl: str,
9393
capsys: pytest.CaptureFixture[str],
9494
remote_list: t.List[str],
9595
) -> None:
9696
"""Test config output with variation of config formats"""
9797
dummy_repo_name = "dummy_repo"
98-
dummy_repo = create_git_dummy_repo(dummy_repo_name)
98+
dummy_repo = create_git_remote_repo(remote_repo_name=dummy_repo_name)
9999

100100
config_file = write_config_remote(
101101
config_path=tmp_path / "myrepos.yaml",
@@ -157,17 +157,17 @@ def test_config_variations(
157157
)
158158
def test_updating_remote(
159159
tmp_path: pathlib.Path,
160-
create_git_dummy_repo: DummyRepoProtocol,
160+
create_git_remote_repo: CreateProjectCallbackFixtureProtocol,
161161
config_tpl: str,
162162
has_extra_remotes: bool,
163163
) -> None:
164164
"""Ensure additions/changes to yaml config are reflected"""
165165

166166
dummy_repo_name = "dummy_repo"
167-
dummy_repo = create_git_dummy_repo(dummy_repo_name)
167+
dummy_repo = create_git_remote_repo(remote_repo_name=dummy_repo_name)
168168

169169
mirror_name = "mirror_repo"
170-
mirror_repo = create_git_dummy_repo(mirror_name)
170+
mirror_repo = create_git_remote_repo(remote_repo_name=mirror_name)
171171

172172
repo_parent = tmp_path / "study" / "myrepo"
173173
repo_parent.mkdir(parents=True)

0 commit comments

Comments
 (0)