2222from .wd_wrapper import WorkDir
2323
2424
25- def pytest_configure () -> None :
25+ def pytest_configure (config : pytest . Config ) -> None :
2626 # 2009-02-13T23:31:30+00:00
2727 os .environ ["SOURCE_DATE_EPOCH" ] = "1234567890"
2828 os .environ ["SETUPTOOLS_SCM_DEBUG" ] = "1"
2929
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+
3040
3141VERSION_PKGS = ["setuptools" , "setuptools_scm" , "packaging" , "build" , "wheel" ]
3242
@@ -38,7 +48,15 @@ def pytest_report_header() -> list[str]:
3848 for pkg in VERSION_PKGS :
3949 pkg_version = version (pkg )
4050 path = __import__ (pkg ).__file__
41- res .append (f"{ pkg } version { pkg_version } from { path !r} " )
51+ if path and "site-packages" in path :
52+ # Replace everything up to and including site-packages with site::
53+ parts = path .split ("site-packages" , 1 )
54+ if len (parts ) > 1 :
55+ path = "site::" + parts [1 ]
56+ elif path and str (Path .cwd ()) in path :
57+ # Replace current working directory with CWD::
58+ path = path .replace (str (Path .cwd ()), "CWD::" )
59+ res .append (f"{ pkg } version { pkg_version } from { path } " )
4260 return res
4361
4462
@@ -80,11 +98,49 @@ def debug_mode() -> Iterator[DebugMode]:
8098 yield debug_mode
8199
82100
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 test@example.com" )
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+
83123@pytest .fixture
84- 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."""
85128 target_wd = tmp_path .resolve () / "wd"
86129 target_wd .mkdir ()
87- 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
88144
89145
90146@pytest .fixture (scope = "session" )
0 commit comments