Skip to content

refactor(stack-encrypt): move long test blocks into tests.rs files - #2055

Draft
coderdan wants to merge 3 commits into
nextfrom
refactor/long-test-blocks-to-files
Draft

coderdan wants to merge 3 commits into
nextfrom
refactor/long-test-blocks-to-files

Conversation

@coderdan

@coderdan coderdan commented Dec 5, 2026

Copy link
Copy Markdown
Contributor

Summary

This pull request moves each long inline test block in two crates into a file of its own. stack-encrypt is the crate that encrypts records and builds their search terms. vitaminc-aead is the crate that seals each field value with authenticated encryption. In both crates, some source files have far more test code than library code. In src/dynamic/record.rs the 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.rs keeps the line mod tests; with the attributes the block had, and the tests move to src/foo/tests.rs. For src/foo/mod.rs the tests move to src/foo/tests.rs. Three test modules have other names, so their files have those names: credential_tests, label_tests and test.

File the block moved out of Test module Block length (lines) New file
packages/stack-encrypt/src/dynamic/record.rs tests 6,976 dynamic/record/tests.rs
packages/stack-encrypt/src/dynamic/query.rs tests 1,673 dynamic/query/tests.rs
packages/stack-encrypt/src/dynamic/term.rs tests 1,273 dynamic/term/tests.rs
packages/stack-encrypt/src/json/derive.rs tests 1,059 json/derive/tests.rs
packages/stack-encrypt/src/keyset.rs tests 937 keyset/tests.rs
packages/stack-encrypt/src/codes.rs tests 731 codes/tests.rs
packages/stack-encrypt/src/dynamic/kind.rs tests 714 dynamic/kind/tests.rs
packages/stack-encrypt/src/json/predicate.rs tests 609 json/predicate/tests.rs
packages/stack-encrypt/src/sem/tokenize.rs tests 603 sem/tokenize/tests.rs
packages/stack-encrypt/src/dynamic/record/source.rs tests 528 dynamic/record/source/tests.rs
packages/stack-encrypt/src/sem/mod.rs tests 505 sem/tests.rs
packages/stack-encrypt/src/dynamic/record/planned.rs tests 498 dynamic/record/planned/tests.rs
packages/stack-encrypt/src/dynamic/context.rs tests 416 dynamic/context/tests.rs
packages/stack-encrypt/src/contexts.rs tests 404 contexts/tests.rs
packages/stack-encrypt/src/cipher.rs credential_tests 386 cipher/credential_tests.rs
packages/stack-encrypt/src/audit.rs tests 381 audit/tests.rs
packages/stack-encrypt/src/json/text.rs tests 343 json/text/tests.rs
packages/stack-encrypt/src/descriptor.rs label_tests 316 descriptor/label_tests.rs
packages/stack-encrypt/src/testing.rs tests 315 testing/tests.rs
packages/vitaminc-aead/src/leaf.rs tests 1,061 leaf/tests.rs
packages/vitaminc-aead/src/typed.rs tests 926 typed/tests.rs
packages/vitaminc-aead/src/plaintext/sources.rs tests 682 plaintext/sources/tests.rs
packages/vitaminc-aead/src/encrypt_sink.rs tests 656 encrypt_sink/tests.rs
packages/vitaminc-aead/src/frame.rs tests 605 frame/tests.rs
packages/vitaminc-aead/src/seal.rs tests 505 seal/tests.rs
packages/vitaminc-aead/src/frame/owned.rs tests 494 frame/owned/tests.rs
packages/vitaminc-aead/src/aes/cipher.rs test 443 aes/cipher/test.rs
packages/vitaminc-aead/src/plaintext/kinds.rs tests 427 plaintext/kinds/tests.rs
packages/vitaminc-aead/src/aad/leaf_type.rs tests 384 aad/leaf_type/tests.rs

The length counts the mod NAME { line and the closing brace. The new files are under each crate's src/.

Inside each moved block, only these things differ:

  • Each line has one level less indentation. A line that starts inside a string literal that spans lines keeps its bytes.
  • rustfmt reformats lines that now fit. In four places it also removes the braces around a closure body that now fits on one line. The four closures are in dynamic/query/tests.rs (one) and dynamic/record/tests.rs (three).
  • Two include_str! paths are relative to the source file, so each now has one more ../. One is the label fixture in descriptor/label_tests.rs. The other is the conformance corpus in leaf/tests.rs.

Three files outside the blocks name a moved test by its file. Each now names the new file:

  • the _comment field in packages/stack-encrypt/tests/fixtures/label_segments.json
  • the module doc of packages/stack-encrypt/tests/contexts.rs
  • two rows of conformance/stack-encrypt/coverage.md, at lines 14 and 142

There 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.

  • Test lists before and after, sorted, with the ignored marker on each test (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 aes with RUSTFLAGS="--cfg vitaminc_aead_rust_crypto", as test:vitaminc-aead:features runs it: 394 before, 394 after, identical.
  • A token check of each new file against the block it came from. The check compares every token, every string literal byte for byte, and every comment up to whitespace. The check ignores a comma before a closing bracket. Every file matched, except for the two 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 is stored_rows::write_stored_rows_v1. It is marked as ignored before and after the move. The run includes the trybuild ui binary, and no .stderr snapshot changed. The @0.10.0 is needed because the lockfile also has vitaminc-aead 0.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.
  • The vitaminc-aead runs of test:vitaminc-aead:features: cargo test -p vitaminc-aead (258 unit tests), with --features aes (306), and with --features aes and 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-aead tests on wasm32-wasip1.

Related

Refs #2040.

Review notes

  • The rule is "a test block longer than about 300 lines". I moved the blocks of 315 lines and more. Four blocks under 300 lines stay in place. In stack-encrypt: wire_bytes in dynamic/query.rs (299) and tests in descriptor.rs (295). In vitaminc-aead: tests in plaintext/value_kind.rs (280) and tests in context.rs (275). So descriptor.rs now has one test module inline and one in a file.
  • Each moved block was the last item in its file, or only test modules came after it. So no line of library code moved. The trybuild snapshots did not change. The line-number entries in .cargo/mutants.toml still 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.yml names leaf.rs and frame.rs for the storage reader code in them, not for their tests, so it does not change.
  • Lines 80 and 225 to 239 of conformance/stack-encrypt/coverage.md record tests that were removed, by the file they were in. I left those rows as they are.

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
@changeset-bot

changeset-bot Bot commented Dec 5, 2026 •

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: dee3333

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@coderabbitai

coderabbitai Bot commented Dec 5, 2026

Copy link
Copy Markdown
Contributor

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

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 coderdan left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 on next, ignoring whitespace and commas.
  • 25 files are identical.
  • 4 files differ, each in a way the description names. Two have one more ../ in an include_str! path. In two, rustfmt removed the braces of a closure whose body now fits on one line: one closure in dynamic/query/tests.rs and three in dynamic/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.

This branch has not been deployed

No deployments
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