#![allow(unused_imports)]
use bare_script::sync::CommandBuilder;
use thiserror::Error as _;
use tokio as _;
#[test]
fn test_basic_execute() {
#[cfg(windows)]
let output = {
CommandBuilder::new("cmd")
.args(["/C", "echo hello"])
.capture_output()
.execute()
};
#[cfg(not(windows))]
let output = {
CommandBuilder::new("echo")
.arg("hello")
.capture_output()
.execute()
};
assert!(output.is_ok());
let output = output.unwrap();
assert!(output.success());
}
#[test]
fn test_args() {
#[cfg(windows)]
let output = {
CommandBuilder::new("cmd")
.args(["/C", "echo hello world"])
.capture_output()
.execute()
};
#[cfg(not(windows))]
let output = {
CommandBuilder::new("echo")
.arg("hello")
.arg("world")
.capture_output()
.execute()
};
assert!(output.is_ok());
let output = output.unwrap();
assert!(output.stdout_str().contains("hello"));
}
#[test]
fn test_env() {
#[cfg(windows)]
let output = {
CommandBuilder::new("cmd")
.args(["/C", "echo %TEST_VAR%"])
.env("TEST_VAR", "test_value")
.capture_output()
.execute()
};
#[cfg(not(windows))]
let output = {
CommandBuilder::new("sh")
.args(["-c", "echo $TEST_VAR"])
.env("TEST_VAR", "test_value")
.capture_output()
.execute()
};
assert!(output.is_ok());
let output = output.unwrap();
assert!(output.stdout_str().contains("test_value"));
}
#[test]
fn test_current_dir() {
use std::env;
let current_dir = env::current_dir().expect("Failed to get current dir");
#[cfg(windows)]
let output = {
CommandBuilder::new("cmd")
.args(["/C", "cd"])
.current_dir(¤t_dir)
.capture_output()
.execute()
};
#[cfg(not(windows))]
let output = {
CommandBuilder::new("pwd")
.current_dir(¤t_dir)
.capture_output()
.execute()
};
assert!(output.is_ok());
}
#[test]
fn test_timeout() {
use std::time::Duration;
#[cfg(windows)]
let result = {
CommandBuilder::new("cmd")
.args(["/C", "ping -n 10 127.0.0.1"])
.capture_output()
.execute_with_timeout(Duration::from_millis(100))
};
#[cfg(not(windows))]
let result = {
CommandBuilder::new("sleep")
.arg("10")
.capture_output()
.execute_with_timeout(Duration::from_millis(100))
};
assert!(result.is_err());
let err = result.unwrap_err();
assert!(matches!(err, bare_script::ScriptError::Timeout(_)));
}
#[test]
fn test_exit_code() {
#[cfg(windows)]
let output = {
CommandBuilder::new("cmd")
.args(["/C", "exit 42"])
.capture_output()
.execute()
};
#[cfg(not(windows))]
let output = {
CommandBuilder::new("sh")
.args(["-c", "exit 42"])
.capture_output()
.execute()
};
assert!(output.is_ok());
let output = output.unwrap();
assert!(!output.success());
assert_eq!(output.code(), Some(42));
}
#[test]
fn test_pipeline() {
use bare_script::sync::Pipeline;
#[cfg(windows)]
let output = {
Pipeline::new("cmd")
.args(["/C", "echo hello world"])
.pipe("findstr")
.arg("hello")
.capture_output()
.execute()
};
#[cfg(not(windows))]
let output = {
Pipeline::new("echo")
.arg("hello world")
.pipe("grep")
.arg("hello")
.capture_output()
.execute()
};
assert!(output.is_ok());
let output = output.unwrap();
assert!(output.stdout_str().contains("hello"));
}
#[test]
fn test_stdio_config() {
#[cfg(windows)]
let output = {
CommandBuilder::new("cmd")
.args(["/C", "echo test"])
.null_stdin()
.inherit_stdout()
.null_stderr()
.execute()
};
#[cfg(not(windows))]
let output = {
CommandBuilder::new("echo")
.arg("test")
.null_stdin()
.inherit_stdout()
.null_stderr()
.execute()
};
assert!(output.is_ok());
}