Skip to main content

vertigo_testing/
runner.rs

1//! A libtest-compatible runner (filters, `--list`, `--test-threads`, `--ignored`) which starts
2//! the environment once per suite, lazily, and gives each browser test its own Chrome session.
3
4use std::{future::Future, pin::Pin, sync::Arc};
5
6use anyhow::{Result, anyhow};
7use libtest_mimic::{Arguments, Failed, Trial};
8use thirtyfour::testing::{BrowserTestError, run_browser_test};
9use tokio::{runtime::Runtime, sync::OnceCell};
10
11use crate::{ctx::Ctx, env::Env};
12
13/// Not `Send`: each test is run by `block_on` on its own runner thread.
14pub type TestFuture = Pin<Box<dyn Future<Output = Result<()>>>>;
15
16pub enum Body<E> {
17    /// Plain HTTP against the app, no browser.
18    Http(fn(Arc<E>) -> TestFuture),
19    Browser(fn(Ctx<E>) -> TestFuture),
20}
21
22pub struct TestCase<E> {
23    pub name: String,
24    pub body: Body<E>,
25    /// Skipped unless run with `--ignored` / `--include-ignored`.
26    pub ignored: bool,
27}
28
29/// Tests of known bugs, not fixed yet: they describe the correct behavior, but are skipped by
30/// default to keep the suite green.
31pub fn known_failures<E>(tests: Vec<TestCase<E>>) -> Vec<TestCase<E>> {
32    tests
33        .into_iter()
34        .map(|test| TestCase {
35            ignored: true,
36            ..test
37        })
38        .collect()
39}
40
41/// `browser_tests![module::test, ...]`: tests taking a [`Ctx`].
42#[macro_export]
43macro_rules! browser_tests {
44    ($($test:path),* $(,)?) => {
45        vec![$($crate::runner::TestCase {
46            name: stringify!($test).replace(' ', ""),
47            body: $crate::runner::Body::Browser(|ctx| Box::pin($test(ctx))),
48            ignored: false,
49        }),*]
50    };
51}
52
53/// `http_tests![module::test, ...]`: tests taking an `Arc` of the environment.
54#[macro_export]
55macro_rules! http_tests {
56    ($($test:path),* $(,)?) => {
57        vec![$($crate::runner::TestCase {
58            name: stringify!($test).replace(' ', ""),
59            body: $crate::runner::Body::Http(|env| Box::pin($test(env))),
60            ignored: false,
61        }),*]
62    };
63}
64
65type SharedEnv<E> = Arc<OnceCell<Result<Arc<E>, String>>>;
66
67/// Runs `tests` as the suite `suite` and exits with libtest's exit code.
68pub fn run_suite<E: Env>(suite: &'static str, tests: Vec<TestCase<E>>) -> ! {
69    let mut args = Arguments::from_args();
70    if args.test_threads.is_none() {
71        let cpus = std::thread::available_parallelism().map_or(1, |n| n.get());
72        args.test_threads = Some(cpus.min(4));
73    }
74
75    let runtime = match Runtime::new() {
76        Ok(runtime) => Arc::new(runtime),
77        Err(err) => {
78            eprintln!("can't start the tokio runtime: {err}");
79            std::process::exit(101);
80        }
81    };
82    let env: SharedEnv<E> = Arc::default();
83
84    let trials = tests
85        .into_iter()
86        .map(|test| {
87            let runtime = runtime.clone();
88            let env = env.clone();
89            let name = test.name.clone();
90            let ignored = test.ignored;
91            Trial::test(name, move || {
92                runtime
93                    .block_on(run_test(suite, &env, test))
94                    .map_err(|err| Failed::from(format!("{err:?}")))
95            })
96            .with_ignored_flag(ignored)
97        })
98        .collect();
99
100    let conclusion = libtest_mimic::run(&args, trials);
101
102    if let Some(Ok(env)) = env.get() {
103        runtime.block_on(env.shutdown());
104    }
105
106    conclusion.exit()
107}
108
109async fn run_test<E: Env>(
110    suite: &'static str,
111    env: &SharedEnv<E>,
112    test: TestCase<E>,
113) -> Result<()> {
114    let env = env
115        .get_or_init(|| async {
116            E::start(suite)
117                .await
118                .map(Arc::new)
119                .map_err(|err| format!("{err:?}"))
120        })
121        .await
122        .clone()
123        .map_err(|err| anyhow!("the test environment didn't start:\n{err}"))?;
124
125    match test.body {
126        Body::Http(body) => body(env).await,
127        Body::Browser(body) => {
128            let driver = env.new_browser().await?;
129            let result = run_browser_test(async { Ok(driver) }, |driver| async move {
130                let ctx = Ctx::new(env.clone(), driver, &test.name);
131                let result = body(ctx.clone()).await;
132                ctx.finish(result).await
133            })
134            .await;
135
136            match result {
137                Ok(()) => Ok(()),
138                Err(BrowserTestError::Body(err)) => Err(err),
139                Err(BrowserTestError::BodyAndCleanup { body, .. }) => Err(body),
140                Err(err) => Err(anyhow!("{err}")),
141            }
142        }
143    }
144}