Skip to content

feat(rules): inline-tests moves inline tests over 30% of their file to a file of their own - #28

Draft
PiquelChips wants to merge 1 commit into
feat/rule-childless-mod-rsfrom
feat/rule-inline-tests
Draft

PiquelChips wants to merge 1 commit into
feat/rule-childless-mod-rsfrom
feat/rule-inline-tests

Conversation

@PiquelChips

@PiquelChips PiquelChips commented Nov 22, 2026 •

Copy link
Copy Markdown
Member

What and why

Unit tests belong in #[cfg(test)] mod tests; with a sibling tests.rs (MT3). As the owner decided, an inline test module may stay only while it makes up at most 30% of its file. piqueld and DirkEngine each list a deviation for inline test blocks, and nothing checks the rule. inline-tests fails on a larger one, and --fix moves it out.

  • What counts: an inline #[cfg(test)] module (any cfg requiring test, such as all(test, unix)) at the top level of a file that some target reaches outside test code, according to the base's module walk. Its lines run from its first attribute to its closing brace, compared with the file's line count. The rule skips integration tests, files only test code reaches, and generated files.
  • --fix works when the braces sit alone at the ends of their lines (rustfmt's layout):
    • The body moves to <module>.rs in the module's directory, with the module's indentation removed. Lines inside a multi-line string literal keep theirs.
    • mod tests; takes the body's place, keeping the attributes.
    • A foo.rs first moves to foo/mod.rs, as self_named_module_files requires. Relative paths in include!, include_str! and include_bytes! then gain a ../: piqueld has one.
    • A crate root other than lib.rs/main.rs (src/bin/tool.rs) gets no fix, since its tests.rs would become a target of its own.
    • cargo fmt afterwards reflows lines the move made shorter.
  • childless-mod-rs (feat(rules): childless-mod-rs moves a mod.rs without children to foo.rs #20) counts the tests this rule moves out as a child, so the two fixes don't fight over a childless mod.rs.
  • Fix: its Edit gains line-range and create variants beside the TOML edits and moves. The created tests.rs names its source file as what it resolves, so a deviation scoped to that file blocks the whole move.
  • Module walk: the base's Modules now records which files only test code reaches (integration tests, benchmarks, modules behind a test cfg). It exposes Modules::child_dir, which the walk and this fix share. test_code::TestAttributes reads cfg(test).

Stacked on #20.

Testing

  • Rebased onto the amended chain. just check-changed feat/rule-childless-mod-rs passes, and re-measuring gives the counts below again.
  • New fixture test:
    • A foo.rs and a childless mod.rs fail, each with an oversized test module. So does a one-line cfg(all(test, unix)) module in lib.rs.
    • A small module passes, and so does an integration test.
    • fix moves both multi-line modules: the literal's line keeps its indentation, and the include_str! path gains ../. The one-line module stays a violation.

Measured with rules::report, read-only, on detached worktrees of each repository's origin/main (since removed). The fix was applied to git archive copies:

top-level inline test modules over 30% (this rule) over 100 lines (fallback) left after --fix
piqueld a17710f52 124 48 (4 under 50 lines) 54 0
sourceorigin e99cc0e6b 10 1 (44 lines) 0 0
DirkEngine 07e8ba44 24 7 (9 and 20 lines the smallest) 12 0

30% is practical: it flags fewer modules than the 100-line fallback in piqueld and DirkEngine, and --fix moves every one. On the fixed copies, cargo check --tests passes for piqueld-core (including the include_str! case), piqueld-config, piquelctl, dirk_proc, dirk_utils and dirk_threads. rustfmt --check shows only line reflows. Once this lands, piqueld's and DirkEngine's inline-test deviations can go.

Checklist

  • The crate's README (its documentation on docs.rs) shows the change, and a repository's xtask can still follow it. (just readme, and --fix's list in Commands.)
  • A change to the public API is noted for the next release, with what each repository's xtask must change. (A new Rule variant; no xtask change.)
  • just check-changed passes.

Note

Written by Claude Opus 5.5 in Claude Code (via T3 Code)

…e of their own

An inline #[cfg(test)] module at a file's top level spans at most 30% of
the file's lines, in files a target reaches outside test code. --fix moves
a larger one's body to tests.rs, dedented except inside string literals,
leaves mod tests; in its place, moves foo.rs to foo/mod.rs when it needs a
directory, and points relative include paths one directory up.
childless-mod-rs counts the tests it moves out as a child.

Fix gains line-range and create edits beside TOML edits and moves. The
module walk records which files only test code reaches, and where a
module's children live.
@PiquelChips
PiquelChips force-pushed the feat/rule-inline-tests branch from abdb91d to 6c41b93 Compare November 22, 2026 22:14
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