mirror of
https://github.com/imjasonh/testscript-rs
synced 2026-07-08 00:55:52 +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;
|
||||
|
||||
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");
|
||||
|
|
@ -23,7 +19,6 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||
.condition("docker", command_exists("docker"))
|
||||
.execute()?;
|
||||
|
||||
println!("All tests completed successfully!");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -169,10 +169,13 @@ impl Builder {
|
|||
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
|
||||
/// connectivity is available and set the condition accordingly.
|
||||
/// connectivity is available and update the condition accordingly.
|
||||
pub fn auto_detect_network(mut self) -> Self {
|
||||
self.params = self.params.auto_detect_network();
|
||||
self
|
||||
|
|
|
|||
|
|
@ -39,6 +39,9 @@ impl RunParams {
|
|||
conditions.insert("debug".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
|
||||
conditions.insert("exec:cat".to_string(), Self::program_exists("cat"));
|
||||
conditions.insert("exec:echo".to_string(), Self::program_exists("echo"));
|
||||
|
|
@ -86,7 +89,10 @@ impl RunParams {
|
|||
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 {
|
||||
self.conditions
|
||||
.insert("net".to_string(), Self::check_network_available());
|
||||
|
|
|
|||
|
|
@ -152,3 +152,33 @@ stdout "env var is set"
|
|||
// 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
|
||||
);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue