Skip to content

Commit 2063408

Browse files
committed
tests: avoid changing the source tree
Signed-off-by: Henry Schreiner <[email protected]>
1 parent 98dedee commit 2063408

15 files changed

+101
-106
lines changed

tests/conftest.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,6 @@ def process_package(
192192
package_dir = tmp_path / "pkg"
193193
shutil.copytree(DIR / "packages" / package.name, package_dir)
194194
monkeypatch.chdir(package_dir)
195-
# Just in case this gets littered into the source tree, clear it out
196-
if Path("dist").is_dir():
197-
shutil.rmtree("dist")
198195

199196

200197
@pytest.fixture

tests/test_broken_fallback.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@
1313
@pytest.mark.configure
1414
@pytest.mark.usefixtures("broken_fallback")
1515
@pytest.mark.parametrize("broken_define", ["BROKEN_CMAKE", "BROKEN_CODE"])
16-
def test_broken_code(broken_define: str, capfd: pytest.CaptureFixture[str]):
17-
build_wheel("dist", {f"cmake.define.{broken_define}": "1"})
18-
wheel = Path("dist") / "broken_fallback-0.0.1-py3-none-any.whl"
16+
def test_broken_code(
17+
broken_define: str, capfd: pytest.CaptureFixture[str], tmp_path: Path
18+
):
19+
dist = tmp_path / "dist"
20+
build_wheel(str(dist), {f"cmake.define.{broken_define}": "1"})
21+
wheel = dist / "broken_fallback-0.0.1-py3-none-any.whl"
1922
with zipfile.ZipFile(wheel) as f:
2023
file_names = set(f.namelist())
2124

tests/test_dynamic_metadata.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,9 +234,9 @@ def test_dual_metadata():
234234
@pytest.mark.compile
235235
@pytest.mark.configure
236236
@pytest.mark.usefixtures("mock_entry_points", "package_dynamic_metadata")
237-
def test_pep517_wheel(virtualenv):
238-
dist = Path("dist")
239-
out = build_wheel("dist")
237+
def test_pep517_wheel(virtualenv, tmp_path: Path) -> None:
238+
dist = tmp_path / "dist"
239+
out = build_wheel(str(dist))
240240
(wheel,) = dist.glob("dynamic-0.0.2-*.whl")
241241
assert wheel == dist / out
242242

tests/test_fortran.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,7 @@
4040
)
4141
def test_pep517_wheel(tmp_path, monkeypatch, virtualenv):
4242
dist = tmp_path / "dist"
43-
dist.mkdir()
4443
monkeypatch.chdir(FORTRAN_EXAMPLE)
45-
if Path("dist").is_dir():
46-
shutil.rmtree("dist")
4744
out = build_wheel(str(dist))
4845
(wheel,) = dist.glob("fibby-0.0.1-*.whl")
4946
assert wheel == dist / out

tests/test_hatchling.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@
1111
@pytest.mark.network
1212
@pytest.mark.integration
1313
@pytest.mark.usefixtures("package_hatchling")
14-
def test_hatchling_sdist(isolated) -> None:
14+
def test_hatchling_sdist(isolated, tmp_path: Path) -> None:
15+
dist = tmp_path / "dist"
1516
isolated.install("build[virtualenv]")
16-
isolated.module("build", "--sdist")
17-
(sdist,) = Path("dist").iterdir()
17+
isolated.module("build", "--sdist", f"--outdir={dist}")
18+
(sdist,) = dist.iterdir()
1819
assert sdist.name == "hatchling_example-0.1.0.tar.gz"
1920
with tarfile.open(sdist) as f:
2021
file_names = set(f.getnames())
@@ -37,12 +38,13 @@ def test_hatchling_sdist(isolated) -> None:
3738
@pytest.mark.parametrize(
3839
"build_args", [(), ("--wheel",)], ids=["sdist_to_wheel", "wheel_directly"]
3940
)
40-
def test_hatchling_wheel(isolated, build_args) -> None:
41+
def test_hatchling_wheel(isolated, build_args, tmp_path: Path) -> None:
42+
dist = tmp_path / "dist"
4143
isolated.install("build[virtualenv]", "scikit-build-core", "hatchling", "pybind11")
42-
isolated.module("build", "--no-isolation", *build_args)
44+
isolated.module("build", "--no-isolation", f"--outdir={dist}", *build_args)
4345
ext_suffix = sysconfig.get_config_var("EXT_SUFFIX")
4446

