|
| 1 | +""" |
| 2 | +this testing framework is developed based on pytest. |
| 3 | +see quick start of pytest: https://docs.pytest.org/en/latest/example/simple.html |
| 4 | +
|
| 5 | +""" |
| 6 | +import os |
| 7 | +import subprocess |
| 8 | +from pathlib import Path |
| 9 | + |
| 10 | +import pytest |
| 11 | +from ruamel.yaml import YAML |
| 12 | +from collections.abc import Mapping, Sequence |
| 13 | + |
| 14 | +TEST_FILE = "kcl.yaml" |
| 15 | +CI_TEST_DIR = "ci-test" |
| 16 | +STDOUT_GOLDEN = "stdout.golden.yaml" |
| 17 | +SETTINGS_FILE = "settings.yaml" |
| 18 | + |
| 19 | +ROOT_STR = "test/integration/konfig" |
| 20 | +ROOT = str(Path(__file__).parent.parent.parent.parent.parent.joinpath(ROOT_STR)) |
| 21 | + |
| 22 | +yaml = YAML(typ="unsafe", pure=True) |
| 23 | + |
| 24 | + |
| 25 | +def find_test_dirs(): |
| 26 | + result = [] |
| 27 | + root_dirs = [ROOT] |
| 28 | + for root_dir in root_dirs: |
| 29 | + for root, _, files in os.walk(root_dir): |
| 30 | + for name in files: |
| 31 | + if name == TEST_FILE: |
| 32 | + result.append(root) |
| 33 | + return result |
| 34 | + |
| 35 | + |
| 36 | +def compare_results(result, golden_result): |
| 37 | + # Convert result and golden_result string to string lines with line ending stripped, then compare. |
| 38 | + |
| 39 | + assert compare_unordered_yaml_objects(list(yaml.load_all(result)), list(yaml.load_all(golden_result))) |
| 40 | + |
| 41 | +# Comparing the contents of two YAML objects for equality in an unordered manner |
| 42 | +def compare_unordered_yaml_objects(result, golden_result): |
| 43 | + if isinstance(result, Mapping) and isinstance(golden_result, Mapping): |
| 44 | + if result.keys() != golden_result.keys(): |
| 45 | + return False |
| 46 | + for key in result.keys(): |
| 47 | + if not compare_unordered_yaml_objects(result[key], golden_result[key]): |
| 48 | + return False |
| 49 | + |
| 50 | + return True |
| 51 | + elif isinstance(result, Sequence) and isinstance(golden_result, Sequence): |
| 52 | + if len(result) != len(golden_result): |
| 53 | + return False |
| 54 | + for item in result: |
| 55 | + if item not in golden_result: |
| 56 | + return False |
| 57 | + for item in golden_result: |
| 58 | + if item not in result: |
| 59 | + return False |
| 60 | + return True |
| 61 | + else: |
| 62 | + return result == golden_result |
| 63 | + |
| 64 | +def has_settings_file(directory): |
| 65 | + settings_file = directory / SETTINGS_FILE |
| 66 | + return settings_file.is_file() |
| 67 | + |
| 68 | + |
| 69 | +print("##### K Language Grammar Test Suite #####") |
| 70 | +test_dirs = find_test_dirs() |
| 71 | +pwd = str(Path(__file__).parent.parent.parent.parent) |
| 72 | +os.environ["PYTHONPATH"] = pwd |
| 73 | + |
| 74 | + |
| 75 | +@pytest.mark.parametrize("test_dir", test_dirs) |
| 76 | +def test_konfigs(test_dir): |
| 77 | + print(f"Testing {test_dir}") |
| 78 | + test_dir = Path(test_dir) |
| 79 | + kcl_file_name = test_dir / TEST_FILE |
| 80 | + ci_test_dir = test_dir / CI_TEST_DIR |
| 81 | + if not ci_test_dir.is_dir(): |
| 82 | + # Skip invalid test cases |
| 83 | + return |
| 84 | + golden_file = ci_test_dir / STDOUT_GOLDEN |
| 85 | + if not golden_file.is_file(): |
| 86 | + # Skip invalid test cases |
| 87 | + return |
| 88 | + kcl_command = ["kcl"] |
| 89 | + if has_settings_file(ci_test_dir): |
| 90 | + kcl_command.append("-Y") |
| 91 | + kcl_command.append(f"{CI_TEST_DIR}/{SETTINGS_FILE}") |
| 92 | + kcl_command.append(f"kcl.yaml") |
| 93 | + else: |
| 94 | + kcl_command.append(f"{TEST_FILE}") |
| 95 | + kcl_command.append("--target") |
| 96 | + kcl_command.append("native") |
| 97 | + process = subprocess.run( |
| 98 | + kcl_command, capture_output=True, cwd=test_dir, env=dict(os.environ) |
| 99 | + ) |
| 100 | + stdout, stderr = process.stdout, process.stderr |
| 101 | + print(f"STDOUT:\n{stdout.decode()}") |
| 102 | + assert ( |
| 103 | + process.returncode == 0 and len(stderr) == 0 |
| 104 | + ), f"Error executing file {kcl_file_name}.\nexit code = {process.returncode}\nstderr = {stderr}" |
| 105 | + if process.returncode == 0 and len(stderr) == 0: |
| 106 | + try: |
| 107 | + with open(golden_file, "r") as golden: |
| 108 | + compare_results(stdout.decode(), golden) |
| 109 | + except FileNotFoundError: |
| 110 | + raise Exception(f"Error reading expected result from file {golden_file}") |
0 commit comments