22
22
from .wd_wrapper import WorkDir
23
23
24
24
25
- def pytest_configure () -> None :
25
+ def pytest_configure (config : pytest . Config ) -> None :
26
26
# 2009-02-13T23:31:30+00:00
27
27
os .environ ["SOURCE_DATE_EPOCH" ] = "1234567890"
28
28
os .environ ["SETUPTOOLS_SCM_DEBUG" ] = "1"
29
29
30
+ # Register custom markers
31
+ config .addinivalue_line (
32
+ "markers" ,
33
+ "git: mark test to use git SCM" ,
34
+ )
35
+ config .addinivalue_line (
36
+ "markers" ,
37
+ "hg: mark test to use mercurial SCM" ,
38
+ )
39
+
30
40
31
41
VERSION_PKGS = ["setuptools" , "setuptools_scm" , "packaging" , "build" , "wheel" ]
32
42
@@ -42,10 +52,10 @@ def pytest_report_header() -> list[str]:
42
52
# Replace everything up to and including site-packages with site::
43
53
parts = path .split ("site-packages" , 1 )
44
54
if len (parts ) > 1 :
45
- path = "site:. " + parts [1 ]
55
+ path = "site:: " + parts [1 ]
46
56
elif path and str (Path .cwd ()) in path :
47
57
# Replace current working directory with CWD::
48
- path = path .replace (str (Path .cwd ()), "CWD:. " )
58
+ path = path .replace (str (Path .cwd ()), "CWD:: " )
49
59
res .append (f"{ pkg } version { pkg_version } from { path } " )
50
60
return res
51
61
@@ -88,11 +98,49 @@ def debug_mode() -> Iterator[DebugMode]:
88
98
yield debug_mode
89
99
90
100
101
+ def setup_git_wd (wd : WorkDir , monkeypatch : pytest .MonkeyPatch | None = None ) -> WorkDir :
102
+ """Set up a WorkDir with git initialized and configured for testing."""
103
+ if monkeypatch :
104
+ monkeypatch .delenv ("HOME" , raising = False )
105
+ wd ("git init" )
106
+ wd (
"git config user.email [email protected] " )
107
+ wd ('git config user.name "a test"' )
108
+ wd .add_command = "git add ."
109
+ wd .commit_command = "git commit -m test-{reason}"
110
+ wd .tag_command = "git tag {tag}"
111
+ return wd
112
+
113
+
114
+ def setup_hg_wd (wd : WorkDir ) -> WorkDir :
115
+ """Set up a WorkDir with mercurial initialized and configured for testing."""
116
+ wd ("hg init" )
117
+ wd .add_command = "hg add ."
118
+ wd .commit_command = 'hg commit -m test-{reason} -u test -d "0 0"'
119
+ wd .tag_command = "hg tag {tag}"
120
+ return wd
121
+
122
+
91
123
@pytest .fixture
92
- def wd (tmp_path : Path ) -> WorkDir :
124
+ def wd (
125
+ tmp_path : Path , request : pytest .FixtureRequest , monkeypatch : pytest .MonkeyPatch
126
+ ) -> WorkDir :
127
+ """WorkDir fixture that automatically configures SCM based on markers."""
93
128
target_wd = tmp_path .resolve () / "wd"
94
129
target_wd .mkdir ()
95
- return WorkDir (target_wd )
130
+ wd = WorkDir (target_wd )
131
+
132
+ # Check for SCM markers on the test function or module
133
+ git_marker = request .node .get_closest_marker ("git" )
134
+ hg_marker = request .node .get_closest_marker ("hg" )
135
+
136
+ # Configure SCM based on markers
137
+ if git_marker :
138
+ setup_git_wd (wd , monkeypatch )
139
+ elif hg_marker :
140
+ setup_hg_wd (wd )
141
+ # If no SCM markers, return unconfigured workdir
142
+
143
+ return wd
96
144
97
145
98
146
@pytest .fixture (scope = "session" )
0 commit comments