45-
(wheel,) = Path("dist").glob("*.whl")
47+
(wheel,) = dist.glob("*.whl")
4648
with zipfile.ZipFile(wheel) as f:
4749
file_names = set(f.namelist())
4850
assert file_names == {

tests/test_pyproject_abi3.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,11 @@ def test_abi3_wheel(tmp_path, monkeypatch, virtualenv, capsys):
2323
dist = tmp_path / "dist"
2424
dist.mkdir()
2525
monkeypatch.chdir(ABI_PKG)
26-
if Path("dist").is_dir():
27-
shutil.rmtree("dist")
2826
if Path("build").is_dir():
2927
shutil.rmtree("build")
3028

3129
out = build_wheel(str(dist))
32-
stdout, stderr = capsys.readouterr()
30+
stdout, _ = capsys.readouterr()
3331
assert "This is a message after success" in stdout
3432
(wheel,) = dist.glob("abi3_example-0.0.1-*.whl")
3533
assert wheel == dist / out

tests/test_pyproject_extra_dirs.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@
1212
@pytest.mark.compile
1313
@pytest.mark.configure
1414
@pytest.mark.usefixtures("package_filepath_pure")
15-
def test_pep517_wheel_extra_dirs(monkeypatch):
15+
def test_pep517_wheel_extra_dirs(monkeypatch, tmp_path: Path):
1616
monkeypatch.setenv("SKBUILD_CMAKE_DEFINE", "SOME_DEFINE3=baz;SOME_DEFINE4=baz")
1717
monkeypatch.setenv("SKBUILD_CMAKE_ARGS", "-DSOME_ARGS1=baz")
1818

19-
dist = Path("dist")
19+
dist = tmp_path / "dist"
2020
out = build_wheel(
21-
"dist",
21+
str(dist),
2222
{"cmake.define.SOME_DEFINE2": "bar", "cmake.define.SOME_DEFINE3": "bar"},
2323
)
2424
(wheel,) = dist.glob("cmake_dirs-0.0.1-*.whl")

tests/test_pyproject_pep517.py

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def compute_uncompressed_hash(inp: Path) -> str:
4343

4444

4545
@pytest.mark.usefixtures("package_simple_pyproject_ext")
46-
def test_pep517_sdist():
46+
def test_pep517_sdist(tmp_path: Path):
4747
expected_metadata = (
4848
inspect.cleandoc(
4949
"""
@@ -57,8 +57,8 @@ def test_pep517_sdist():
5757
)
5858
+ "\n\n"
5959
)
60-
dist = Path("dist")
61-
out = build_sdist("dist")
60+
dist = tmp_path / "dist"
61+
out = build_sdist(str(dist))
6262

6363
(sdist,) = dist.iterdir()
6464
assert sdist.name == "cmake_example-0.0.1.tar.gz"
@@ -83,11 +83,11 @@ def test_pep517_sdist():
8383

8484

8585
@mark_hashes_different
86-
def test_pep517_sdist_hash(monkeypatch, package_simple_pyproject_ext):
86+
def test_pep517_sdist_hash(monkeypatch, package_simple_pyproject_ext, tmp_path: Path):
8787
# Unset SOURCE_DATE_EPOCH in order to guarantee the hash match
8888
monkeypatch.delenv("SOURCE_DATE_EPOCH", raising=False)
89-
dist = Path("dist")
90-
out = build_sdist("dist")
89+
dist = tmp_path / "dist"
90+
out = build_sdist(str(dist))
9191
sdist = dist / out
9292
hash = compute_uncompressed_hash(sdist)
9393
assert hash == package_simple_pyproject_ext.sdist_hash
@@ -99,18 +99,17 @@ def test_pep517_sdist_hash(monkeypatch, package_simple_pyproject_ext):
9999

100100

101101
@pytest.mark.usefixtures("package_simple_pyproject_ext")
102-
def test_pep517_sdist_time_hash():
103-
dist = Path("dist")
102+
def test_pep517_sdist_time_hash(tmp_path: Path):
103+
dist = tmp_path / "dist"
104104

105-
out = build_sdist("dist")
105+
out = build_sdist(str(dist))
106106
sdist = dist / out
107107
hash1 = hashlib.sha256(sdist.read_bytes()).hexdigest()
108108

109109
time.sleep(2)
110110
Path("src/main.cpp").touch()
111111

112-
if Path("dist").is_dir():
113-
shutil.rmtree("dist")
112+
shutil.rmtree(dist)
114113

115114
out = build_sdist(str(dist))
116115
sdist = dist / out
@@ -121,17 +120,16 @@ def test_pep517_sdist_time_hash():
121120

122121

123122
@pytest.mark.usefixtures("package_simple_pyproject_ext")
124-
def test_pep517_sdist_time_hash_nonreproducable():
125-
dist = Path("dist")
123+
def test_pep517_sdist_time_hash_nonreproducable(tmp_path: Path):
124+
dist = tmp_path / "dist"
126125

127-
out = build_sdist("dist", {"sdist.reproducible": "false"})
126+
out = build_sdist(str(dist), {"sdist.reproducible": "false"})
128127
sdist = dist / out
129128
hash1 = hashlib.sha256(sdist.read_bytes()).hexdigest()
130129

131130
time.sleep(2)
132131

133-
if Path("dist").is_dir():
134-
shutil.rmtree("dist")
132+
shutil.rmtree(dist)
135133

136134
out = build_sdist(str(dist))
137135
sdist = dist / out
@@ -144,9 +142,9 @@ def test_pep517_sdist_time_hash_nonreproducable():
144142
@mark_hashes_different
145143
@pytest.mark.parametrize("reverse_order", [False, True])
146144
def test_pep517_sdist_time_hash_set_epoch(
147-
monkeypatch, reverse_order, package_simple_pyproject_ext
145+
monkeypatch, reverse_order, package_simple_pyproject_ext, tmp_path: Path
148146
):
149-
dist = Path("dist")
147+
dist = tmp_path / "dist"
150148
monkeypatch.setenv(
151149
"SOURCE_DATE_EPOCH", package_simple_pyproject_ext.source_date_epoch
152150
)
@@ -176,11 +174,12 @@ def each_unignored_file_ordered(*args, **kwargs):
176174
("SKBUILD_CMAKE_ARGS", "-DCMAKE_C_FLAGS=-DFOO=1 -DBAR="),
177175
],
178176
)
179-
def test_passing_cxx_flags(monkeypatch, env_var, setting):
177+
def test_passing_cxx_flags(monkeypatch, env_var, setting, tmp_path: Path):
180178
# Note: This is sensitive to the types of quotes for SKBUILD_CMAKE_ARGS
181179
monkeypatch.setenv(env_var, setting)
182-
build_wheel("dist", {"cmake.targets": ["cmake_example"]}) # Could leave empty
183-
(wheel,) = Path("dist").glob("cmake_example-0.0.1-py3-none-*.whl")
180+
dist = tmp_path / "dist"
181+
build_wheel(str(dist), {"cmake.targets": ["cmake_example"]}) # Could leave empty
182+
(wheel,) = dist.glob("cmake_example-0.0.1-py3-none-*.whl")
184183
with zipfile.ZipFile(wheel) as f:
185184
file_names = set(f.namelist())
186185

@@ -198,9 +197,11 @@ def test_passing_cxx_flags(monkeypatch, env_var, setting):
198197
@pytest.mark.compile
199198
@pytest.mark.configure
200199
@pytest.mark.usefixtures("package_simple_pyproject_ext")
201-
def test_pep517_wheel(virtualenv):
202-
dist = Path("dist")
203-
out = build_wheel("dist", {"cmake.targets": ["cmake_example"]}) # Could leave empty
200+
def test_pep517_wheel(virtualenv, tmp_path: Path):
201+
dist = tmp_path / "dist"
202+
out = build_wheel(
203+
str(dist), {"cmake.targets": ["cmake_example"]}
204+
) # Could leave empty
204205
(wheel,) = dist.glob("cmake_example-0.0.1-*.whl")
205206
assert wheel == dist / out
206207

@@ -248,9 +249,9 @@ def test_pep517_wheel(virtualenv):
248249
@pytest.mark.compile
249250
@pytest.mark.configure
250251
@pytest.mark.usefixtures("package_simple_pyproject_source_dir")
251-
def test_pep517_wheel_source_dir(virtualenv):
252-
dist = Path("dist")
253-
out = build_wheel("dist", config_settings={"skbuild.wheel.build-tag": "1foo"})
252+
def test_pep517_wheel_source_dir(virtualenv, tmp_path: Path):
253+
dist = tmp_path / "dist"
254+
out = build_wheel(str(dist), config_settings={"skbuild.wheel.build-tag": "1foo"})
254255
(wheel,) = dist.glob("cmake_example-0.0.1-*.whl")
255256
assert wheel == dist / out
256257

@@ -306,18 +307,17 @@ def test_pep517_wheel_source_dir(virtualenv):
306307
@pytest.mark.skip(reason="Doesn't work yet")
307308
@pytest.mark.compile
308309
@pytest.mark.configure
309-
def test_pep517_wheel_time_hash(monkeypatch):
310+
def test_pep517_wheel_time_hash(monkeypatch, tmp_path: Path):
310311
monkeypatch.setenv("SOURCE_DATE_EPOCH", "12345")
311-
dist = Path("dist")
312-
out = build_wheel("dist")
312+
dist = tmp_path / "dist"
313+
out = build_wheel(str(dist))
313314
wheel = dist / out
314315
hash1 = hashlib.sha256(wheel.read_bytes()).hexdigest()
315316

316317
time.sleep(2)
317318
Path("src/main.cpp").touch()
318319

319-
if Path("dist").is_dir():
320-
shutil.rmtree("dist")
320+
shutil.rmtree(dist)
321321

322322
out = build_wheel(str(dist))
323323
wheel = dist / out
@@ -385,7 +385,7 @@ def test_pep639_license_files_metadata():
385385

386386

387387
@pytest.mark.usefixtures("package_pep639_pure")
388-
def test_pep639_license_files_sdist():
388+
def test_pep639_license_files_sdist(tmp_path: Path):
389389
expected_metadata = (
390390
inspect.cleandoc(
391391
"""
@@ -400,8 +400,8 @@ def test_pep639_license_files_sdist():
400400
+ "\n\n"
401401
)
402402

403-
dist = Path("dist")
404-
out = build_sdist("dist")
403+
dist = tmp_path / "dist"
404+
out = build_sdist(str(dist))
405405

406406
(sdist,) = dist.iterdir()
407407
assert sdist.name == "pep639_pure-0.1.0.tar.gz"
@@ -425,9 +425,9 @@ def test_pep639_license_files_sdist():
425425

426426

427427
@pytest.mark.usefixtures("package_pep639_pure")
428-
def test_pep639_license_files_wheel():
429-
dist = Path("dist")
430-
out = build_wheel("dist", {})
428+
def test_pep639_license_files_wheel(tmp_path: Path):
429+
dist = tmp_path / "dist"
430+
out = build_wheel(str(dist), {})
431431
(wheel,) = dist.glob("pep639_pure-0.1.0-*.whl")
432432
assert wheel == dist / out
433433

0 commit comments

Comments
 (0)