use crate::setup;
use std::time::Duration;
use xshell::cmd;
#[test]
fn test_run_timeout_success() {
let sh = setup();
let command = cmd!(sh, "xsleep 1");
let result = command.timeout(Duration::from_secs(3)).run();
assert!(result.is_ok(), "Command should complete successfully within the timeout");
}
#[test]
fn test_run_timeout_failure() {
let sh = setup();
let command = cmd!(sh, "xsleep 5");
let result = command.timeout(Duration::from_secs(3)).run();
assert!(result.is_err(), "Command should fail due to timeout");
}
#[test]
fn test_read_timeout_success() {
let sh = setup();
let command = cmd!(sh, "xecho Hello, world!");
let result = command.timeout(Duration::from_secs(3)).read();
assert!(result.is_ok(), "Command should complete successfully within the timeout");
assert_eq!(result.unwrap(), "Hello, world!");
}
#[test]
fn test_read_timeout_failure() {
let sh = setup();
let command = cmd!(sh, "xsleep 5");
let result = command.timeout(Duration::from_secs(3)).read();
assert!(result.is_err(), "Command should fail due to timeout");
}
#[test]
fn test_read_stderr_timeout_success() {
let sh = setup();
let command = cmd!(sh, "xecho -e Error message");
let result = command.timeout(Duration::from_secs(3)).read_stderr();
assert!(result.is_ok(), "Command should complete successfully within the timeout");
assert_eq!(result.unwrap(), "Error message");
}
#[test]
fn test_read_stderr_timeout_failure() {
let sh = setup();
let command = cmd!(sh, "xsleep 5");
let result = command.timeout(Duration::from_secs(3)).read_stderr();
assert!(result.is_err(), "Command should fail due to timeout");
}
#[test]
fn test_output_timeout_success() {
let sh = setup();
let command = cmd!(sh, "xecho Hello, world!");
let result = command.timeout(Duration::from_secs(3)).output();
assert!(result.is_ok(), "Command should complete successfully within the timeout");
let output = result.unwrap();
assert_eq!(String::from_utf8_lossy(&output.stdout).trim(), "Hello, world!");
}
#[test]
fn test_output_timeout_failure() {
let sh = setup();
let command = cmd!(sh, "xsleep 5");
let result = command.timeout(Duration::from_secs(3)).output();
assert!(result.is_err(), "Command should fail due to timeout");
}