1
0
Fork 0
mirror of https://github.com/imjasonh/testscript-rs synced 2026-07-08 17:16:38 +00:00
testscript-rs/examples/advanced_conditions.rs
copilot-swe-agent[bot] a8a00f3483 Remove auto_detect methods and make program detection comprehensive by default
Co-authored-by: imjasonh <210737+imjasonh@users.noreply.github.com>
2025-09-27 03:51:03 +00:00

47 lines
1.3 KiB
Rust

//! Example demonstrating network and advanced condition support
use testscript_rs::testscript;
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Example 1: Basic usage - all conditions detected automatically
testscript::run("testdata").execute()?;
// Example 2: Manual condition setting with environment variables
// Set a test environment variable
std::env::set_var("CI", "true");
testscript::run("testdata")
.condition("net", check_network_available())
.condition("docker", command_exists("docker"))
.execute()?;
Ok(())
}
/// Check if network is available
fn check_network_available() -> bool {
std::process::Command::new("ping")
.args(if cfg!(windows) {
vec!["-n", "1", "-w", "1000", "1.1.1.1"]
} else {
vec!["-c", "1", "-W", "1", "1.1.1.1"]
})
.output()
.map(|output| output.status.success())
.unwrap_or(false)
}
/// Check if a command exists
fn command_exists(program: &str) -> bool {
#[cfg(windows)]
let check_cmd = "where";
#[cfg(not(windows))]
let check_cmd = "which";
std::process::Command::new(check_cmd)
.arg(program)
.output()
.map(|output| output.status.success())
.unwrap_or(false)
}