chiark / gitweb /
apitest: Restructure setup
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 21 Feb 2021 22:19:30 +0000 (22:19 +0000)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 21 Feb 2021 22:19:39 +0000 (22:19 +0000)
NFC

Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
apitest.rs
wdriver.rs

index ba4552663796a49fdab900fee2c24561942e5649..f3c658fbf39456bc3fc975a56d5d13eec37b7042 100644 (file)
@@ -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::<Vec<_>>();
 
-    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<O>(module_paths: &[&str]) ->
+  (O, cleanup_notify::Handle, Instance, SetupCore)
+  where O: StructOpt + AsRef<Opts>
+{
+  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, &current_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, &current_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,
index 0e19e4ae7e0e28f238ea85079c87424786ae7364..0795d283c9670e15ed31289f76219b607328268a 100644 (file)
@@ -34,6 +34,9 @@ impl Deref for Opts {
   type Target = apitest::Opts;
   fn deref(&self) -> &Self::Target { &self.at }
 }
+impl AsRef<apitest::Opts> for Opts {
+  fn as_ref(&self) -> &apitest::Opts { &self.at }
+}
 
 #[derive(Debug)]
 pub struct FinalInfoCollection;
@@ -43,9 +46,9 @@ type WindowState = Option<String>;
 
 #[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, &current_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, &current_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 {