From bda6b7235d704c8261f97ba63a9a969f7f024cb0 Mon Sep 17 00:00:00 2001 From: Marci Date: Thu, 27 Aug 2026 23:44:00 +0800 Subject: [PATCH 1/2] =?UTF-8?q?ci:=20=E8=A6=86=E7=9B=96=E7=8E=87=E6=8A=A5?= =?UTF-8?q?=E5=91=8A=E2=80=94=E2=80=94Ubuntu=20=E7=9F=A9=E9=98=B5=E6=94=B6?= =?UTF-8?q?=E9=9B=86=20XPlat=20Code=20Coverage=EF=BC=8CSummary=20=E8=A1=A8?= =?UTF-8?q?=E6=A0=BC=20+=20artifact=20=E4=BF=9D=E7=95=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/ci.yml | 63 ++++++++++++++++++++++++++++++++++++++-- CHANGELOG.md | 1 + README.en.md | 3 +- README.md | 3 +- 4 files changed, 66 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3362785..ae5c807 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -16,11 +16,13 @@ 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" + coverage: true - os: windows-latest frameworks: "net48" + coverage: false runs-on: ${{ matrix.os }} steps: - uses: actions/checkout@v7 @@ -39,7 +41,64 @@ jobs: shell: bash env: FRAMEWORKS: ${{ matrix.frameworks }} + COLLECT_COVERAGE: ${{ matrix.coverage }} 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.coverage + 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.coverage + 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 亦自动收集并写入运行摘要) ``` ## 项目结构 From 2b791db05de8997929e438f46ba1ccf12f8b2543 Mon Sep 17 00:00:00 2001 From: Marci Date: Thu, 27 Aug 2026 23:49:01 +0800 Subject: [PATCH 2/2] =?UTF-8?q?ci:=20=E8=A6=86=E7=9B=96=E7=8E=87=E5=BC=80?= =?UTF-8?q?=E5=85=B3=E6=8C=89=20OS=20=E6=8E=A8=E6=96=AD=EF=BC=8C=E9=81=BF?= =?UTF-8?q?=E5=85=8D=E7=9F=A9=E9=98=B5=E5=80=BC=E8=BF=9B=E5=85=A5=E5=BF=85?= =?UTF-8?q?=E9=9C=80=E6=A3=80=E6=9F=A5=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/ci.yml | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ae5c807..707e5ed 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -19,10 +19,8 @@ jobs: # 现代 .NET 在 linux 跑(附带覆盖率收集);net48(验证 netstandard2.0 资产)需要 Windows 运行时 - os: ubuntu-latest frameworks: "net8.0 net10.0" - coverage: true - os: windows-latest frameworks: "net48" - coverage: false runs-on: ${{ matrix.os }} steps: - uses: actions/checkout@v7 @@ -41,7 +39,8 @@ jobs: shell: bash env: FRAMEWORKS: ${{ matrix.frameworks }} - COLLECT_COVERAGE: ${{ matrix.coverage }} + # 覆盖率只在 Ubuntu 收集(矩阵值不进 job 名,避免必需检查名随之变化) + COLLECT_COVERAGE: ${{ matrix.os == 'ubuntu-latest' }} run: | for fw in $FRAMEWORKS; do if [ "$COLLECT_COVERAGE" = "true" ]; then @@ -54,7 +53,7 @@ jobs: # 覆盖率摘要写入 job Summary(自包含,不依赖外部服务);原始 cobertura XML 附为 artifact - name: Coverage summary - if: matrix.coverage + if: matrix.os == 'ubuntu-latest' shell: bash run: | python3 - <<'PYEOF' @@ -96,7 +95,7 @@ jobs: PYEOF - name: Upload coverage artifacts - if: matrix.coverage + if: matrix.os == 'ubuntu-latest' uses: actions/upload-artifact@v7 with: name: coverage-${{ github.run_number }}