|
| 1 | +# Copyright (c) 2011-2019 Ulf Magnusson |
| 2 | +# SPDX-License-Identifier: ISC |
| 3 | +# |
| 4 | +# Shared fixtures and assertion helpers for the Kconfiglib pytest suite. |
| 5 | + |
| 6 | +import glob |
| 7 | +import os |
| 8 | +import sys |
| 9 | + |
| 10 | +import pytest |
| 11 | + |
| 12 | +# Ensure kconfiglib is importable from the project root |
| 13 | +sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..")) |
| 14 | + |
| 15 | +from kconfiglib import TRI_TO_STR # noqa: E402 |
| 16 | + |
| 17 | +# --------------------------------------------------------------------------- |
| 18 | +# Fixtures |
| 19 | +# --------------------------------------------------------------------------- |
| 20 | + |
| 21 | + |
| 22 | +@pytest.fixture(autouse=True) |
| 23 | +def _clean_env(monkeypatch): |
| 24 | + """Save and restore environment variables between tests. |
| 25 | +
|
| 26 | + Also removes KCONFIG_ALLCONFIG to prevent accidental config loading. |
| 27 | + """ |
| 28 | + monkeypatch.delenv("KCONFIG_ALLCONFIG", raising=False) |
| 29 | + yield |
| 30 | + |
| 31 | + |
| 32 | +@pytest.fixture(autouse=True) |
| 33 | +def _cleanup_config_files(): |
| 34 | + """Remove config_test* files after each test.""" |
| 35 | + yield |
| 36 | + tests_dir = os.path.join(os.path.dirname(__file__)) |
| 37 | + for f in glob.glob(os.path.join(tests_dir, "config_test*")): |
| 38 | + os.remove(f) |
| 39 | + # Also clean from project root (some tests write there) |
| 40 | + project_root = os.path.join(os.path.dirname(__file__), "..") |
| 41 | + for f in glob.glob(os.path.join(project_root, "config_test*")): |
| 42 | + os.remove(f) |
| 43 | + |
| 44 | + |
| 45 | +# --------------------------------------------------------------------------- |
| 46 | +# Assertion helpers |
| 47 | +# |
| 48 | +# These take an explicit Kconfig instance `c` rather than closing over one. |
| 49 | +# --------------------------------------------------------------------------- |
| 50 | + |
| 51 | + |
| 52 | +def verify_value(c, sym_name, val): |
| 53 | + """Verify that a symbol has a particular value.""" |
| 54 | + if isinstance(val, int): |
| 55 | + val = TRI_TO_STR[val] |
| 56 | + |
| 57 | + sym = c.syms[sym_name] |
| 58 | + assert sym.str_value == val, f"{sym_name} value mismatch" |
| 59 | + |
| 60 | + |
| 61 | +def assign_and_verify_value(c, sym_name, val, new_val): |
| 62 | + """Assign val to a symbol and verify its value becomes new_val.""" |
| 63 | + if isinstance(new_val, int): |
| 64 | + new_val = TRI_TO_STR[new_val] |
| 65 | + |
| 66 | + sym = c.syms[sym_name] |
| 67 | + assert sym.set_value(val), f"Failed to assign '{val}' to {sym_name}" |
| 68 | + assert sym.str_value == new_val, f"{sym_name} value after assignment" |
| 69 | + |
| 70 | + |
| 71 | +def assign_and_verify(c, sym_name, user_val): |
| 72 | + """Like assign_and_verify_value(), with the expected value being the |
| 73 | + value just set.""" |
| 74 | + assign_and_verify_value(c, sym_name, user_val, user_val) |
| 75 | + |
| 76 | + |
| 77 | +def assign_and_verify_user_value(c, sym_name, val, user_val, valid): |
| 78 | + """Assign a user value and verify the new user value and validity.""" |
| 79 | + sym = c.syms[sym_name] |
| 80 | + assert sym.set_value(val) == valid, f"{sym_name} validity mismatch for '{val}'" |
| 81 | + assert sym.user_value == user_val, f"{sym_name} user_value mismatch" |
| 82 | + |
| 83 | + |
| 84 | +def verify_str(item, expected): |
| 85 | + """Verify str(item) matches expected (strip leading/trailing newline).""" |
| 86 | + assert str(item) == expected[1:-1] |
0 commit comments