Repository navigation
Conversation
A long inline test block makes a source file hard to read. The code a reader wants sits among hundreds of lines of tests. In dynamic/record.rs the tests are nearly seven times as long as the code. Each test block over 300 lines moves to a file beside its module. The file has the name of the test module, so each test keeps its module path and its name. The move changes no test. The descriptor tests read the label fixture through a path relative to the source file, so that path now has one more `../`. The fixture's comment and the coverage table now name the new files. Refs #2040
A long inline test block makes a source file hard to read. The code a reader wants sits among hundreds of lines of tests. Each test block over 300 lines moves to a file beside its module. The file has the name of the test module, so each test keeps its module path and its name. The move changes no test. The leaf tests read the conformance corpus through a path relative to the source file, so that path now has one more `../`. Refs #2040
|
|
Important Draft PR not reviewedDraft PRs are not automatically reviewed by default.
To automatically review draft PRs, update your CodeRabbit configuration: reviews:
auto_review:
drafts: true |
The CRAP (Change Risk Anti-Patterns) gate scores every function under the crate's source folder and fails on a complex function with no coverage. The gate skipped test code in two ways: a `tests/` directory by an exclude, and an inline test module because the tool does not score one. A test module in a file of its own is neither. So the gate scored 346 test functions as production code and failed on seven of them. The task now excludes `tests.rs` and `*_tests.rs`, as the stack-auth task already does for `tests.rs`. With the excludes, the tool scores the same 1,779 functions as it does on the base branch. Refs #2040
coderdan
left a comment
There was a problem hiding this comment.
Review of this pull request at eaad50f66. One finding, fixed in dee3333d8.
How the move was checked
GitHub will not show a diff of this size, so a script compared each moved block with the block it came from:
- For each of the 29 new files, the script put the file back into its parent module in place of the
mod …;line. The script then compared the result with the parent file onnext, ignoring whitespace and commas. - 25 files are identical.
- 4 files differ, each in a way the description names. Two have one more
../in aninclude_str!path. In two, rustfmt removed the braces of a closure whose body now fits on one line: one closure indynamic/query/tests.rsand three indynamic/record/tests.rs. - Outside the moved blocks, three files change, and each change is a path in a comment or a doc table.
Finding: the CRAP gate scores the moved tests as production code
The CRAP (stack-encrypt) job failed on eaad50f66. The CRAP (Change Risk Anti-Patterns) gate scores every function under the crate's source folder. The gate skipped a tests/ directory by an exclude, and the tool does not score an inline test module. A test module in a file of its own is neither. So the gate scored 346 test functions and failed on seven of them, for example sample at 552.
dee3333d8 adds --exclude '**/tests.rs' --exclude '**/*_tests.rs' to the crap:stack-encrypt task in packages/stack-encrypt/tasks.toml. The stack-auth task already excluded **/tests.rs. With the excludes, cargo crap scores 1,779 functions on this branch and 1,779 on next, and the two sets match by file, function and complexity. That comparison used cargo-crap 0.2.2 on a laptop, with no coverage file. CI uses 0.6.1, so the job on dee3333d8 is the real check.
Not a finding, but a choice to confirm
Four test blocks of 275 to 299 lines stay inline, because the rule is "about 300 lines". So descriptor.rs has one test module inline and one in a file.
Summary
This pull request moves each long inline test block in two crates into a file of its own.
stack-encryptis the crate that encrypts records and builds their search terms.vitaminc-aeadis the crate that seals each field value with authenticated encryption. In both crates, some source files have far more test code than library code. Insrc/dynamic/record.rsthe tests are nearly seven times as long as the code.Each test block over 300 lines is now in a file beside its module. The file has the name of the test module, so every test keeps its module path and its name. The move changes no test and no library code.
Changes
A file
src/foo.rskeeps the linemod tests;with the attributes the block had, and the tests move tosrc/foo/tests.rs. Forsrc/foo/mod.rsthe tests move tosrc/foo/tests.rs. Three test modules have other names, so their files have those names:credential_tests,label_testsandtest.packages/stack-encrypt/src/dynamic/record.rstestsdynamic/record/tests.rspackages/stack-encrypt/src/dynamic/query.rstestsdynamic/query/tests.rspackages/stack-encrypt/src/dynamic/term.rstestsdynamic/term/tests.rspackages/stack-encrypt/src/json/derive.rstestsjson/derive/tests.rspackages/stack-encrypt/src/keyset.rstestskeyset/tests.rspackages/stack-encrypt/src/codes.rstestscodes/tests.rspackages/stack-encrypt/src/dynamic/kind.rstestsdynamic/kind/tests.rspackages/stack-encrypt/src/json/predicate.rstestsjson/predicate/tests.rspackages/stack-encrypt/src/sem/tokenize.rstestssem/tokenize/tests.rspackages/stack-encrypt/src/dynamic/record/source.rstestsdynamic/record/source/tests.rspackages/stack-encrypt/src/sem/mod.rstestssem/tests.rspackages/stack-encrypt/src/dynamic/record/planned.rstestsdynamic/record/planned/tests.rspackages/stack-encrypt/src/dynamic/context.rstestsdynamic/context/tests.rspackages/stack-encrypt/src/contexts.rstestscontexts/tests.rspackages/stack-encrypt/src/cipher.rscredential_testscipher/credential_tests.rspackages/stack-encrypt/src/audit.rstestsaudit/tests.rspackages/stack-encrypt/src/json/text.rstestsjson/text/tests.rspackages/stack-encrypt/src/descriptor.rslabel_testsdescriptor/label_tests.rspackages/stack-encrypt/src/testing.rsteststesting/tests.rspackages/vitaminc-aead/src/leaf.rstestsleaf/tests.rspackages/vitaminc-aead/src/typed.rsteststyped/tests.rspackages/vitaminc-aead/src/plaintext/sources.rstestsplaintext/sources/tests.rspackages/vitaminc-aead/src/encrypt_sink.rstestsencrypt_sink/tests.rspackages/vitaminc-aead/src/frame.rstestsframe/tests.rspackages/vitaminc-aead/src/seal.rstestsseal/tests.rspackages/vitaminc-aead/src/frame/owned.rstestsframe/owned/tests.rspackages/vitaminc-aead/src/aes/cipher.rstestaes/cipher/test.rspackages/vitaminc-aead/src/plaintext/kinds.rstestsplaintext/kinds/tests.rspackages/vitaminc-aead/src/aad/leaf_type.rstestsaad/leaf_type/tests.rsThe length counts the
mod NAME {line and the closing brace. The new files are under each crate'ssrc/.Inside each moved block, only these things differ:
dynamic/query/tests.rs(one) anddynamic/record/tests.rs(three).include_str!paths are relative to the source file, so each now has one more../. One is the label fixture indescriptor/label_tests.rs. The other is the conformance corpus inleaf/tests.rs.Three files outside the blocks name a moved test by its file. Each now names the new file:
_commentfield inpackages/stack-encrypt/tests/fixtures/label_segments.jsonpackages/stack-encrypt/tests/contexts.rsconformance/stack-encrypt/coverage.md, at lines 14 and 142There is no changeset, because the change is internal and no published surface changes.
Verification
I ran each command below with Rust 1.94.1 in the root Cargo workspace.
cargo nextest list --message-format json):-p stack-encrypt --all-features: 1,657 tests before, 1,657 after. The two lists are identical.-p vitaminc-aead --all-features: 444 before, 444 after, identical.-p vitaminc-aead --features aeswithRUSTFLAGS="--cfg vitaminc_aead_rust_crypto", astest:vitaminc-aead:featuresruns it: 394 before, 394 after, identical.include_str!paths and the four closure bodies listed above.cargo fmt --all --check: no changes.cargo clippy --locked --workspace --all-features --all-targets -- -D warnings: no warnings.cargo nextest run --locked -p stack-encrypt -p vitaminc-aead@0.10.0 --all-features: 2,100 tests run, 2,100 passed, 1 skipped. The skipped test isstored_rows::write_stored_rows_v1. It is marked as ignored before and after the move. The run includes the trybuilduibinary, and no.stderrsnapshot changed. The@0.10.0is needed because the lockfile also hasvitaminc-aead0.5.1 from crates.io.cargo test --locked --doc -p stack-encrypt -p vitaminc-aead@0.10.0 --all-features: 95 and 52 doc tests passed.RUSTDOCFLAGS="-D warnings" cargo doc --locked --no-deps -p stack-encrypt -p vitaminc-aead@0.10.0 --all-features, with and without--document-private-items: no warnings.vitaminc-aeadruns oftest:vitaminc-aead:features:cargo test -p vitaminc-aead(258 unit tests), with--features aes(306), and with--features aesand the RustCrypto cfg (304). All passed.I did not run the tests that need live CipherStash credentials. I did not rebuild the other Cargo workspaces: EQL (Encrypt Query Language, the SQL side), protect-ffi, recipher-js, the Go guests and the fuzz crates. Only test code moved, and test code is not part of what those workspaces build. I did not run the
vitaminc-aeadtests onwasm32-wasip1.Related
Refs #2040.
Review notes
stack-encrypt:wire_bytesindynamic/query.rs(299) andtestsindescriptor.rs(295). Invitaminc-aead:testsinplaintext/value_kind.rs(280) andtestsincontext.rs(275). Sodescriptor.rsnow has one test module inline and one in a file..cargo/mutants.tomlstill match. The cargo-mutants tool skips a module declared with#[cfg(test)]:cargo mutants --list-files(27.1.0) lists none of the new files..github/workflows/kani.ymlnamesleaf.rsandframe.rsfor the storage reader code in them, not for their tests, so it does not change.conformance/stack-encrypt/coverage.mdrecord tests that were removed, by the file they were in. I left those rows as they are.