From 8636a80aacf0e07b5c378f37d0ec7dd970a7c087 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Sun, 21 Feb 2021 22:19:30 +0000 Subject: [PATCH] apitest: Restructure setup NFC Signed-off-by: Ian Jackson --- apitest.rs | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++- wdriver.rs | 51 +++++++++----------------------------- 2 files changed, 82 insertions(+), 41 deletions(-) diff --git a/apitest.rs b/apitest.rs index ba455266..f3c658fb 100644 --- a/apitest.rs +++ b/apitest.rs @@ -129,6 +129,13 @@ pub struct Opts { pub tests: WantedTestsOpt, } +pub struct SetupCore { + pub ds: DirSubst, + pub mgmt_conn: MgmtChannel, + pub server_child: process::Child, + pub wanted_tests: TrackWantedTests, +} + #[derive(Clone,Debug)] #[derive(StructOpt)] pub struct WantedTestsOpt { @@ -163,7 +170,7 @@ impl Drop for TrackWantedTests { .filter(|s| !self.found.contains(s)) .collect::>(); - if !missing_tests.is_empty() { + if !missing_tests.is_empty() && !self.found.is_empty() { for f in &self.found { eprintln!("fyi: test that exists: {}", f); } @@ -673,6 +680,69 @@ pub fn prepare_game(ds: &DirSubst, table: &str) -> InstanceName { instance } +#[throws(AE)] +pub fn setup_core(module_paths: &[&str]) -> + (O, cleanup_notify::Handle, Instance, SetupCore) + where O: StructOpt + AsRef +{ + let mut builder = env_logger::Builder::new(); + builder + .format_timestamp_micros() + .format_level(true) + .filter_module("otter_apitest_tests", log::LevelFilter::Debug); + + for module in module_paths { + builder + .filter_module(module, log::LevelFilter::Debug); + } + + builder + .filter_level(log::LevelFilter::Info) + .parse_env("OTTER_WDT_LOG") + .init(); + debug!("starting"); + + let caller_opts = O::from_args(); + let opts = caller_opts.as_ref(); + + let current_exe: String = env::current_exe() + .context("find current executable")? + .to_str() + .ok_or_else(|| anyhow!("current executable path is not UTF-8 !"))? + .to_owned(); + + if !opts.no_bwrap { + reinvoke_via_bwrap(&opts, ¤t_exe) + .context("reinvoke via bwrap")?; + } + + info!("pid = {}", nix::unistd::getpid()); + sleep(opts.pause.into()); + + let cln = cleanup_notify::Handle::new()?; + let ds = prepare_tmpdir(&opts, ¤t_exe)?; + + let (mgmt_conn, server_child) = + prepare_gameserver(&cln, &ds).always_context("setup game server")?; + + let instance_name = + prepare_game(&ds, TABLE).context("setup game")?; + + let wanted_tests = opts.tests.track(); + + (caller_opts, + cln, + Instance( + instance_name + ), + SetupCore { + ds, + mgmt_conn, + server_child, + wanted_tests, + }) +} + #[derive(Debug)] pub struct Window { pub name: String, diff --git a/wdriver.rs b/wdriver.rs index 0e19e4ae..0795d283 100644 --- a/wdriver.rs +++ b/wdriver.rs @@ -34,6 +34,9 @@ impl Deref for Opts { type Target = apitest::Opts; fn deref(&self) -> &Self::Target { &self.at } } +impl AsRef for Opts { + fn as_ref(&self) -> &apitest::Opts { &self.at } +} #[derive(Debug)] pub struct FinalInfoCollection; @@ -43,9 +46,9 @@ type WindowState = Option; #[derive(Debug)] pub struct Setup { + pub opts: Opts, pub ds: DirSubst, pub mgmt_conn: MgmtChannel, - pub opts: Opts, pub server_child: process::Child, wanted_tests: TrackWantedTests, driver: T4d, @@ -652,50 +655,21 @@ impl Drop for Setup { #[throws(AE)] pub fn setup(exe_module_path: &str) -> (Setup, Instance) { - env_logger::Builder::new() - .format_timestamp_micros() - .format_level(true) - .filter_module("otter_webdriver_tests", log::LevelFilter::Debug) - .filter_module(exe_module_path, log::LevelFilter::Debug) - .filter_level(log::LevelFilter::Info) - .parse_env("OTTER_WDT_LOG") - .init(); - debug!("starting"); - - let current_exe: String = env::current_exe() - .context("find current executable")? - .to_str() - .ok_or_else(|| anyhow!("current executable path is not UTF-8 !"))? - .to_owned(); - - let opts = Opts::from_args(); - if !opts.no_bwrap { - reinvoke_via_bwrap(&opts, ¤t_exe) - .context("reinvoke via bwrap")?; - } - - info!("pid = {}", nix::unistd::getpid()); - sleep(opts.pause.into()); - - let cln = cleanup_notify::Handle::new()?; - let ds = prepare_tmpdir(&opts, ¤t_exe)?; + let (opts, cln, instance, apitest::SetupCore { + ds, + mgmt_conn, + server_child, + wanted_tests + }) = apitest::setup_core(&[exe_module_path, "otter_webdriver_tests"])?; prepare_xserver(&cln, &ds).always_context("setup X server")?; - let (mgmt_conn, server_child) = - prepare_gameserver(&cln, &ds).always_context("setup game server")?; - - let instance_name = - prepare_game(&ds, TABLE).context("setup game")?; - let final_hook = FinalInfoCollection; prepare_geckodriver(&opts, &cln).always_context("setup webdriver server")?; let (driver, screenshot_count, windows_squirreled) = prepare_thirtyfour().always_context("prepare web session")?; - let wanted_tests = opts.tests.track(); - (Setup { ds, mgmt_conn, @@ -707,10 +681,7 @@ pub fn setup(exe_module_path: &str) -> (Setup, Instance) { current_window: None, windows_squirreled, final_hook, - }, - Instance( - instance_name - )) + }, instance) } impl Setup { -- 2.30.2