use std::{path::PathBuf, process::Command};
const PKG: &str = "v_utils";
const EXAMPLE: &str = "logging_emit";
fn run_and_collect() -> String {
let tmp = tempfile::tempdir().expect("create tempdir");
let xdg_state_home = tmp.path();
let log_dir = xdg_state_home.join(PKG);
std::fs::create_dir_all(&log_dir).expect("mkdir log_dir");
std::fs::write(log_dir.join("_log_directives"), "info\n").expect("write _log_directives");
let manifest_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
let cargo = std::env::var_os("CARGO").unwrap_or_else(|| "cargo".into());
let build_status = Command::new(&cargo)
.args(["build", "--quiet", "--example", EXAMPLE, "--features", "xdg"])
.current_dir(&manifest_dir)
.status()
.expect("cargo build --example");
assert!(build_status.success(), "cargo build failed");
let target_dir = std::env::var_os("CARGO_TARGET_DIR")
.map(PathBuf::from)
.unwrap_or_else(|| manifest_dir.parent().expect("workspace root").join("target"));
let binary = target_dir.join("debug").join("examples").join(EXAMPLE);
let output = Command::new(&binary)
.current_dir(&manifest_dir)
.env("XDG_STATE_HOME", xdg_state_home)
.env_remove("RUST_LOG")
.env_remove("LOG_DIRECTIVES")
.env_remove("__IS_INTEGRATION_TEST")
.output()
.expect("spawn example binary");
assert!(
output.status.success(),
"example exited non-zero: {}\nstderr:\n{}",
output.status,
String::from_utf8_lossy(&output.stderr)
);
let stdout = String::from_utf8(output.stdout).expect("stdout utf8");
let stderr = String::from_utf8(output.stderr).expect("stderr utf8");
let file = std::fs::read_to_string(log_dir.join(".log")).expect("read log file");
let combined = format!("===== STDOUT =====\n{stdout}===== STDERR =====\n{stderr}===== FILE =====\n{file}");
combined.replace(&xdg_state_home.display().to_string(), "<TMPDIR>")
}
#[test]
fn clientside_writes_stdout_stderr_and_file() {
let combined = run_and_collect();
insta::with_settings!({
filters => vec![
(r"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d+Z", "<TIMESTAMP>"),
(r"\x1b\[[0-9;]*m", ""),
(r"ThreadId\(\d+\)", "ThreadId(N)"),
],
}, {
insta::assert_snapshot!(combined);
});
}