1
0
Fork 0
mirror of https://github.com/imjasonh/testscript-rs synced 2026-07-08 09:05:34 +00:00
testscript-rs/testdata/advanced_conditions.txt
Copilot 8c3127afea
Add network and advanced condition support with automatic detection and CI optimization (#21)
## Add Network and Advanced Condition Support with Automatic Detection -
COMPLETE 

This PR implements comprehensive network and advanced condition support
for testscript-rs, bringing feature parity with Go's testscript package
with automatic detection like the original.

### Core Features Implemented 

#### 1. **Network Conditions (`[net]`) - Always Available &
CI-Optimized**
- Auto-detects network availability with CI-optimized approach:
- **TCP-first detection**: Tries TCP connection to DNS servers (faster
than ping)
  - **Ping fallback**: Uses ping with shorter timeouts if TCP fails
- Cross-platform implementation optimized for CI environments
- Available by default like Go testscript - no manual setup required
- Can be used in test scripts as `[net]` for network-required tests or
`[!net]` to skip when offline

#### 2. **Environment Variable Conditions (`[env:VAR]`)**
- Dynamic condition checking for environment variables at execution time
- No pre-registration required - conditions are evaluated when
encountered
- Supports negation: `[!env:MISSING_VAR]` to test when variables are not
set
- Perfect for CI-specific tests: `[env:CI] exec deploy --dry-run`

#### 3. **Optimized Program Detection - Built-in Like Go**
- **15+ essential programs detected automatically** including:
  - **Basic commands**: cat, echo, ls, mkdir, rm, cp, mv, grep, find
  - **Core development tools**: git, make, curl, python, node, docker  
  - **System tools**: sleep, true, false, sh
- Cross-platform program existence checking (uses `where` on Windows,
`which` on Unix)
- Optimized for CI performance with reduced startup time
- No manual configuration needed - works exactly like Go testscript

### API Simplification

**Simple, Go-like API** - everything detected automatically:
```rust
testscript::run("testdata")
    .execute()  // Network + 15+ programs detected automatically
    .unwrap();
```

**Manual conditions still supported** for custom scenarios:
```rust
testscript::run("testdata")
    .condition("custom", my_custom_check())
    .execute()
    .unwrap();
```

### CI Optimization

- **Network detection**: TCP-first approach with 500ms timeouts (vs
1000ms+ ping)
- **Program list**: Optimized from 35+ to 15+ essential programs for
faster startup
- **Better reliability**: Handles restricted CI network environments
- **Ubuntu CI compatibility**: Resolved timeout and network access
issues

### Real-World Use Cases

This enables powerful testing scenarios that work out of the box:

```
# Network-dependent API tests (automatic detection, CI-optimized)
[net] exec curl https://api.example.com/health
[net] stdout "OK"

# Tool-specific tests (automatic detection)
[exec:docker] exec docker run --rm hello-world
[exec:docker] stdout "Hello from Docker!"

[exec:git] exec git status
[exec:python] exec python --version

# Environment-based behavior
[env:CI] exec deploy --dry-run --verbose
[!env:PRODUCTION] exec run-dangerous-test

# Platform + environment combinations
[unix] [env:DISPLAY] exec gui-test --headless
[windows] [exec:powershell] exec Get-Process
```

### Implementation Details

- **Network detection**: Multi-approach detection (TCP + ping fallback)
optimized for CI reliability
- **Program detection**: Essential program list covering most common
development tools
- **Dynamic evaluation**: Environment conditions are checked at
execution time, not initialization
- **Error handling**: Clear error messages for unknown conditions
- **Cross-platform**: Works consistently across Windows, macOS, and
Linux with CI optimizations

### Go Testscript Compatibility 

This implementation now matches Go testscript's behavior:
- All common conditions available by default without setup
- Network condition automatically detected with CI optimization
- Essential program detection built-in with performance focus
- Clean, simple API that "just works"
- Environment variable conditions work dynamically
- CI-friendly performance characteristics

### Testing

Added comprehensive test coverage with 7 new test cases covering:
- Network condition detection (automatic, CI-optimized)
- Environment variable condition parsing and evaluation  
- Optimized program detection (automatic)
- Condition combination scenarios
- Cross-platform compatibility
- Helper function validation

All existing tests continue to pass (47 total), ensuring no regressions.

### Backward Compatibility

This is a purely additive change. All existing functionality is
preserved:
- Platform conditions (`[unix]`, `[windows]`, `[linux]`, `[darwin]`)
- Build type conditions (`[debug]`, `[release]`)
- Manual `.condition()` API still works for custom conditions
- All existing test scripts continue to work unchanged

Fixes imjasonh/testscript-rs#10

<!-- START COPILOT CODING AGENT SUFFIX -->



<details>

<summary>Original prompt</summary>

> 
> ----
> 
> *This section details on the original issue you should resolve*
> 
> <issue_title>Add network and advanced condition support</issue_title>
> <issue_description>## Feature Request: Advanced Condition Support
> 
> Go's testscript supports advanced conditions beyond platform
detection:
> 
> ## Missing Conditions
> 
> 1. **Network conditions**: `[net]` - Test network availability
> 2. **Advanced platform conditions**: More granular OS/arch detection
> 3. **Environment-based conditions**: Custom environment variable
conditions
> 
> ## Proposed API
> 
> ```rust
> testscript::run("testdata")
>     .condition("net", check_network_available())
>     .condition("docker", command_exists("docker"))
>     .condition("env:CI", std::env::var("CI").is_ok())
>     .execute()
>     .unwrap();
> ```
> 
> ## Built-in Condition Helpers
> 
> ```rust
> // Automatic network detection
> testscript::run("testdata")
>     .auto_detect_network()
>     .auto_detect_programs(&["docker", "git", "npm"])
>     .execute()
>     .unwrap();
> ```
> 
> ## Implementation Notes
> 
> - Network detection could ping a reliable host
> - Program detection should use cross-platform approach (not just
`which`)
> - Environment conditions could support pattern matching
> - Consider caching condition results for performance
> 
> ## Use Cases
> 
> - **Docker tests**: `[docker] exec docker run hello-world`
> - **Network tests**: `[net] exec curl example.com`
> - **CI-specific tests**: `[env:CI] exec deploy
--dry-run`</issue_description>
> 
> ## Comments on the Issue (you are @copilot in this section)
> 
> <comments>
> </comments>
> 


</details>
Fixes imjasonh/testscript-rs#10

<!-- START COPILOT CODING AGENT TIPS -->
---

💡 You can make Copilot smarter by setting up custom instructions,
customizing its development environment and configuring Model Context
Protocol (MCP) servers. Learn more [Copilot coding agent
tips](https://gh.io/copilot-coding-agent-tips) in the docs.

---------

Signed-off-by: Jason Hall <jason@chainguard.dev>
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: imjasonh <210737+imjasonh@users.noreply.github.com>
Co-authored-by: Jason Hall <jason@chainguard.dev>
2025-09-27 17:00:36 +00:00

26 lines
946 B
Text

# Demonstrate complete advanced condition API
# Set an environment variable for testing
env TEST_DEMO=active
# Test network condition (will work if network is available)
[net] exec echo "Network is working"
# Test environment variable condition
[env:TEST_DEMO] exec echo "Environment variable TEST_DEMO is set"
[env:TEST_DEMO] stdout "Environment variable TEST_DEMO is set"
# Test program detection (echo should be available everywhere)
[exec:echo] exec echo "echo command is available"
[exec:echo] stdout "echo command is available"
# Test negated environment condition
[!env:NONEXISTENT_VAR] exec echo "NONEXISTENT_VAR is not set"
[!env:NONEXISTENT_VAR] stdout "NONEXISTENT_VAR is not set"
# Test platform conditions (existing functionality)
[unix] exec echo "Running on Unix-like system"
[windows] exec echo "Running on Windows"
exec echo "All condition tests completed successfully"
stdout "All condition tests completed successfully"