mirror of
https://github.com/openai/codex.git
synced 2026-09-05 15:18:41 +00:00
58 lines
2.0 KiB
Rust
58 lines
2.0 KiB
Rust
//! Test support for running the Windows exec-server under Wine.
|
|
|
|
use std::future::Future;
|
|
|
|
use anyhow::Context;
|
|
use anyhow::Result;
|
|
use tokio::io::AsyncBufReadExt;
|
|
use tokio::io::BufReader;
|
|
use wine_test_support::WineTestCommand;
|
|
use wine_test_support::WineTestCommandContext;
|
|
|
|
/// Runs the Windows exec-server under Wine for the duration of a scoped operation.
|
|
pub struct WineExecServer;
|
|
|
|
impl WineExecServer {
|
|
/// Starts the server, passes its WebSocket URL to `operation`, and tears it down afterward.
|
|
pub async fn scope<T, F, Fut>(self, operation: F) -> Result<T>
|
|
where
|
|
F: FnOnce(String) -> Fut,
|
|
Fut: Future<Output = Result<T>>,
|
|
{
|
|
self.scope_with_command_context(|exec_server_url, _context| {
|
|
operation(exec_server_url)
|
|
})
|
|
.await
|
|
}
|
|
|
|
/// Starts the server and passes its URL and shared-prefix command context to `operation`.
|
|
pub async fn scope_with_command_context<T, F, Fut>(self, operation: F) -> Result<T>
|
|
where
|
|
F: FnOnce(String, WineTestCommandContext) -> Fut,
|
|
Fut: Future<Output = Result<T>>,
|
|
{
|
|
let executable = codex_utils_cargo_bin::cargo_bin("wine-windows-exec-server")?;
|
|
let mut exec_server = WineTestCommand::new(executable)
|
|
.env("CODEX_HOME", r"C:\codex-home")
|
|
.spawn()?;
|
|
let stdout = exec_server.take_stdout();
|
|
let command_context = exec_server.command_context();
|
|
|
|
exec_server
|
|
.scope(async move {
|
|
let mut lines = BufReader::new(stdout).lines();
|
|
let exec_server_url = loop {
|
|
let line = lines
|
|
.next_line()
|
|
.await?
|
|
.context("Wine exec-server exited before reporting its URL")?;
|
|
if line.starts_with("ws://") {
|
|
break line;
|
|
}
|
|
};
|
|
operation(exec_server_url, command_context).await
|
|
})
|
|
.await
|
|
}
|
|
}
|