Skip to content

test(docs): compile published Java snippets to catch API drift#266

Merged
DemchaAV merged 2 commits into
developfrom
test/docs-example-compile-guard
Jun 29, 2026
Merged

test(docs): compile published Java snippets to catch API drift#266
DemchaAV merged 2 commits into
developfrom
test/docs-example-compile-guard

Conversation

@DemchaAV

Copy link
Copy Markdown
Owner

Why

DocumentationExamplesTest renders hand-kept Java copies of representative examples, but nothing compiles the literal docs/*.md fences. A published snippet can therefore drift from the API — a renamed or removed method silently breaks the documented example while the build stays green.

What changed

  • DocumentationSnippetCompileTest — walks docs/**/*.md and, for each java fence carrying an opt-in marker comment <!-- doc-example: id=… mode=method|members|class -->, wraps it per mode and compiles it in-memory (ToolProvider.getSystemJavaCompiler()) against the test classpath. Compiler errors fail the test and name the snippet id + file; warnings are ignored. A second test guards marker well-formedness (unique id, supported mode, a fence actually follows), and the run asserts it found at least one snippet so a moved docs folder can't make the guard pass vacuously.
  • The marker is an HTML comment, so it renders to nothing on GitHub and keeps the page clean. Teaching fragments that reference prose-only symbols (a bare invoice variable) carry no marker and are left untouched, keeping the guard free of false positives.
  • Marked the five self-contained snippets in docs/first-document.md (2) and docs/templates/business-templates.md (3).

Complements DocumentationExamplesTest (which renders example copies); this guard pins the published markdown itself to the API.

Verification

  • ./mvnw -B -ntp test -Dtest='DocumentationSnippetCompileTest,DocumentationExamplesTest,DocumentationCoverageTest,CanonicalSurfaceGuardTest,PackageMapGuardTest,VersionConsistencyGuardTest' -pl . → 36 tests, 0 failures.
  • Drift check: renaming a builder call in a marked fence fails the test with [business-invoice — docs/templates/business-templates.md] cannot find symbol: method …, then reverted.

Full verify runs in CI.

DemchaAV added 2 commits June 29, 2026 12:54
DocumentationExamplesTest renders hand-kept Java copies of examples, but
nothing compiles the literal docs/*.md fences, so a published snippet can
drift from the API while the build stays green.

DocumentationSnippetCompileTest walks docs/**/*.md and, for each java fence
carrying an opt-in marker comment <!-- doc-example: id=... mode=... -->,
wraps it per mode (method / members / class) and compiles it in-memory via
ToolProvider.getSystemJavaCompiler() against the test classpath. Compiler
errors fail the test and name the snippet id + file; a second test guards
marker well-formedness, and the run asserts at least one snippet is found so
a moved docs folder cannot make the guard pass vacuously. Markers are HTML
comments, invisible on GitHub; teaching fragments stay unmarked.

Marks the five self-contained snippets in first-document.md (2) and
business-templates.md (3). Complements DocumentationExamplesTest, which
renders example copies; this guard pins the published markdown to the API.
Make DocumentationSnippetCompileTest verify its own failure path so a
regression in the parse/wrap/compile pipeline cannot pass vacuously:

- compilerReportsErrorForBrokenSnippet feeds a snippet that references a
  missing symbol and asserts the error is surfaced and attributed to its id.
- knownCanonicalTypeResolvesOnTestClasspath compiles a trivial canonical
  call, so a classpath failure is distinguishable from a documentation break.

Also harden the mechanics: attribute diagnostics to snippets by file-object
identity instead of a substring match; lift only the leading run of imports
(an import-shaped line inside a body stays put); match the java fence exactly
(no `javascript`); create the compiler output dir inside the try; guard
sanitized unit-name uniqueness. Drop the unused class mode.
@DemchaAV DemchaAV merged commit 2e9f577 into develop Jun 29, 2026
11 checks passed
@DemchaAV DemchaAV deleted the test/docs-example-compile-guard branch June 29, 2026 12:44
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