| 1 |
# SPDX-FileCopyrightText: Peter Pentchev <[email protected]> |
| 2 |
# SPDX-License-Identifier: BSD-2-Clause |
| 3 |
|
| 4 |
"""Run the confget tests using the Python methods. |
| 5 |
|
| 6 |
Load the test data, then run the tests using the objects provided by |
| 7 |
the Python library, not by executing the command-line tool. |
| 8 |
""" |
| 9 |
|
| 10 |
import itertools |
| 11 |
import pathlib |
| 12 |
from typing import Dict |
| 13 |
from unittest import mock |
| 14 |
|
| 15 |
import pytest |
| 16 |
|
| 17 |
import confget |
| 18 |
from confget import format as cformat |
| 19 |
|
| 20 |
from .data import test_defs as tdefs |
| 21 |
from .data import test_load as tload |
| 22 |
|
| 23 |
|
| 24 |
TESTS = tload.load_all_tests(tdefs.get_test_path(None)) |
| 25 |
|
| 26 |
FULL_TEST_DATA_BEFORE_RIF = [ |
| 27 |
(fname, test) |
| 28 |
for fname, idx, test in sorted( |
| 29 |
itertools.chain( |
| 30 |
*[ |
| 31 |
[(tfile[0], idx, test) for idx, test in enumerate(tfile[1].tests)] |
| 32 |
for tfile in TESTS.items() |
| 33 |
] |
| 34 |
) |
| 35 |
) |
| 36 |
] |
| 37 |
|
| 38 |
FULL_TEST_DATA = list( |
| 39 |
itertools.chain( |
| 40 |
*[ |
| 41 |
[(str(fname), use_rif, test) for use_rif in (False, True)] |
| 42 |
for fname, test in FULL_TEST_DATA_BEFORE_RIF |
| 43 |
] |
| 44 |
) |
| 45 |
) |
| 46 |
|
| 47 |
SKIP_ARGS = {"check_only"} |
| 48 |
|
| 49 |
|
| 50 |
# We keep the fname parameter for visibility in the test logs. |
| 51 |
# Also, this function is only ever invoked on the test data we built above, so |
| 52 |
# there will hopefully be no confusion about the boolean parameter. |
| 53 |
@pytest.mark.parametrize(("fname", "use_rif", "test"), FULL_TEST_DATA) |
| 54 |
def test_run(fname: str, use_rif: bool, test: tdefs.SingleTestDef) -> None: # noqa: ARG001,FBT001 |
| 55 |
"""Instantiate a confget object, load the data, check it.""" |
| 56 |
if set(test.args.keys()) & SKIP_ARGS: |
| 57 |
return |
| 58 |
if use_rif and not test.backend.startswith("ini"): |
| 59 |
return |
| 60 |
|
| 61 |
config = test.get_config() |
| 62 |
|
| 63 |
def get_file_data() -> Dict[str, Dict[str, str]]: |
| 64 |
"""Read the file data: backend or read_ini_file().""" |
| 65 |
if use_rif: |
| 66 |
return confget.read_ini_file(config) |
| 67 |
|
| 68 |
backend = test.get_backend() |
| 69 |
obj = backend(config) |
| 70 |
return obj.read_file() |
| 71 |
|
| 72 |
if test.stdin: |
| 73 |
infile = tdefs.get_test_path(test.stdin) |
| 74 |
with mock.patch("sys.stdin", new=pathlib.Path(infile).open(encoding="UTF-8")): |
| 75 |
data = get_file_data() |
| 76 |
else: |
| 77 |
data = get_file_data() |
| 78 |
|
| 79 |
res = cformat.filter_vars(config, data) |
| 80 |
output = test.do_xform(line for line in res) |
| 81 |
test.output.check_result(output) |