#![allow(unused_imports)]
use bare_script::sync::CommandBuilder;
use std::io::BufRead;
use std::process::Stdio;
use thiserror::Error as _;
use tokio as _;
#[test]
fn test_command_not_found() {
let result = CommandBuilder::new("nonexistent_command_12345")
.capture_output()
.execute();
assert!(result.is_err());
}
#[test]
fn test_invalid_working_dir() {
let result = CommandBuilder::new("echo")
.arg("test")
.current_dir("/nonexistent/path/12345")
.capture_output()
.execute();
assert!(result.is_err());
}
#[test]
fn test_empty_args() {
#[cfg(windows)]
let output = {
CommandBuilder::new("cmd")
.args(["/C", "echo"])
.capture_output()
.execute()
};
#[cfg(not(windows))]
let output = {
CommandBuilder::new("echo")
.args([])
.capture_output()
.execute()
};
assert!(output.is_ok());
let output = output.unwrap();
assert!(output.success());
}
#[test]
fn test_special_chars() {
#[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 world")
.capture_output()
.execute()
};
assert!(output.is_ok());
}
#[test]
fn test_multiple_envs() {
#[cfg(windows)]
let output = {
CommandBuilder::new("cmd")
.args(["/C", "echo %VAR1% %VAR2%"])
.env("VAR1", "value1")
.env("VAR2", "value2")
.capture_output()
.execute()
};
#[cfg(not(windows))]
let output = {
CommandBuilder::new("sh")
.args(["-c", "echo $VAR1 $VAR2"])
.env("VAR1", "value1")
.env("VAR2", "value2")
.capture_output()
.execute()
};
assert!(output.is_ok());
let output = output.unwrap();
assert!(output.stdout_str().contains("value1"));
assert!(output.stdout_str().contains("value2"));
}
#[test]
fn test_env_remove() {
#[cfg(windows)]
let output = {
CommandBuilder::new("cmd")
.args(["/C", "echo %PATH%"])
.env_remove("PATH")
.capture_output()
.execute()
};
#[cfg(not(windows))]
let output = {
CommandBuilder::new("sh")
.args(["-c", "echo $PATH"])
.env_remove("PATH")
.capture_output()
.execute()
};
let output = output.unwrap();
let stdout = output.stdout_str();
assert!(output.success() || stdout.len() < 50);
}
#[test]
fn test_env_clear() {
#[cfg(windows)]
let output = {
CommandBuilder::new("cmd")
.args(["/C", "echo %PATH%"])
.env_clear()
.env("PATH", "/test") .capture_output()
.execute()
};
#[cfg(not(windows))]
let output = {
CommandBuilder::new("sh")
.args(["-c", "echo $PATH"])
.env_clear()
.env("PATH", "/test")
.capture_output()
.execute()
};
assert!(output.is_ok());
}
#[test]
fn test_spawn() {
use std::io::{BufRead, BufReader};
#[cfg(windows)]
let child = {
CommandBuilder::new("cmd")
.args(["/C", "echo spawned"])
.stdout(Stdio::piped())
.spawn()
};
#[cfg(not(windows))]
let child = {
CommandBuilder::new("echo")
.arg("spawned")
.stdout(Stdio::piped())
.spawn()
};
assert!(child.is_ok());
let mut child = child.unwrap();
let stdout = child.stdout.take();
if let Some(stdout) = stdout {
let reader = BufReader::new(stdout);
let lines: Vec<String> = reader.lines().filter_map(|l| l.ok()).collect();
assert!(!lines.is_empty());
}
let status = child.wait();
assert!(status.is_ok());
}
#[test]
fn test_status() {
#[cfg(windows)]
let status = { CommandBuilder::new("cmd").args(["/C", "exit 0"]).status() };
#[cfg(not(windows))]
let status = { CommandBuilder::new("true").status() };
assert!(status.is_ok());
let status = status.unwrap();
assert!(status.success());
}
#[test]
fn test_status_failure() {
#[cfg(windows)]
let status = { CommandBuilder::new("cmd").args(["/C", "exit 1"]).status() };
#[cfg(not(windows))]
let status = { CommandBuilder::new("false").status() };
assert!(status.is_ok());
let status = status.unwrap();
assert!(!status.success());
}