diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3362785..707e5ed 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -16,7 +16,7 @@ jobs: fail-fast: false matrix: include: - # 现代 .NET 在 linux 跑;net48(验证 netstandard2.0 资产)需要 Windows 运行时 + # 现代 .NET 在 linux 跑(附带覆盖率收集);net48(验证 netstandard2.0 资产)需要 Windows 运行时 - os: ubuntu-latest frameworks: "net8.0 net10.0" - os: windows-latest @@ -39,7 +39,65 @@ jobs: shell: bash env: FRAMEWORKS: ${{ matrix.frameworks }} + # 覆盖率只在 Ubuntu 收集(矩阵值不进 job 名,避免必需检查名随之变化) + COLLECT_COVERAGE: ${{ matrix.os == 'ubuntu-latest' }} run: | for fw in $FRAMEWORKS; do - dotnet test StreamFrame.slnx -c Release --no-build -f $fw + if [ "$COLLECT_COVERAGE" = "true" ]; then + dotnet test StreamFrame.slnx -c Release --no-build -f $fw \ + --collect:"XPlat Code Coverage" --results-directory "coverage/$fw" + else + dotnet test StreamFrame.slnx -c Release --no-build -f $fw + fi done + + # 覆盖率摘要写入 job Summary(自包含,不依赖外部服务);原始 cobertura XML 附为 artifact + - name: Coverage summary + if: matrix.os == 'ubuntu-latest' + shell: bash + run: | + python3 - <<'PYEOF' + import glob, os, xml.etree.ElementTree as ET + + def fmt(rate): + return f"{rate * 100:.1f}%" + + lines = ["", "## 测试覆盖率(XPlat Code Coverage)", "", + "| 目标 | Line | Branch | 覆盖行/总行 |", "|---|---|---|---|"] + + for tfm_dir in sorted(glob.glob("coverage/*")): + tfm = os.path.basename(tfm_dir) + reports = glob.glob(os.path.join(tfm_dir, "**", "coverage.cobertura.xml"), recursive=True) + if not reports: + continue + # 同一 TFM 只应有一份(单一测试项目);防御性取第一份 + root = ET.parse(reports[0]).getroot() + line_rate = float(root.get("line-rate", "0")) + branch_rate = float(root.get("branch-rate", "0")) + all_lines = list(root.iter("line")) + covered_lines = sum(1 for l in all_lines if int(l.get("hits", "0")) > 0) + lines.append(f"| {tfm} | {fmt(line_rate)} | {fmt(branch_rate)} | {covered_lines}/{len(all_lines)} |") + + if tfm.endswith("net8.0"): + detail = ["", f"
按程序集({tfm})", "", + "| 程序集 | Line | Branch |", "|---|---|---|"] + for pkg in root.iter("package"): + detail.append(f"| {pkg.get('name', '?')} | " + f"{fmt(float(pkg.get('line-rate', '0')))} | " + f"{fmt(float(pkg.get('branch-rate', '0')))} |") + detail += ["", "
", ""] + lines += detail + + summary = "\n".join(lines) + "\n" + print(summary) + with open(os.environ.get("GITHUB_STEP_SUMMARY", "/dev/stdout"), "a", encoding="utf-8") as f: + f.write(summary) + PYEOF + + - name: Upload coverage artifacts + if: matrix.os == 'ubuntu-latest' + uses: actions/upload-artifact@v7 + with: + name: coverage-${{ github.run_number }} + path: coverage/ + retention-days: 14 diff --git a/CHANGELOG.md b/CHANGELOG.md index f6f074a..b2d9b6d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ ## [Unreleased] ### 新增 +- CI 覆盖率报告:Ubuntu 矩阵(net8/net10)测试时收集 XPlat Code Coverage,写入 job Summary(总体 + 按程序集明细),原始 cobertura XML 以 artifact 保留 14 天;当前基线约行覆盖 85% / 分支 81%。本地可用 `dotnet test -f net8.0 --collect:"XPlat Code Coverage"` 复现。 - 心跳保活示例(demo 场景 4):周期心跳 + 接收空闲超时的完整范式——有心跳时连接稳定(零状态事件),停止心跳后空闲判定触发重连;README 活性探测小节同步补充代码片段。 ### 修复 diff --git a/README.en.md b/README.en.md index e910439..fe76915 100644 --- a/README.en.md +++ b/README.en.md @@ -193,8 +193,9 @@ The `netstandard2.0` asset is validated by the **full net48 test suite** (real T ```bash dotnet build StreamFrame.slnx dotnet test -dotnet run --project samples/StreamFrame.Demo # 3-scenario end-to-end demo +dotnet run --project samples/StreamFrame.Demo # 4-scenario end-to-end demo dotnet run -c Release --project bench/StreamFrame.Benchmarks # benchmarks (~5-10 min) +dotnet test -f net8.0 --collect:"XPlat Code Coverage" # coverage (CI also collects & summarizes) ``` ## Project layout diff --git a/README.md b/README.md index 6543010..d524a25 100644 --- a/README.md +++ b/README.md @@ -192,8 +192,9 @@ BenchmarkDotNet 实测(详见 [bench/README.md](bench/README.md),可本地 ```bash dotnet build StreamFrame.slnx dotnet test -dotnet run --project samples/StreamFrame.Demo # 三场景端到端 demo +dotnet run --project samples/StreamFrame.Demo # 四场景端到端 demo dotnet run -c Release --project bench/StreamFrame.Benchmarks # 性能基准(约 5-10 分钟) +dotnet test -f net8.0 --collect:"XPlat Code Coverage" # 覆盖率(CI 亦自动收集并写入运行摘要) ``` ## 项目结构