//! The binary's failure path, end to end: when the main window cannot be //! opened (no display server, no Vulkan or GL driver), `voelin-gpui` prints //! why and exits with status 1 instead of staying alive with no window and no //! message. //! //! It has to start the real binary. GPUI's Linux platform runs the launch //! callback before it starts its event loop, and the loop forgets a quit asked //! for before it started, so a failure handled by quitting inside the //! callback hung the process; the test platform of the unit tests has no such //! loop and cannot show it. //! //! The no-driver test needs a test X server in `VOELIN_X11_TEST_DISPLAY` (an //! Xvfb, as CI has; never the desktop's display) and is skipped without one. #![cfg(target_os = "linux")] use std::fs; use std::os::unix::fs::PermissionsExt as _; use std::path::{Path, PathBuf}; use std::process::{Command, ExitStatus, Stdio}; use std::time::{Duration, Instant}; /// Where the drivers are not: the Vulkan loader, EGL's vendor library /// directory and Mesa's driver directory all point here. const NOWHERE: &str = "/nonexistent-voelin-gpui-test"; /// Loaded machines are slow, so the wait is generous; a run that works takes a /// second or two. const WAIT: Duration = Duration::from_secs(120); /// The scratch directory of one run, removed at the end. struct Scratch(PathBuf); impl Drop for Scratch { fn drop(&mut self) { let _ = fs::remove_dir_all(&self.0); } } /// How a run of the binary went. enum Outcome { /// It ended, with this status and this much of the log. Ended(ExitStatus, String), /// It picked a GPU: the window can open, so nothing failed. Opened, /// It was still running after [`WAIT`] with no GPU picked. Hung(String), } /// Run the binary with the drivers hidden (`hide_drivers`) on `display` /// (none: no display server at all) and wait for it to end, to pick a GPU or /// to run out of time. Nothing of the user's state, display or session bus is /// in its environment. fn run_binary(name: &str, display: Option<&str>, hide_drivers: bool) -> Outcome { let scratch = Scratch( Path::new(env!("CARGO_TARGET_TMPDIR")).join(format!("{name}-{}", std::process::id())), ); let home = scratch.0.join("home"); let runtime = scratch.0.join("runtime"); fs::create_dir_all(&runtime).unwrap(); fs::set_permissions(&runtime, fs::Permissions::from_mode(0o700)).unwrap(); let log = scratch.0.join("stderr.log"); let mut command = Command::new(env!("CARGO_BIN_EXE_voelin-gpui")); command .env_remove("DISPLAY") .env_remove("WAYLAND_DISPLAY") .env_remove("WAYLAND_SOCKET") .env_remove("XDG_SESSION_TYPE") .env_remove("DBUS_SESSION_BUS_ADDRESS") .env_remove("RUST_LOG") .env("HOME", &home) .env("XDG_RUNTIME_DIR", &runtime) .env("XDG_CONFIG_HOME", home.join("config")) .env("XDG_DATA_HOME", home.join("data")) .env("XDG_STATE_HOME", home.join("state")) .env("XDG_CACHE_HOME", home.join("cache")) .env("VOELIN_DATA_DIR", home.join("voelin")) // The log line that tells a GPU was picked. .env("VOELIN_LOG", "warn,voelin_gpui=info,gpui_wgpu=info") .stdin(Stdio::null()) .stdout(Stdio::null()) .stderr(fs::File::create(&log).unwrap()); if let Some(display) = display { command.env("DISPLAY", display); } if hide_drivers { command .env("VK_DRIVER_FILES", format!("{NOWHERE}/vulkan.json")) .env("VK_ICD_FILENAMES", format!("{NOWHERE}/vulkan.json")) .env("__EGL_VENDOR_LIBRARY_DIRS", NOWHERE) .env("__EGL_VENDOR_LIBRARY_FILENAMES", format!("{NOWHERE}/egl.json")) .env("LIBGL_DRIVERS_PATH", NOWHERE); } let mut child = command.spawn().unwrap(); let deadline = Instant::now() + WAIT; loop { if let Some(status) = child.try_wait().unwrap() { return Outcome::Ended(status, fs::read_to_string(&log).unwrap_or_default()); } let seen = fs::read_to_string(&log).unwrap_or_default(); let outcome = if seen.contains("Selected GPU adapter") { Outcome::Opened } else if Instant::now() > deadline { Outcome::Hung(seen) } else { std::thread::sleep(Duration::from_millis(50)); continue; }; let _ = child.kill(); let _ = child.wait(); return outcome; } } #[test] fn a_window_that_cannot_open_for_want_of_a_driver_ends_the_binary_with_an_error() { let Some(display) = std::env::var("VOELIN_X11_TEST_DISPLAY").ok().filter(|d| !d.is_empty()) else { eprintln!("skipped: VOELIN_X11_TEST_DISPLAY is not set"); return; }; match run_binary("no-driver", Some(&display), true) { Outcome::Ended(status, seen) => { assert_eq!(status.code(), Some(1), "{seen}"); assert!(seen.contains("cannot open the main window"), "{seen}"); } Outcome::Opened => eprintln!( "skipped: this machine's driver was found in spite of the environment, \ so the window can open" ), Outcome::Hung(seen) => panic!( "still running after {WAIT:?} with no window and no GPU: a quit asked for in the \ launch callback is lost\n{seen}" ), } } #[test] fn without_a_display_server_the_binary_ends_with_an_error() { match run_binary("no-display", None, false) { Outcome::Ended(status, seen) => { assert_eq!(status.code(), Some(1), "{seen}"); assert!(seen.contains("cannot open the main window"), "{seen}"); assert!(seen.contains("no display"), "{seen}"); } Outcome::Opened => panic!("a GPU was picked without a display server"), Outcome::Hung(seen) => panic!( "still running after {WAIT:?} with no window: GPUI's headless platform keeps \ the app alive\n{seen}" ), } }