use core::panic; use std::ffi::OsStr; use comlexr::{cmd, pipe}; use tempfile::tempdir; #[test] fn pipe_status() { let dir = tempdir().unwrap(); let file = dir.path().join("out"); let mut pipe = pipe!(cmd!("echo", "test") | cmd!("tee", &file)); let status = pipe.status().unwrap(); assert!(status.success()); } #[test] fn pipe_stdin_output() { let dir = tempdir().unwrap(); let file = dir.path().join("out"); let mut pipe = pipe!(stdin = "test"; cmd!("sed", "s|e|oa|") | cmd!("tee", &file)); println!("{}", file.display()); let output = pipe.output().unwrap(); assert!(output.status.success()); assert_eq!(String::from_utf8_lossy(&output.stdout), "toast"); } #[test] fn pipe_stdin_output_single_command() { let mut pipe = pipe!(stdin = "test"; cmd!("sed", "s|e|oa|")); let output = pipe.output().unwrap(); assert!(output.status.success()); assert_eq!(String::from_utf8_lossy(&output.stdout), "toast"); } #[test] fn pipe_expect_fail() { let mut pipe = pipe!(stdin = "test"; cmd!("false") | cmd!("sed", "s|e|oa|")); let error = pipe.output().unwrap_err(); match error { comlexr::ExecutorError::FailedCommand { command, exit_code } => { assert_eq!(command.get_program(), OsStr::new("false")); assert_eq!(exit_code, 1); } err => panic!("Expected exit code, got: {err}"), } } #[test] fn pipe_expect_fail_command_not_exist() { let mut pipe = pipe!(stdin = "test"; cmd!("no_command") | cmd!("sed", "s|e|oa|")); let error = pipe.output().unwrap_err(); match error { comlexr::ExecutorError::Io(err) => assert_eq!(err.raw_os_error().unwrap(), 2), err => panic!("Expected IO Error, got: {err}"), } } #[test] fn pipe_block() { let mut pipe = pipe!(stdin = "test"; { let c = cmd!("sed", "s|e|oa|"); println!("{c:?}"); c }); let output = pipe.output().unwrap(); assert!(output.status.success()); assert_eq!(String::from_utf8_lossy(&output.stdout), "toast"); } #[test] fn pipe_ref() { let mut command = { let c = cmd!("sed", "s|e|oa|"); println!("{c:?}"); c }; let mut pipe = pipe!(stdin = "test"; &mut command); let output = pipe.output().unwrap(); assert!(output.status.success()); assert_eq!(String::from_utf8_lossy(&output.stdout), "toast"); } #[test] fn pipe_fn() { let command = || { let c = cmd!("sed", "s|e|oa|"); println!("{c:?}"); c }; let mut pipe = pipe!(stdin = "test"; command()); let output = pipe.output().unwrap(); assert!(output.status.success()); assert_eq!(String::from_utf8_lossy(&output.stdout), "toast"); }