|
10 | 10 | from spin.cmds import meson |
11 | 11 | from spin.containers import DotDict |
12 | 12 |
|
| 13 | +# Fake meson: prints to stdout and stderr, and fails for subcommands in FAILING_CMDS |
| 14 | +FAKE_MESON = """ |
| 15 | +import sys |
| 16 | +
|
| 17 | +FAILING_CMDS = {failing_cmds!r} |
| 18 | +
|
| 19 | +subcommand = sys.argv[1] |
| 20 | +print("fake meson " + subcommand + ": stdout") |
| 21 | +print("fake meson " + subcommand + ": stderr", file=sys.stderr) |
| 22 | +sys.exit(1 if subcommand in FAILING_CMDS else 0) |
| 23 | +""" |
| 24 | + |
13 | 25 |
|
14 | 26 | @pytest.fixture |
15 | | -def failing_meson(tmp_path, monkeypatch): |
16 | | - monkeypatch.setenv("PYTHONIOENCODING", "utf-8") |
17 | | - script = tmp_path / "meson.py" |
18 | | - script.write_text( |
19 | | - "import sys\n" |
20 | | - "print('Meson setup: configuration error – wrong Cython version')\n" |
21 | | - "print('Diagnostic from stderr', file=sys.stderr)\n" |
22 | | - "sys.exit(1)\n", |
23 | | - encoding="utf-8", |
24 | | - ) |
25 | | - monkeypatch.setattr(meson, "_meson_cli", lambda: [sys.executable, str(script)]) |
26 | | - monkeypatch.setattr(meson, "get_config", lambda: DotDict({})) |
27 | | - return str(tmp_path / "build") |
| 27 | +def fake_meson(tmp_path, monkeypatch): |
| 28 | + """Return a factory that installs a fake meson and returns a build dir.""" |
28 | 29 |
|
| 30 | + def make(failing_cmds=()): |
| 31 | + script = tmp_path / "meson.py" |
| 32 | + script.write_text(FAKE_MESON.format(failing_cmds=list(failing_cmds))) |
| 33 | + # Run the fake script in place of the meson binary |
| 34 | + monkeypatch.setattr(meson, "_meson_cli", lambda: [sys.executable, str(script)]) |
| 35 | + monkeypatch.setattr(meson, "get_config", lambda: DotDict({})) |
| 36 | + return str(tmp_path / "build") |
29 | 37 |
|
30 | | -@pytest.mark.parametrize("quiet", [False, True]) |
31 | | -def test_configuration_failure_output(failing_meson, capfd, quiet): |
| 38 | + return make |
| 39 | + |
| 40 | + |
| 41 | +def test_quiet_configuration_failure_output(fake_meson, capfd): |
| 42 | + build_dir = fake_meson(failing_cmds=["setup"]) |
32 | 43 | with pytest.raises(RuntimeError, match="Meson configuration failed"): |
33 | 44 | meson.build.callback( |
34 | | - meson_args=(), build_dir=failing_meson, prefix="/usr", quiet=quiet |
| 45 | + meson_args=(), build_dir=build_dir, prefix="/usr", quiet=True |
35 | 46 | ) |
36 | 47 | captured = capfd.readouterr() |
37 | 48 | output = captured.out + captured.err |
38 | | - assert output.count("Meson setup: configuration error – wrong Cython version") == 1 |
39 | | - assert output.count("Diagnostic from stderr") == 1 |
| 49 | + assert output.count("fake meson setup: stdout") == 1 |
| 50 | + assert output.count("fake meson setup: stderr") == 1 |
40 | 51 |
|
41 | 52 |
|
42 | | -def test_run_configuration_failure_output(failing_meson, monkeypatch, capfd): |
| 53 | +def test_run_configuration_failure_output(fake_meson, monkeypatch, capfd): |
| 54 | + build_dir = fake_meson(failing_cmds=["setup"]) |
43 | 55 | monkeypatch.setattr(meson, "_get_configured_command", lambda name: meson.build) |
44 | 56 | with ( |
45 | 57 | click.Context(meson.run), |
46 | 58 | pytest.raises(RuntimeError, match="Meson configuration failed"), |
47 | 59 | ): |
48 | | - meson.run.callback( |
49 | | - args=("unused-command",), build=True, build_dir=failing_meson |
50 | | - ) |
| 60 | + meson.run.callback(args=("unused-command",), build=True, build_dir=build_dir) |
51 | 61 | captured = capfd.readouterr() |
52 | | - assert "configuration error – wrong Cython version" in captured.err |
53 | | - assert "Diagnostic from stderr" in captured.err |
54 | | - assert "configuration error" not in captured.out |
| 62 | + assert "fake meson setup: stdout" in captured.err |
| 63 | + assert "fake meson setup: stderr" in captured.err |
| 64 | + assert "fake meson setup" not in captured.out |
55 | 65 |
|
56 | 66 |
|
57 | | -def test_successful_quiet_build_output(failing_meson, capfd): |
58 | | - script = os.path.join(os.path.dirname(failing_meson), "meson.py") |
59 | | - with open(script, "w", encoding="utf-8") as file: |
60 | | - file.write("print('Successful build output')\n") |
61 | | - meson.build.callback( |
62 | | - meson_args=(), build_dir=failing_meson, prefix="/usr", quiet=True |
63 | | - ) |
| 67 | +def test_successful_quiet_build_output(fake_meson, capfd): |
| 68 | + build_dir = fake_meson() |
| 69 | + meson.build.callback(meson_args=(), build_dir=build_dir, prefix="/usr", quiet=True) |
64 | 70 | captured = capfd.readouterr() |
65 | | - assert "Successful build output" not in captured.out + captured.err |
| 71 | + assert "fake meson" not in captured.out + captured.err |
66 | 72 |
|
67 | 73 |
|
68 | 74 | def make_paths(root, paths): |
|
0 commit comments