|
| 1 | +#!/usr/bin/env bash |
| 2 | +# coverage.sh - Clean build, run tests with coverage, and generate a human-readable report. |
| 3 | +# Usage: ./scripts/coverage.sh [--project <test-project-csproj>] [--open] |
| 4 | +# --project <path> Run coverage for a single test project (relative to src/) |
| 5 | +# --open Open HTML report in browser after generation |
| 6 | +set -euo pipefail |
| 7 | + |
| 8 | +REPO_ROOT="$(cd "$(dirname "$0")/.." && pwd)" |
| 9 | +SRC_DIR="$REPO_ROOT/src" |
| 10 | +RESULTS_DIR="$REPO_ROOT/test_results" |
| 11 | +COVERAGE_DIR="$RESULTS_DIR/coverage" |
| 12 | +REPORT_DIR="$RESULTS_DIR/report" |
| 13 | +SOLUTION="ReactiveUI.Binding.SourceGenerators.slnx" |
| 14 | + |
| 15 | +# Parse arguments |
| 16 | +PROJECT="" |
| 17 | +OPEN_REPORT=false |
| 18 | +while [[ $# -gt 0 ]]; do |
| 19 | + case "$1" in |
| 20 | + --project) |
| 21 | + PROJECT="$2" |
| 22 | + shift 2 |
| 23 | + ;; |
| 24 | + --open) |
| 25 | + OPEN_REPORT=true |
| 26 | + shift |
| 27 | + ;; |
| 28 | + *) |
| 29 | + echo "Unknown argument: $1" >&2 |
| 30 | + exit 1 |
| 31 | + ;; |
| 32 | + esac |
| 33 | +done |
| 34 | + |
| 35 | +# ── Step 1: Clean ────────────────────────────────────────────────────────────── |
| 36 | +echo "=== Cleaning ===" |
| 37 | +cd "$SRC_DIR" |
| 38 | +dotnet clean "$SOLUTION" -c Release --verbosity quiet 2>/dev/null || true |
| 39 | + |
| 40 | +echo "Removing bin/ and obj/ directories..." |
| 41 | +find "$SRC_DIR" -type d \( -name bin -o -name obj \) -exec rm -rf {} + 2>/dev/null || true |
| 42 | + |
| 43 | +# Clean previous results |
| 44 | +rm -rf "$RESULTS_DIR" |
| 45 | +mkdir -p "$COVERAGE_DIR" "$REPORT_DIR" |
| 46 | + |
| 47 | +# ── Step 2: Run tests with coverage ─────────────────────────────────────────── |
| 48 | +echo "" |
| 49 | +echo "=== Building and running tests with coverage ===" |
| 50 | +cd "$SRC_DIR" |
| 51 | + |
| 52 | +if [[ -n "$PROJECT" ]]; then |
| 53 | + echo "Project: $PROJECT" |
| 54 | + dotnet test --project "$PROJECT" -c Release \ |
| 55 | + --results-directory "$COVERAGE_DIR" \ |
| 56 | + -- --coverage --coverage-output-format cobertura |
| 57 | +else |
| 58 | + echo "Solution: $SOLUTION" |
| 59 | + dotnet test --solution "$SOLUTION" -c Release \ |
| 60 | + --results-directory "$COVERAGE_DIR" \ |
| 61 | + -- --coverage --coverage-output-format cobertura |
| 62 | +fi |
| 63 | + |
| 64 | +# ── Step 3: Find cobertura files ────────────────────────────────────────────── |
| 65 | +echo "" |
| 66 | +echo "=== Locating coverage files ===" |
| 67 | + |
| 68 | +# Coverage files may land in --results-directory or in per-project TestResults/ |
| 69 | +COBERTURA_FILES="" |
| 70 | +while IFS= read -r -d '' file; do |
| 71 | + COBERTURA_FILES="${COBERTURA_FILES};${file}" |
| 72 | +done < <(find "$COVERAGE_DIR" "$SRC_DIR" -name "*.cobertura.xml" -print0 2>/dev/null) |
| 73 | + |
| 74 | +# Strip leading semicolon |
| 75 | +COBERTURA_FILES="${COBERTURA_FILES#;}" |
| 76 | + |
| 77 | +if [[ -z "$COBERTURA_FILES" ]]; then |
| 78 | + echo "ERROR: No cobertura coverage files found." >&2 |
| 79 | + exit 1 |
| 80 | +fi |
| 81 | + |
| 82 | +FILE_COUNT=$(echo "$COBERTURA_FILES" | tr ';' '\n' | wc -l) |
| 83 | +echo "Found $FILE_COUNT cobertura file(s)" |
| 84 | + |
| 85 | +# ── Step 4: Generate reports ────────────────────────────────────────────────── |
| 86 | +echo "" |
| 87 | +echo "=== Generating coverage reports ===" |
| 88 | + |
| 89 | +reportgenerator \ |
| 90 | + -reports:"$COBERTURA_FILES" \ |
| 91 | + -targetdir:"$REPORT_DIR" \ |
| 92 | + -reporttypes:"Html;TextSummary;MarkdownSummaryGithub" |
| 93 | + |
| 94 | +# ── Step 5: Display summary ────────────────────────────────────────────────── |
| 95 | +echo "" |
| 96 | +echo "========================================================================" |
| 97 | +echo " COVERAGE SUMMARY" |
| 98 | +echo "========================================================================" |
| 99 | +echo "" |
| 100 | +cat "$REPORT_DIR/Summary.txt" |
| 101 | + |
| 102 | +# ── Step 6: Extract uncovered lines for agent consumption ───────────────────── |
| 103 | +echo "" |
| 104 | +echo "========================================================================" |
| 105 | +echo " UNCOVERED LINES / BRANCHES" |
| 106 | +echo "========================================================================" |
| 107 | +echo "" |
| 108 | + |
| 109 | +# Parse cobertura XML to find files with < 100% coverage and list uncovered lines. |
| 110 | +# Uses xmllint (libxml2) if available, otherwise falls back to awk. |
| 111 | +generate_uncovered_report() { |
| 112 | + local cobertura_file="$1" |
| 113 | + |
| 114 | + # Use python3 for reliable XML parsing — available on virtually all Linux systems |
| 115 | + python3 - "$cobertura_file" <<'PYEOF' |
| 116 | +import sys |
| 117 | +import xml.etree.ElementTree as ET |
| 118 | +from collections import defaultdict |
| 119 | +
|
| 120 | +tree = ET.parse(sys.argv[1]) |
| 121 | +root = tree.getroot() |
| 122 | +
|
| 123 | +uncovered = defaultdict(lambda: {"lines": [], "branches": []}) |
| 124 | +
|
| 125 | +for package in root.iter("package"): |
| 126 | + for cls in package.iter("class"): |
| 127 | + filename = cls.get("filename", "") |
| 128 | + # Make path relative if possible |
| 129 | + for prefix in ["/home/", "/src/"]: |
| 130 | + idx = filename.find(prefix) |
| 131 | + if idx >= 0: |
| 132 | + filename = filename[idx:] |
| 133 | + break |
| 134 | +
|
| 135 | + for line in cls.iter("line"): |
| 136 | + line_num = line.get("number") |
| 137 | + hits = int(line.get("hits", "0")) |
| 138 | + condition = line.get("condition-coverage", "") |
| 139 | +
|
| 140 | + if hits == 0: |
| 141 | + uncovered[filename]["lines"].append(int(line_num)) |
| 142 | + elif condition and "100%" not in condition: |
| 143 | + uncovered[filename]["branches"].append( |
| 144 | + (int(line_num), condition) |
| 145 | + ) |
| 146 | +
|
| 147 | +if not uncovered: |
| 148 | + print("All lines and branches are covered!") |
| 149 | + sys.exit(0) |
| 150 | +
|
| 151 | +# Sort files for stable output |
| 152 | +for filename in sorted(uncovered.keys()): |
| 153 | + info = uncovered[filename] |
| 154 | + if not info["lines"] and not info["branches"]: |
| 155 | + continue |
| 156 | + print(f"\n--- {filename}") |
| 157 | + if info["lines"]: |
| 158 | + # Collapse consecutive lines into ranges |
| 159 | + lines = sorted(set(info["lines"])) |
| 160 | + ranges = [] |
| 161 | + start = lines[0] |
| 162 | + end = lines[0] |
| 163 | + for n in lines[1:]: |
| 164 | + if n == end + 1: |
| 165 | + end = n |
| 166 | + else: |
| 167 | + ranges.append(f"{start}" if start == end else f"{start}-{end}") |
| 168 | + start = end = n |
| 169 | + ranges.append(f"{start}" if start == end else f"{start}-{end}") |
| 170 | + print(f" Uncovered lines: {', '.join(ranges)}") |
| 171 | + if info["branches"]: |
| 172 | + for line_num, cond in sorted(set(info["branches"])): |
| 173 | + print(f" Partial branch at line {line_num}: {cond}") |
| 174 | +PYEOF |
| 175 | +} |
| 176 | + |
| 177 | +# Process each cobertura file |
| 178 | +IFS=';' read -ra FILES <<< "$COBERTURA_FILES" |
| 179 | +for f in "${FILES[@]}"; do |
| 180 | + if [[ -f "$f" ]]; then |
| 181 | + generate_uncovered_report "$f" |
| 182 | + fi |
| 183 | +done |
| 184 | + |
| 185 | +# ── Step 7: Summary footer ─────────────────────────────────────────────────── |
| 186 | +echo "" |
| 187 | +echo "========================================================================" |
| 188 | +echo " HTML report: $REPORT_DIR/index.html" |
| 189 | +echo " Markdown: $REPORT_DIR/SummaryGithub.md" |
| 190 | +echo " Text: $REPORT_DIR/Summary.txt" |
| 191 | +echo "========================================================================" |
| 192 | + |
| 193 | +if $OPEN_REPORT; then |
| 194 | + xdg-open "$REPORT_DIR/index.html" 2>/dev/null || \ |
| 195 | + open "$REPORT_DIR/index.html" 2>/dev/null || \ |
| 196 | + echo "Could not open browser. Open manually: $REPORT_DIR/index.html" |
| 197 | +fi |
0 commit comments