Agent Skillszerx-lab/zap › rust-unit-tests

rust-unit-tests

GitHub

指导在 warp Rust 代码库中编写、改进和运行 crate 级单元测试,涵盖测试文件结构、断言规范、异步处理及 UI/模型测试工具使用。

.agents/skills/rust-unit-tests/SKILL.md zerx-lab/zap

Trigger Scenarios

需要为 Rust 函数或模块编写单元测试 优化现有 Rust 测试用例以提高覆盖率或清晰度 配置 Rust 项目的测试结构和依赖注入

Install

npx skills add zerx-lab/zap --skill rust-unit-tests -g -y
More Options

Non-standard path

npx skills add https://github.com/zerx-lab/zap/tree/main/.agents/skills/rust-unit-tests -g -y

Use without installing

npx skills use zerx-lab/zap@rust-unit-tests

指定 Agent (Claude Code)

npx skills add zerx-lab/zap --skill rust-unit-tests -a claude-code -g -y

安装 repo 全部 skill

npx skills add zerx-lab/zap --all -g -y

预览 repo 内 skill

npx skills add zerx-lab/zap --list

SKILL.md

Frontmatter
{
    "name": "rust-unit-tests",
    "description": "Write, improve, and run Rust unit tests in the warp Rust codebase."
}

Rust Unit Tests in warp

Scope

  • This skill focuses on crate-level unit tests.
  • Favor incremental, well-scoped tests that exercise a single function or behavior per case.

Where unit tests live

  • Put unit tests in separate files named ${filename}_tests.rs or mod_test.rs.
  • Include the test module at the end of the corresponding source file:
#[cfg(test)]
#[path = "filename_tests.rs"] // or "mod_test.rs"
mod tests;

Writing good tests

  • Use descriptive names: fn parses_utf8_sequence_when_valid().
  • Prefer assert_eq!/assert_ne! over assert! for clearer diffs.
  • Use #[should_panic] only when panic semantics are intended API.
  • Minimize global state; inject dependencies via traits/constructors to make logic testable without heavy mocking.
  • When adding enums or expanding behavior, prefer exhaustive matches in code under test and mirror cases in tests.
  • Be mindful of terminal model locking: avoid patterns that acquire multiple model.lock() calls in the same call stack from tests.

Async and feature-gated code

  • For async logic, use #[tokio::test] when the code requires a runtime.
  • Prefer runtime feature checks (e.g., FeatureFlag::X.is_enabled()) over #[cfg(...)] so tests don’t require recompilation to toggle behavior.

Quickstart harness (UI/model tests)

  • Prefer warpui::App::test for deterministic unit tests around views/models.
  • Initialize app models once, then mutate via update and assert via read.
use warpui::App;
// In app crate tests prefer `crate::test_util::...`; from other crates use `warp::test_util::...`.
use warp::test_util::{terminal::initialize_app_for_terminal_view, add_window_with_terminal};

#[test]
fn example() {
    App::test((), |mut app| async move {
        // One-time app setup for terminal/view tests
        initialize_app_for_terminal_view(&mut app); // includes settings init
        let term = add_window_with_terminal(&mut app, None);

        // Act
        term.update(&mut app, |view, _ctx| {
            view.model.lock().simulate_block("ls", "out");
        });

        // Assert
        term.read(&app, |view, _ctx| {
            assert!(view.model.lock().block_list().len() > 0);
        });
    })
}

Common helpers to use

  • Terminal model shortcuts: TerminalModel::mock(..), .simulate_block(..), .finish_block(), .simulate_cmd(..).
  • Builders for focused tests: terminal::model::test_utils::{TestBlockListBuilder, TestBlockBuilder}.
  • Virtual filesystem for IO-heavy code:
use virtual_fs::{VirtualFS, Stub};
VirtualFS::test("case", |_dirs, mut fs| {
    fs.with_files(vec![Stub::FileWithContent("path/file.txt", "contents")]);
    // run logic and assert
});
  • Feature flags (scoped):
