|
4 | 4 | from os.path import join as pjoin |
5 | 5 | from os.path import normpath |
6 | 6 |
|
| 7 | +import click |
7 | 8 | import pytest |
8 | 9 |
|
9 | 10 | from spin.cmds import meson |
10 | 11 | from spin.containers import DotDict |
11 | 12 |
|
12 | 13 |
|
| 14 | +@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") |
| 28 | + |
| 29 | + |
| 30 | +@pytest.mark.parametrize("quiet", [False, True]) |
| 31 | +def test_configuration_failure_output(failing_meson, capfd, quiet): |
| 32 | + with pytest.raises(RuntimeError, match="Meson configuration failed"): |
| 33 | + meson.build.callback( |
| 34 | + meson_args=(), build_dir=failing_meson, prefix="/usr", quiet=quiet |
| 35 | + ) |
| 36 | + captured = capfd.readouterr() |
| 37 | + 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 |
| 40 | + |
| 41 | + |
| 42 | +def test_run_configuration_failure_output(failing_meson, monkeypatch, capfd): |
| 43 | + monkeypatch.setattr(meson, "_get_configured_command", lambda name: meson.build) |
| 44 | + with ( |
| 45 | + click.Context(meson.run), |
| 46 | + pytest.raises(RuntimeError, match="Meson configuration failed"), |
| 47 | + ): |
| 48 | + meson.run.callback( |
| 49 | + args=("unused-command",), build=True, build_dir=failing_meson |
| 50 | + ) |
| 51 | + 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 |
| 55 | + |
| 56 | + |
| 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 | + ) |
| 64 | + captured = capfd.readouterr() |
| 65 | + assert "Successful build output" not in captured.out + captured.err |
| 66 | + |
| 67 | + |
13 | 68 | def make_paths(root, paths): |
14 | 69 | for p in paths: |
15 | 70 | os.makedirs(pjoin(root, p.lstrip("/"))) |
|
0 commit comments