#![allow(dead_code)]
use std::io;
use std::path::PathBuf;
use std::process::Child;
use perforce_cli::cmd::SubCommand;
use perforce_cli::spawn::ParameterizedSpawn;
pub fn args_of(command: &std::process::Command) -> Vec<String> {
command
.get_args()
.map(|arg| arg.to_string_lossy().into_owned())
.collect()
}
pub fn expect_args(cmd: &impl SubCommand, expected: &[&str]) {
assert_eq!(args_of(&cmd.setup_command("p4")), expected);
}
macro_rules! expect_chain {
([$($e:literal),* $(,)?], $($chain:tt)*) => {{
let cmd = $($chain)*;
expect_args(&cmd, &[$($e),*]);
}};
}
pub(crate) use expect_chain;
pub fn echo_bin(name: &str) -> PathBuf {
let path = std::env::temp_dir().join(format!("p4cli_{name}_echo_args.bat"));
std::fs::write(&path, "@echo %*\r\n").expect("write echo helper");
path
}
pub fn parse_echo(stdout: &[u8]) -> Vec<String> {
String::from_utf8_lossy(stdout)
.split_whitespace()
.map(|s| s.trim_matches('"').to_string())
.collect()
}
pub fn spawned_args<C, I>(cmd: &mut C, input: I) -> Vec<String>
where
C: ParameterizedSpawn<I, Output = Child, Error = io::Error>,
{
let output = cmd
.spawn_with(input)
.expect("spawn echo helper")
.wait_with_output()
.expect("wait for echo helper");
assert!(output.status.success(), "echo helper exited unsuccessfully");
parse_echo(&output.stdout)
}