Skip to content

Add golden-master tests for the Aot generator and measure its coverage - #204

Merged
manuc66 merged 5 commits into
masterfrom
feature/generator-coverage
Aug 13, 2026
Merged

Add golden-master tests for the Aot generator and measure its coverage#204
manuc66 merged 5 commits into
masterfrom
feature/generator-coverage

Conversation

@manuc66

@manuc66 manuc66 commented Aug 13, 2026

Copy link
Copy Markdown
Owner

Problem

The JsonSubTypes.Aot generator is an analyzer (ReferenceOutputAssembly=false), so its code is never loaded in a test process: coverlet measured 0% over ~300 lines and Sonar saw no coverage, dragging the project's new_coverage gate down to ~48%.

Fix

  • JsonSubTypes.Aot.Generated: committed test domain (TestDomain.cs) plus the .cs files the generator emits for it, compiled as real sources. Named without the generator's .g.cs suffix so Sonar analyzes them as regular code.
  • JsonSubTypes.Aot.Generator.Tests: runs the generator in-process via the Roslyn driver (covers JsonSubTypesGenerator.cs), executes the generated converters (serialization/deserialization), and a golden-master test asserts the committed files exactly match the current generator output.
  • Generator bug fix: two base types with the same short name in different namespaces produced colliding converter names (CS0101 / file collision). Names are now qualified with the sanitized namespace only when they collide; the registry keeps short member names so existing consumers are unaffected. Covered by a dedicated driver test.
  • CI: the modern and analysis jobs run the new tests with coverage and hand the report to Codecov/Sonar. The analysis job now also runs on pull requests (PR parameters + GITHUB_TOKEN for quality-gate decoration). .g.cs are excluded from Sonar's duplication check but still analyzed.

Tests

  • 13 generator tests pass on net8.0 and net10.0.
  • Sample, existing Aot tests (73) and the full solution build pass.
  • Locally, the generator goes from 0% to ~91% line coverage, and the 7 committed generated files are measured.

Honest note(s)

  • The PR's analysis job will run Sonar on the branch for the first time; whether the committed .cs files are treated as regular (non-generated) code is verified by that run, not locally.
  • The committed files must be regenerated (and re-committed) whenever the generator output changes; the golden-master test enforces this.

…ted code

The generator was an analyzer (ReferenceOutputAssembly=false), so its code was
never loaded in a test process: coverlet measured 0% over ~300 lines and Sonar
saw no coverage on JsonSubTypes.Aot.

- JsonSubTypes.Aot.Generated: the committed test domain (TestDomain.cs) plus the
  .g.cs files the generator emits for it, compiled as real sources so Sonar
  analyzes them and coverlet measures them.
- JsonSubTypes.Aot.Generator.Tests: runs the generator in-process via the Roslyn
  driver (covers JsonSubTypesGenerator.cs), executes the generated converters
  (serialization/deserialization), and verifies the committed .g.cs match the
  current generator output (golden master, breaks on any output drift).
- CI: the modern and analysis jobs run these tests with coverage and hand the
  report to Codecov/Sonar. The analysis job now also runs on pull requests
  (pull-request parameters + GITHUB_TOKEN for quality-gate decoration), and
  .g.cs are excluded from Sonar's duplication check but still analyzed.
…suffix

Bug: two base types with the same short name in different namespaces produced
two converters with the same class name and file hint (CS0101, file collision).
Converter names now stay short when unique and are qualified with the sanitized
namespace only when the short name collides; the registry keeps the short member
name so existing consumers (sample, tests) are unaffected. Covered by a driver
test with two homonymous Animal classes.

The committed golden master files in JsonSubTypes.Aot.Generated are renamed from
.g.cs to .cs so Sonar does not treat them as generated code; the golden-master
test maps the generator's .g.cs hint names to the committed files.
@codecov

codecov Bot commented Aug 13, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.21%. Comparing base (98926e0) to head (48eae2c).

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #204      +/-   ##
==========================================
- Coverage   92.32%   90.21%   -2.11%     
==========================================
  Files          18       19       +1     
  Lines         964     1789     +825     
  Branches      200      294      +94     
==========================================
+ Hits          890     1614     +724     
- Misses         41      106      +65     
- Partials       33       69      +36     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

The pull-request branch name (github.head_ref) is user-controlled and was
interpolated directly into the shell script, enabling injection. Pass it via
an environment variable instead (GitHub sanitizes env values in run blocks).

Add codecov.yml excluding the golden-master project (JsonSubTypes.Aot.Generated
and the generator tests) from the project/patch checks: its committed generated
code is exercised only partially by the generator tests, so counting it dragged
the project coverage to 64% and the patch check to 9%.
With target: auto, any drop fails; adding the now-analyzed generator code
shifted the baseline by ~2%. A 5% threshold absorbs legitimate variation
while still catching real regressions.
Only Animal was tested, so the other generated converters (Person, Gadget,
DottedGadget, Payload, Game) sat at ~0% coverage. Add serialize/deserialize
tests for each, plus a JsonSerializable context so the nested Payload->Game
hierarchy resolves at runtime. Generated converters now show real coverage.
@manuc66
manuc66 merged commit 24880da into master Aug 13, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant