|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Build fail-closed, layer-specific evidence from real Surefire XML reports. |
| 3 | +
|
| 4 | +This is not a code coverage percentage or a claim that OpenCLI websites work. |
| 5 | +Only explicitly selected suites contribute to a layer. No network is used. |
| 6 | +""" |
| 7 | +import argparse |
| 8 | +import json |
| 9 | +import pathlib |
| 10 | +import re |
| 11 | +import sys |
| 12 | +import xml.etree.ElementTree as ET |
| 13 | + |
| 14 | +LAYERS = ('enumeration', 'argv', 'protocol', 'typed-result', 'real-execution') |
| 15 | + |
| 16 | + |
| 17 | +def _suite_summary(suite): |
| 18 | + declared = {key: int(suite.attrib.get(key, '0')) for key in ('tests', 'failures', 'errors', 'skipped')} |
| 19 | + cases = suite.findall('testcase') |
| 20 | + actual = {'tests': len(cases), 'failures': sum(c.find('failure') is not None for c in cases), |
| 21 | + 'errors': sum(c.find('error') is not None for c in cases), |
| 22 | + 'skipped': sum(c.find('skipped') is not None for c in cases)} |
| 23 | + if declared != actual or any(v < 0 for v in declared.values()): |
| 24 | + raise ValueError('declared testcase counts disagree with XML evidence') |
| 25 | + actual['executed'] = actual['tests'] - actual['skipped'] |
| 26 | + return actual |
| 27 | + |
| 28 | + |
| 29 | +def build_report(reports_dir, *, head, branch, java_version, maven_version, exit_code, required_suites): |
| 30 | + """Return a JSON-serializable report; malformed/missing evidence fails closed.""" |
| 31 | + problems = [] |
| 32 | + if not re.fullmatch(r'[0-9a-fA-F]{40}', head or ''): |
| 33 | + problems.append('exact 40-character source HEAD is required') |
| 34 | + if not branch or not java_version.strip() or not maven_version.strip(): |
| 35 | + problems.append('branch and actual tool versions are required') |
| 36 | + if exit_code != 0: |
| 37 | + problems.append('verification command exited nonzero') |
| 38 | + if not required_suites or any(layer not in LAYERS or not names for layer, names in required_suites.items()): |
| 39 | + problems.append('non-empty required suites must use recognized layers') |
| 40 | + suites = {} |
| 41 | + for path in sorted(pathlib.Path(reports_dir).glob('TEST-*.xml')): |
| 42 | + try: |
| 43 | + root = ET.parse(path).getroot() |
| 44 | + nodes = [root] if root.tag == 'testsuite' else list(root.findall('testsuite')) |
| 45 | + if not nodes: |
| 46 | + raise ValueError('no testsuite') |
| 47 | + for suite in nodes: |
| 48 | + name = suite.attrib['name'] |
| 49 | + if name in suites: |
| 50 | + raise ValueError('duplicate testsuite') |
| 51 | + suites[name] = _suite_summary(suite) |
| 52 | + except (ET.ParseError, OSError, KeyError, ValueError): |
| 53 | + # Do not copy testcase failure bodies or captured application output. |
| 54 | + problems.append('invalid or duplicate Surefire report: ' + path.name) |
| 55 | + layers = {} |
| 56 | + for layer in LAYERS: |
| 57 | + names = list(required_suites.get(layer, [])) |
| 58 | + counts = dict.fromkeys(('tests', 'executed', 'skipped', 'failures', 'errors'), 0) |
| 59 | + missing = [name for name in names if name not in suites] |
| 60 | + for name in names: |
| 61 | + for key in counts: |
| 62 | + counts[key] += suites.get(name, {}).get(key, 0) |
| 63 | + state = 'NOT_RUN' |
| 64 | + if names: |
| 65 | + state = 'PASS' if (not missing and counts['executed'] > 0 and |
| 66 | + not any(counts[key] for key in ('skipped', 'failures', 'errors'))) else 'FAIL' |
| 67 | + layers[layer] = dict(counts, status=state, requiredSuites=names, missingSuites=missing) |
| 68 | + if state == 'FAIL': |
| 69 | + problems.append('required layer lacks passing, non-skipped execution: ' + layer) |
| 70 | + return {'schemaVersion': 1, 'status': 'FAIL' if problems else 'PASS', |
| 71 | + 'head': head, 'branch': branch, 'javaVersion': java_version, |
| 72 | + 'mavenVersion': maven_version, 'commandExitCode': exit_code, |
| 73 | + 'layers': layers, 'problems': problems, |
| 74 | + 'scope': 'Explicit Surefire suites only; synthetic argv probes are not live OpenCLI verification.'} |
| 75 | + |
| 76 | + |
| 77 | +def main(argv=None): |
| 78 | + parser = argparse.ArgumentParser(description=__doc__) |
| 79 | + parser.add_argument('--reports', type=pathlib.Path, required=True) |
| 80 | + parser.add_argument('--head', required=True) |
| 81 | + parser.add_argument('--branch', required=True) |
| 82 | + parser.add_argument('--java-version-file', type=pathlib.Path, required=True) |
| 83 | + parser.add_argument('--maven-version-file', type=pathlib.Path, required=True) |
| 84 | + parser.add_argument('--exit-code', type=int, required=True) |
| 85 | + parser.add_argument('--suite', action='append', required=True, help='layer=fully.qualified.TestClass; repeatable') |
| 86 | + parser.add_argument('--output', type=pathlib.Path, required=True) |
| 87 | + args = parser.parse_args(argv) |
| 88 | + required = {} |
| 89 | + for item in args.suite: |
| 90 | + layer, separator, name = item.partition('=') |
| 91 | + if not separator or layer not in LAYERS or not name: |
| 92 | + parser.error('--suite must be recognized-layer=fully.qualified.TestClass') |
| 93 | + if name in required.setdefault(layer, []): |
| 94 | + parser.error('duplicate --suite') |
| 95 | + required[layer].append(name) |
| 96 | + try: |
| 97 | + report = build_report(args.reports, head=args.head, branch=args.branch, |
| 98 | + java_version=args.java_version_file.read_text(encoding='utf-8'), |
| 99 | + maven_version=args.maven_version_file.read_text(encoding='utf-8'), |
| 100 | + exit_code=args.exit_code, required_suites=required) |
| 101 | + except OSError as exc: |
| 102 | + print('Missing tool-version evidence: ' + exc.__class__.__name__, file=sys.stderr) |
| 103 | + return 1 |
| 104 | + args.output.parent.mkdir(parents=True, exist_ok=True) |
| 105 | + args.output.write_text(json.dumps(report, ensure_ascii=False, indent=2) + '\n', encoding='utf-8') |
| 106 | + print(json.dumps({'status': report['status'], 'head': report['head'], 'layers': report['layers']})) |
| 107 | + return 0 if report['status'] == 'PASS' else 1 |
| 108 | + |
| 109 | + |
| 110 | +if __name__ == '__main__': |
| 111 | + sys.exit(main()) |
0 commit comments