testing-patterns

A set of rules for writing tests in a Rust project using real temporary files and isolated environments. Each test gets its own temporary setup that is cleaned up afterward.

In plain words
What is it for?
It helps structure tests that use real filesystem operations, temporary directories, environment variables, and asynchronous code without tests interfering with one another.
Why use it?
It avoids fragile mock setups and makes tests behave more like the production code they are checking.

Cursor rule for Cursor

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add rules/cafreeman/foundry-mcp/testing-patterns
Clone the repo
git clone --depth 1 https://github.com/cafreeman/foundry-mcp

Made for: Cursor.

Per session 14 Only the description is in the session, so the agent can decide to use it. The body loads when it is invoked.
When invoked 1,392 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.00014 $0.01392
Opus 5 $0.00007 $0.00696
Sonnet 5 $0.00003 $0.00278
Haiku 4.5 $0.00001 $0.00139

Measured yesterday against content hash 3a0a48c248b7, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

testing-patterns scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured yesterday.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

.cursor/rules/testing-patterns.mdc · 213 lines

How it starts

The opening of the file, as written. The whole thing — 213 lines — stays where its author put it; the contents beside it link to each section on GitHub.

Foundry MCP Testing Patterns & Infrastructure

Overview

This project uses a modern, reliable testing approach with assert_fs + temp-env that provides perfect isolation and real filesystem operations. The old trait-based mocking approach was replaced due to "leaky abstractions" and complexity.

Core Testing Philosophy

Why This Approach Won:

  • Real Operations: Tests use actual filesystem calls, matching production exactly
  • Perfect Isolation: Each test gets unique temporary environment with automatic cleanup
  • No Mocking Complexity: Eliminates fragile trait hierarchies and dependency injection
  • Production Fidelity: Tests catch real bugs that mocked tests might miss

TestEnvironment Pattern

Basic Structure

#[test]
fn test_name() {
    use crate::test_utils::TestEnvironment;
    let env = TestEnvironment::new().unwrap();

    let _ = env.with_env_async(|| async {
        // Test logic with real filesystem operations
        // Full environment isolation guaranteed
    });
}

Key Components

  • TestEnvironment: Manages temporary directories and environment variables
  • with_env_async: Bridges sync temp-env with async test execution
  • assert_fs::TempDir: Automatic cleanup and cross-platform support
  • temp-env: Scoped environment variable management

Environment Isolation

How It Works

// Each test gets isolated environment:
// - HOME = temp directory
// - CURSOR_CONFIG_DIR = temp/.cursor
// - CLAUDE_CONFIG_DIR = temp/.claude
// - Foundry specs = temp/.foundry

env.with_env_async(|| async {
    // Test runs in completely isolated environment
    // No risk of polluting user's real ~/.foundry
});

Benefits

  • Parallel Execution: Tests can run simultaneously without interference
  • Panic Safety: Environment restored even if tests panic
  • Automatic Cleanup: No manual teardown required
  • Cross-Platform: Works consistently across macOS, Linux, Windows

Helper Methods

Read the full file on GitHub · 213 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. yesterday First seen · 213 lines · 14 tokens per session scan A 3a0a48c248b7

Subscribe to this mod's changes

testing-patterns is a cursor rule published in the GitHub repository cafreeman/foundry-mcp (4 stars, last pushed 5mo ago), licensed MIT. It adds 14 tokens to every session and 1,392 once invoked, about $0.0001 per session on Opus 5. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-31.