feat: add tests for guide code snippets#552
Conversation
804f216 to
352ef6f
Compare
There was a problem hiding this comment.
Pull request overview
Adds an integration-test framework to compile/check code blocks in the docs/ guide, and updates multiple guide snippets so they build under the new framework.
Changes:
- Introduces
cot-testcrate +libtest-mimicrunner to validate fenced code blocks indocs/*.md(Rust/TOML/Askama templates). - Updates many guide snippets (hidden
# ...lines, async fixes, etc.) to compile under the new checking approach. - Adds supporting glue:
just test-docs, nextest concurrency override, and small framework/API touch-ups (e.g.,TestRequestBuilder::cache,ForeignKey::new).
Reviewed changes
Copilot reviewed 25 out of 27 changed files in this pull request and generated 7 comments.
Show a summary per file
| File | Description |
|---|---|
| justfile | Adds test-docs task/alias to run the new doc-snippet tests. |
| docs/upgrade-guide.md | Adjusts a Rust code fence and list indentation. |
| docs/testing.md | Updates examples; adds async fixes; minor formatting changes. |
| docs/templates.md | Updates Askama examples and adds extra supporting code for compilation. |
| docs/static-files.md | Adds hidden lines so static-file snippets compile. |
| docs/site/Cargo.lock | Bumps cot-site git rev in the docs site lockfile. |
| docs/README.md | Reworks headings and adds placeholder section for doc-test instructions. |
| docs/openapi.md | Updates OpenAPI examples (return types, derives, route registration) to compile. |
| docs/introduction.md | Adds hidden scaffolding + a rust,has_main snippet for the main example. |
| docs/forms.md | Updates form examples to use newer APIs (into_response()?) and adds hidden scaffolding. |
| docs/db-models.md | Makes DB examples compile (adds hidden model scaffolding; comments out sample URLs). |
| docs/caching.md | Adds hidden scaffolding so cache examples compile. |
| docs/admin-panel.md | Updates admin-panel snippets for newer APIs and adds missing scaffolding. |
| cot/src/test.rs | Adds TestRequestBuilder::cache() behind cache feature. |
| cot/src/project.rs | Updates a panic message to reference cot::Project. |
| cot/src/db/relations.rs | Adds ForeignKey::new(...) convenience constructor. |
| cot-test/tests/doc_code_blocks.rs | New libtest-mimic runner that parses Markdown and runs per-block checks. |
| cot-test/templates/html_main.rs | Template-compilation harness for Askama snippets. |
| cot-test/templates/base.html | Base template used by Askama snippet compilation. |
| cot-test/src/lib.rs | New helper crate that generates a temp project and runs cargo check for snippets. |
| cot-test/Cargo.toml | Defines new internal crate and custom test harness entry. |
| cot-macros/tests/ui/func_query_invalid_field.stderr | Updates expected UI test stderr output wording. |
| cot-macros/tests/ui/attr_main_args.stderr | Updates expected stderr to reference cot::Project. |
| cot-macros/tests/ui/attr_main_args.rs | Updates return type to cot::Project. |
| Cargo.toml | Adds cot-test to workspace + new workspace deps (comrak, libtest-mimic, etc.). |
| Cargo.lock | Updates lockfile for new deps and cot-test crate. |
| .config/nextest.toml | Adds nextest grouping to serialize doc-code-block tests. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
| Branch | docs-tests-2 |
| Testbed | github-ubuntu-latest |
Click to view all benchmark results
| Benchmark | Latency | Benchmark Result microseconds (µs) (Result Δ%) | Upper Boundary microseconds (µs) (Limit %) |
|---|---|---|---|
| empty_router/empty_router | 📈 view plot 🚷 view threshold | 5,949.40 µs(-2.65%)Baseline: 6,111.63 µs | 7,723.65 µs (77.03%) |
| json_api/json_api | 📈 view plot 🚷 view threshold | 1,016.30 µs(-5.50%)Baseline: 1,075.40 µs | 1,324.49 µs (76.73%) |
| nested_routers/nested_routers | 📈 view plot 🚷 view threshold | 969.91 µs(-2.38%)Baseline: 993.60 µs | 1,204.82 µs (80.50%) |
| single_root_route/single_root_route | 📈 view plot 🚷 view threshold | 930.50 µs(-2.69%)Baseline: 956.27 µs | 1,168.37 µs (79.64%) |
| single_root_route_burst/single_root_route_burst | 📈 view plot 🚷 view threshold | 20,713.00 µs(+17.12%)Baseline: 17,685.14 µs | 21,147.11 µs (97.95%) |
75dc48c to
5b957f4
Compare
|
Note: this PR still needs a corresponding one in cot-site for hiding rust lines starting with |
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 23 out of 25 changed files in this pull request and generated 2 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Codecov Report❌ Patch coverage is
Flags with carried forward coverage won't be shown. Click here to find out more.
... and 1 file with indirect coverage changes 🚀 New features to boost your workflow:
|
| // For specific symbols, only add if the symbol is NOT already imported | ||
| let mut found = false; | ||
| for line in code.lines() { | ||
| if line.starts_with("use ") && line.contains(symbol_name) { |
There was a problem hiding this comment.
Will this handle cases where symbol names are aliased and not necessarily what we're looking for?
Eg, use cot::common_types::Email and use cot::email::Email as EmailService. If we have common_types::Email in COMMON_IMPORTS, then use cot::email::Email as EmailService could still be a match. No?
There was a problem hiding this comment.
That's true, but I think it's fine, isn't it? When one imports an item with an alias, it's typically either to assign a better-suited name to it, or to avoid conflicts with other items with the same names. In either case I think we want to opt out of importing such item the second time as part of the common imports.
Does that make sense?
| self | ||
| } | ||
|
|
||
| /// Add a cache to the request builder. |
There was a problem hiding this comment.
wondering if we'll also need to add one for email
There was a problem hiding this comment.
Ah! Interesting that we don't have it already. Let me add one.
This adds a test framework to test the code snippets in the guide (the docs/ directory). Currently, the framework defines four different test configurations: * `toml`, tested by parsing as a Cot config file * `html.j2`, tested by trying to build an Askama template * `rust`, tested by building the snippet as an extension of a default project template (as generated by `cot-cli`) * `rust,has_main`, a version of the above that also defines its own `main` function It should be fairly easy to extend this by defining more configurations, if needed. The framework defines a number of utilities to make the testing more pleasant: * Many symbols are imported by default * Similarly to rustdoc, lines can be prepended with `# ` to indicate lines that are not displayed in the guide, but required for the test to build * For the Askama tests, there are some dummy files and structures created This is part of the major effort to make the guide always up-to-date. As a result of this, a number of code snippets were modified as they did not build before. This concludes the work on #471.
This adds a test framework to test the code snippets in the guide (the docs/ directory). Currently, the framework defines four different test configurations:
toml, tested by parsing as a Cot config filehtml.j2, tested by trying to build an Askama templaterust, tested by building the snippet as an extension of a default project template (as generated bycot-cli)rust,has_main, a version of the above that also defines its ownmainfunctionIt should be fairly easy to extend this by defining more configurations, if needed.
The framework defines a number of utilities to make the testing more pleasant:
#to indicate lines that are not displayed in the guide, but required for the test to buildThis is part of the major effort to make the guide always up-to-date. As a result of this, a number of code snippets were modified as they did not build before.
This concludes the work on #471.
Type of change