#![cfg(windows)]
use std::sync::Arc;
use rpi_tools::{ExecutionErrorCode, OsExecutionEnv, Shell, ShellExecOptions};
macro_rules! shell_or_skip {
($env:expr) => {
match $env.exec("true", ShellExecOptions::default()).await {
Ok(_) => {}
Err(error) if error.code == ExecutionErrorCode::ShellUnavailable => {
eprintln!("skipping: no usable bash on this host ({error})");
return;
}
Err(error) => panic!("unexpected shell error: {error:?}"),
}
};
}
#[tokio::test]
async fn resolves_git_bash_with_utf8_output_and_host_cwd() {
let temp = tempfile::tempdir().unwrap();
std::fs::write(temp.path().join("marker.txt"), "cwd-reached").unwrap();
let environment = std::env::vars()
.filter(|(name, _)| {
let name = name.to_ascii_uppercase();
name != "LANG" && !name.starts_with("LC_")
})
.collect();
let env = OsExecutionEnv::with_cwd(temp.path().to_path_buf()).with_environment(environment);
shell_or_skip!(env);
let out = env
.exec("uname -s", ShellExecOptions::default())
.await
.expect("bash runs");
let uname = out.stdout.trim();
assert!(
uname.starts_with("MINGW") || uname.starts_with("MSYS"),
"expected Git Bash/MSYS, got {uname:?} — the WSL launcher or cmd was used"
);
let out = env
.exec("cat marker.txt", ShellExecOptions::default())
.await
.expect("cwd is reachable from the shell");
assert_eq!(
out.stdout.trim(),
"cwd-reached",
"wrong cwd: {:?}",
out.stdout
);
let out = env
.exec(r#"printf '中文输出\n'"#, ShellExecOptions::default())
.await
.expect("utf-8 output");
assert_eq!(out.stdout, "中文输出\n");
assert!(
!out.stdout.contains('\u{fffd}'),
"output was decoded with the wrong encoding: {:?}",
out.stdout
);
let out = env
.exec(
r#"printf '%s|%s' "${LANG:-}" "${LC_ALL:-}""#,
ShellExecOptions::default(),
)
.await
.expect("locale");
assert_eq!(out.stdout, "C.UTF-8|");
let out = env
.exec(
r#"printf '%s|%s' "${LANG:-}" "${LC_ALL:-}""#,
ShellExecOptions {
env: Some([("LANG".to_string(), "C".to_string())].into()),
..Default::default()
},
)
.await
.expect("explicit locale");
assert_eq!(out.stdout, "C|");
}
#[tokio::test]
async fn non_utf8_output_decodes_with_the_console_code_page() {
if rpi_tools::output_decode::console_encoding() != encoding_rs::GBK {
eprintln!("skipping: console code page is not GBK (936) on this host");
return;
}
let temp = tempfile::tempdir().unwrap();
let env = Arc::new(OsExecutionEnv::with_cwd(temp.path().to_path_buf()));
match env.exec("true", ShellExecOptions::default()).await {
Ok(_) => {}
Err(error) if error.code == ExecutionErrorCode::ShellUnavailable => {
eprintln!("skipping: no usable bash on this host ({error})");
return;
}
Err(error) => panic!("unexpected shell error: {error:?}"),
}
let out = env
.exec(
r#"printf '\326\320\316\304\n'"#,
ShellExecOptions::default(),
)
.await
.expect("legacy output");
assert_eq!(out.stdout, "中文\n");
}