1
- import getpass
2
1
import pathlib
3
2
import shutil
4
- import textwrap
5
3
import typing as t
6
4
7
5
import pytest
8
6
9
- from libvcs ._internal .run import run
10
- from libvcs ._internal .shortcuts import create_project
11
- from libvcs .sync .git import GitSync
12
7
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
13
14
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 " )
17
18
18
19
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 )
24
33
25
34
26
35
@pytest .fixture (autouse = True , scope = "session" )
27
- @pytest .mark .usefixtures ("set_user_path " )
36
+ @pytest .mark .usefixtures ("set_home " )
28
37
def xdg_config_path (user_path : pathlib .Path ):
29
38
p = user_path / ".config"
30
39
p .mkdir ()
@@ -43,11 +52,6 @@ def clean():
43
52
return conf_path
44
53
45
54
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
-
51
55
@pytest .fixture (autouse = True )
52
56
def set_xdg_config_path (monkeypatch : pytest .MonkeyPatch , xdg_config_path : pathlib .Path ):
53
57
monkeypatch .setenv ("XDG_CONFIG_HOME" , str (xdg_config_path ))
@@ -64,90 +68,3 @@ def clean():
64
68
65
69
request .addfinalizer (clean )
66
70
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
0 commit comments