|
| 1 | +#!env python |
| 2 | +import sys |
| 3 | +from time import sleep |
| 4 | +from typing import Any, NotRequired, TypedDict |
| 5 | + |
| 6 | +from e3.testsuite import Log, ParsedTest, TestResult, TestStatus |
| 7 | +from e3.testsuite import Testsuite as Suite |
| 8 | +from e3.testsuite.driver import TestDriver |
| 9 | + |
| 10 | + |
| 11 | +class BareDriver(TestDriver): |
| 12 | + def add_test(self, dag): |
| 13 | + self.add_fragment(dag, "run") |
| 14 | + |
| 15 | + def run(self, prev: dict[str, Any], slot: int) -> None: |
| 16 | + self.test_env: TestEnv # type: ignore |
| 17 | + sleep(2) |
| 18 | + |
| 19 | + for r in self.test_env.get("results", []): |
| 20 | + result = TestResult( |
| 21 | + self.test_name + ("." + r.get("name", "") if r.get("name") else ""), |
| 22 | + status=r["status"], |
| 23 | + msg=r.get("msg", ""), |
| 24 | + ) |
| 25 | + |
| 26 | + for k in ("log", "out", "expected"): |
| 27 | + if k in r: |
| 28 | + setattr(result, k, Log(r[k])) # type: ignore |
| 29 | + |
| 30 | + self.push_result(result) |
| 31 | + |
| 32 | + |
| 33 | +class TestResultSpec(TypedDict): |
| 34 | + name: NotRequired[str] |
| 35 | + status: TestStatus |
| 36 | + msg: NotRequired[str] |
| 37 | + log: NotRequired[str] |
| 38 | + out: NotRequired[str] |
| 39 | + expected: NotRequired[str] |
| 40 | + |
| 41 | + |
| 42 | +class TestEnv(TypedDict): |
| 43 | + results: NotRequired[list[TestResultSpec]] |
| 44 | + |
| 45 | + |
| 46 | +class Testsuite(Suite): |
| 47 | + @property |
| 48 | + def test_driver_map(self) -> dict[str, type[TestDriver]]: |
| 49 | + return {"bare": BareDriver} |
| 50 | + |
| 51 | + @property |
| 52 | + def default_driver(self) -> str: |
| 53 | + return "bare" |
| 54 | + |
| 55 | + def get_test_list(self, sublist): |
| 56 | + def test(name: str, env: TestEnv): |
| 57 | + return ParsedTest(name, None, env, ".", name) # type: ignore |
| 58 | + |
| 59 | + all_tests = [ |
| 60 | + test( |
| 61 | + "01-test-one-result", |
| 62 | + { |
| 63 | + "results": [ |
| 64 | + { |
| 65 | + "status": TestStatus.PASS, |
| 66 | + } |
| 67 | + ] |
| 68 | + }, |
| 69 | + ), |
| 70 | + test( |
| 71 | + "02-test-multiple-results", |
| 72 | + { |
| 73 | + "results": [ |
| 74 | + { |
| 75 | + "name": "sub1", |
| 76 | + "status": TestStatus.PASS, |
| 77 | + }, |
| 78 | + { |
| 79 | + "name": "sub2", |
| 80 | + "status": TestStatus.FAIL, |
| 81 | + "msg": "Failure message", |
| 82 | + }, |
| 83 | + { |
| 84 | + "name": "sub3", |
| 85 | + "status": TestStatus.FAIL, |
| 86 | + "msg": "Failure message", |
| 87 | + "log": "Long\nExecution\nLog", |
| 88 | + }, |
| 89 | + { |
| 90 | + "status": TestStatus.PASS, |
| 91 | + }, |
| 92 | + ] |
| 93 | + }, |
| 94 | + ), |
| 95 | + test( |
| 96 | + "03-test-multiple-passing-results", |
| 97 | + { |
| 98 | + "results": [ |
| 99 | + { |
| 100 | + "name": "sub1", |
| 101 | + "status": TestStatus.PASS, |
| 102 | + }, |
| 103 | + { |
| 104 | + "name": "sub2", |
| 105 | + "status": TestStatus.PASS, |
| 106 | + }, |
| 107 | + { |
| 108 | + "name": "sub3", |
| 109 | + "status": TestStatus.PASS, |
| 110 | + }, |
| 111 | + { |
| 112 | + "status": TestStatus.PASS, |
| 113 | + }, |
| 114 | + ] |
| 115 | + }, |
| 116 | + ), |
| 117 | + test( |
| 118 | + "04-test-only-passing-sub-results", |
| 119 | + { |
| 120 | + "results": [ |
| 121 | + { |
| 122 | + "name": "sub1", |
| 123 | + "status": TestStatus.PASS, |
| 124 | + }, |
| 125 | + { |
| 126 | + "name": "sub2", |
| 127 | + "status": TestStatus.PASS, |
| 128 | + }, |
| 129 | + { |
| 130 | + "name": "sub3", |
| 131 | + "status": TestStatus.PASS, |
| 132 | + }, |
| 133 | + ] |
| 134 | + }, |
| 135 | + ), |
| 136 | + test( |
| 137 | + "05-test-only-sub-results-one-failing", |
| 138 | + { |
| 139 | + "results": [ |
| 140 | + { |
| 141 | + "name": "sub1", |
| 142 | + "status": TestStatus.PASS, |
| 143 | + }, |
| 144 | + { |
| 145 | + "name": "sub2", |
| 146 | + "status": TestStatus.FAIL, |
| 147 | + }, |
| 148 | + { |
| 149 | + "name": "sub3", |
| 150 | + "status": TestStatus.PASS, |
| 151 | + }, |
| 152 | + ] |
| 153 | + }, |
| 154 | + ), |
| 155 | + test( |
| 156 | + "06-test-with-diff", |
| 157 | + { |
| 158 | + "results": [ |
| 159 | + { |
| 160 | + "status": TestStatus.FAIL, |
| 161 | + "msg": "Failure short message", |
| 162 | + "out": "Actual\nOutput\nText", |
| 163 | + "expected": "Expected\nOutput\nText content", |
| 164 | + "log": "Long\nExecution\nLog", |
| 165 | + } |
| 166 | + ] |
| 167 | + }, |
| 168 | + ), |
| 169 | + test("07-test-with-no-results", {}), |
| 170 | + ] |
| 171 | + |
| 172 | + return ( |
| 173 | + all_tests |
| 174 | + if len(sublist) == 0 |
| 175 | + else [t for t in all_tests if t.test_name in sublist] |
| 176 | + ) |
| 177 | + |
| 178 | + |
| 179 | +if __name__ == "__main__": |
| 180 | + sys.exit(Testsuite().testsuite_main()) |
0 commit comments