-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoverage_report.py
More file actions
1219 lines (1057 loc) · 47.6 KB
/
coverage_report.py
File metadata and controls
1219 lines (1057 loc) · 47.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""ptyunit/coverage_report.py — Parse bash xtrace output and generate coverage reports.
Reads a trace file produced by coverage.sh (PS4='+${BASH_SOURCE}:${LINENO} ')
and source files from --src to produce line-level coverage reports.
Usage:
python3 coverage_report.py --trace <file> --src <dir> [--format text|json|html] [--min N]
"""
import argparse
import datetime
import fnmatch
import glob
import json
import os
import re
import subprocess
import sys
from collections import defaultdict
from pathlib import Path
# Matches bash function declaration lines — never executed by set -x, only definitions.
# Handles: name() { name () function name { function name() {
_FUNC_DEF_RE = re.compile(
r'^function\s+[a-zA-Z_]\w*(\s*\(\))?\s*\{?\s*$'
r'|^[a-zA-Z_]\w*\s*\(\)\s*\{?\s*$'
)
_BRANCH_RE = re.compile(
r'\bif\b|\belif\b|\bwhile\b|\bfor\b|\buntil\b|\bcase\b|\|\||\&\&'
)
# Matches a bash case branch label line — patterns separated by | followed by ).
# Allows glob chars (*, ?, -, _, .) but excludes = (assignments) and $( (subshells).
# Handles an optional trailing ;; for single-line branches like pretty|tap|junit) ;;
_CASE_LABEL_RE = re.compile(r'^[^=$(]*\)\s*(?:;;)?\s*$')
_BASH_KEYWORDS = frozenset({
'if', 'then', 'else', 'elif', 'fi', 'for', 'while', 'do', 'done',
'case', 'esac', 'in', 'function', 'return', 'local', 'export',
'declare', 'readonly', 'break', 'continue', 'exit', 'source',
'select', 'until', 'shift', 'unset', 'set', 'true', 'false',
'echo', 'printf', 'read', 'exec', 'eval',
})
def _ptyunit_version() -> str:
try:
return (Path(__file__).parent / 'VERSION').read_text().strip()
except (IOError, OSError):
return 'unknown'
def _pct_color(pct: float) -> str:
"""Return a CSS color string for a coverage percentage."""
if pct >= 90:
return '#4caf50'
if pct >= 80:
return '#8bc34a'
if pct >= 60:
return '#ff9800'
return '#f44336'
def _highlight_bash(text: str) -> str:
"""Tokenize a bash source line and return syntax-highlighted HTML."""
out = []
i = 0
n = len(text)
while i < n:
c = text[i]
# Comment: # to end of line
if c == '#':
out.append(f'<span class="hc">{_esc(text[i:])}</span>')
break
# Single-quoted string
if c == "'":
j = text.find("'", i + 1)
j = j + 1 if j != -1 else n
out.append(f'<span class="hs">{_esc(text[i:j])}</span>')
i = j
continue
# Double-quoted string
if c == '"':
j = i + 1
while j < n:
if text[j] == '\\':
j += 2
elif text[j] == '"':
j += 1
break
else:
j += 1
out.append(f'<span class="hs">{_esc(text[i:j])}</span>')
i = j
continue
# Backtick command substitution
if c == '`':
j = text.find('`', i + 1)
j = j + 1 if j != -1 else n
out.append(f'<span class="hv">{_esc(text[i:j])}</span>')
i = j
continue
# Variable: $VAR ${...} $(...)
if c == '$':
j = i + 1
if j < n:
nc = text[j]
if nc == '{':
depth, j = 1, j + 1
while j < n and depth:
if text[j] == '{':
depth += 1
elif text[j] == '}':
depth -= 1
j += 1
elif nc == '(':
depth, j = 1, j + 1
while j < n and depth:
if text[j] == '(':
depth += 1
elif text[j] == ')':
depth -= 1
j += 1
elif nc.isalpha() or nc == '_':
while j < n and (text[j].isalnum() or text[j] == '_'):
j += 1
elif nc in '#?@*!-':
j += 1
elif nc.isdigit():
j += 1
out.append(f'<span class="hv">{_esc(text[i:j])}</span>')
i = j
continue
# Word — keyword or plain identifier
if c.isalpha() or c == '_':
j = i
while j < n and (text[j].isalnum() or text[j] in '_-'):
j += 1
word = text[i:j]
if word in _BASH_KEYWORDS:
out.append(f'<span class="hk">{_esc(word)}</span>')
else:
out.append(_esc(word))
i = j
continue
out.append(_esc(c))
i += 1
return ''.join(out)
def _file_anchor(relative: str) -> str:
"""Sanitize a relative file path into a valid HTML id."""
return re.sub(r'[^a-zA-Z0-9]', '-', relative)
def _esc(s: str) -> str:
"""HTML-escape a string."""
return s.replace('&', '&').replace('<', '<').replace('>', '>').replace('"', '"')
# ── Source analysis ────────────────────────────────────────────────────────────
_TEST_DIRS = frozenset({'tests', 'test', 'self-tests', 'self_tests', 'spec', 'specs'})
# Lines (or blocks) annotated with this pragma are excluded from the executable
# set entirely — neither hit nor miss. Two forms:
# Standalone comment: # @pty_skip → excludes all lines until the
# block closes (same or lower indent)
# Inline: some code # @pty_skip → excludes just that one line
_NOCOVER_PRAGMA = re.compile(r'#\s*@pty_skip\b')
_BLOCK_CLOSERS = frozenset({'fi', 'done', 'esac', ';;', ')', '}'})
# Matches the terminator word in a heredoc opener: << WORD, <<'WORD', <<- WORD, etc.
_HEREDOC_RE = re.compile(r'<<-?\s*[\'"]?([A-Za-z_][A-Za-z0-9_]*)[\'"]?\s*$')
def _load_coverageignore(src_dir: str) -> list:
"""Return glob patterns from .coverageignore in src_dir (or its parent)."""
patterns = []
for search in (src_dir, os.path.dirname(src_dir)):
ignore_file = os.path.join(search, '.coverageignore')
if os.path.isfile(ignore_file):
with open(ignore_file) as f:
for line in f:
line = line.strip()
if line and not line.startswith('#'):
patterns.append(line)
break
return patterns
def _is_ignored(filepath: str, src_dir: str, patterns: list) -> bool:
"""Return True if filepath matches any .coverageignore pattern."""
rel = os.path.relpath(filepath, src_dir)
for pat in patterns:
# Strip trailing slash — used to denote directories in .coverageignore
pat = pat.rstrip('/')
if fnmatch.fnmatch(rel, pat):
return True
# Also match files inside a directory pattern (e.g. "bench" matches "bench/foo.sh")
if fnmatch.fnmatch(rel.split(os.sep)[0], pat):
return True
return False
def find_source_files(src_dir: str) -> list:
"""Find all .sh files under src_dir, skipping test directories, test files, and .coverageignore patterns."""
ignore_patterns = _load_coverageignore(src_dir)
files = []
for root, dirs, names in os.walk(src_dir):
dirs[:] = [d for d in dirs if d not in _TEST_DIRS]
for name in names:
if name.endswith('.sh') and not name.startswith('test-') and not name.endswith('-test.sh'):
filepath = os.path.join(root, name)
if not _is_ignored(filepath, src_dir, ignore_patterns):
files.append(filepath)
return sorted(files)
def count_source_lines(filepath: str) -> dict:
"""Return {line_number: line_content} for executable lines.
Lines annotated with # @pty_skip are excluded from the executable set.
Two forms:
Standalone comment → block skip: excludes all following lines until a
block-closer (fi/done/esac/}/)) at the same or
lower indentation level.
Inline on code line → single-line skip: excludes only that line.
"""
executable = {}
skip_indent = None # set to indent level when inside a @pty_skip block
heredoc_end = None # set to terminator word when inside a heredoc
try:
with open(filepath, 'r', errors='replace') as f:
for i, line in enumerate(f, 1):
stripped = line.strip()
if not stripped:
continue
# ── Heredoc content: skip until terminator ────────────────
if heredoc_end is not None:
if line.rstrip('\n') == heredoc_end or line.strip() == heredoc_end:
heredoc_end = None
continue # skip content and terminator lines
indent = len(line) - len(line.lstrip())
# ── @pty_skip block: check for end-of-block ───────────────
if skip_indent is not None:
if indent <= skip_indent and stripped in _BLOCK_CLOSERS:
skip_indent = None # block closed; resume normal counting
continue # line is inside skip block (or the closer itself)
# ── Pure comment line ─────────────────────────────────────
if stripped.startswith('#'):
if _NOCOVER_PRAGMA.search(stripped):
skip_indent = indent # begin block skip
continue
# ── Structural keywords (never executable) ────────────────
if stripped in ('{', '}', 'fi', 'do', 'done', 'then', 'else',
'elif', 'esac', ';;', ')', ';;)', '*)'):
continue
if _FUNC_DEF_RE.match(stripped):
continue
# ── Inline @pty_skip pragma on a code line ────────────────
if _NOCOVER_PRAGMA.search(line):
continue
# ── Detect heredoc opener ─────────────────────────────────
m = _HEREDOC_RE.search(stripped)
if m:
heredoc_end = m.group(1)
executable[i] = stripped
except (IOError, OSError):
pass
return executable
# ── App detection ──────────────────────────────────────────────────────────────
def detect_app_info(src_dir: str) -> dict:
"""Detect application name and version from project metadata."""
name = None
version = None
search_dirs = [src_dir]
parent = os.path.dirname(os.path.realpath(src_dir))
if parent and parent != os.path.realpath(src_dir):
search_dirs.append(parent)
for d in search_dirs:
ver_file = os.path.join(d, 'VERSION')
if os.path.isfile(ver_file):
try:
v = Path(ver_file).read_text().strip()
if v:
version = version or v
break
except (IOError, OSError):
pass
for d in search_dirs:
pkg = os.path.join(d, 'package.json')
if os.path.isfile(pkg):
try:
data = json.loads(Path(pkg).read_text())
name = name or data.get('name')
version = version or data.get('version')
except (json.JSONDecodeError, IOError, OSError):
pass
if not name:
try:
result = subprocess.run(
['git', 'remote', 'get-url', 'origin'],
capture_output=True, text=True, cwd=src_dir, timeout=3
)
if result.returncode == 0:
m = re.search(r'[:/]([^/]+)/([^/]+?)(?:\.git)?$', result.stdout.strip())
if m:
name = m.group(2)
except Exception:
pass
if not name:
name = os.path.basename(os.path.realpath(src_dir)) or 'project'
return {'name': name, 'version': version}
# ── Language analysis ──────────────────────────────────────────────────────────
_LANG_COLORS = {
'.sh': ('#4ec94e', 'Shell'),
'.bash': ('#89e051', 'Bash'),
'.py': ('#3572a5', 'Python'),
'.rb': ('#701516', 'Ruby'),
'.js': ('#f1e05a', 'JavaScript'),
'.ts': ('#2b7489', 'TypeScript'),
'.go': ('#00add8', 'Go'),
'.rs': ('#dea584', 'Rust'),
}
_LANG_DEFAULT_COLOR = '#666'
def detect_languages(results: list) -> list:
"""Tally executable line counts by file extension.
Returns list of {lang, color, pct} sorted by pct descending.
"""
totals = defaultdict(int)
grand_total = 0
for r in results:
ext = os.path.splitext(r['relative'])[1].lower()
totals[ext] += r['total']
grand_total += r['total']
if grand_total == 0:
return []
out = []
for ext, count in sorted(totals.items(), key=lambda x: -x[1]):
color, lang = _LANG_COLORS.get(
ext, (_LANG_DEFAULT_COLOR, ext.lstrip('.').capitalize() or 'Other')
)
pct = round(count / grand_total * 100, 1)
out.append({'lang': lang, 'color': color, 'pct': pct})
return out
# ── Cyclomatic complexity ──────────────────────────────────────────────────────
def compute_complexity(filepath: str) -> int:
"""Estimate cyclomatic complexity for a bash file.
Counts branching constructs: if, elif, while, for, until, case, &&, ||.
Returns score >= 1 (baseline = 1).
"""
score = 1
try:
with open(filepath, 'r', errors='replace') as f:
for line in f:
stripped = line.strip()
if stripped.startswith('#'):
continue
score += len(_BRANCH_RE.findall(stripped))
except (IOError, OSError):
pass
return score
def _complexity_badge(score: int) -> str:
"""Return an HTML badge for a complexity score."""
if score <= 5:
label, cls = 'Low', 'cx-low'
elif score <= 10:
label, cls = 'Med', 'cx-med'
elif score <= 20:
label, cls = 'High', 'cx-high'
else:
label, cls = 'Crit', 'cx-crit'
return f'<span class="cx {cls}" title="Cyclomatic complexity ≈ {score}">{label}</span>'
# ── Folder grouping ────────────────────────────────────────────────────────────
def group_by_folder(results: list) -> list:
"""Group file results by directory path, sorted alphabetically.
Returns list of: {folder, files, hit, total, pct}
Empty string folder = root-level files.
"""
folders = defaultdict(list)
for r in results:
parts = r['relative'].split('/')
folder = '/'.join(parts[:-1]) if len(parts) > 1 else ''
folders[folder].append(r)
out = []
for folder in sorted(folders.keys()):
files = folders[folder]
hit = sum(f['hit'] for f in files)
total = sum(f['total'] for f in files)
pct = round(hit / total * 100, 1) if total else 0.0
out.append({'folder': folder, 'files': files, 'hit': hit, 'total': total, 'pct': pct})
return out
# ── Cross-run comparison ───────────────────────────────────────────────────────
def load_previous_stats(coverage_dir: str, current_ts: str):
"""Find and load the most recent JSON sidecar strictly before current_ts."""
pattern = os.path.join(coverage_dir, '????_??_??_??_??_??.json')
candidates = [
f for f in glob.glob(pattern)
if os.path.basename(f)[:-5] < current_ts
]
if not candidates:
return None
try:
return json.loads(Path(max(candidates)).read_text())
except (IOError, OSError, json.JSONDecodeError):
return None
def save_stats_json(coverage_dir: str, timestamp: str, results: list) -> None:
"""Write a JSON sidecar alongside the HTML report for future diff."""
total_hit = sum(r['hit'] for r in results)
total_total = sum(r['total'] for r in results)
data = {
'timestamp': timestamp,
'total_hit': total_hit,
'total_total': total_total,
'total_pct': round(total_hit / total_total * 100, 1) if total_total else 0.0,
'files': {
r['relative']: {'hit': r['hit'], 'total': r['total'], 'pct': r['pct']}
for r in results
},
}
path = os.path.join(coverage_dir, f'{timestamp}.json')
try:
with open(path, 'w') as fh:
json.dump(data, fh, indent=2)
except (IOError, OSError):
pass
def _delta_html(prev_pct: float, curr_pct: float, data_only: bool = False) -> str:
"""Return an HTML delta badge comparing prev and current coverage %."""
delta = curr_pct - prev_pct
if abs(delta) < 0.05:
if data_only:
return '0'
return '<span class="d d-flat" title="No change from previous run" data-val="0">±0</span>'
if delta > 0:
if data_only:
return f'{delta:.2f}'
return f'<span class="d d-up" data-val="{delta:.2f}">▲ +{delta:.1f}%</span>'
if data_only:
return f'{delta:.2f}'
return f'<span class="d d-dn" data-val="{delta:.2f}">▼ {delta:.1f}%</span>'
def _threshold_note(pct: float, delta=None) -> str:
"""Return a short interpretation of coverage quality."""
if pct < 60:
note = 'Below minimum threshold — significant coverage gaps'
elif pct < 80:
note = f'Below industry standard (80% baseline)'
elif pct < 90:
note = 'Meets baseline (80%)'
else:
note = 'Strong coverage (≥ 90%)'
if delta is not None and delta < -5:
note += f' · ⚠ Regression: {delta:+.1f}%'
return note
# ── Trace parsing + coverage ───────────────────────────────────────────────────
def parse_trace(trace_path: str, src_dir: str) -> dict:
"""Parse xtrace output and return {absolute_filepath: {line_numbers_hit}}."""
hits = defaultdict(set)
pattern = re.compile(r'^\++(.+?):(\d+)\s')
src_prefix = os.path.realpath(src_dir)
try:
with open(trace_path, 'r', errors='replace') as f:
for line in f:
m = pattern.match(line)
if not m:
continue
filepath = m.group(1)
lineno = int(m.group(2))
try:
abs_path = os.path.realpath(filepath)
except (ValueError, OSError):
continue
if abs_path.startswith(src_prefix):
hits[abs_path].add(lineno)
except (IOError, OSError) as e:
print(f'coverage: error reading trace: {e}', file=sys.stderr)
return dict(hits)
def _apply_case_label_hits(executable: dict, file_hits: set) -> set:
"""Return expanded hit set where case branch labels inherit coverage from
their first body line.
A case label on line N (e.g. ``-h|--help)``) is marked hit when the next
executable line after N is already in file_hits. This reflects reality:
bash's PS4 tracer never emits a trace for the label itself, only for the
commands inside the branch.
"""
exec_sorted = sorted(executable)
expanded = set(file_hits)
for idx, lineno in enumerate(exec_sorted):
if lineno in expanded:
continue
if not _CASE_LABEL_RE.match(executable[lineno]):
continue
if idx + 1 < len(exec_sorted) and exec_sorted[idx + 1] in file_hits:
expanded.add(lineno)
return expanded
def compute_coverage(src_dir: str, hits: dict) -> list:
"""Compute per-file coverage stats."""
results = []
for filepath in find_source_files(src_dir):
abs_path = os.path.realpath(filepath)
executable = count_source_lines(abs_path)
total = len(executable)
if total == 0:
continue
file_hits = _apply_case_label_hits(executable, hits.get(abs_path, set()))
hit = len(file_hits & set(executable.keys()))
missed = total - hit
pct = round((hit / total * 100) if total > 0 else 0, 1)
missed_lines = sorted(set(executable.keys()) - file_hits)
try:
rel = os.path.relpath(abs_path, src_dir)
except ValueError:
rel = abs_path
results.append({
'file': abs_path,
'relative': rel,
'total': total,
'hit': hit,
'missed': missed,
'pct': pct,
'missed_lines': missed_lines,
})
return sorted(results, key=lambda r: r['relative'])
# ── Text / JSON formatters ─────────────────────────────────────────────────────
def format_text(results: list) -> str:
"""Plain text coverage report."""
total_hit = sum(r['hit'] for r in results)
total_total = sum(r['total'] for r in results)
total_pct = (total_hit / total_total * 100) if total_total > 0 else 0
lines = ['─' * 72,
f'{"File":<45} {"Lines":>6} {"Hit":>6} {"Miss":>6} {"Cov":>6}',
'─' * 72]
for r in results:
lines.append(f"{r['relative']:<45} {r['total']:>6} {r['hit']:>6} {r['missed']:>6} {r['pct']:>5.0f}%")
lines += ['─' * 72,
f"{'TOTAL':<45} {total_total:>6} {total_hit:>6} {total_total - total_hit:>6} {total_pct:>5.0f}%",
'─' * 72]
return '\n'.join(lines)
def format_json(results: list) -> str:
"""JSON coverage report."""
total_hit = sum(r['hit'] for r in results)
total_total = sum(r['total'] for r in results)
return json.dumps({
'total_lines': total_total,
'total_hit': total_hit,
'total_pct': round(total_hit / total_total * 100, 1) if total_total > 0 else 0,
'files': [{
'file': r['relative'], 'lines': r['total'], 'hit': r['hit'],
'missed': r['missed'], 'pct': r['pct'], 'missed_lines': r['missed_lines'],
} for r in results],
}, indent=2)
# ── HTML formatter ─────────────────────────────────────────────────────────────
_CSS = '''
/* ── Reset ──────────────────────────────────────────────────── */
*,*::before,*::after{box-sizing:border-box;margin:0;padding:0}
html{scroll-behavior:smooth;scrollbar-width:thin;scrollbar-color:#2e2e2e #161616}
::-webkit-scrollbar{width:6px;height:6px}
::-webkit-scrollbar-track{background:#161616}
::-webkit-scrollbar-thumb{background:#2e2e2e;border-radius:3px}
::-webkit-scrollbar-thumb:hover{background:#3a3a3a}
/* ── Base ────────────────────────────────────────────────────── */
body{
font-family:'Cascadia Code','SF Mono','Fira Code','Consolas',monospace;
background:#161616;color:#c8c8c8;font-size:14px;line-height:1.5;
padding-bottom:52px;
}
/* ── Report header ───────────────────────────────────────────── */
.rh{padding:18px 28px 14px;border-bottom:1px solid #222}
.app-name{font-size:1.35em;font-weight:700;color:#fff;letter-spacing:-0.01em}
.app-meta{color:#555;font-size:0.8em;margin-top:3px}
/* ── Total bar ───────────────────────────────────────────────── */
.total-bar{
background:#1a1a1a;border-bottom:1px solid #222;
padding:12px 28px;display:flex;align-items:baseline;
flex-wrap:wrap;gap:8px 18px;
}
.total-pct{font-size:2.4em;font-weight:700;line-height:1}
.total-lines{color:#777;font-size:0.88em}
.total-delta{font-size:0.8em;font-weight:700;padding:2px 8px;border-radius:3px}
.total-delta.tup{background:#0d1f0d;color:#4caf50}
.total-delta.tdn{background:#1f0d0d;color:#f44336}
.total-note{flex-basis:100%;color:#555;font-size:0.76em;font-style:italic;padding-top:1px}
/* ── Main layout ─────────────────────────────────────────────── */
main{padding:0 28px 40px;max-width:1600px}
.sh{
color:#666;font-size:0.72em;font-weight:700;
text-transform:uppercase;letter-spacing:0.1em;
padding:22px 0 6px;border-bottom:1px solid #1e1e1e;margin-bottom:2px;
}
/* ── Summary table ───────────────────────────────────────────── */
#ftbl{width:100%;border-collapse:collapse;margin:4px 0 36px}
#ftbl th{
background:#1a1a1a;padding:7px 12px;text-align:left;
border-bottom:2px solid #242424;color:#666;
font-size:0.72em;font-weight:700;text-transform:uppercase;
letter-spacing:0.07em;white-space:nowrap;user-select:none;
}
#ftbl th.s{cursor:pointer}
#ftbl th.s:hover{color:#aaa}
#ftbl th.sa .si::after{content:' ▲';color:#4e8cbf}
#ftbl th.sd .si::after{content:' ▼';color:#4e8cbf}
#ftbl td{
padding:5px 12px;border-bottom:1px solid #1a1a1a;
white-space:nowrap;vertical-align:middle;
}
#ftbl tr.fr:hover td{background:#1b1b1b}
.r{text-align:right!important;font-variant-numeric:tabular-nums}
/* ── Coverage mini-bar ───────────────────────────────────────── */
.bar{
display:inline-flex;width:80px;height:8px;border-radius:3px;
overflow:hidden;background:#242424;vertical-align:middle;margin-left:6px;
}
.bh{background:#3a8a3a}.bm{background:#8a3a3a}
.pv{font-weight:700}
/* ── Delta badges ────────────────────────────────────────────── */
.d{font-size:0.75em;font-weight:700;padding:1px 6px;border-radius:3px;white-space:nowrap}
.d-up{background:#0d1f0d;color:#66bb6a}
.d-dn{background:#1f0d0d;color:#ef5350}
.d-flat{color:#3a3a3a}
/* ── Complexity badges ───────────────────────────────────────── */
.cx{font-size:0.7em;font-weight:700;padding:1px 6px;border-radius:3px;
white-space:nowrap;letter-spacing:0.02em}
.cx-low{background:#0d1f0d;color:#66bb6a}
.cx-med{background:#1f1f0d;color:#ffca28}
.cx-high{background:#1f150d;color:#ffa726}
.cx-crit{background:#1f0d0d;color:#ef5350}
/* ── File links ──────────────────────────────────────────────── */
a.fl{color:#4e8cbf;text-decoration:none}
a.fl:hover{text-decoration:underline}
.fdir{color:#555;font-size:0.85em}
.new-badge{font-size:0.7em;background:#0d1533;color:#4e8cbf;
padding:1px 5px;border-radius:3px;margin-left:4px}
/* ── Folder groups (source section) ─────────────────────────── */
.fg{margin-bottom:2px}
.fg>summary,.fs>summary{
list-style:none;display:flex;align-items:center;
gap:10px;cursor:pointer;user-select:none;
}
.fg>summary::-webkit-details-marker,
.fs>summary::-webkit-details-marker{display:none}
.fg>summary{
padding:7px 12px;background:#1a1a1a;
border-top:1px solid #222;border-radius:2px 2px 0 0;
}
.fg>summary:hover{background:#1e1e1e}
.chv{
font-size:0.58em;color:#444;flex-shrink:0;
display:inline-block;transition:transform 0.13s ease;
}
.fg[open]>summary .chv,.fs[open]>summary .chv{transform:rotate(90deg)}
.flabel{color:#999;font-weight:700;flex:1;font-size:0.92em}
.fstats{color:#c8c8c8;font-size:0.8em}
.fpct{font-weight:700}
/* ── File sections (inside folder groups) ────────────────────── */
.fs{margin-bottom:1px}
.fs>summary{
padding:5px 12px 5px 26px;
border-bottom:1px solid #1a1a1a;
}
.fs>summary:hover{background:#1b1b1b}
.fs-name{color:#4e8cbf;flex:1;font-size:0.9em}
.fs-stats{color:#c8c8c8;font-size:0.78em}
/* ── Annotated source ────────────────────────────────────────── */
pre.src{margin:0;font-size:0.88em;overflow-x:auto;
border-bottom:2px solid #1e1e1e;font-family:inherit;background:#111}
.sl{display:block;padding:0 12px 0 0;white-space:pre;line-height:1.45}
.sl:hover{background:#1b1b1b}
.sl-h{background:#0e2d18}.sl-m{background:#2d1010}
.ln{
display:inline-block;width:3.5em;color:#3a3a3a;
text-align:right;padding-right:10px;margin-right:10px;
border-right:1px solid #272727;
font-variant-numeric:tabular-nums;user-select:none;
}
.sl-h .ln{border-right-color:#1e5530;color:#3a6a4a}
.sl-m .ln{border-right-color:#5a1e1e;color:#7a4040}
/* ── Syntax highlighting ─────────────────────────────────────── */
.hk{color:#569cd6}
.hs{color:#ce9178}
.hc{color:#6a9955;font-style:italic}
.hv{color:#9cdcfe}
/* ── File dir in table ───────────────────────────────────────── */
.fdir{color:#666;font-size:0.9em}
/* ── Sticky footer ───────────────────────────────────────────── */
.foot{
position:fixed;bottom:0;left:0;right:0;
background:#111;border-top:1px solid #1e1e1e;
padding:7px 28px;display:flex;align-items:center;
gap:18px;font-size:0.76em;color:#444;z-index:200;
}
.foot a{color:#4e8cbf;text-decoration:none}
.foot a:hover{text-decoration:underline}
.fgrow{flex:1}
.fpath{color:#333;max-width:55%;overflow:hidden;
text-overflow:ellipsis;white-space:nowrap}
'''
_JS = r'''
(function(){
/* ── Sort summary table ── */
var tbl=document.getElementById('ftbl');
if(tbl){
var col=-1,dir=1;
tbl.querySelectorAll('th.s').forEach(function(th){
th.insertAdjacentHTML('beforeend','<span class="si"></span>');
th.addEventListener('click',function(){
var c=parseInt(th.dataset.col,10);
dir=(col===c)?-dir:1; col=c;
tbl.querySelectorAll('th').forEach(function(t){t.classList.remove('sa','sd')});
th.classList.add(dir>0?'sa':'sd');
var tb=tbl.querySelector('tbody');
var rows=Array.prototype.slice.call(tb.querySelectorAll('tr.fr'));
rows.sort(function(a,b){
var av=(a.cells[c]&&(a.cells[c].dataset.val||a.cells[c].textContent.trim()))||'';
var bv=(b.cells[c]&&(b.cells[c].dataset.val||b.cells[c].textContent.trim()))||'';
var an=parseFloat(av),bn=parseFloat(bv);
return(isNaN(an)||isNaN(bn))?av.localeCompare(bv)*dir:(an-bn)*dir;
});
rows.forEach(function(r){tb.appendChild(r)});
});
});
}
/* ── Open details section on hash navigation ── */
function openHash(){
var id=window.location.hash.slice(1);
if(!id)return;
var el=document.getElementById(id);
if(!el)return;
var p=el;
while(p){if(p.tagName==='DETAILS')p.open=true;p=p.parentElement}
el.scrollIntoView({behavior:'smooth',block:'start'});
}
window.addEventListener('hashchange',openHash);
openHash();
})();
'''
_INDEX_JS = r'''
(function(){
var nav=document.querySelector('nav');
var links=document.querySelectorAll('nav a');
var iframe=document.querySelector('iframe[name="report"]');
function mark(href){
links.forEach(function(a){a.classList.toggle('active',a.getAttribute('href')===href)});
}
function scrollToActive(){
var active=document.querySelector('nav a.active');
if(active&&nav)nav.scrollLeft=active.offsetLeft-nav.clientWidth+active.offsetWidth+24;
}
if(iframe){mark(iframe.getAttribute('src'));scrollToActive();}
links.forEach(function(a){
a.addEventListener('click',function(){
mark(a.getAttribute('href'));
setTimeout(scrollToActive,0);
});
});
})();
'''
def format_html(
results: list,
src_dir: str,
prev_stats=None,
app_info=None,
script_path: str = None,
generated_dt: datetime.datetime = None,
) -> str:
"""HTML coverage report with folder hierarchy, comparison deltas, and sorting."""
total_hit = sum(r['hit'] for r in results)
total_total = sum(r['total'] for r in results)
total_pct = round((total_hit / total_total * 100) if total_total > 0 else 0, 1)
version = _ptyunit_version()
app_name = (app_info or {}).get('name') or 'Coverage Report'
app_ver = (app_info or {}).get('version')
dt = generated_dt or datetime.datetime.now()
dt_str = _format_display_date(dt)
dt_iso = dt.strftime('%Y-%m-%dT%H:%M:%S')
# ── Total bar: compute delta and tint ────────────────────────────────────
prev_total_pct = (prev_stats or {}).get('total_pct')
if prev_total_pct is not None:
total_delta = total_pct - prev_total_pct
if total_delta > 0.05:
delta_html = (f'<span class="total-delta tup">▲ +{total_delta:.1f}%</span>')
elif total_delta < -0.05:
delta_html = (f'<span class="total-delta tdn">▼ {total_delta:.1f}%</span>')
else:
delta_html = ''
note = _threshold_note(total_pct, total_delta)
else:
delta_html = ''
note = _threshold_note(total_pct)
# ── App meta line ────────────────────────────────────────────────────────
meta_parts = []
if app_ver:
meta_parts.append(f'v{app_ver}')
# Skip "ptyunit vX" attribution when measuring ptyunit itself (redundant)
if app_name != 'ptyunit':
meta_parts.append(f'ptyunit v{version}')
meta_parts.append(f'<time datetime="{dt_iso}">{dt_str}</time>')
app_meta = ' · '.join(meta_parts)
# ── Build summary table rows (flat, sortable) ────────────────────────────
prev_files = (prev_stats or {}).get('files', {})
tbody_rows = []
for r in results:
anchor = _file_anchor(r['relative'])
parts = r['relative'].split('/')
fname = parts[-1]
fdir = ('/'.join(parts[:-1]) + '/') if len(parts) > 1 else ''
# Coverage bar
hit_w = max(0, min(80, int(r['pct'] * 0.8)))
miss_w = 80 - hit_w
bar = (f'<span class="bar"><span class="bh" style="width:{hit_w}px"></span>'
f'<span class="bm" style="width:{miss_w}px"></span></span>')
# Complexity
cx_score = compute_complexity(r['file'])
cx_badge = _complexity_badge(cx_score)
# Delta vs previous report
prev_file = prev_files.get(r['relative'])
if prev_file is not None:
delta_cell = _delta_html(prev_file['pct'], r['pct'])
delta_val = _delta_html(prev_file['pct'], r['pct'], data_only=True)
elif prev_stats is not None:
# File is new this run
delta_cell = '<span class="new-badge">new</span>'
delta_val = '0'
else:
# No previous run to compare against — leave cell empty
delta_cell = ''
delta_val = '0'
tbody_rows.append(
f'<tr class="fr">'
f'<td data-val="{_esc(r["relative"])}">'
f'{"<span class=fdir>" + _esc(fdir) + "</span>" if fdir else ""}'
f'<a href="#{anchor}" class="fl">{_esc(fname)}</a>'
f'</td>'
f'<td class="r" data-val="{r["total"]}">{r["total"]}</td>'
f'<td class="r" data-val="{r["hit"]}">{r["hit"]}</td>'
f'<td class="r" data-val="{r["missed"]}">{r["missed"]}</td>'
f'<td class="r" data-val="{r["pct"]}">'
f'<span class="pv">{r["pct"]:.0f}%</span>{bar}'
f'</td>'
f'<td class="r" data-val="{cx_score}">{cx_badge}</td>'
f'<td class="r" data-val="{delta_val}">{delta_cell}</td>'
f'</tr>'
)
# ── Build source section (folder hierarchy) ──────────────────────────────
source_sections = []
for grp in group_by_folder(results):
folder = grp['folder']
folder_label = (folder + '/') if folder else '(root)'
anchor_folder = _file_anchor(folder) if folder else 'root'
# Per-folder delta
folder_delta = ''
if prev_stats is not None:
# Compute folder prev pct from individual file deltas
prev_f_hits = sum(
prev_files.get(f['relative'], {}).get('hit', 0) for f in grp['files']
)
prev_f_total = sum(
prev_files.get(f['relative'], {}).get('total', 0) for f in grp['files']
)
if prev_f_total > 0:
prev_f_pct = round(prev_f_hits / prev_f_total * 100, 1)
folder_delta = ' ' + _delta_html(prev_f_pct, grp['pct'])
source_sections.append(
f'<details class="fg" id="folder-{anchor_folder}" open>'
f'<summary>'
f'<span class="chv">▶</span>'
f'<span class="flabel">{_esc(folder_label)}</span>'
f'<span class="fstats">'
f'{len(grp["files"])} file{"s" if len(grp["files"]) != 1 else ""}'
f' · <span class="fpct" style="color:{_pct_color(grp["pct"])}">{grp["pct"]:.0f}%</span>'
f'{folder_delta}'
f'</span>'
f'</summary>'
)
for r in grp['files']:
anchor = _file_anchor(r['relative'])
fname = r['relative'].split('/')[-1]
missed_set = set(r['missed_lines'])
executable = count_source_lines(r['file'])
prev_file = prev_files.get(r['relative'])
file_delta = ''
if prev_file is not None:
file_delta = ' ' + _delta_html(prev_file['pct'], r['pct'])
elif prev_stats is not None:
file_delta = ' <span class="new-badge">new</span>'
line_spans = []
try:
with open(r['file'], 'r', errors='replace') as fh:
for i, line in enumerate(fh, 1):
highlighted = _highlight_bash(line.rstrip())
if i in executable:
cls = 'sl-m' if i in missed_set else 'sl-h'
line_spans.append(
f'<span class="sl {cls}"><span class="ln">{i}</span>{highlighted}</span>'
)
else:
line_spans.append(
f'<span class="sl"><span class="ln">{i}</span>{highlighted}</span>'
)
except (IOError, OSError):
line_spans.append('<span class="sl">(could not read source)</span>')
# Join spans with no separator — newlines inside <pre> render as blank lines
source_sections.append(