Skip to content

Commit 873a321

Browse files
lorepirrihenryiii
andauthored
tests: fix test cmake config (#847)
The function `configure_args ` that is used to prepare the arguments in `tests/test_cmake_config.py` does not consider that there might be a `CMAKE_GENERATOR` set in the environment (e.g. set to `Ninja`): https://github.com/scikit-build/scikit-build-core/blob/901b1d31d9b7b47ee93aa2ef6f887c15e82e7f7a/tests/test_cmake_config.py#L21-L30 The `configure()` in `src/scikit_build_core/cmake.py` mutates `single_config` just right before calling the `Run(...).live(...)` https://github.com/scikit-build/scikit-build-core/blob/901b1d31d9b7b47ee93aa2ef6f887c15e82e7f7a/src/scikit_build_core/cmake.py#L210-L212 It is not ideal to have tests affected by non-controlled environmental variables (e.g. `CMAKE_GENERATOR`). This PR introduces parametrization of three tests that fail otherwise on Windows (see #843), and monkey-patching of the environment variable `CMAKE_GENERATOR`. Likewise, `sysconfig.get_platform()` is monkey-patched and parametrized with `win` and `linux`. The module `cmake` needs to be reloaded every time, because of the `single_config` member of `CMakes` initialized in the factory for the `dataclass`: https://github.com/scikit-build/scikit-build-core/blob/901b1d31d9b7b47ee93aa2ef6f887c15e82e7f7a/src/scikit_build_core/cmake.py#L84 --------- Signed-off-by: Henry Schreiner <[email protected]> Co-authored-by: Henry Schreiner <[email protected]>
1 parent 7cb185b commit 873a321

File tree

1 file changed

+66
-10
lines changed

1 file changed

+66
-10
lines changed

tests/test_cmake_config.py

Lines changed: 66 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import os
44
import shutil
5+
import sysconfig
56
from pathlib import Path
67
from typing import TYPE_CHECKING
78

@@ -18,11 +19,51 @@
1819
DIR = Path(__file__).parent.resolve()
1920

2021

21-
def configure_args(config: CMaker, *, init: bool = False) -> Generator[str, None, None]:
22+
def single_config(param: None | str) -> bool:
23+
if param is None:
24+
return not sysconfig.get_platform().startswith("win")
25+
26+
return param in {"Ninja", "Makefiles"}
27+
28+
29+
@pytest.fixture(
30+
params=[
31+
pytest.param(None, id="default"),
32+
pytest.param("Ninja", id="ninja"),
33+
pytest.param(
34+
"Makefiles",
35+
id="makefiles",
36+
marks=pytest.mark.skipif(
37+
sysconfig.get_platform().startswith("win"), reason="run on Windows only"
38+
),
39+
),
40+
pytest.param(
41+
"Others",
42+
id="others",
43+
marks=pytest.mark.skipif(
44+
sysconfig.get_platform().startswith("win"), reason="run on Windows only"
45+
),
46+
),
47+
]
48+
)
49+
def generator(
50+
request: pytest.FixtureRequest, monkeypatch: pytest.MonkeyPatch
51+
) -> str | None:
52+
if request.param is None:
53+
monkeypatch.delenv("CMAKE_GENERATOR", raising=False)
54+
else:
55+
monkeypatch.setenv("CMAKE_GENERATOR", request.param)
56+
57+
return request.param
58+
59+
60+
def configure_args(
61+
config: CMaker, *, init: bool = False, single_config: bool = False
62+
) -> Generator[str, None, None]:
2263
yield f"-S{config.source_dir}"
2364
yield f"-B{config.build_dir}"
2465

25-
if config.single_config:
66+
if single_config:
2667
yield f"-DCMAKE_BUILD_TYPE:STRING={config.build_type}"
2768

2869
if init:
@@ -31,7 +72,11 @@ def configure_args(config: CMaker, *, init: bool = False) -> Generator[str, None
3172

3273

3374
@pytest.mark.configure()
34-
def test_init_cache(fp, tmp_path):
75+
def test_init_cache(
76+
generator: str,
77+
tmp_path: Path,
78+
fp,
79+
):
3580
fp.register(
3681
[fp.program("cmake"), "-E", "capabilities"],
3782
stdout='{"version":{"string":"3.14.0"}}',
@@ -51,7 +96,9 @@ def test_init_cache(fp, tmp_path):
5196
{"SKBUILD": True, "SKBUILD_VERSION": "1.0.0", "SKBUILD_PATH": config.source_dir}
5297
)
5398

54-
cmd = list(configure_args(config, init=True))
99+
cmd = list(
100+
configure_args(config, init=True, single_config=single_config(generator))
101+
)
55102
print("Registering: cmake", *cmd)
56103
fp.register([fp.program("cmake"), *cmd])
57104
fp.register([fp.program("cmake3"), *cmd])
@@ -87,7 +134,11 @@ def test_too_old(fp, monkeypatch):
87134

88135

89136
@pytest.mark.configure()
90-
def test_cmake_args(tmp_path, fp):
137+
def test_cmake_args(
138+
generator: str,
139+
tmp_path: Path,
140+
fp,
141+
):
91142
fp.register(
92143
[fp.program("cmake"), "-E", "capabilities"],
93144
stdout='{"version":{"string":"3.15.0"}}',
@@ -99,24 +150,29 @@ def test_cmake_args(tmp_path, fp):
99150

100151
config = CMaker(
101152
CMake.default_search(),
102-
source_dir=DIR / "packages/simple_pure",
153+
source_dir=DIR / "packages" / "simple_pure",
103154
build_dir=tmp_path / "build",
104155
build_type="Release",
105156
)
106157

107-
cmd = list(configure_args(config))
158+
cmd = list(configure_args(config, single_config=single_config(generator)))
108159
cmd.append("-DSOMETHING=one")
109160
print("Registering: cmake", *cmd)
110161
fp.register([fp.program("cmake"), *cmd])
111162
fp.register([fp.program("cmake3"), *cmd])
112163

113164
config.configure(cmake_args=["-DSOMETHING=one"])
114-
165+
# config.configure might mutate config.single_config
166+
assert config.single_config == single_config(generator)
115167
assert len(fp.calls) == 2
116168

117169

118170
@pytest.mark.configure()
119-
def test_cmake_paths(tmp_path, fp):
171+
def test_cmake_paths(
172+
generator: str,
173+
tmp_path: Path,
174+
fp,
175+
):
120176
fp.register(
121177
[fp.program("cmake"), "-E", "capabilities"],
122178
stdout='{"version":{"string":"3.15.0"}}',
@@ -135,7 +191,7 @@ def test_cmake_paths(tmp_path, fp):
135191
module_dirs=[tmp_path / "module"],
136192
)
137193

138-
cmd = list(configure_args(config))
194+
cmd = list(configure_args(config, single_config=single_config(generator)))
139195
print("Registering: cmake", *cmd)
140196
fp.register([fp.program("cmake"), *cmd])
141197
fp.register([fp.program("cmake3"), *cmd])

0 commit comments

Comments
 (0)