ruchy 4.2.0

A systems scripting language that transpiles to idiomatic Rust with extreme quality engineering
Documentation
# CLI Testing Guide

**Toyota Way: Quality Built Into Process - CLI Command Testing Excellence**

## Quick Start

Run the complete CLI testing suite:
```bash
make test-ruchy-commands
```

## Testing Infrastructure Overview

### 🎯 **Comprehensive Testing Pyramid**

```
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Fuzz Tests    β”‚ ← Millions of random inputs (requires nightly)
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Property Tests  β”‚ ← Mathematical invariants (5 tests)
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚Integration Testsβ”‚ ← End-to-end scenarios (8 tests)
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚   Unit Tests    β”‚ ← Component testing (via coverage)
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚   Examples      β”‚ ← Executable documentation (4 scenarios)
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
```

### **Coverage Standards (Toyota Way)**
- **Minimum**: 80% line coverage (enforced by pre-commit hooks)
- **Target**: 90% line coverage (aspirational)
- **Current fmt command**: 87.80% βœ…

## Available Commands

### **Primary Test Suite**
```bash
make test-ruchy-commands    # Run all CLI tests (733ms total)
```

### **Individual Test Categories**
```bash
make test-cli-integration   # Integration tests (176ms)
make test-cli-properties    # Property tests (193ms)  
make test-cli-coverage      # Coverage analysis (48s)
make test-cli-performance   # Performance benchmarking
```

### **Coverage & Performance**
```bash
make test-cli-coverage      # Generate coverage report
./scripts/cli_coverage.sh   # Detailed coverage analysis
./scripts/performance_analysis.sh  # Performance breakdown
```

## Test Categories Explained

### **1. Integration Tests** (`tests/cli_integration.rs`)
**End-to-end testing of CLI commands with real files**

```bash
cargo test --test cli_integration
```

**Coverage:**
- βœ… Happy path scenarios
- βœ… Error handling (missing files, invalid syntax)
- βœ… Complex expressions and edge cases
- βœ… Idempotency verification

**Example Test:**
```rust
#[test]
fn test_fmt_formats_simple_function() {
    let temp_dir = TempDir::new().unwrap();
    let test_file = temp_dir.path().join("test.ruchy");
    
    fs::write(&test_file, "fun test(x:i32)->i32{x*2}").unwrap();
    
    Command::cargo_bin("ruchy").unwrap()
        .args(["fmt", test_file.to_str().unwrap()])
        .assert()
        .success();
    
    let formatted = fs::read_to_string(&test_file).unwrap();
    assert!(formatted.contains("fun test(x: i32) -> i32"));
}
```

### **2. Property Tests** (`tests/cli_properties.rs`)
**Mathematical invariants that must always hold**

```bash
cargo test --test cli_properties
```

**Verified Properties:**
- **Idempotency**: `format(format(x)) == format(x)`
- **Function name preservation**: Names never corrupted
- **Operator preservation**: Semantic structure maintained
- **Determinism**: Same input β†’ identical output
- **Control flow preservation**: if/else structures maintained

**Example Property:**
```rust
#[test]
fn prop_fmt_idempotent() {
    let test_cases = [
        "fun test() -> i32 { 42 }",
        "fun add(x: i32, y: i32) -> i32 { x + y }",
        // ... more cases
    ];

    for code in test_cases {
        // Format twice, results must be identical
        assert_eq!(format_once(code), format_twice(code));
    }
}
```

### **3. Executable Examples** (`examples/fmt_example.rs`)
**Runnable documentation with built-in tests**

```bash
cargo run --example fmt_example
```

**Scenarios Covered:**
- Basic function formatting
- Complex expression formatting
- Multiple functions
- Error handling demonstration

### **4. Fuzz Tests** (`fuzz/fuzz_targets/`)
**Random input testing for robustness**

```bash
# Requires nightly Rust
cargo +nightly fuzz run fuzz_fmt
cargo +nightly fuzz run fuzz_cli
```

**Targets:**
- `fuzz_fmt.rs`: Tests fmt command with random inputs
- `fuzz_cli.rs`: Tests CLI interface with random inputs

## Quality Gates (Pre-commit Hooks)

