|
| 1 | +# SPDX-FileCopyrightText: Copyright The Zephyr Project Contributors |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +""" |
| 5 | +End-to-end DT Doctor checks, run by ctest against a real application build. |
| 6 | +
|
| 7 | +The analyzer and the SCA wrapper are exercised as real subprocesses against the |
| 8 | +build's edt.pickle, with deliberately-failing compile and link commands using |
| 9 | +the same toolchain the application was built with. |
| 10 | +""" |
| 11 | + |
| 12 | +import subprocess |
| 13 | +import sys |
| 14 | +from pathlib import Path |
| 15 | + |
| 16 | +import pytest |
| 17 | + |
| 18 | +ZEPHYR_BASE = Path(__file__).parents[3] |
| 19 | +ANALYZER = ZEPHYR_BASE / "scripts" / "dts" / "dtdoctor_analyzer.py" |
| 20 | +WRAPPER = ZEPHYR_BASE / "scripts" / "dts" / "dtdoctor_sca_wrapper.py" |
| 21 | + |
| 22 | + |
| 23 | +def ord_symbol(edt, label): |
| 24 | + return f"__device_dts_ord_{edt.label2node[label].dep_ordinal}" |
| 25 | + |
| 26 | + |
| 27 | +def run_analyzer(edt_pickle, symbol): |
| 28 | + return subprocess.run( |
| 29 | + [sys.executable, str(ANALYZER), "--edt-pickle", str(edt_pickle), "--symbol", symbol], |
| 30 | + capture_output=True, |
| 31 | + text=True, |
| 32 | + ) |
| 33 | + |
| 34 | + |
| 35 | +def run_wrapper_around(cmd, edt_pickle): |
| 36 | + return subprocess.run( |
| 37 | + [sys.executable, str(WRAPPER), "--edt-pickle", str(edt_pickle), "--", *cmd], |
| 38 | + capture_output=True, |
| 39 | + text=True, |
| 40 | + ) |
| 41 | + |
| 42 | + |
| 43 | +def test_analyzer_reports_disabled_node(edt, edt_pickle): |
| 44 | + proc = run_analyzer(edt_pickle, ord_symbol(edt, "dtdoctor_disabled")) |
| 45 | + assert proc.returncode == 0 |
| 46 | + assert "DT Doctor" in proc.stdout |
| 47 | + assert "is disabled in" in proc.stdout |
| 48 | + assert "dtdoctor-disabled-device" in proc.stdout |
| 49 | + assert "'dtdoctor,dev'" in proc.stdout |
| 50 | + assert "'dtdoctor-dev'" in proc.stdout |
| 51 | + assert "'status' property to 'okay'" in proc.stdout |
| 52 | + |
| 53 | + |
| 54 | +def test_analyzer_reports_enabled_node_without_driver(edt, edt_pickle): |
| 55 | + proc = run_analyzer(edt_pickle, ord_symbol(edt, "dtdoctor_enabled")) |
| 56 | + assert proc.returncode == 0 |
| 57 | + assert "is enabled but no driver" in proc.stdout |
| 58 | + |
| 59 | + |
| 60 | +TESTDATA_SNIPPETS = [ |
| 61 | + "int dev = __device_dts_ord_{ordinal};\n", |
| 62 | + "int get_dev(void) {{ return __device_dts_ord_{ordinal}; }}\n", |
| 63 | +] |
| 64 | + |
| 65 | + |
| 66 | +@pytest.mark.parametrize('template', TESTDATA_SNIPPETS, ids=['file-scope', 'function-scope']) |
| 67 | +def test_wrapper_diagnoses_compile_error(edt, edt_pickle, cc, tmp_path, template): |
| 68 | + ordinal = edt.label2node["dtdoctor_disabled"].dep_ordinal |
| 69 | + bad_c = tmp_path / "bad.c" |
| 70 | + bad_c.write_text(template.format(ordinal=ordinal), encoding="utf-8") |
| 71 | + proc = run_wrapper_around([cc, "-c", str(bad_c), "-o", str(tmp_path / "bad.o")], edt_pickle) |
| 72 | + assert proc.returncode != 0 |
| 73 | + assert "DT Doctor" in proc.stdout |
| 74 | + assert "is disabled in" in proc.stdout |
| 75 | + |
| 76 | + |
| 77 | +def test_wrapper_diagnoses_link_error(edt, edt_pickle, cc, tmp_path): |
| 78 | + ordinal = edt.label2node["dtdoctor_disabled"].dep_ordinal |
| 79 | + bad_c = tmp_path / "bad.c" |
| 80 | + bad_c.write_text( |
| 81 | + f"extern int __device_dts_ord_{ordinal};\n" |
| 82 | + f"int main(void) {{ return __device_dts_ord_{ordinal}; }}\n", |
| 83 | + encoding="utf-8", |
| 84 | + ) |
| 85 | + obj = tmp_path / "bad.o" |
| 86 | + subprocess.run([cc, "-c", str(bad_c), "-o", str(obj)], check=True) |
| 87 | + |
| 88 | + link_cmd = [cc, str(obj), "-nostdlib", "-o", str(tmp_path / "bad.elf")] |
| 89 | + proc = run_wrapper_around(link_cmd, edt_pickle) |
| 90 | + assert proc.returncode != 0 |
| 91 | + if "undefined reference" not in proc.stderr and "undefined symbol" not in proc.stderr: |
| 92 | + pytest.skip(f"unsupported linker error format: {proc.stderr[:200]}") |
| 93 | + assert "DT Doctor" in proc.stdout |
| 94 | + assert "is disabled in" in proc.stdout |
0 commit comments