Skip to content

Commit b36c8c6

Browse files
authored
test(rust): add filesystem fixture support (#1136)
* test(rust): add filesystem fixture support Add an internal ccusage-test-support crate that wraps assert_fs TempDir for Rust tests. The helper provides an fs_fixture! macro for inline fixture trees and a Fixture type for incremental setup, so tests can rely on drop-based cleanup instead of hand-rolled env::temp_dir paths. Use the helper in representative ccusage tests to exercise both the macro and incremental setup patterns. This gives future Rust fixture tests a concrete local example while preserving existing test behavior. * docs(skills): fold fixture guidance into test skills Move TypeScript fs-fixture guidance out of a standalone skill and into the Vitest reference, where it is used. Document the new Rust filesystem fixture helper in ccusage-testing so agents can find the Rust workflow from the Rust test skill. Remove the root skill routing entry for fs-fixture because the dedicated skill is no longer the right discovery surface. * test(rust): tighten fixture helper polish Add must_use annotations to fixture helper methods that return paths so accidental ignored path values are visible during test development. Remove the hardcoded fs-fixture pnpm version from the Vitest reference and keep the version-agnostic lookup command instead.
1 parent d96cb92 commit b36c8c6

10 files changed

Lines changed: 385 additions & 68 deletions

File tree

‎.agents/skills/ccusage-testing/SKILL.md‎

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,44 @@ direnv exec . pnpm run test
2929
Put Rust unit tests near the module they exercise with `#[cfg(test)] mod tests`. When splitting a large module, move its tests with the code instead of leaving unrelated tests in `main.rs`.
3030

3131
Use fixture-backed Rust tests for parser, path discovery, SQLite loading,
32-
dedupe, aggregation, pricing, and CLI output parity. For focused cargo commands,
33-
read `../tdd/references/rust-running.md`; for Rust test syntax examples, read
32+
dedupe, aggregation, pricing, and CLI output parity. Prefer
33+
`ccusage-test-support` for temporary filesystem setup instead of hand-rolled
34+
`env::temp_dir()` paths. For focused cargo commands, read
35+
`../tdd/references/rust-running.md`; for Rust test syntax examples, read
3436
`../tdd/references/rust-test-examples.md`.
3537

38+
## Rust Filesystem Fixtures
39+
40+
Use the internal `ccusage-test-support` crate for Rust tests that need temporary
41+
files or directories. The fixture owns an `assert_fs::TempDir`, so everything is
42+
removed automatically when the fixture variable drops at the end of the test.
43+
44+
For small inline trees, prefer `fs_fixture!`:
45+
46+
```rust
47+
use ccusage_test_support::fs_fixture;
48+
49+
let fixture = fs_fixture!({
50+
"projects/example/session.jsonl": "{}\n",
51+
});
52+
53+
let file_path = fixture.path("projects/example/session.jsonl");
54+
```
55+
56+
For incremental setup, use `Fixture` directly:
57+
58+
```rust
59+
use ccusage_test_support::Fixture;
60+
61+
let fixture = Fixture::new();
62+
fixture.create_dir_all("projects/example/session");
63+
fixture.write_file("projects/example/session/chat.jsonl", "{}\n");
64+
```
65+
66+
Keep the fixture variable alive for as long as paths under it are used. Do not
67+
return or store only a `PathBuf` from a short inner scope, because dropping the
68+
fixture removes the directory.
69+
3670
## Test Readability
3771

3872
- Avoid `try`/`catch` in tests for expected failures. Use `Result` tests, `matches!`, or explicit error assertions.

‎.agents/skills/ccusage-typescript/references/vitest.md‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,30 @@ prefer Rust tests through `ccusage-testing`.
2121
- Skipped local-data smoke tests are acceptable when real user log directories
2222
catch schema drift, but they must not fail on clean CI machines.
2323

24+
Locate and read the local fs-fixture README before using less common options or
25+
APIs:
26+
27+
```sh
28+
fd -a README.md node_modules/.pnpm | rg "fs-fixture"
29+
```
30+
31+
Prefer `await using` so cleanup is automatic:
32+
33+
```ts
34+
import { createFixture } from 'fs-fixture';
35+
36+
await using fixture = await createFixture({
37+
'projects/example/session.jsonl': '{}\n',
38+
});
39+
40+
const filePath = fixture.getPath('projects/example/session.jsonl');
41+
```
42+
43+
Use object trees for small inline fixtures. Use template directory input when
44+
many tests share the same larger fixture shape. Prefer `fixture.getPath(...)`
45+
over manually joining against `fixture.path`, and use `fixture.writeFile()` or
46+
`fixture.writeJson()` when a test needs to build data incrementally.
47+
2448
## Assertions
2549

2650
- Prefer behavior-focused tests over schema-shape tests unless schema

‎.agents/skills/fs-fixture/SKILL.md‎

Lines changed: 0 additions & 44 deletions
This file was deleted.

‎CLAUDE.md‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ Use these skills before working in this repository:
1111
- `ccusage-rust-profile` - native Rust CLI profiling, branch-vs-main speed comparisons, profile reading, and optimization validation.
1212
- `ccusage-testing` - Rust cargo tests, CLI snapshots, Claude model names, and LiteLLM pricing test rules.
1313
- `ccusage-typescript` - TypeScript package/tooling work, Vitest tests, Bun scripts, package launchers, schema tooling, and benchmark scripts.
14-
- `fs-fixture` - disposable filesystem trees for tests with `createFixture()` and local README lookup.
1514
- `ccusage-agent-sources` - agent adapter log locations, token mappings, cost rules, and CLI behavior.
1615
- `ccusage-docs` - VitePress docs structure, screenshot placement, accessibility, and markdown linting conventions.
1716
- `skill-creator` - repo-local skill creation, SKILL.md frontmatter, description trigger quality, and reference layout.

0 commit comments

Comments
 (0)