test(tokenless): raise unit test coverage from 75% to 90%#1398
test(tokenless): raise unit test coverage from 75% to 90%#1398Forrest-ly wants to merge 5 commits into
Conversation
Add ~170 unit tests across all 4 tokenless crates to improve code coverage from 75.36% to 90.09%, covering edge cases in compression, stash store, env_check, stats recording, and CLI dispatch. Key changes: - Extract run_command() from run() in main.rs for unit testability - Add symlink, env var override, dry-run mode tests for CLI - Add stash round-trip, depth truncation, CJK tests for schema compressor - Add schema migration, corrupt row, SLS writer tests for stats - Add Default trait, stash error conversion tests for ccr - Reformat multi-line assertions in integration tests for tarpaulin accuracy Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
PR 评审结论该 PR 为 4 个 tokenless crate 新增约 170 个单元测试,将覆盖率从 75% 提升至 90%。唯一的生产代码改动是一次干净、最小化的重构:从 但若干新增测试在开发机上运行并不安全,另一些在 CI 中会 flaky。合并前应修复。 1. 高危 — 破坏性测试会清空/篡改开发者真实的 tokenless 状态
这正是 env-override 测试已经正确采用的模式(如 `get_db_path_with_env_override` 将 `TOKENLESS_STATS_DB` 指向 tempdir)。stats/stash 命令测试应照此办理——在测试中将该 env var 指向一个 tempdir DB,结束时移除。当前情况下,在开发机上 `cargo test --workspace` 是个隐患。 2. 中危 — 依赖网络的单元测试
这些在离线/沙箱 CI 中会 flaky,且并非真正的单元测试。建议用 `#[ignore]` 配合 network feature 门控,或 mock `check_network`。 3. 中危 — env var 变更无序列化保证,存在竞态workspace 为 edition 2024,`std::env::set_var`/`remove_var` 为 `unsafe`——测试中已正确包裹。但正确性完全依赖于测试计划中的 `--test-threads=1`。直接 `cargo test`(默认并行)会让这些进程级 env 变更产生竞态:`TOKENLESS_STATS_DB`、`TOKENLESS_STASH_DB`、`TOKENLESS_STATS_ENABLED`、`TOKENLESS_SLS_ENABLED`、`TOKENLESS_COMPRESSION_ENABLED`、`TOKENLESS_TOOL_READY_SPEC` 均在多测试间被变更,且无 `serial_test`/mutex。建议要么引入 `serial_test`(或 `std::sync::Mutex` 守卫),要么将被测函数重构为接受参数而非读取进程 env;至少在测试旁注释单线程要求。 4. 低危 — 100 MB 文件写入`read_input_file_too_large` 向 tempdir 写入 100 MiB + 1。作为单元测试过慢过占磁盘;建议加 `#[ignore]`,或用更小的边界值同样触发 limit 检查。 结论测试覆盖的新增价值很高,生产代码重构也干净。但发现 #1 为阻塞项——在贡献者机器上运行该测试套件会静默清空其真实 `~/.tokenless/stats.db` 并翻转其配置。合并前请修复 stats/stash 命令测试的 DB 隔离;#2/#3 可同步处理或作为紧随跟进项。 |
Tests that mutated TOKENLESS_SLS_ENABLED via set_var raced with the Stats Enable/Disable test (which calls TokenlessConfig::load() then save()), writing sls_enabled=false to ~/.tokenless/config.json. The tokenless-stats test binary reads that file concurrently and fails. Replaced 9 unsafe env-var-mutating tests with a safe alternative that calls Stats Disable then Status to cover the DISABLED display branch. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- Add TempDbGuard to redirect stats/stash DB to temp dirs under $HOME - Serialize env var mutations with Mutex in both main_tests and env_check_tests - Remove tests that write to real config (save() path cannot be redirected) - Mark network-dependent tests with #[ignore] - Remove httpbin URL from write_test_spec to prevent real HTTP calls - Reduce read_input_file_too_large from 100MB to 64MB+1 (matches MAX_INPUT_BYTES) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
@shiloong 感谢详细的 review,所有 4 个问题已在最新提交 ( 1. ✅ 高危 — DB 隔离 (已修复)新增
所有写 DB 的测试已改用 另外, 2. ✅ 中危 — 网络依赖 (已修复)
3. ✅ 中危 — env var 竞态 (已修复)
4. ✅ 低危 — 100 MB 文件 (已修复)
测试结果:432 passed, 0 failed, 2 ignored (网络测试)。 |
validate_db_path canonicalizes the parent directory, which fails on CI where ~/.tokenless/ does not exist yet. Create the directory first. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Summary
run()提取run_command()函数以支持 CLI dispatch 单元测试各 crate 覆盖率
新增测试覆盖
Test plan
cargo test --workspace -- --test-threads=1全部 443 个测试通过cargo tarpaulin --workspace覆盖率 90.09%