//! Tests for network and advanced condition support use std::fs; use tempfile::TempDir; use testscript_rs::{run::RunParams, testscript}; #[test] fn test_env_conditions() { let temp_dir = TempDir::new().unwrap(); let testdata_dir = temp_dir.path().join("testdata"); fs::create_dir(&testdata_dir).unwrap(); // Set an environment variable for testing std::env::set_var("TEST_CONDITION", "true"); let test_content = r#"[env:TEST_CONDITION] exec echo "env condition works" stdout "env condition works" [!env:NONEXISTENT_VAR] exec echo "negated env condition works" stdout "negated env condition works" [env:NONEXISTENT_VAR] exec echo "should be skipped" ! stdout "should be skipped" "#; fs::write(testdata_dir.join("env_test.txt"), test_content).unwrap(); let result = testscript::run(testdata_dir.to_string_lossy()).execute(); assert!( result.is_ok(), "Environment condition test failed: {:?}", result ); // Clean up std::env::remove_var("TEST_CONDITION"); } #[test] fn test_network_condition_builtin() { let temp_dir = TempDir::new().unwrap(); let testdata_dir = temp_dir.path().join("testdata"); fs::create_dir(&testdata_dir).unwrap(); // Create a test that should work whether network is available or not let test_content = r#"[net] exec echo "network available" [!net] exec echo "network not available" # At least one should execute exec echo "test completed" stdout "test completed" "#; fs::write(testdata_dir.join("network_test.txt"), test_content).unwrap(); // Network condition should be available automatically let result = testscript::run(testdata_dir.to_string_lossy()).execute(); assert!( result.is_ok(), "Network condition test failed: {:?}", result ); } #[test] fn test_program_detection_builtin() { let temp_dir = TempDir::new().unwrap(); let testdata_dir = temp_dir.path().join("testdata"); fs::create_dir(&testdata_dir).unwrap(); // Test with echo which should be available on all platforms and detected by default let test_content = r#"[exec:echo] exec echo "echo is available" stdout "echo is available" # Test with a program that likely doesn't exist on most systems [!exec:nonexistent_rare_program_xyz123] exec echo "rare program not found" stdout "rare program not found" "#; fs::write(testdata_dir.join("program_test.txt"), test_content).unwrap(); // Program detection should happen automatically let result = testscript::run(testdata_dir.to_string_lossy()).execute(); assert!( result.is_ok(), "Program detection test failed: {:?}", result ); } #[test] fn test_runparams_condition_helpers() { // Test the helper functions directly std::env::set_var("HELPER_TEST", "value"); assert!(RunParams::check_env_condition("env:HELPER_TEST")); assert!(!RunParams::check_env_condition("env:NONEXISTENT")); assert!(!RunParams::check_env_condition("not_env_condition")); std::env::remove_var("HELPER_TEST"); } #[test] fn test_manual_condition_files() { // Test our new testdata files to make sure they work properly // Test environment conditions file - programs should be detected automatically let result = testscript::run("testdata") .condition("net", true) // Force network to true for testing .execute(); // Note: This might fail if some testdata files have issues, // but our specific files should work if let Err(e) = result { println!("Some testdata files failed (expected): {}", e); } } #[test] fn test_combined_conditions() { let temp_dir = TempDir::new().unwrap(); let testdata_dir = temp_dir.path().join("testdata"); fs::create_dir(&testdata_dir).unwrap(); // Set environment for testing std::env::set_var("COMBINED_TEST", "true"); let test_content = r#"# Test combining different condition types # Use separate lines since parser doesn't support multiple conditions per line [unix] exec echo "unix detected" [windows] exec echo "windows detected" # This should work on any platform with the env var [env:COMBINED_TEST] exec echo "env var is set" stdout "env var is set" "#; fs::write(testdata_dir.join("combined_test.txt"), test_content).unwrap(); // All conditions should be available automatically let result = testscript::run(testdata_dir.to_string_lossy()).execute(); assert!( result.is_ok(), "Combined condition test failed: {:?}", result ); // Clean up std::env::remove_var("COMBINED_TEST"); } #[test] fn test_net_condition_available_by_default() { let testdata_dir = tempfile::tempdir().unwrap(); let test_content = r#" # Test that [net] condition is available by default # This should work without calling auto_detect_network() [net] exec echo "Network available" [!net] exec echo "Network not available" exec echo "Test completed" stdout "Test completed" "#; fs::write( testdata_dir.path().join("net_default_test.txt"), test_content, ) .unwrap(); // Test WITHOUT calling auto_detect_network() - should still work let result = testscript::run(testdata_dir.path().to_string_lossy()).execute(); assert!( result.is_ok(), "Network condition should be available by default: {:?}", result ); }