mirror of
https://github.com/LGUG2Z/komorebi.git
synced 2026-11-19 15:18:40 +01:00
feat(e2e): sync the neutral core from kfde d4f67164
This commit is contained in:
1 parent
97712f7510
commit
32af5164ef
3 files changed
+139
-8
No files matched your search
@@ -1,6 +1,7 @@
|
||||
"""Counted checks, so that one failure does not hide the ones after it."""
|
||||
|
||||
from collections.abc import Callable
|
||||
from collections.abc import Callable, Generator
|
||||
from contextlib import contextmanager
|
||||
|
||||
|
||||
class Checks:
|
||||
@@ -17,22 +18,39 @@ class Checks:
|
||||
self.failures: list[str] = []
|
||||
self.skipped: list[str] = []
|
||||
self.known: list[str] = []
|
||||
self.scope = ""
|
||||
"""Where the checks being made come from (`shared`, `kde`, `windows`), or empty. It prefixes
|
||||
the description of each check, and a subclass that records checks may read it."""
|
||||
|
||||
@contextmanager
|
||||
def scoped(self, scope: str) -> Generator[None]:
|
||||
"""Makes the checks inside the block those of `scope`, and puts the scope before it back
|
||||
after, so that scopes nest."""
|
||||
before, self.scope = self.scope, scope
|
||||
try:
|
||||
yield
|
||||
finally:
|
||||
self.scope = before
|
||||
|
||||
def labelled(self, description: str) -> str:
|
||||
"""The description with the scope in front of it, `[kde] ...`, when there is one."""
|
||||
return f"[{self.scope}] {description}" if self.scope else description
|
||||
|
||||
def ok(self, description: str) -> None:
|
||||
"""Records a check that passed."""
|
||||
self.passed += 1
|
||||
print(f" ok {description}")
|
||||
print(f" ok {self.labelled(description)}")
|
||||
|
||||
def bad(self, description: str) -> None:
|
||||
"""Records a check that failed."""
|
||||
self.failed += 1
|
||||
self.failures.append(description)
|
||||
print(f" FAIL {description}")
|
||||
self.failures.append(self.labelled(description))
|
||||
print(f" FAIL {self.labelled(description)}")
|
||||
|
||||
def skip(self, description: str, reason: str) -> None:
|
||||
"""Records a check that was not made, and why: the platform cannot do what it asks."""
|
||||
self.skipped.append(f"{description} ({reason})")
|
||||
print(f" skip {description}: {reason}")
|
||||
self.skipped.append(f"{self.labelled(description)} ({reason})")
|
||||
print(f" skip {self.labelled(description)}: {reason}")
|
||||
|
||||
def known_failure(self, description: str, condition: bool, reason: str) -> bool:
|
||||
"""Records a check that is known to fail, with the reason, to follow up on later.
|
||||
@@ -43,8 +61,8 @@ class Checks:
|
||||
if condition:
|
||||
self.bad(f"{description}: it holds now, so it is no longer a known failure")
|
||||
else:
|
||||
self.known.append(f"{description} ({reason})")
|
||||
print(f" known failure {description}: {reason}")
|
||||
self.known.append(f"{self.labelled(description)} ({reason})")
|
||||
print(f" known failure {self.labelled(description)}: {reason}")
|
||||
return condition
|
||||
|
||||
def check(self, description: str, condition: bool | Callable[[], bool]) -> bool:
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
"""A suite as a list: shared scenarios, and what a platform adds to them.
|
||||
|
||||
A suite file says what runs, in order, and each step labels the checks it makes: `shared` for a
|
||||
scenario that runs on every platform, and a platform's own name for what only that platform can
|
||||
check. The output reads `[shared] ...` and `[kde] ...`, and the same labels say, for each check,
|
||||
which port it belongs to.
|
||||
"""
|
||||
|
||||
from collections.abc import Callable
|
||||
from dataclasses import dataclass
|
||||
|
||||
from komorebi_vm.core.checks import Checks
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class Step:
|
||||
"""A thing to run, and the scope of the checks it makes."""
|
||||
|
||||
scope: str
|
||||
run: Callable[[], None]
|
||||
|
||||
|
||||
def shared(scenario: Callable[..., None], *args: object) -> Step:
|
||||
"""A scenario of `komorebi_vm.core.scenarios`, called with `args`."""
|
||||
return Step("shared", lambda: scenario(*args))
|
||||
|
||||
|
||||
def platform_only(scope: str, extra: Callable[..., None], *args: object) -> Step:
|
||||
"""What one platform adds: an extra of that platform, called with `args`, under its name."""
|
||||
return Step(scope, lambda: extra(*args))
|
||||
|
||||
|
||||
def run_suite(checks: Checks, *steps: Step) -> None:
|
||||
"""Runs the steps in order, each with its scope on `checks`."""
|
||||
for step in steps:
|
||||
with checks.scoped(step.scope):
|
||||
step.run()
|
||||
@@ -0,0 +1,76 @@
|
||||
import pytest
|
||||
|
||||
from komorebi_vm.core.checks import Checks
|
||||
from komorebi_vm.core.suite import platform_only, run_suite, shared
|
||||
|
||||
|
||||
def test_checks_are_labelled_with_the_scope_they_are_made_in(
|
||||
capsys: pytest.CaptureFixture[str],
|
||||
) -> None:
|
||||
checks = Checks()
|
||||
checks.check("plain", True)
|
||||
with checks.scoped("kde"):
|
||||
checks.check("labelled", True)
|
||||
checks.check("bad", False)
|
||||
checks.check("plain again", True)
|
||||
out = capsys.readouterr().out
|
||||
assert " ok plain\n" in out and " ok [kde] labelled\n" in out
|
||||
assert " ok plain again\n" in out
|
||||
assert checks.failures == ["[kde] bad"]
|
||||
|
||||
|
||||
def test_scopes_nest_and_the_outer_one_comes_back() -> None:
|
||||
checks = Checks()
|
||||
with checks.scoped("a"):
|
||||
with checks.scoped("b"):
|
||||
assert checks.scope == "b"
|
||||
assert checks.scope == "a"
|
||||
assert checks.scope == ""
|
||||
|
||||
|
||||
def test_the_scope_comes_back_when_the_block_raises() -> None:
|
||||
checks = Checks()
|
||||
with pytest.raises(RuntimeError), checks.scoped("kde"):
|
||||
raise RuntimeError
|
||||
assert checks.scope == ""
|
||||
|
||||
|
||||
def test_skips_and_known_failures_carry_the_scope() -> None:
|
||||
checks = Checks()
|
||||
with checks.scoped("windows"):
|
||||
checks.skip("x", "no pointer")
|
||||
checks.known_failure("y", False, "why")
|
||||
assert checks.skipped == ["[windows] x (no pointer)"]
|
||||
assert checks.known == ["[windows] y (why)"]
|
||||
|
||||
|
||||
def test_a_subclass_that_overrides_check_still_gets_the_scope() -> None:
|
||||
class Recorded(Checks):
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
self.seen: list[tuple[str, str]] = []
|
||||
|
||||
def check(self, description, condition): # noqa: ANN001, ANN201
|
||||
self.seen.append((self.scope, description))
|
||||
return super().check(description, condition)
|
||||
|
||||
checks = Recorded()
|
||||
run_suite(checks, shared(lambda c: c.check("one", True), checks))
|
||||
assert checks.seen == [("shared", "one")]
|
||||
|
||||
|
||||
def test_a_suite_runs_its_steps_in_order_each_in_its_own_scope() -> None:
|
||||
checks = Checks()
|
||||
order: list[str] = []
|
||||
|
||||
def scenario(name: str) -> None:
|
||||
order.append(f"{checks.scope}:{name}")
|
||||
|
||||
run_suite(
|
||||
checks,
|
||||
platform_only("kde", scenario, "first"),
|
||||
shared(scenario, "second"),
|
||||
platform_only("kde", scenario, "third"),
|
||||
)
|
||||
assert order == ["kde:first", "shared:second", "kde:third"]
|
||||
assert checks.scope == ""
|
||||
Reference in new issue
Block a user