|
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 |
|
| 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 | + |
| 25 | + |
| 26 | +@pytest.fixture |
| 27 | +def fake_meson(tmp_path, monkeypatch): |
| 28 | + """Return a factory that installs a fake meson and returns a build dir.""" |
| 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") |
| 37 | + |
| 38 | + return make |
| 39 | + |
| 40 | + |
| 41 | +def test_quiet_configuration_failure_output(fake_meson, capfd): |
| 42 | + build_dir = fake_meson(failing_cmds=["setup"]) |
| 43 | + with pytest.raises(RuntimeError, match="Meson configuration failed"): |
| 44 | + meson.build.callback( |
| 45 | + meson_args=(), build_dir=build_dir, prefix="/usr", quiet=True |
| 46 | + ) |
| 47 | + captured = capfd.readouterr() |
| 48 | + output = captured.out + captured.err |
| 49 | + assert output.count("fake meson setup: stdout") == 1 |
| 50 | + assert output.count("fake meson setup: stderr") == 1 |
| 51 | + |
| 52 | + |
| 53 | +def test_run_configuration_failure_output(fake_meson, monkeypatch, capfd): |
| 54 | + build_dir = fake_meson(failing_cmds=["setup"]) |
| 55 | + monkeypatch.setattr(meson, "_get_configured_command", lambda name: meson.build) |
| 56 | + with ( |
| 57 | + click.Context(meson.run), |
| 58 | + pytest.raises(RuntimeError, match="Meson configuration failed"), |
| 59 | + ): |
| 60 | + meson.run.callback(args=("unused-command",), build=True, build_dir=build_dir) |
| 61 | + captured = capfd.readouterr() |
| 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 |
| 65 | + |
| 66 | + |
| 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) |
| 70 | + captured = capfd.readouterr() |
| 71 | + assert "fake meson" not in captured.out + captured.err |
| 72 | + |
12 | 73 |
|
13 | 74 | def make_paths(root, paths): |
14 | 75 | for p in paths: |
|
0 commit comments