|
| 1 | +""" |
| 2 | +The license section: a program without LICENSE gets "GPL", with a warning, |
| 3 | +and an explicit LICENSE is emitted as written, with no warning. |
| 4 | +""" |
| 5 | + |
| 6 | +import logging |
| 7 | +from pathlib import Path |
| 8 | + |
| 9 | +from tests.framework.compiler import run_ir_generation |
| 10 | + |
| 11 | +TESTS_DIR = Path(__file__).parent |
| 12 | + |
| 13 | + |
| 14 | +def _license_global(text): |
| 15 | + """The LICENSE global as compile_to_ir emits it: NUL-terminated bytes.""" |
| 16 | + data = text.encode() + b"\x00" |
| 17 | + elems = ", ".join(f"i8 {b}" for b in data) |
| 18 | + return ( |
| 19 | + f'@"LICENSE" = dso_local global [{len(data)} x i8] [{elems}], section "license"' |
| 20 | + ) |
| 21 | + |
| 22 | + |
| 23 | +def _license_ir(name, tmp_path, caplog): |
| 24 | + ll_path = tmp_path / "output.ll" |
| 25 | + with caplog.at_level(logging.WARNING, logger="pythonbpf.license_pass"): |
| 26 | + run_ir_generation(TESTS_DIR / name, ll_path) |
| 27 | + warnings = [ |
| 28 | + r.getMessage() for r in caplog.records if r.name == "pythonbpf.license_pass" |
| 29 | + ] |
| 30 | + return ll_path.read_text(), warnings |
| 31 | + |
| 32 | + |
| 33 | +def test_missing_license_defaults_to_gpl(tmp_path, caplog): |
| 34 | + ir, warnings = _license_ir("passing_tests/license_default.py", tmp_path, caplog) |
| 35 | + assert _license_global("GPL") in ir |
| 36 | + assert any('defaulting to "GPL"' in w for w in warnings), warnings |
| 37 | + |
| 38 | + |
| 39 | +def test_explicit_license_is_kept(tmp_path, caplog): |
| 40 | + ir, warnings = _license_ir("failing_tests/license_non_gpl.py", tmp_path, caplog) |
| 41 | + assert _license_global("Proprietary") in ir |
| 42 | + assert not warnings, warnings |
0 commit comments