| name | test-runner-worktree |
| description | Run Rust cargo tests properly in git worktrees with environment variable sourcing. Use when cargo test fails, when seeing authentication errors, when test results differ between runs, or before running pre-commit checks in worktrees. Essential for integration tests requiring LANGSMITH_API_KEY or ANTHROPIC_API_KEY. |
| scope | project |
Test Runner for Worktrees
Guide for running integration tests properly in both git worktrees and the main repository, with emphasis on environment variable sourcing and working directory context.
⚠️ CRITICAL FIRST STEP: Source Environment Variables
Before running ANY tests, pre-commit checks, or cargo commands in a worktree, ALWAYS source environment variables:
source /workspace/.devcontainer/.env
Why this matters:
- Integration tests require
LANGSMITH_API_KEY and other credentials
- Pre-commit checks (
cargo test, cargo clippy, etc.) will fail without proper environment
- Worktrees do NOT automatically inherit environment variables from the main workspace
- Tests will fail with authentication errors or panics if variables are missing
- This was the root cause of test failures in issue #232
This applies to:
- ✅
cargo test - Any test command
- ✅ Pre-commit checks - The full
cargo fmt && cargo check && cargo clippy && cargo test suite
- ✅
cargo clippy - Linting may trigger tests
- ✅
cargo build - Build may run build scripts that need credentials
- ✅ Any cargo command that might run tests or build scripts
Quick Check:
[ -n "$LANGSMITH_API_KEY" ] && echo "✓ Ready to test" || echo "✗ Run: source /workspace/.devcontainer/.env"
Quick Start for Pre-Commit Checks:
source /workspace/.devcontainer/.env && cargo fmt && cargo check --workspace --all-features && cargo clippy --workspace --all-features -- -D warnings && cargo test --workspace --all-features && cargo fmt --check
Overview
During development in git worktrees, running tests requires careful attention to:
- Environment variables - MUST be sourced from
/workspace/.devcontainer/.env before testing
- Working directory - Tests may behave differently in main vs worktree due to SDK version differences
- Context awareness - Understanding whether you're in main workspace or a feature branch worktree
This skill codifies the lessons learned from issues #186 and #232 to prevent future confusion and wasted time.
Key Principles
1. Always Check Environment Variables First
Problem: Repeatedly asking users for credentials when they're already available in the environment.
Solution: Check if environment variables are set BEFORE asking the user.
[ -n "$LANGSMITH_API_KEY" ] && echo "LANGSMITH_API_KEY is set" || echo "LANGSMITH_API_KEY not set"
[ -n "$ANTHROPIC_API_KEY" ] && echo "ANTHROPIC_API_KEY is set" || echo "ANTHROPIC_API_KEY not set"
If not set, source from devcontainer:
source /workspace/.devcontainer/.env
[ -n "$LANGSMITH_API_KEY" ] && echo "LANGSMITH_API_KEY is set" || echo "LANGSMITH_API_KEY not set"
Never expose actual values:
echo "LANGSMITH_API_KEY=$LANGSMITH_API_KEY"
[ -n "$LANGSMITH_API_KEY" ] && echo "Set" || echo "Not set"
2. Always Verify Current Working Directory
Problem: Running tests from the wrong directory leads to unexpected failures and confusion.
Key Difference: Tests run from worktrees use the feature branch SDK, while tests from main workspace use the main branch SDK.
Example from #186:
- Background tests ran from
/workspace (main branch) → FAILED (old SDK without Queued enum)
- Worktree tests ran from
/workspace/wip/codekiln-186-test-helpers → PASSED (new SDK with Queued from PR #185)
Solution: Always verify working directory before running tests.
pwd
cd /workspace/wip/<worktree-name>
cargo test --test integration_test -- --ignored --nocapture
3. Understand Test Types and Their Requirements
Different tests have different requirements and durations:
Unit Tests (No API Required):
cargo test --test integration_deployment_workflow test_deployment_url_extraction -- --nocapture
Helper Tests (API Required, 1-5 seconds):
cargo test --test integration_deployment_workflow test_list_github_integrations -- --ignored --nocapture
cargo test --test integration_deployment_workflow test_list_github_repositories -- --ignored --nocapture
cargo test --test integration_deployment_workflow test_find_integration_for_repo -- --ignored --nocapture
Full Workflow Tests (5-30 minutes):
cargo test --test integration_deployment_workflow test_deployment_workflow -- --ignored --nocapture
Workflows
Workflow 1: Run Tests in Worktree (Recommended)
When working on a feature branch in a worktree, always run tests from the worktree directory.
Steps:
cd /workspace/wip/<worktree-name>
source /workspace/.devcontainer/.env
pwd
[ -n "$LANGSMITH_API_KEY" ] && echo "✓ LANGSMITH_API_KEY is set" || echo "✗ LANGSMITH_API_KEY not set"
cargo test --test integration_deployment_workflow test_deployment_url_extraction -- --nocapture
cargo test --test integration_deployment_workflow test_list_github_integrations -- --ignored --nocapture
cargo test --test integration_deployment_workflow test_deployment_workflow -- --ignored --nocapture
Why this matters:
- Worktree has the feature branch code
- Feature branch may have SDK changes not in main
- Tests will use the correct SDK version
Workflow 2: Run Tests in Main Workspace
When testing against the main/release branch SDK, run from main workspace.
Steps:
cd /workspace
git branch --show-current
[ -n "$LANGSMITH_API_KEY" ] && echo "LANGSMITH_API_KEY is set" || echo "LANGSMITH_API_KEY not set"
cargo test --workspace --all-features
Workflow 3: Pre-Commit Testing
Before committing, run all checks from the worktree.
Steps:
cd /workspace/wip/<worktree-name>
source /workspace/.devcontainer/.env
cargo fmt
cargo check --workspace --all-features
cargo clippy --workspace --all-features -- -D warnings
cargo test --workspace --all-features
cargo fmt --check
Why from worktree:
- Tests your feature branch changes
- Catches issues before CI
- Prevents wasted CI cycles
Common Mistakes to Avoid
❌ Mistake 1: Running Pre-Commit Checks Without Sourcing Environment
Wrong approach:
cd /workspace/wip/<worktree-name>
cargo fmt && cargo check --workspace --all-features && cargo clippy --workspace --all-features -- -D warnings && cargo test --workspace --all-features && cargo fmt --check
Symptom: Tests fail with cryptic errors like:
byte index 8 is out of bounds of \``
thread 'main' panicked at cli/src/commands/prompt.rs:183:52
- Authentication failures in integration tests
Correct approach:
cd /workspace/wip/<worktree-name>
source /workspace/.devcontainer/.env
cargo fmt && cargo check --workspace --all-features && cargo clippy --workspace --all-features -- -D warnings && cargo test --workspace --all-features && cargo fmt --check
Why this happens:
- Worktrees don't inherit environment variables from main workspace
- Tests that require credentials fail without
LANGSMITH_API_KEY
- Some tests read empty environment variables and panic on string operations
- This is the #1 cause of mysterious test failures in worktrees
❌ Mistake 2: Asking User for Credentials When Already Set
Wrong approach:
echo "Please provide your LANGSMITH_API_KEY"
Correct approach:
if [ -z "$LANGSMITH_API_KEY" ]; then
source /workspace/.devcontainer/.env
if [ -z "$LANGSMITH_API_KEY" ]; then
echo "LANGSMITH_API_KEY not found. Please set it in /workspace/.devcontainer/.env"
fi
fi
❌ Mistake 3: Running Tests from Wrong Directory
Wrong approach:
cd /workspace
cargo test --test integration_deployment_workflow -- --ignored
Correct approach:
pwd
cd /workspace/wip/codekiln-186-test-helpers
cargo test --test integration_deployment_workflow -- --ignored
❌ Mistake 4: Exposing Sensitive Environment Variables
Wrong approach:
echo "LANGSMITH_API_KEY=$LANGSMITH_API_KEY"
echo "Using key: ${LANGSMITH_API_KEY}"
Correct approach:
[ -n "$LANGSMITH_API_KEY" ] && echo "LANGSMITH_API_KEY is set" || echo "LANGSMITH_API_KEY not set"
❌ Mistake 5: Not Understanding Test Context
Wrong approach:
cd /workspace
cargo test --test integration_test -- --ignored &
Correct approach:
cd /workspace/wip/<worktree-name>
cargo test --test integration_test -- --ignored
Environment Variable Reference
Required Variables for Integration Tests
LangSmith API:
LANGSMITH_API_KEY - API key for LangSmith authentication
LANGSMITH_ORGANIZATION_ID - Organization ID (optional, auto-detected)
Anthropic API:
ANTHROPIC_API_KEY - API key for Claude models
Checking Variables (Template)
echo "Checking required environment variables..."
[ -n "$LANGSMITH_API_KEY" ] && echo "✓ LANGSMITH_API_KEY is set" || echo "✗ LANGSMITH_API_KEY not set"
[ -n "$ANTHROPIC_API_KEY" ] && echo "✓ ANTHROPIC_API_KEY is set" || echo "✗ ANTHROPIC_API_KEY not set"
if [ -z "$LANGSMITH_API_KEY" ] || [ -z "$ANTHROPIC_API_KEY" ]; then
echo "Sourcing from /workspace/.devcontainer/.env..."
source /workspace/.devcontainer/.env
[ -n "$LANGSMITH_API_KEY" ] && echo "✓ LANGSMITH_API_KEY is set" || echo "✗ LANGSMITH_API_KEY not set"
[ -n "$ANTHROPIC_API_KEY" ] && echo "✓ ANTHROPIC_API_KEY is set" || echo "✗ ANTHROPIC_API_KEY not set"
fi
Test Execution Templates
Template 1: Run Single Test
cd /workspace/wip/<worktree-name>
source /workspace/.devcontainer/.env
pwd
cargo test --test <test_file> <test_name> -- --ignored --nocapture
Template 2: Run All Helper Tests
cd /workspace/wip/<worktree-name>
source /workspace/.devcontainer/.env
cargo test --test integration_deployment_workflow test_list_ -- --ignored --nocapture
Template 3: Run Long-Running Test in Background
cd /workspace/wip/<worktree-name>
source /workspace/.devcontainer/.env
cargo test --test integration_deployment_workflow test_deployment_workflow -- --ignored --nocapture > test_output.log 2>&1 &
echo $! > test_pid.txt
tail -f test_output.log
Troubleshooting
Tests Fail with "API key not found"
Symptom: Tests fail immediately with authentication errors.
Diagnosis:
[ -n "$LANGSMITH_API_KEY" ] && echo "Set" || echo "Not set"
Solution:
source /workspace/.devcontainer/.env
[ -n "$LANGSMITH_API_KEY" ] && echo "Set" || echo "Not set"
Tests Fail with "type/enum not found"
Symptom: Tests fail with compilation errors about missing types or enum variants.
Diagnosis:
pwd
Solution:
cd /workspace/wip/<worktree-name>
cargo test --test integration_test -- --ignored --nocapture
Tests Pass in Worktree, Fail in CI
Symptom: Tests work locally in worktree but fail when pushed to CI.
Common Causes:
- Feature branch depends on another PR not yet merged
- SDK changes in worktree not compatible with main branch
Diagnosis:
cd /workspace
git checkout main
git pull origin main
cargo test --workspace --all-features
Solution:
- Ensure prerequisite PRs are merged first
- Update feature branch to include necessary changes
Background Tests Show Different Results
Symptom: Running tests in background from main workspace shows failures, but running same test from worktree passes.
Cause: Background tests are using main branch SDK, not feature branch SDK.
Solution:
cd /workspace/wip/<worktree-name>
cargo test --test integration_test -- --ignored --nocapture
Security Best Practices
Never Expose Credentials
✅ CORRECT:
[ -n "$LANGSMITH_API_KEY" ] && echo "Set" || echo "Not set"
cargo test --test integration_test -- --ignored --nocapture
❌ WRONG:
echo $LANGSMITH_API_KEY
echo "Key: $LANGSMITH_API_KEY"
env | grep LANGSMITH
Store Credentials Securely
Best practices:
- Store in
/workspace/.devcontainer/.env (gitignored)
- Never commit
.env files
- Use placeholders in documentation:
<your-api-key>
Integration with Other Tools
With git-worktrees Skill
After creating a worktree, use this skill to run tests:
git worktree add -b codekiln/186-test-helpers wip/codekiln-186-test-helpers main
cd wip/codekiln-186-test-helpers
[ -n "$LANGSMITH_API_KEY" ] && echo "Set" || source /workspace/.devcontainer/.env
cargo test --test integration_test -- --ignored --nocapture
With Pre-Commit Checklist
Before committing, run the pre-commit checklist from worktree:
cd /workspace/wip/<worktree-name>
cargo fmt && \
cargo check --workspace --all-features && \
cargo clippy --workspace --all-features -- -D warnings && \
cargo test --workspace --all-features && \
cargo fmt --check
Quick Reference
Environment Check One-Liner
[ -n "$LANGSMITH_API_KEY" ] && echo "✓ LANGSMITH_API_KEY set" || (source /workspace/.devcontainer/.env && [ -n "$LANGSMITH_API_KEY" ] && echo "✓ LANGSMITH_API_KEY set after source" || echo "✗ LANGSMITH_API_KEY not found")
Worktree Test One-Liner
cd /workspace/wip/<worktree-name> && [ -n "$LANGSMITH_API_KEY" ] || source /workspace/.devcontainer/.env && cargo test --test integration_test -- --ignored --nocapture
Complete Test Flow
cd /workspace/wip/<worktree-name>
source /workspace/.devcontainer/.env
pwd
cargo test --test integration_test -- --ignored --nocapture
Related Documentation
- Issue #186 - Where environment sourcing patterns were discovered
- Issue #232 - Where test failures led to skill improvements
- git-worktrees skill - For creating and managing worktrees
- Pre-Commit Checklist -
@docs/dev/README.md "Pre-Commit Checklist" section
- GitHub Workflow -
@docs/dev/github-workflow.md
Key Takeaways
- ALWAYS source environment variables FIRST:
source /workspace/.devcontainer/.env
- This applies to ALL cargo commands in worktrees, including pre-commit checks
cargo test, cargo clippy, cargo check, cargo build may all require environment variables
- Pre-commit checks MUST source environment first - Don't run the pre-commit suite without sourcing
- Always verify
pwd before running tests - Ensure you're in the correct worktree
- Run tests from worktree when working on feature branch - Use feature branch SDK
- Never expose sensitive environment variable values - Check without showing
- Understand test context (main workspace vs worktree) - Different SDK versions
The #1 cause of test failures in worktrees is missing environment variables. Always source them first, especially before running pre-commit checks.
The #1 symptom: Cryptic panics like byte index 8 is out of bounds of \`` that disappear after sourcing environment variables.
These principles save time, prevent confusion, and improve security.