File size: 14,651 Bytes
2569bec | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 | use std::any::Any;
use std::collections::HashMap;
use std::fs;
use std::future::Future;
use std::panic::AssertUnwindSafe;
use std::path::Path;
use std::path::PathBuf;
use std::time::Duration;
use anyhow::Context;
use anyhow::Result;
use anyhow::anyhow;
use codex_utils_pty::SpawnedProcess;
use codex_utils_pty::TerminalSize;
use futures::FutureExt;
use pretty_assertions::assert_eq;
use tempfile::TempDir;
use tokio::io::AsyncBufReadExt;
use tokio::io::BufReader;
use tokio::process::Command as TokioCommand;
use tokio::time::timeout;
use super::WineRuntimePaths;
use super::WineTestCommand;
use super::WineTestProcess;
use super::install_powershell_runtime;
async fn waiting_smoke_process() -> Result<WineTestProcess> {
let executable = codex_utils_cargo_bin::cargo_bin("wine-smoke")?;
let mut process = WineTestCommand::new(executable).arg("--wait").spawn()?;
let mut lines = BufReader::new(process.take_stdout()).lines();
let ready_line = lines
.next_line()
.await?
.context("Windows smoke process exited before becoming ready")?;
assert_eq!(ready_line, "WINE_TEST_READY");
Ok(process)
}
fn prefix_path(process: &WineTestProcess) -> PathBuf {
process
.processes
.as_ref()
.expect("Wine process guard")
.prefix
.path()
.to_path_buf()
}
fn assert_prefix_removed(prefix: &Path) {
assert!(
!prefix.exists(),
"Wine prefix remains: {}",
prefix.display()
);
}
fn assert_panic_message(panic: Box<dyn Any + Send>, expected: &str) {
assert_eq!(panic.downcast_ref::<&str>(), Some(&expected));
}
async fn assert_future_panics<T>(future: impl Future<Output = T>, expected: &str) {
let panic = match AssertUnwindSafe(future).catch_unwind().await {
Ok(_) => panic!("future should panic"),
Err(panic) => panic,
};
assert_panic_message(panic, expected);
}
async fn process_with_failing_wineserver_stop() -> Result<WineTestProcess> {
let mut process = waiting_smoke_process().await?;
let processes = process.processes.as_mut().expect("Wine process guard");
let mut command = TokioCommand::from(processes.stop_wineserver_command());
let status = command
.status()
.await
.context("pre-stop isolated wineserver")?;
assert!(status.success(), "wineserver exited with {status}");
processes.runtime.wineserver = processes.prefix.path().join("missing-wineserver");
Ok(process)
}
#[tokio::test]
async fn dropping_without_teardown_panics() -> Result<()> {
let process = waiting_smoke_process().await?;
let prefix = prefix_path(&process);
assert_future_panics(
async move { drop(process) },
"WineTestProcess dropped without async teardown",
)
.await;
assert_prefix_removed(&prefix);
Ok(())
}
#[tokio::test]
async fn dropping_while_panicking_does_not_panic_again() -> Result<()> {
let process = waiting_smoke_process().await?;
let prefix = prefix_path(&process);
assert_future_panics(
async move {
let _process = process;
panic!("sentinel panic");
},
"sentinel panic",
)
.await;
assert_prefix_removed(&prefix);
Ok(())
}
#[tokio::test]
async fn async_teardown_disarms_drop_bomb() -> Result<()> {
let process = waiting_smoke_process().await?;
let prefix = prefix_path(&process);
process.shutdown().await?;
assert_prefix_removed(&prefix);
Ok(())
}
#[tokio::test]
async fn take_stdout_panics_when_called_twice() -> Result<()> {
let mut process = waiting_smoke_process().await?;
let prefix = prefix_path(&process);
assert_future_panics(
async {
process.take_stdout();
},
"Wine process stdout has already been taken",
)
.await;
process.shutdown().await?;
assert_prefix_removed(&prefix);
Ok(())
}
#[tokio::test]
async fn scope_returns_value_and_tears_down() -> Result<()> {
let process = waiting_smoke_process().await?;
let prefix = prefix_path(&process);
let value = process
.scope(async { Ok::<_, anyhow::Error>("scope value") })
.await?;
assert_eq!(value, "scope value");
assert_prefix_removed(&prefix);
Ok(())
}
#[tokio::test]
async fn scope_returns_body_error_and_tears_down() -> Result<()> {
let process = waiting_smoke_process().await?;
let prefix = prefix_path(&process);
let error = process
.scope(async { Err::<(), _>(anyhow!("scope body failed")) })
.await
.expect_err("scope body should fail");
assert_eq!(error.to_string(), "scope body failed");
assert_prefix_removed(&prefix);
Ok(())
}
#[tokio::test]
async fn scope_panic_preserves_panic_and_tears_down() -> Result<()> {
let process = waiting_smoke_process().await?;
let prefix = prefix_path(&process);
assert_future_panics(
process.scope::<()>(async { panic!("scope panic") }),
"scope panic",
)
.await;
assert_prefix_removed(&prefix);
Ok(())
}
#[tokio::test]
async fn shutdown_reports_nonzero_process_exit() -> Result<()> {
let executable = codex_utils_cargo_bin::cargo_bin("wine-smoke")?;
let mut process = WineTestCommand::new(executable).arg("--fail").spawn()?;
let prefix = prefix_path(&process);
let status = process
.processes
.as_mut()
.expect("Wine process guard")
.child
.wait()
.await?;
assert!(
!status.success(),
"Windows smoke process unexpectedly passed"
);
let error = process.shutdown().await.expect_err("shutdown should fail");
assert!(error.to_string().starts_with("Windows process exited with"));
assert_prefix_removed(&prefix);
Ok(())
}
#[tokio::test]
async fn scope_preserves_body_error_when_teardown_also_fails() -> Result<()> {
let process = process_with_failing_wineserver_stop().await?;
let prefix = prefix_path(&process);
let error = process
.scope(async { Err::<(), _>(anyhow!("scope body failed")) })
.await
.expect_err("scope body and teardown should fail");
assert!(
error
.to_string()
.starts_with("Wine teardown also failed: stop isolated wineserver"),
"unexpected error: {error:#}"
);
assert_eq!(
error.chain().last().map(ToString::to_string),
Some("scope body failed".to_string())
);
assert_prefix_removed(&prefix);
Ok(())
}
#[tokio::test]
async fn shutdown_returns_teardown_error() -> Result<()> {
let process = process_with_failing_wineserver_stop().await?;
let prefix = prefix_path(&process);
let error = process
.shutdown()
.await
.expect_err("shutdown should report a wineserver failure");
assert_eq!(error.to_string(), "stop isolated wineserver");
assert_prefix_removed(&prefix);
Ok(())
}
#[test]
fn powershell_runtime_is_materialized_at_the_windows_fallback_path() -> Result<()> {
let prefix = TempDir::new()?;
let runtime = TempDir::new()?;
fs::create_dir(runtime.path().join("Modules"))?;
fs::write(runtime.path().join("pwsh.exe"), b"pwsh")?;
fs::write(runtime.path().join("Modules").join("marker.txt"), b"module")?;
install_powershell_runtime(prefix.path(), runtime.path())?;
let installed = prefix
.path()
.join("drive_c")
.join("Program Files")
.join("PowerShell")
.join("7");
assert_eq!(fs::read(installed.join("pwsh.exe"))?, b"pwsh");
assert_eq!(
fs::read(installed.join("Modules").join("marker.txt"))?,
b"module"
);
Ok(())
}
#[test]
fn powershell_runtime_follows_runfiles_symlinks() -> Result<()> {
let prefix = TempDir::new()?;
let runtime = TempDir::new()?;
let backing = TempDir::new()?;
let backing_file = backing.path().join("pwsh.exe");
fs::write(&backing_file, b"pwsh")?;
std::os::unix::fs::symlink(&backing_file, runtime.path().join("pwsh.exe"))?;
install_powershell_runtime(prefix.path(), runtime.path())?;
let installed = prefix
.path()
.join("drive_c")
.join("Program Files")
.join("PowerShell")
.join("7")
.join("pwsh.exe");
assert_eq!(fs::read(installed)?, b"pwsh");
Ok(())
}
#[tokio::test]
async fn pinned_powershell_runs_under_wine_with_a_pty() -> Result<()> {
// Keep this integration smoke test local to the Wine support crate. The
// production-shaped PowerShell launch path belongs to exec-server tests.
// The marker makes the assertion resilient to Wine or PTY startup chatter.
const POWERSHELL_SMOKE_MARKER: &str = "WINE_PWSH_SMOKE";
// Besides proving that the pinned runtime starts, report the properties
// that shell detection and command construction rely on: PowerShell 7 Core
// running with Windows semantics and a backslash path separator.
const POWERSHELL_SMOKE_SCRIPT: &str = concat!(
"$ErrorActionPreference = 'Stop'; ",
"[Console]::OutputEncoding = [System.Text.UTF8Encoding]::new($false); ",
"$separatorCode = [int]([System.IO.Path]::DirectorySeparatorChar); ",
"if ($PSVersionTable.PSVersion.Major -ne 7) { throw 'expected PowerShell 7' }; ",
"if ($PSVersionTable.PSEdition -ne 'Core') { throw 'expected PowerShell Core' }; ",
"if (-not $IsWindows) { throw 'expected Windows semantics' }; ",
"if ($separatorCode -ne 92) { throw 'expected backslash path separator' }; ",
"Write-Output ('WINE_PWSH_SMOKE|' + ",
"$PSVersionTable.PSVersion.ToString() + '|' + ",
"$PSVersionTable.PSEdition + '|' + ",
"$IsWindows.ToString().ToLowerInvariant() + '|' + $separatorCode)",
);
let runtime = WineRuntimePaths::from_runfiles()?;
let prefix = TempDir::new()?;
install_powershell_runtime(prefix.path(), &runtime.powershell_runtime)?;
let mut env = std::env::vars().collect::<HashMap<_, _>>();
env.remove("DISPLAY");
env.extend([
(
"HOME".to_string(),
prefix.path().to_string_lossy().into_owned(),
),
(
"XDG_RUNTIME_DIR".to_string(),
prefix.path().to_string_lossy().into_owned(),
),
("WINEARCH".to_string(), "win64".to_string()),
(
"WINEPREFIX".to_string(),
prefix.path().to_string_lossy().into_owned(),
),
(
"WINEDLLPATH".to_string(),
runtime.dll_path.to_string_lossy().into_owned(),
),
(
"WINESERVER".to_string(),
runtime.wineserver.to_string_lossy().into_owned(),
),
("WINEDEBUG".to_string(), "-all".to_string()),
(
"WINEDLLOVERRIDES".to_string(),
"mscoree,mshtml,winegstreamer=".to_string(),
),
("LANG".to_string(), "C.UTF-8".to_string()),
("LC_ALL".to_string(), "C.UTF-8".to_string()),
("LC_CTYPE".to_string(), "C.UTF-8".to_string()),
("TEMP".to_string(), r"C:\windows\temp".to_string()),
("TMP".to_string(), r"C:\windows\temp".to_string()),
]);
let args = [
r"C:\Program Files\PowerShell\7\pwsh.exe".to_string(),
"-NoLogo".to_string(),
"-NoProfile".to_string(),
"-NonInteractive".to_string(),
"-Command".to_string(),
POWERSHELL_SMOKE_SCRIPT.to_string(),
];
let wine = runtime.wine.to_string_lossy().into_owned();
let SpawnedProcess {
session,
mut stdout_rx,
mut stderr_rx,
exit_rx,
} = codex_utils_pty::spawn_pty_process(
&wine,
&args,
prefix.path(),
&env,
/*arg0*/ &None,
TerminalSize::default(),
/*inherited_fds*/ &[],
)
.await?;
let command_result = timeout(Duration::from_secs(30), async {
let stdout = async {
let mut output = Vec::new();
while let Some(chunk) = stdout_rx.recv().await {
output.extend(chunk);
}
output
};
let stderr = async {
let mut output = Vec::new();
while let Some(chunk) = stderr_rx.recv().await {
output.extend(chunk);
}
output
};
let (stdout, stderr, exit_code) = tokio::join!(stdout, stderr, exit_rx);
Ok::<_, anyhow::Error>((stdout, stderr, exit_code.context("wait for PowerShell")?))
})
.await
.context("PowerShell smoke test timed out")
.and_then(std::convert::identity);
drop(session);
let shutdown_result = timeout(Duration::from_secs(10), async {
let mut command = TokioCommand::new(&runtime.wineserver);
command
.args(["-k", "-w"])
.env("HOME", prefix.path())
.env("WINEPREFIX", prefix.path())
.env("XDG_RUNTIME_DIR", prefix.path())
.kill_on_drop(true);
let status = command.status().await.context("stop isolated wineserver")?;
anyhow::ensure!(
status.success() || status.code() == Some(1),
"wineserver exited with {status}"
);
Ok::<_, anyhow::Error>(())
})
.await
.context("stop isolated wineserver timed out")
.and_then(std::convert::identity);
let (stdout, stderr, exit_code) = match (command_result, shutdown_result) {
(Ok(output), Ok(())) => output,
(Err(error), Ok(())) => return Err(error),
(Ok(_), Err(error)) => return Err(error),
(Err(error), Err(shutdown_error)) => {
return Err(error.context(format!("Wine teardown also failed: {shutdown_error:#}")));
}
};
anyhow::ensure!(
exit_code == 0,
"PowerShell exited with {}; stderr: {}",
exit_code,
String::from_utf8_lossy(&stderr),
);
let output = String::from_utf8(stdout)?;
let marker_start = output
.find(POWERSHELL_SMOKE_MARKER)
.with_context(|| format!("PowerShell smoke marker was missing from {output:?}"))?;
let smoke = output[marker_start..]
.lines()
.next()
.context("PowerShell smoke marker line was incomplete")?
.trim_end_matches('\r');
let fields = smoke.split('|').collect::<Vec<_>>();
assert_eq!(
fields.len(),
5,
"unexpected PowerShell smoke output: {smoke}"
);
assert_eq!(fields[0], POWERSHELL_SMOKE_MARKER);
assert_eq!(
fields[1].split('.').next(),
Some("7"),
"expected PowerShell 7.x, got {}",
fields[1],
);
assert_eq!(&fields[2..], &["Core", "true", "92"]);
Ok(())
}
|