**Gate 16: CLI Coverage Check** (MANDATORY)
- Enforces β‰₯80% coverage for CLI commands
- Blocks commits below threshold
- Uses quick coverage analysis mode

```bash
# Triggered automatically on git commit
# Or run manually:
./scripts/cli_coverage.sh --quick
```

## Performance Standards

### **Toyota Way Performance Metrics**

| Component | Time | Target | Status |
|-----------|------|---------|---------|
| Integration Tests | 176ms | <1s | βœ… |
| Property Tests | 193ms | <1s | βœ… |  
| Total Test Suite | 733ms | <120s | βœ… |
| Coverage Analysis | 48.9s | <60s | βœ… |

### **Benchmarking**
```bash
make test-cli-performance  # Uses hyperfine for precise timing
```

## Adding New CLI Commands

### **Step 1: Integration Tests**
Create tests in `tests/cli_integration.rs`:

```rust
mod your_command_tests {
    #[test]
    fn test_your_command_happy_path() {
        // Test successful execution
    }
    
    #[test] 
    fn test_your_command_error_handling() {
        // Test error scenarios
    }
    
    #[test]
    fn test_your_command_edge_cases() {
        // Test boundary conditions
    }
}
```

### **Step 2: Property Tests**
Add to `tests/cli_properties.rs`:

```rust
#[test]
fn prop_your_command_invariant() {
    // Test mathematical properties that must always hold
}
```

### **Step 3: Executable Example**
Create `examples/your_command_example.rs`:

```rust
fn main() -> Result<(), Box<dyn std::error::Error>> {
    println!("🎯 Your Command Examples");
    // Demonstrate usage with real examples
}
```

### **Step 4: Coverage Target**
Ensure your command achieves β‰₯80% line coverage.

## Troubleshooting

### **Coverage Below 80%**
```bash
# Detailed coverage report
make test-cli-coverage

# Open HTML report
open target/cli-coverage/html/index.html

# Add missing tests for uncovered lines
```

### **Slow Tests**
```bash
# Performance analysis
./scripts/performance_analysis.sh

# Individual component timing
time cargo test --test cli_integration
```

### **Failing Tests**
```bash
# Verbose output
cargo test --test cli_integration -- --nocapture

# Run specific test
cargo test test_fmt_formats_simple_function
```

## Architecture Notes

### **File Structure**
```
tests/
β”œβ”€β”€ cli_integration.rs     # End-to-end CLI testing
β”œβ”€β”€ cli_properties.rs      # Mathematical property testing
└── ...

examples/
β”œβ”€β”€ fmt_example.rs         # Executable fmt documentation
└── ...

scripts/
β”œβ”€β”€ cli_coverage.sh        # Coverage analysis
└── performance_analysis.sh # Performance breakdown

fuzz/
└── fuzz_targets/
    β”œβ”€β”€ fuzz_fmt.rs        # Fuzz testing for fmt
    └── fuzz_cli.rs        # Fuzz testing for CLI
```

### **Makefile Integration**
The testing infrastructure integrates with the main Makefile:

```makefile
test-ruchy-commands: test-cli-integration test-cli-properties test-cli-fuzz test-cli-examples
    @echo "🎯 All CLI command testing complete!"
```

## Success Metrics

### **Quantitative Targets (ALL ACHIEVED βœ…)**
- βœ… **Code Coverage**: 87.80% (target: β‰₯80%)
- βœ… **Test Execution Time**: 733ms (target: ≀120s)
- βœ… **Property Test Coverage**: 5 invariants (target: β‰₯5)
- βœ… **Integration Test Scenarios**: 8 tests covering happy/error/edge cases
- βœ… **Zero Regressions**: All tests passing

### **Qualitative Standards (ALL ACHIEVED βœ…)**
- βœ… **Toyota Way Compliance**: Quality built into process
- βœ… **Clear Documentation**: Every failure provides fix guidance
- βœ… **Developer Experience**: Single command comprehensive validation
- βœ… **Maintainability**: Self-documenting test structure

---

**This CLI testing infrastructure embodies Toyota Way principles: quality is built into the development process, not inspected in afterward. Every command is battle-tested with mathematical rigor and comprehensive coverage.**