#![allow(clippy::expect_used, clippy::unwrap_used)]
use std::io::{Read, Write};
use std::path::PathBuf;
use super::{Command, GlobalFlags, ParseError, parse, run};
pub(super) fn s(v: &[&str]) -> Vec<String> {
v.iter().map(|x| x.to_string()).collect()
}
pub(super) fn run_with(args: &[&str], stdin: &str) -> (u8, String, String) {
let parsed: Result<(GlobalFlags, Command), ParseError> = parse(&s(args));
let (globals, command) = parsed.expect("parse must succeed in run_with");
let mut out = Vec::new();
let mut err = Vec::new();
let code = run(globals, command, stdin.as_bytes(), &mut out, &mut err);
(
code,
String::from_utf8_lossy(&out).into_owned(),
String::from_utf8_lossy(&err).into_owned(),
)
}
pub(super) struct FailingReader;
impl Read for FailingReader {
fn read(&mut self, _buf: &mut [u8]) -> std::io::Result<usize> {
Err(std::io::Error::other("simulated stdin failure"))
}
}
pub(super) struct FailingWriter {
pub(super) budget: usize,
}
impl Write for FailingWriter {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
if self.budget == 0 {
return Err(std::io::Error::other("disk full"));
}
let n = buf.len().min(self.budget);
self.budget -= n;
Ok(n)
}
fn flush(&mut self) -> std::io::Result<()> {
Ok(())
}
}
pub(super) struct CwdGuard {
original: PathBuf,
_lock: std::sync::MutexGuard<'static, ()>,
}
static CWD_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());
impl CwdGuard {
pub(super) fn change_to(target: &std::path::Path) -> std::io::Result<Self> {
let lock = CWD_LOCK.lock().unwrap_or_else(|p| p.into_inner());
let original = std::env::current_dir()?;
std::env::set_current_dir(target)?;
Ok(Self {
original,
_lock: lock,
})
}
}
impl Drop for CwdGuard {
fn drop(&mut self) {
let _ = std::env::set_current_dir(&self.original);
}
}
pub(super) fn make_engine_failing_repo(label: &str) -> PathBuf {
let dir = std::env::temp_dir().join(format!(
"ptuf-cli-engine-fail-{}-{}-{}",
label,
std::process::id(),
line!()
));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(dir.join(".git")).expect("mkdir .git");
std::fs::write(
dir.join(".ptuf.yaml"),
"plugins:\n - path: ./missing-plugin.yaml\n",
)
.expect("write .ptuf.yaml");
dir
}