Skip to content

Commit d4e727e

Browse files
wip: restructure testing
1 parent 731ae4d commit d4e727e

File tree

3 files changed

+113
-13
lines changed

3 files changed

+113
-13
lines changed

testing/conftest.py

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,21 @@
2222
from .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

3141
VERSION_PKGS = ["setuptools", "setuptools_scm", "packaging", "build", "wheel"]
3242

@@ -42,10 +52,10 @@ def pytest_report_header() -> list[str]:
4252
# Replace everything up to and including site-packages with site::
4353
parts = path.split("site-packages", 1)
4454
if len(parts) > 1:
45-
path = "site:." + parts[1]
55+
path = "site::" + parts[1]
4656
elif path and str(Path.cwd()) in path:
4757
# Replace current working directory with CWD::
48-
path = path.replace(str(Path.cwd()), "CWD:.")
58+
path = path.replace(str(Path.cwd()), "CWD::")
4959
res.append(f"{pkg} version {pkg_version} from {path}")
5060
return res
5161

@@ -88,11 +98,49 @@ def debug_mode() -> Iterator[DebugMode]:
8898
yield debug_mode
8999

90100

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+
91123
@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."""
93128
target_wd = tmp_path.resolve() / "wd"
94129
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
96144

97145

98146
@pytest.fixture(scope="session")

testing/test_integration.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,8 @@
3434
c = Configuration()
3535

3636

37-
@pytest.fixture
38-
def wd(wd: WorkDir) -> WorkDir:
39-
wd("git init")
40-
wd("git config user.email [email protected]")
41-
wd('git config user.name "a test"')
42-
wd.add_command = "git add ."
43-
wd.commit_command = "git commit -m test-{reason}"
44-
return wd
37+
# File-level marker for git tests
38+
pytestmark = pytest.mark.git
4539

4640

4741
def test_pyproject_support(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:

testing/wd_wrapper.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class WorkDir:
1212
commit_command: str
1313
signed_commit_command: str
1414
add_command: str
15+
tag_command: str
1516

1617
def __repr__(self) -> str:
1718
return f"<WD {self.cwd}>"
@@ -68,3 +69,60 @@ def get_version(self, **kw: Any) -> str:
6869
version = get_version(root=self.cwd, fallback_root=self.cwd, **kw)
6970
print(self.cwd.name, version, sep=": ")
7071
return version
72+
73+
def create_basic_setup_py(
74+
self, name: str = "test-package", use_scm_version: str = "True"
75+
) -> None:
76+
"""Create a basic setup.py file with setuptools_scm configuration."""
77+
self.write(
78+
"setup.py",
79+
f"""__import__('setuptools').setup(
80+
name="{name}",
81+
use_scm_version={use_scm_version},
82+
)""",
83+
)
84+
85+
def create_basic_pyproject_toml(
86+
self, name: str = "test-package", dynamic_version: bool = True
87+
) -> None:
88+
"""Create a basic pyproject.toml file with setuptools_scm configuration."""
89+
dynamic_section = 'dynamic = ["version"]' if dynamic_version else ""
90+
self.write(
91+
"pyproject.toml",
92+
f"""[build-system]
93+
requires = ["setuptools>=64", "setuptools_scm>=8"]
94+
build-backend = "setuptools.build_meta"
95+
96+
[project]
97+
name = "{name}"
98+
{dynamic_section}
99+
100+
[tool.setuptools_scm]
101+
""",
102+
)
103+
104+
def create_basic_setup_cfg(self, name: str = "test-package") -> None:
105+
"""Create a basic setup.cfg file with metadata."""
106+
self.write(
107+
"setup.cfg",
108+
f"""[metadata]
109+
name = {name}
110+
""",
111+
)
112+
113+
def create_test_file(
114+
self, filename: str = "test.txt", content: str = "test content"
115+
) -> None:
116+
"""Create a test file and commit it to the repository."""
117+
# Create parent directories if they don't exist
118+
path = self.cwd / filename
119+
path.parent.mkdir(parents=True, exist_ok=True)
120+
self.write(filename, content)
121+
self.add_and_commit()
122+
123+
def create_tag(self, tag: str = "1.0.0") -> None:
124+
"""Create a tag using the configured tag_command."""
125+
if hasattr(self, "tag_command"):
126+
self(self.tag_command, tag=tag)
127+
else:
128+
raise RuntimeError("No tag_command configured")

0 commit comments

Comments
 (0)