|
| 1 | +""" |
| 2 | +The purpose of this module is to report e3-testsuite events to the Ada & SPARK VS Code |
| 3 | +extension. |
| 4 | +
|
| 5 | +It provides a callback to be used with the e3-testsuite notification system via the |
| 6 | +--notify-events CLI argument. See the documentation of the |
| 7 | +`e3.testsuite.event_notifications` module for more information about that system. |
| 8 | +
|
| 9 | +Upon receiving e3-testsuite events, the callback prints them to stdout, as JSON objects |
| 10 | +of a specific structure expected by the Ada & SPARK VS Code extension. That structure is |
| 11 | +defined by the `TestsuiteNotification` type in `e3TestsuiteNotifications.ts`. |
| 12 | +""" |
| 13 | + |
| 14 | +import json |
| 15 | +import os |
| 16 | +import sys |
| 17 | +from typing import Literal, NotRequired, TypedDict |
| 18 | + |
| 19 | +from e3.testsuite import Testsuite |
| 20 | +from e3.testsuite import event_notifications as EN |
| 21 | +from e3.testsuite.event_notifications import TestNotification |
| 22 | + |
| 23 | +NotificationType = Literal["queue", "start", "result", "end"] |
| 24 | + |
| 25 | + |
| 26 | +class Event(TypedDict): |
| 27 | + kind: NotificationType |
| 28 | + testName: str |
| 29 | + resultPath: NotRequired[str] |
| 30 | + |
| 31 | + |
| 32 | +def callback(e: TestNotification): |
| 33 | + if isinstance(e, EN.TestQueueNotification): |
| 34 | + v = Event(kind="queue", testName=e.test_name) |
| 35 | + elif isinstance(e, EN.TestStartNotification): |
| 36 | + v = Event(kind="start", testName=e.test_name) |
| 37 | + elif isinstance(e, EN.TestResultNotification): |
| 38 | + v = Event( |
| 39 | + kind="result", testName=e.test_name, resultPath=e.yaml_result_filename |
| 40 | + ) |
| 41 | + elif isinstance(e, EN.TestEndNotification): |
| 42 | + v = Event(kind="end", testName=e.test_name) |
| 43 | + else: |
| 44 | + v = None |
| 45 | + |
| 46 | + if v is not None: |
| 47 | + json.dump(v, sys.stdout) |
| 48 | + sys.stdout.write(os.linesep) |
| 49 | + sys.stdout.flush() |
| 50 | + |
| 51 | + |
| 52 | +def init_callback(ts: Testsuite): |
| 53 | + return callback |
0 commit comments