|
| 1 | +import shutil |
| 2 | +from pathlib import Path |
| 3 | +from typing import Generator |
| 4 | + |
| 5 | +import pytest |
| 6 | +from testing_utils import ( |
| 7 | + BazelTools, |
| 8 | + BuildTools, |
| 9 | + LogContainer, |
| 10 | + Scenario, |
| 11 | + ScenarioResult, |
| 12 | +) |
| 13 | + |
| 14 | + |
| 15 | +def temp_dir_common( |
| 16 | + tmp_path_factory: pytest.TempPathFactory, base_name: str, *args: str |
| 17 | +) -> Generator[Path, None, None]: |
| 18 | + """ |
| 19 | + Create temporary directory and remove it after test. |
| 20 | + Common implementation to be reused by fixtures. |
| 21 | +
|
| 22 | + Returns generator providing numbered path to temporary directory. |
| 23 | + E.g., '<TMP_PATH>/<BASE_NAME>-<ARG1>-<ARG2><NUMBER>/'. |
| 24 | +
|
| 25 | + Parameters |
| 26 | + ---------- |
| 27 | + tmp_path_factory : pytest.TempPathFactory |
| 28 | + Factory for temporary directories. |
| 29 | + base_name : str |
| 30 | + Base directory name. |
| 31 | + 'self.__class__.__name__' use is recommended. |
| 32 | + *args : Any |
| 33 | + Other parameters to be included in directory name. |
| 34 | + """ |
| 35 | + parts = [base_name, *args] |
| 36 | + dir_name = "-".join(parts) |
| 37 | + dir_path = tmp_path_factory.mktemp(dir_name, numbered=True) |
| 38 | + yield dir_path |
| 39 | + shutil.rmtree(dir_path) |
| 40 | + |
| 41 | + |
| 42 | +class FitScenario(Scenario): |
| 43 | + """ |
| 44 | + CIT test scenario definition. |
| 45 | + """ |
| 46 | + |
| 47 | + @pytest.fixture(scope="class") |
| 48 | + def build_tools(self) -> BuildTools: |
| 49 | + return BazelTools(option_prefix="rust") |
| 50 | + |
| 51 | + def expect_command_failure(self, *args, **kwargs) -> bool: |
| 52 | + """ |
| 53 | + Expect command failure (e.g., non-zero return code or hang). |
| 54 | + """ |
| 55 | + return False |
| 56 | + |
| 57 | + @pytest.fixture(scope="class") |
| 58 | + def results( |
| 59 | + self, |
| 60 | + command: list[str], |
| 61 | + execution_timeout: float, |
| 62 | + *args, |
| 63 | + **kwargs, |
| 64 | + ) -> ScenarioResult: |
| 65 | + result = self._run_command(command, execution_timeout, args, kwargs) |
| 66 | + success = result.return_code == 0 and not result.hang |
| 67 | + if self.expect_command_failure() and success: |
| 68 | + raise RuntimeError(f"Command execution succeeded unexpectedly: {result=}") |
| 69 | + if not self.expect_command_failure() and not success: |
| 70 | + raise RuntimeError(f"Command execution failed unexpectedly: {result=}") |
| 71 | + return result |
| 72 | + |
| 73 | + @pytest.fixture(scope="class") |
| 74 | + def logs_target(self, target_path: Path, logs: LogContainer) -> LogContainer: |
| 75 | + """ |
| 76 | + Logs with messages generated strictly by the tested code. |
| 77 | +
|
| 78 | + Parameters |
| 79 | + ---------- |
| 80 | + target_path : Path |
| 81 | + Path to test scenario executable. |
| 82 | + logs : LogContainer |
| 83 | + Unfiltered logs. |
| 84 | + """ |
| 85 | + return logs.get_logs(field="target", pattern=f"{target_path.name}.*") |
| 86 | + |
| 87 | + @pytest.fixture(scope="class") |
| 88 | + def logs_info_level(self, logs_target: LogContainer) -> LogContainer: |
| 89 | + """ |
| 90 | + Logs with messages with INFO level. |
| 91 | +
|
| 92 | + Parameters |
| 93 | + ---------- |
| 94 | + logs_target : LogContainer |
| 95 | + Logs with messages generated strictly by the tested code. |
| 96 | + """ |
| 97 | + return logs_target.get_logs(field="level", value="INFO") |
| 98 | + |
| 99 | + @pytest.fixture(autouse=True) |
| 100 | + def print_to_report( |
| 101 | + self, |
| 102 | + request: pytest.FixtureRequest, |
| 103 | + logs: LogContainer, |
| 104 | + logs_target: LogContainer, |
| 105 | + ) -> None: |
| 106 | + """ |
| 107 | + Print traces to stdout. |
| 108 | +
|
| 109 | + Allowed "--traces" values: |
| 110 | + - "none" - show no traces. |
| 111 | + - "target" - show traces generated by test code. |
| 112 | + - "all" - show all traces. |
| 113 | +
|
| 114 | + Parameters |
| 115 | + ---------- |
| 116 | + request : FixtureRequest |
| 117 | + Test request built-in fixture. |
| 118 | + logs : LogContainer |
| 119 | + Test scenario execution logs. |
| 120 | + logs_target : LogContainer |
| 121 | + Logs with messages generated strictly by the tested code. |
| 122 | + """ |
| 123 | + traces_param = request.config.getoption("--traces") |
| 124 | + match traces_param: |
| 125 | + case "all": |
| 126 | + traces = logs |
| 127 | + case "target": |
| 128 | + traces = logs_target |
| 129 | + case "none": |
| 130 | + traces = LogContainer() |
| 131 | + case _: |
| 132 | + raise RuntimeError(f'Invalid "--traces" value: {traces_param}') |
| 133 | + |
| 134 | + for trace in traces: |
| 135 | + print(trace) |
0 commit comments