|
| 1 | +# This runner only supports recording tests |
| 2 | +# For subsetting, use 'ng' test runner instead |
| 3 | +# It's possible to use 'karma' runner for recording, and 'ng' runner for subsetting, for the same test session |
| 4 | +import json |
| 5 | +from typing import Annotated, Dict, Generator, List |
| 6 | + |
| 7 | +import click |
| 8 | + |
| 9 | +from ..args4p import typer |
| 10 | +from ..commands.record.case_event import CaseEvent |
| 11 | +from ..testpath import TestPath |
| 12 | +from . import smart_tests |
| 13 | + |
| 14 | + |
| 15 | +@smart_tests.subset |
| 16 | +def subset(client, _with: Annotated[str | None, typer.Option( |
| 17 | + '--with', help="Specify 'ng' to use the Angular test runner for subsetting")] = None, ): |
| 18 | + # TODO: implement the --with ng option |
| 19 | + |
| 20 | + # read lines as test file names |
| 21 | + for t in client.stdin(): |
| 22 | + client.test_path(t.rstrip("\n")) |
| 23 | + |
| 24 | + client.run() |
| 25 | + |
| 26 | + |
| 27 | +@smart_tests.record.tests |
| 28 | +def record_tests(client, |
| 29 | + reports: Annotated[List[str], typer.Argument(multiple=True, help="Test report files to process")], |
| 30 | + ): |
| 31 | + client.parse_func = JSONReportParser(client).parse_func |
| 32 | + |
| 33 | + for r in reports: |
| 34 | + client.report(r) |
| 35 | + |
| 36 | + client.run() |
| 37 | + |
| 38 | + |
| 39 | +class JSONReportParser: |
| 40 | + """ |
| 41 | + Sample Karma report format: |
| 42 | + { |
| 43 | + "browsers": {...}, |
| 44 | + "result": { |
| 45 | + "24461741": [ |
| 46 | + { |
| 47 | + "fullName": "path/to/spec.ts should do something", |
| 48 | + "description": "should do something", |
| 49 | + "id": "spec0", |
| 50 | + "log": [], |
| 51 | + "skipped": false, |
| 52 | + "disabled": false, |
| 53 | + "pending": false, |
| 54 | + "success": true, |
| 55 | + "suite": [ |
| 56 | + "path/to/spec.ts" |
| 57 | + ], |
| 58 | + "time": 92, |
| 59 | + "executedExpectationsCount": 1, |
| 60 | + "passedExpectations": [...], |
| 61 | + "properties": null |
| 62 | + } |
| 63 | + ] |
| 64 | + }, |
| 65 | + "summary": {...} |
| 66 | + } |
| 67 | + """ |
| 68 | + |
| 69 | + def __init__(self, client): |
| 70 | + self.client = client |
| 71 | + |
| 72 | + def parse_func(self, report_file: str) -> Generator[Dict, None, None]: # type: ignore |
| 73 | + data: Dict |
| 74 | + with open(report_file, 'r') as json_file: |
| 75 | + try: |
| 76 | + data = json.load(json_file) |
| 77 | + except Exception: |
| 78 | + click.echo( |
| 79 | + click.style("Error: Failed to load Json report file: {}".format(report_file), fg='red'), err=True) |
| 80 | + return |
| 81 | + |
| 82 | + if not self._validate_report_format(data): |
| 83 | + click.echo( |
| 84 | + "Error: {} does not appear to be valid Karma report format. " |
| 85 | + "Make sure you are using karma-json-reporter or a compatible reporter.".format( |
| 86 | + report_file), err=True) |
| 87 | + return |
| 88 | + |
| 89 | + results = data.get("result", {}) |
| 90 | + for browser_id, specs in results.items(): |
| 91 | + if isinstance(specs, list): |
| 92 | + for event in self._parse_specs(specs): |
| 93 | + yield event |
| 94 | + |
| 95 | + def _validate_report_format(self, data: Dict) -> bool: |
| 96 | + if not isinstance(data, dict): |
| 97 | + return False |
| 98 | + |
| 99 | + if "result" not in data: |
| 100 | + return False |
| 101 | + |
| 102 | + results = data.get("result", {}) |
| 103 | + if not isinstance(results, dict): |
| 104 | + return False |
| 105 | + |
| 106 | + for browser_id, specs in results.items(): |
| 107 | + if not isinstance(specs, list): |
| 108 | + return False |
| 109 | + |
| 110 | + for spec in specs: |
| 111 | + if not isinstance(spec, dict): |
| 112 | + return False |
| 113 | + # Check for required fields |
| 114 | + if "suite" not in spec or "time" not in spec: |
| 115 | + return False |
| 116 | + # Field suite should have at least one element (filename) |
| 117 | + suite = spec.get("suite", []) |
| 118 | + if not isinstance(suite, list) or len(suite) == 0: |
| 119 | + return False |
| 120 | + |
| 121 | + return True |
| 122 | + |
| 123 | + def _parse_specs(self, specs: List[Dict]) -> List[Dict]: |
| 124 | + events: List[Dict] = [] |
| 125 | + |
| 126 | + for spec in specs: |
| 127 | + # TODO: |
| 128 | + # In NextWorld, test filepaths are included in the suite tag |
| 129 | + # But generally in a Karma test report, a suite tag can be any string |
| 130 | + # For the time being let's get filepaths from the suite tag, |
| 131 | + # until we find a standard way to include filepaths in the test reports |
| 132 | + suite = spec.get("suite", []) |
| 133 | + filename = suite[0] if suite else "" |
| 134 | + |
| 135 | + test_path: TestPath = [ |
| 136 | + self.client.make_file_path_component(filename), |
| 137 | + {"type": "testcase", "name": spec.get("fullName", spec.get("description", ""))} |
| 138 | + ] |
| 139 | + |
| 140 | + duration_msec = spec.get("time", 0) |
| 141 | + status = self._case_event_status_from_spec(spec) |
| 142 | + stderr = self._parse_stderr(spec) |
| 143 | + |
| 144 | + events.append(CaseEvent.create( |
| 145 | + test_path=test_path, |
| 146 | + duration_secs=duration_msec / 1000 if duration_msec else 0, |
| 147 | + status=status, |
| 148 | + stderr=stderr |
| 149 | + )) |
| 150 | + |
| 151 | + return events |
| 152 | + |
| 153 | + def _case_event_status_from_spec(self, spec: Dict) -> int: |
| 154 | + if spec.get("skipped", False) or spec.get("disabled", False) or spec.get("pending", False): |
| 155 | + return CaseEvent.TEST_SKIPPED |
| 156 | + |
| 157 | + if spec.get("success", False): |
| 158 | + return CaseEvent.TEST_PASSED |
| 159 | + else: |
| 160 | + return CaseEvent.TEST_FAILED |
| 161 | + |
| 162 | + def _parse_stderr(self, spec: Dict) -> str: |
| 163 | + log_messages = spec.get("log", []) |
| 164 | + if not log_messages: |
| 165 | + return "" |
| 166 | + |
| 167 | + return "\n".join(str(msg) for msg in log_messages if msg) |
0 commit comments