use warp::features::FeatureFlag; // or `use crate::features::FeatureFlag;` inside the app crate
let _flag = FeatureFlag::CreatingSharedSessions.override_enabled(true);
  • UI numeric assertions (lines):
assert_lines_approx_eq!(actual_lines, INLINE_BANNER_HEIGHT);
  • Concurrency: keep model.lock() scopes minimal; avoid nested/re-entrant locks in the same call chain.
  • Don’t call initialize_settings_for_tests directly when using initialize_app_for_terminal_view (it already calls it).
  • Async needs: use #[tokio::test] when a real runtime is required; otherwise prefer App::test.
  • Tests touching global/external state: consider serial_test's #[serial] or local mocking instead of parallelism.

Running unit tests

  • Workspace (parallel):
cargo nextest run --no-fail-fast --workspace --exclude command-signatures-v2
  • Single crate:
cargo nextest run -p <crate_name>
  • Single test (filter by name):
cargo nextest run -E 'test(<substring>)'
  • Doc tests:
cargo test --doc

Linting and formatting

Run before submitting changes:

cargo fmt
cargo clippy --workspace --all-targets --all-features --tests -- -D warnings

For a full local check before a PR, you can also run:

./script/presubmit

Version History

  • 5d87445 Current 2026-07-24 17:35

Same Skill Collection

.agents/skills/add-feature-flag/SKILL.md
.agents/skills/add-telemetry/SKILL.md
.agents/skills/dedupe-issue-local/SKILL.md
.agents/skills/diagnose-ci-failures/SKILL.md
.agents/skills/fix-errors/SKILL.md
.agents/skills/implement-specs/SKILL.md
.agents/skills/promote-feature/SKILL.md
.agents/skills/remove-feature-flag/SKILL.md
.agents/skills/resolve-merge-conflicts/SKILL.md
.agents/skills/review-pr-local/SKILL.md
.agents/skills/review-pr/SKILL.md
.agents/skills/spec-driven-implementation/SKILL.md
.agents/skills/triage-issue-local/SKILL.md
.agents/skills/update-skill/SKILL.md
.agents/skills/warp-integration-test/SKILL.md
.agents/skills/warp-ui-guidelines/SKILL.md
.agents/skills/write-product-spec/SKILL.md
.agents/skills/write-tech-spec/SKILL.md
resources/bundled/mcp_skills/figma/figma-code-connect-components/SKILL.md
resources/bundled/mcp_skills/figma/figma-create-design-system-rules/SKILL.md
resources/bundled/mcp_skills/figma/figma-create-new-file/SKILL.md
resources/bundled/mcp_skills/figma/figma-generate-library/SKILL.md
resources/bundled/mcp_skills/figma/figma-implement-design/SKILL.md
resources/bundled/skills/add-mcp-server/SKILL.md
resources/bundled/skills/create-skill/SKILL.md
resources/bundled/skills/create-tab-config/SKILL.md
resources/bundled/skills/feedback/SKILL.md
resources/bundled/skills/modify-settings/SKILL.md
resources/bundled/skills/pr-comments/SKILL.md
resources/bundled/skills/tab-configs/SKILL.md
resources/bundled/skills/update-tab-config/SKILL.md
resources/channel-gated-skills/dogfood/triage-vulnerabilities/SKILL.md
resources/bundled/mcp_skills/figma/edit-figma-design/SKILL.md
resources/bundled/mcp_skills/figma/figma-generate-design/SKILL.md
resources/bundled/mcp_skills/figma/figma-use/SKILL.md
resources/bundled/skills/claude-api/SKILL.md

Metadata

Files
0
Version
5d87445
Hash
89cfc959
Indexed
2026-07-24 17:35

inicio - Wiki
Copyright © 2011-2026 iteam. Current version is 2.155.2. UTC+08:00, 2026-08-16 20:12
浙ICP备14020137号-1 $mapa de visitantes$