mirror of
https://github.com/imjasonh/testscript-rs
synced 2026-07-08 09:05:34 +00:00
Address PR feedback: remove printlns and make [net] condition available by default
Co-authored-by: imjasonh <210737+imjasonh@users.noreply.github.com>
This commit is contained in:
parent
d01b8865de
commit
22499cb0ee
4 changed files with 42 additions and 8 deletions
|
|
@ -3,17 +3,13 @@
|
||||||
use testscript_rs::testscript;
|
use testscript_rs::testscript;
|
||||||
|
|
||||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
println!("Running testscript with advanced condition support...");
|
|
||||||
|
|
||||||
// Example 1: Basic usage with auto-detection
|
// Example 1: Basic usage with auto-detection
|
||||||
println!("\n=== Example 1: Auto-detect network and programs ===");
|
|
||||||
testscript::run("testdata")
|
testscript::run("testdata")
|
||||||
.auto_detect_network()
|
.auto_detect_network()
|
||||||
.auto_detect_programs(&["docker", "git", "npm", "echo"])
|
.auto_detect_programs(&["docker", "git", "npm", "echo"])
|
||||||
.execute()?;
|
.execute()?;
|
||||||
|
|
||||||
// Example 2: Manual condition setting with environment variables
|
// Example 2: Manual condition setting with environment variables
|
||||||
println!("\n=== Example 2: Environment-based conditions ===");
|
|
||||||
|
|
||||||
// Set a test environment variable
|
// Set a test environment variable
|
||||||
std::env::set_var("CI", "true");
|
std::env::set_var("CI", "true");
|
||||||
|
|
@ -23,7 +19,6 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
.condition("docker", command_exists("docker"))
|
.condition("docker", command_exists("docker"))
|
||||||
.execute()?;
|
.execute()?;
|
||||||
|
|
||||||
println!("All tests completed successfully!");
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -169,10 +169,13 @@ impl Builder {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Automatically detect network availability and set the 'net' condition
|
/// Re-detect network availability and update the 'net' condition
|
||||||
|
///
|
||||||
|
/// The 'net' condition is automatically checked at startup, but this method
|
||||||
|
/// can be used to refresh the network status if needed.
|
||||||
///
|
///
|
||||||
/// This will attempt to ping reliable hosts to determine if network
|
/// This will attempt to ping reliable hosts to determine if network
|
||||||
/// connectivity is available and set the condition accordingly.
|
/// connectivity is available and update the condition accordingly.
|
||||||
pub fn auto_detect_network(mut self) -> Self {
|
pub fn auto_detect_network(mut self) -> Self {
|
||||||
self.params = self.params.auto_detect_network();
|
self.params = self.params.auto_detect_network();
|
||||||
self
|
self
|
||||||
|
|
|
||||||
|
|
@ -39,6 +39,9 @@ impl RunParams {
|
||||||
conditions.insert("debug".to_string(), cfg!(debug_assertions));
|
conditions.insert("debug".to_string(), cfg!(debug_assertions));
|
||||||
conditions.insert("release".to_string(), !cfg!(debug_assertions));
|
conditions.insert("release".to_string(), !cfg!(debug_assertions));
|
||||||
|
|
||||||
|
// Add network condition - check if network is available by default
|
||||||
|
conditions.insert("net".to_string(), Self::check_network_available());
|
||||||
|
|
||||||
// Check for common programs
|
// Check for common programs
|
||||||
conditions.insert("exec:cat".to_string(), Self::program_exists("cat"));
|
conditions.insert("exec:cat".to_string(), Self::program_exists("cat"));
|
||||||
conditions.insert("exec:echo".to_string(), Self::program_exists("echo"));
|
conditions.insert("exec:echo".to_string(), Self::program_exists("echo"));
|
||||||
|
|
@ -86,7 +89,10 @@ impl RunParams {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Automatically detect network availability and set the 'net' condition
|
/// Re-detect network availability and update the 'net' condition
|
||||||
|
///
|
||||||
|
/// The 'net' condition is automatically checked at startup, but this method
|
||||||
|
/// can be used to refresh the network status if needed.
|
||||||
pub fn auto_detect_network(mut self) -> Self {
|
pub fn auto_detect_network(mut self) -> Self {
|
||||||
self.conditions
|
self.conditions
|
||||||
.insert("net".to_string(), Self::check_network_available());
|
.insert("net".to_string(), Self::check_network_available());
|
||||||
|
|
|
||||||
|
|
@ -152,3 +152,33 @@ stdout "env var is set"
|
||||||
// Clean up
|
// Clean up
|
||||||
std::env::remove_var("COMBINED_TEST");
|
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
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue