mirror of
https://github.com/imjasonh/testscript-rs
synced 2026-07-08 09:05:34 +00:00
55 lines
1.6 KiB
Rust
55 lines
1.6 KiB
Rust
//! Example demonstrating network and advanced condition support
|
|
|
|
use testscript_rs::testscript;
|
|
|
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
println!("Running testscript with advanced condition support...");
|
|
|
|
// Example 1: Basic usage with auto-detection
|
|
println!("\n=== Example 1: Auto-detect network and programs ===");
|
|
testscript::run("testdata")
|
|
.auto_detect_network()
|
|
.auto_detect_programs(&["docker", "git", "npm", "echo"])
|
|
.execute()?;
|
|
|
|
// Example 2: Manual condition setting with environment variables
|
|
println!("\n=== Example 2: Environment-based conditions ===");
|
|
|
|
// 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()?;
|
|
|
|
println!("All tests completed successfully!");
|
|
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)
|
|
}
|