chiark / gitweb /
apitest: Tests tracking: Encapsulate
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 21 Feb 2021 21:53:39 +0000 (21:53 +0000)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 21 Feb 2021 21:53:39 +0000 (21:53 +0000)
NFC

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

index 3f2472b752a1f73e7277564b4fa0f01839235e00..ba4552663796a49fdab900fee2c24561942e5649 100644 (file)
@@ -126,13 +126,52 @@ pub struct Opts {
   pub pause: humantime::Duration,
 
   #[structopt(flatten)]
-  pub tests: WantedTests,
+  pub tests: WantedTestsOpt,
 }
 
 #[derive(Clone,Debug)]
 #[derive(StructOpt)]
-pub struct WantedTests {
-  pub tests: Vec<String>,
+pub struct WantedTestsOpt {
+  tests: Vec<String>,
+}
+
+#[derive(Debug)]
+pub struct TrackWantedTests {
+  wanted: WantedTestsOpt,
+  found: BTreeSet<String>,
+}
+
+impl WantedTestsOpt {
+  pub fn track(&self) -> TrackWantedTests {
+    TrackWantedTests { wanted: self.clone(), found: default() }
+  }
+}
+
+impl TrackWantedTests {
+  pub fn wantp(&mut self, tname: &str) -> bool {
+    self.found.insert(tname.to_owned());
+    let y =
+      self.wanted.tests.is_empty() ||
+      self.wanted.tests.iter().any(|s| s==tname);
+    y
+  }
+}
+
+impl Drop for TrackWantedTests {
+  fn drop(&mut self) {
+    let missing_tests = self.wanted.tests.iter().cloned()
+      .filter(|s| !self.found.contains(s))
+      .collect::<Vec<_>>();
+
+    if !missing_tests.is_empty() {
+      for f in &self.found {
+        eprintln!("fyi: test that exists: {}", f);
+      }
+      for m in &missing_tests {
+        eprintln!("warning: unknown test requested: {}", m);
+      }
+    }
+  }
 }
 
 #[derive(Clone,Debug)]
index 6b80c3b1c293c431ab16b8f7a27bc98809e99a0a..0e19e4ae7e0e28f238ea85079c87424786ae7364 100644 (file)
@@ -47,7 +47,7 @@ pub struct Setup {
   pub mgmt_conn: MgmtChannel,
   pub opts: Opts,
   pub server_child: process::Child,
-  found_tests: BTreeSet<String>,
+  wanted_tests: TrackWantedTests,
   driver: T4d,
   current_window: WindowState,
   screenshot_count: ScreenShotCount,
@@ -359,11 +359,7 @@ macro_rules! ctx_with_setup {
 
 impl Setup {
   pub fn want_test(&mut self, tname: &str) -> bool {
-    self.found_tests.insert(tname.to_owned());
-    let y =
-      self.opts.tests.tests.is_empty() ||
-      self.opts.tests.tests.iter().any(|s| s==tname);
-    y
+    self.wanted_tests.wantp(tname)
   }
 
   #[throws(AE)]
@@ -651,19 +647,6 @@ impl Drop for Setup {
     })()
       .context("screenshots, in Setup::drop")
       .just_warn();
-
-    let missing_tests = self.opts.tests.tests.iter().cloned()
-      .filter(|s| !self.found_tests.contains(s))
-      .collect::<Vec<_>>();
-
-    if !missing_tests.is_empty() {
-      for f in &self.found_tests {
-        eprintln!("fyi: test that exists: {}", f);
-      }
-      for m in &missing_tests {
-        eprintln!("warning: unknown test requested: {}", m);
-      }
-    }
   }
 }
 
@@ -711,13 +694,15 @@ pub fn setup(exe_module_path: &str) -> (Setup, Instance) {
   let (driver, screenshot_count, windows_squirreled) =
     prepare_thirtyfour().always_context("prepare web session")?;
 
+  let wanted_tests = opts.tests.track();
+
   (Setup {
     ds,
     mgmt_conn,
     server_child,
     driver,
     opts,
-    found_tests: default(),
+    wanted_tests,
     screenshot_count,
     current_window: None,
     windows_squirreled,