use std::cell::{Cell, RefCell};
use std::collections::VecDeque;
use std::fmt::Write as _;
use std::future::Future;
use std::io::Write as _;
use std::pin::{Pin, pin};
use std::rc::Rc;
use std::sync::OnceLock;
use std::time::{Duration, Instant};
use anyhow::{Context, Result};
use clap::{CommandFactory, FromArgMatches, Parser};
use futures_util::stream::{FuturesUnordered, StreamExt};
use crate::Fixture;
use crate::driver::Engine;
use crate::sessions::{self, Outcome};
pub type TestFuture<'a> = Pin<Box<dyn Future<Output = Result<()>> + 'a>>;
pub type Start<F> = for<'a> fn(&'a mut crate::TestDriver, &'a mut F) -> TestFuture<'a>;
static HEADED: OnceLock<bool> = OnceLock::new();
static ENGINE: OnceLock<Engine> = OnceLock::new();
pub(crate) fn headed() -> bool {
match HEADED.get() {
Some(&headed) => headed,
None => headed_from(false, std::env::var_os("E2E_HEADED").as_deref()),
}
}
fn headed_from(flag: bool, env: Option<&std::ffi::OsStr>) -> bool {
flag || env.is_some_and(|value| value != "0" && value != "false")
}
pub fn engine() -> Engine {
match ENGINE.get() {
Some(&engine) => engine,
None => Engine::detect().unwrap_or(Engine::Firefox),
}
}
fn choose(named: Option<Engine>) -> Result<Engine> {
let named = match named {
Some(engine) => Some(engine),
None => match std::env::var("E2E_BROWSER") {
Ok(value) => Some(value.parse::<Engine>()?),
Err(_) => None,
},
};
let Some(engine) = named else {
return Engine::require();
};
if !engine.available() && !engine.browser_present() {
anyhow::bail!(
"--browser {engine} needs {driver}, and neither it nor a {engine} to \
fetch one for is on this machine.",
driver = engine.driver()
);
}
Ok(engine)
}
pub struct Suite<F: Fixture> {
tests: Vec<Test<F>>,
config: crate::Config,
}
struct Test<F: Fixture> {
name: String,
start: Start<F>,
setup: F::Setup,
}
impl<F: Fixture> Default for Suite<F> {
#[inline]
fn default() -> Self {
Self::new()
}
}
impl<F: Fixture> Suite<F> {
#[inline]
pub fn new() -> Self {
Self {
tests: Vec::new(),
config: F::config(),
}
}
pub fn add(&mut self, name: impl Into<String>, start: Start<F>, setup: F::Setup) {
self.tests.push(Test {
name: name.into(),
start,
setup,
});
}
pub fn run(self) -> ! {
let parsed = Options::command()
.name(program().leak() as &str)
.about(self.config.about.clone())
.long_about(format!(
"{}\n\n\
Also read: E2E_HEADED, which --headed overrides, E2E_BROWSER, which \
--browser overrides, E2E_DIST to serve a frontend that is already built \
rather than building one, E2E_THEME, E2E_MOTION and E2E_SCALE for what \
the browser reports, E2E_SHOTS for where snapshots go, and the usual \
RUST_LOG.",
self.config.about
))
.get_matches();
let options = match Options::from_arg_matches(&parsed) {
Ok(options) => options,
Err(error) => error.exit(),
};
let code = match self.try_run(options) {
Ok(true) => 0,
Ok(false) => 1,
Err(error) => {
eprintln!("error: {error:?}");
2
}
};
std::process::exit(code)
}
fn try_run(self, options: Options) -> Result<bool> {
let _run_lock = if options.list {
None
} else {
let wait = match options.lock_wait {
0 => None,
seconds => Some(Duration::from_secs(u64::from(seconds))),
};
let directory = crate::run_lock::directory(options.lock)?;
crate::run_lock::acquire(directory.as_deref(), &self.config.lock, wait)?
};
sessions::name_dir(&self.config.sessions);
let picked = match (options.last_session, options.session.as_deref()) {
(true, _) => Some(sessions::latest()?),
(false, Some(name)) => Some(sessions::load(name)?),
(false, None) => None,
};
let mut chosen = self
.tests
.into_iter()
.filter(|test| options.wants(&test.name))
.collect::<Vec<_>>();
let mut prose = commentary(options.list);
if let Some(session) = &picked {
let left = session.unfinished();
let (passed, failed, missed) = session.counts();
chosen.retain(|test| left.contains(&test.name.as_str()));
_ = writeln!(
prose,
"picking up {}: {passed} passed, {failed} failed, {missed} not run",
session.name
);
for (name, message, seconds) in session.failures() {
if !chosen.iter().any(|test| test.name == name) {
continue;
}
match seconds {
Some(seconds) => {
_ = writeln!(prose, "\n---- {name} failed there after {seconds:.1}s ----");
}
None => _ = writeln!(prose, "\n---- {name} failed there ----"),
}
for line in message.lines() {
_ = writeln!(prose, " {line}");
}
_ = writeln!(prose);
}
if chosen.is_empty() {
_ = writeln!(prose, "nothing left to run in {}", session.name);
}
}
if options.list {
for test in &chosen {
println!("{}: test", test.name);
}
return Ok(true);
}
if chosen.is_empty() {
anyhow::ensure!(
options.allow_empty,
"no tests selected by the filter — this is not a pass; the executable may be stale"
);
return Ok(true);
}
let engine = choose(options.browser)?;
_ = HEADED.set(headed_from(
options.headed,
std::env::var_os("E2E_HEADED").as_deref(),
));
_ = ENGINE.set(engine);
F::init_tracing();
let driver = engine.provision()?;
tracing::info!("Driving {engine} with {}", driver.display());
let runtime = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()?;
let frontend = self.config.frontend;
runtime.block_on(async move {
crate::dist::build(&frontend).await?;
drive(chosen, &options).await
})
}
}
async fn drive<F: Fixture>(tests: Vec<Test<F>>, options: &Options) -> Result<bool> {
let started = Instant::now();
let total = tests.len();
let session = sessions::Session::new(tests.iter().map(|test| test.name.clone()));
println!(
"running {total} test{} as {}",
if total == 1 { "" } else { "s" },
session.name
);
let queue = Rc::new(RefCell::new(VecDeque::from(tests)));
let report = Rc::new(RefCell::new(Report::new(session)));
let cancel = Rc::new(Cancel::default());
let grace = Duration::from_secs(u64::from(options.grace));
let limit = match options.timeout {
0 => None,
seconds => Some(Duration::from_secs(u64::from(seconds))),
};
let mut workers = (0..usize::from(options.parallel).min(total.max(1)))
.map(|_| worker(queue.clone(), report.clone(), cancel.clone(), grace, limit))
.collect::<FuturesUnordered<_>>();
let mut interrupt = pin!(interrupted()?);
let mut gave_up = None;
loop {
tokio::select! {
result = workers.next() => match result {
Some(Ok(())) => {}
Some(Err(error)) => {
eprintln!("a worker gave up: {error:?}");
gave_up.get_or_insert(error);
}
None => break,
},
result = &mut interrupt, if !cancel.stopped() => {
result?;
println!(
"\ninterrupted: finishing what is running, then saving the session \
(giving it {}s)",
options.grace
);
cancel.stop();
}
}
}
let Report {
passed,
failures,
warnings,
mut session,
} = Rc::try_unwrap(report)
.map_err(|_| anyhow::anyhow!("a worker outlived the run"))?
.into_inner();
session.whole = !cancel.stopped();
let elapsed = started.elapsed();
if !failures.is_empty() {
println!("\nfailures:\n");
for (name, error) in &failures {
let mut report = String::new();
_ = write!(report, "{error:?}");
println!("---- {name} ----");
for line in report.lines() {
println!(" {line}");
}
println!();
}
println!("failures:");
for (name, _) in &failures {
println!(" {name}");
}
}
if !warnings.is_empty() {
println!("\nwarnings:");
for warning in &warnings {
println!(" {warning:#}");
}
}
let ok = failures.is_empty() && session.whole && gave_up.is_none();
let (_, _, missed) = session.counts();
println!(
"\ntest result: {}. {passed} passed; {} failed; {missed} not run; finished in {:.2}s",
if ok { "ok" } else { "FAILED" },
failures.len(),
elapsed.as_secs_f64()
);
let slowest = session.slowest(SLOWEST);
if !slowest.is_empty() {
let said = slowest
.iter()
.map(|(name, seconds)| format!("{name} {seconds:.1}s"))
.collect::<Vec<_>>()
.join(", ");
println!("slowest: {said}");
}
match session.save() {
Ok(path) => println!("session {} written to {}", session.name, path.display()),
Err(error) => eprintln!("the session could not be written: {error:?}"),
}
if !ok {
let package = std::env::var("CARGO_PKG_NAME").ok();
let exe = std::env::current_exe()
.ok()
.or_else(|| std::env::args_os().next().map(std::path::PathBuf::from));
println!(
"run the rest with: {}",
rerun_hint(package.as_deref(), exe.as_deref())
);
}
if let Some(error) = gave_up {
return Err(error);
}
Ok(ok)
}
#[cfg(unix)]
fn interrupted() -> Result<impl Future<Output = Result<()>>> {
use tokio::signal::unix::{SignalKind, signal};
let mut term = signal(SignalKind::terminate()).context("installing a SIGTERM handler")?;
let mut interrupt = signal(SignalKind::interrupt()).context("installing a SIGINT handler")?;
Ok(async move {
tokio::select! {
_ = interrupt.recv() => Ok(()),
_ = term.recv() => Ok(()),
}
})
}
#[cfg(not(unix))]
fn interrupted() -> Result<impl Future<Output = Result<()>>> {
Ok(async { tokio::signal::ctrl_c().await.context("waiting for Ctrl-C") })
}
#[derive(Default)]
struct Cancel {
stopped: Cell<bool>,
woken: tokio::sync::Notify,
}
impl Cancel {
fn stopped(&self) -> bool {
self.stopped.get()
}
fn stop(&self) {
self.stopped.set(true);
self.woken.notify_waiters();
}
async fn wait(&self) {
if self.stopped.get() {
return;
}
self.woken.notified().await;
}
}
struct Report {
passed: usize,
failures: Vec<(String, anyhow::Error)>,
warnings: Vec<anyhow::Error>,
session: sessions::Session,
}
impl Report {
fn new(session: sessions::Session) -> Self {
Self {
passed: 0,
failures: Vec::new(),
warnings: Vec::new(),
session,
}
}
fn record(&mut self, name: String, result: Result<()>, took: Option<Duration>) {
let seconds = took.map(|took| took.as_secs_f64());
match result {
Ok(()) => {
self.passed += 1;
self.session.record(&name, Outcome::Passed, None, seconds);
println!("test {name} ... ok");
}
Err(error) => {
self.session
.record(&name, Outcome::Failed, Some(format!("{error:?}")), seconds);
println!("test {name} ... FAILED");
self.failures.push((name, error));
}
}
}
fn warn(&mut self, warning: anyhow::Error) {
println!("warning: {warning:#}");
self.warnings.push(warning);
}
}
fn settle(report: &RefCell<Report>, closing: Result<Option<anyhow::Error>>) -> Result<()> {
if let Some(slow) = closing? {
report.borrow_mut().warn(slow);
}
Ok(())
}
async fn retire(report: &RefCell<Report>, browser: impl Seat, after: &str) {
if let Err(error) = settle(report, browser.close().await) {
report
.borrow_mut()
.warn(error.context(format!("closing the browser {after}")));
}
}
pub(crate) trait Seat: Sized {
fn waiting(&self) -> crate::Waiting;
async fn run<F: Fixture>(
&mut self,
start: Start<F>,
setup: F::Setup,
limit: Option<Duration>,
) -> Result<()>;
async fn close(self) -> Result<Option<anyhow::Error>>;
}
impl Seat for crate::Browser {
fn waiting(&self) -> crate::Waiting {
self.driver.waiting()
}
async fn run<F: Fixture>(
&mut self,
start: Start<F>,
setup: F::Setup,
limit: Option<Duration>,
) -> Result<()> {
crate::run_one(self, start, setup, limit).await
}
async fn close(self) -> Result<Option<anyhow::Error>> {
crate::Browser::close(self).await
}
}
async fn worker<F: Fixture>(
queue: Rc<RefCell<VecDeque<Test<F>>>>,
report: Rc<RefCell<Report>>,
cancel: Rc<Cancel>,
grace: Duration,
limit: Option<Duration>,
) -> Result<()> {
worker_with(queue, report, cancel, grace, limit, crate::Browser::open).await
}
async fn worker_with<F: Fixture, B: Seat, O: std::future::Future<Output = Result<B>>>(
queue: Rc<RefCell<VecDeque<Test<F>>>>,
report: Rc<RefCell<Report>>,
cancel: Rc<Cancel>,
grace: Duration,
limit: Option<Duration>,
mut open_browser: impl FnMut() -> O,
) -> Result<()> {
let mut browser = None;
let mut used = 0usize;
while !cancel.stopped() {
let Some(Test { name, start, setup }) = queue.borrow_mut().pop_front() else {
break;
};
let mut open = match browser.take() {
Some(browser) => browser,
None => {
used = 0;
let opening = open_browser();
let stopped = async {
cancel.wait().await;
tokio::time::sleep(grace).await;
};
match tokio::select! {
result = opening => Some(result),
() = stopped => None,
} {
Some(Ok(browser)) => browser,
Some(Err(error)) => {
report.borrow_mut().record(name, Err(error), None);
continue;
}
None => {
println!("test {name} ... cut short during browser startup");
break;
}
}
}
};
let overrun = async {
cancel.wait().await;
tokio::time::sleep(grace).await;
};
let waiting = open.waiting();
let began = Instant::now();
let running = async {
let running = open.run(start, setup, limit);
let Some(limit) = limit else {
return running.await;
};
match tokio::time::timeout(limit + CLEANUP_MARGIN, running).await {
Ok(result) => result,
Err(_) => Err(anyhow::anyhow!(
"{}, and did not stop when it was told to",
crate::overran(Some(limit), &waiting)
)),
}
};
let finished = tokio::select! {
result = running => Some(result),
() = overrun => None,
};
let Some(mut result) = finished else {
println!("test {name} ... cut short");
retire(&report, open, "after an interrupted test").await;
break;
};
used += 1;
if result.is_err() {
if let Err(error) = settle(&report, open.close().await) {
tracing::warn!(?error, "closing the browser after a failing test");
result =
result.map_err(|original| crate::driver::with_cleanup(original, Err(error)));
}
} else if keeps_browser(used) {
browser = Some(open);
} else {
retire(&report, open, "that had run its course").await;
}
report
.borrow_mut()
.record(name, result, Some(began.elapsed()));
}
if let Some(browser) = browser {
retire(&report, browser, "after its last test").await;
}
Ok(())
}
#[cfg(test)]
struct Nothing;
#[cfg(test)]
impl Fixture for Nothing {
type Setup = ();
async fn start((): ()) -> Result<Self> {
anyhow::bail!("the runner's own tests start no fixture")
}
fn url(&self) -> String {
String::new()
}
async fn quit(self) -> Result<()> {
Ok(())
}
}
#[cfg(test)]
mod lifecycle_tests {
use super::*;
#[test]
fn a_browser_is_replaced_once_it_has_run_its_course() {
assert!(keeps_browser(1));
assert!(keeps_browser(BROWSER_LIFE - 1));
assert!(!keeps_browser(BROWSER_LIFE));
assert!(!keeps_browser(BROWSER_LIFE + 1));
}
fn queue() -> Rc<RefCell<VecDeque<Test<Nothing>>>> {
Rc::new(RefCell::new(
["first", "second"]
.into_iter()
.map(|name| Test {
name: name.to_owned(),
start: |_, _| Box::pin(async { panic!("a failed browser never runs a test") }),
setup: (),
})
.collect(),
))
}
struct Stand {
close: fn() -> Result<Option<anyhow::Error>>,
closed: Rc<Cell<usize>>,
}
impl Seat for Stand {
fn waiting(&self) -> crate::Waiting {
crate::Waiting::default()
}
async fn run<F: Fixture>(
&mut self,
_: Start<F>,
_: F::Setup,
_: Option<Duration>,
) -> Result<()> {
Ok(())
}
async fn close(self) -> Result<Option<anyhow::Error>> {
self.closed.set(self.closed.get() + 1);
(self.close)()
}
}
fn report() -> Rc<RefCell<Report>> {
Rc::new(RefCell::new(Report::new(sessions::Session::new([
"first".to_owned(),
"second".to_owned(),
]))))
}
#[tokio::test]
async fn startup_failures_are_recorded_and_the_queue_continues() {
let queue = queue();
let report = report();
let attempts = Cell::new(0);
worker_with(
queue.clone(),
report.clone(),
Rc::new(Cancel::default()),
Duration::ZERO,
None,
|| {
attempts.set(attempts.get() + 1);
async { Err::<Stand, _>(anyhow::anyhow!("startup-original-marker")) }
},
)
.await
.unwrap();
assert_eq!(attempts.get(), 2);
assert!(queue.borrow().is_empty());
let report = report.borrow();
assert_eq!(report.session.counts(), (0, 2, 0));
assert_eq!(report.failures[0].0, "first");
assert_eq!(report.failures[1].0, "second");
assert!(
report
.failures
.iter()
.all(|(_, error)| error.to_string().contains("startup-original-marker"))
);
}
#[tokio::test]
async fn a_failed_close_after_the_last_passing_test_is_a_warning() {
let queue = queue();
let report = report();
let closed = Rc::new(Cell::new(0));
worker_with(
queue.clone(),
report.clone(),
Rc::new(Cancel::default()),
Duration::ZERO,
None,
|| {
let closed = closed.clone();
async move {
Ok(Stand {
close: || Err(anyhow::anyhow!("invalid session id: close-marker")),
closed,
})
}
},
)
.await
.unwrap();
assert_eq!(closed.get(), 1);
assert!(queue.borrow().is_empty());
let report = report.borrow();
assert_eq!(report.session.counts(), (2, 0, 0));
assert_eq!(report.passed, 2);
assert!(report.failures.is_empty());
assert_eq!(report.warnings.len(), 1);
let warning = format!("{:#}", report.warnings[0]);
assert!(warning.contains("after its last test"), "{warning}");
assert!(warning.contains("close-marker"), "{warning}");
}
#[test]
fn a_slow_quit_after_passing_tests_is_a_warning() {
let report = report();
report.borrow_mut().record("first".to_owned(), Ok(()), None);
let slow = anyhow::Error::from(crate::driver::TimedOut("quit".to_owned()))
.context("browser quit: geckodriver");
settle(&report, Ok(Some(slow))).unwrap();
let report = report.borrow();
assert!(report.failures.is_empty());
assert_eq!(report.passed, 1);
assert_eq!(report.warnings.len(), 1);
assert!(format!("{:#}", report.warnings[0]).contains("quit timed out"));
}
#[test]
fn a_slow_quit_does_not_excuse_a_failing_test_or_a_failed_cleanup() {
let report = report();
report.borrow_mut().record(
"first".to_owned(),
Err(anyhow::anyhow!("test-marker")),
None,
);
let slow = anyhow::Error::from(crate::driver::TimedOut("quit".to_owned()));
settle(&report, Ok(Some(slow))).unwrap();
assert_eq!(report.borrow().failures.len(), 1);
assert_eq!(report.borrow().session.counts(), (0, 1, 1));
let error = settle(&report, Err(anyhow::anyhow!("reap-marker"))).unwrap_err();
assert!(error.to_string().contains("reap-marker"));
assert_eq!(report.borrow().warnings.len(), 1);
}
#[tokio::test(start_paused = true)]
async fn cancellation_drops_pending_startup_without_failing_unrun_tests() {
struct Dropped(Rc<Cell<bool>>);
impl Drop for Dropped {
fn drop(&mut self) {
self.0.set(true);
}
}
let queue = queue();
let report = report();
let cancel = Rc::new(Cancel::default());
let dropped = Rc::new(Cell::new(false));
worker_with(
queue.clone(),
report.clone(),
cancel.clone(),
Duration::from_secs(1),
None,
|| {
let guard = Dropped(dropped.clone());
cancel.stop();
async move {
let _guard = guard;
std::future::pending::<Result<Stand>>().await
}
},
)
.await
.unwrap();
assert!(dropped.get());
assert_eq!(queue.borrow().len(), 1);
assert_eq!(report.borrow().session.counts(), (0, 0, 2));
assert!(report.borrow().failures.is_empty());
}
}
#[cfg(all(test, unix))]
mod signal_tests {
use std::io::{BufRead, Write as _};
use std::process::{Command, Stdio};
use std::sync::mpsc;
use std::time::{Duration, Instant};
use tokio::io::AsyncReadExt;
const CHILD_ENV: &str = "E2E_INTERRUPTED_SIGNAL_CHILD";
const READY: &str = "armed";
const CAUGHT: &str = "interrupted";
const WAIT: Duration = Duration::from_secs(10);
#[test]
fn a_sigterm_resolves_interrupted_before_its_future_is_polled() {
catches(libc::SIGTERM);
}
#[test]
fn a_sigint_resolves_interrupted_before_its_future_is_polled() {
catches(libc::SIGINT);
}
fn catches(signal: i32) {
if std::env::var_os(CHILD_ENV).is_some() {
child();
}
let exe = std::env::current_exe().expect("this test's own binary");
let mut child = Command::new(exe)
.arg(match signal {
libc::SIGTERM => {
"harness::signal_tests::a_sigterm_resolves_interrupted_before_its_future_is_polled"
}
libc::SIGINT => {
"harness::signal_tests::a_sigint_resolves_interrupted_before_its_future_is_polled"
}
_ => unreachable!("only SIGTERM and SIGINT are tested"),
})
.arg("--exact")
.arg("--nocapture")
.env(CHILD_ENV, "1")
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.spawn()
.expect("spawning the child copy of this test binary");
struct KillOnDrop(std::process::Child);
impl Drop for KillOnDrop {
fn drop(&mut self) {
let _ = self.0.kill();
let _ = self.0.wait();
}
}
let stdout = child.stdout.take().expect("the child's stdout");
let mut stdin = child.stdin.take().expect("the child's stdin");
let mut child = KillOnDrop(child);
let (lines_tx, lines_rx) = mpsc::channel();
std::thread::spawn(move || {
for line in std::io::BufReader::new(stdout).lines() {
let Ok(line) = line else { break };
if lines_tx.send(line).is_err() {
break;
}
}
});
let recv = |what: &str| {
let deadline = Instant::now() + WAIT;
loop {
let left = deadline.saturating_duration_since(Instant::now());
match lines_rx.recv_timeout(left) {
Ok(line) if line == what => return,
Ok(_) => continue,
Err(_) => panic!("the child never said {what:?} within {WAIT:?}"),
}
}
};
recv(READY);
let pid = child.0.id() as i32;
assert_eq!(
unsafe { libc::kill(pid, signal) },
0,
"sending signal {signal} to the child"
);
stdin
.write_all(b"continue\n")
.expect("letting the child poll its interrupt future");
stdin.flush().expect("flushing the child's acknowledgement");
recv(CAUGHT);
let status = child.0.wait().expect("waiting for the child to exit");
assert!(status.success(), "child exited as {status:?}, not cleanly");
}
fn child() -> ! {
let runtime = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.expect("building the child's runtime");
runtime.block_on(async {
let interrupt =
super::interrupted().expect("installing handlers before announcing readiness");
println!("{READY}");
std::io::stdout().flush().expect("flushing armed");
let mut acknowledgement = [0];
tokio::io::stdin()
.read_exact(&mut acknowledgement)
.await
.expect("waiting for the parent acknowledgement");
interrupt
.await
.expect("interrupted() failed instead of catching the signal");
println!("{CAUGHT}");
std::io::stdout().flush().expect("flushing interrupted");
});
std::process::exit(0)
}
}
const DEFAULT_PARALLEL: u16 = 2;
const DEFAULT_GRACE: u16 = 30;
const SLOWEST: usize = 3;
const DEFAULT_TIMEOUT: u16 = 30;
const CLEANUP_MARGIN: Duration = Duration::from_secs(30);
const DEFAULT_LOCK_WAIT: u16 = 300;
fn commentary(listing: bool) -> Box<dyn std::io::Write> {
if listing {
Box::new(std::io::stderr())
} else {
Box::new(std::io::stdout())
}
}
const BROWSER_LIFE: usize = 20;
fn keeps_browser(used: usize) -> bool {
used < BROWSER_LIFE
}
fn program() -> String {
if let Ok(package) = std::env::var("CARGO_PKG_NAME") {
return package;
}
std::env::args()
.next()
.as_deref()
.map(std::path::Path::new)
.and_then(|path| path.file_name())
.map(|name| name.to_string_lossy().into_owned())
.unwrap_or_else(|| String::from("e2e"))
}
fn rerun_hint(package: Option<&str>, exe: Option<&std::path::Path>) -> String {
const FLAG: &str = "--last-session";
let Some(exe) = exe else {
return format!("{} {FLAG}", program());
};
let kind = exe
.parent()
.and_then(|dir| dir.file_name())
.and_then(|dir| dir.to_str());
let target = exe
.file_stem()
.and_then(|stem| stem.to_str())
.map(strip_hash)
.filter(|name| !name.is_empty());
match (package, kind, target) {
(Some(package), Some("deps"), Some(target)) => {
format!("cargo test -p {package} --test {target} -- {FLAG}")
}
(Some(package), Some("examples"), Some(target)) => {
format!("cargo run -p {package} --example {target} -- {FLAG}")
}
_ => format!("{} {FLAG}", exe.display()),
}
}
fn strip_hash(name: &str) -> &str {
match name.rsplit_once('-') {
Some((base, hash))
if !base.is_empty()
&& hash.len() == 16
&& hash.bytes().all(|b| matches!(b, b'0'..=b'9' | b'a'..=b'f')) =>
{
base
}
_ => name,
}
}
#[cfg(test)]
mod rerun_hint_tests {
use std::path::Path;
use super::{rerun_hint, strip_hash};
#[test]
fn strips_only_a_cargo_hash() {
assert_eq!(strip_hash("api-5d4aace97df90d4a"), "api");
assert_eq!(strip_hash("my-suite-5d4aace97df90d4a"), "my-suite");
assert_eq!(strip_hash("static_page"), "static_page");
assert_eq!(strip_hash("my-suite"), "my-suite");
assert_eq!(strip_hash("api-5D4AACE97DF90D4A"), "api-5D4AACE97DF90D4A");
assert_eq!(strip_hash("-5d4aace97df90d4a"), "-5d4aace97df90d4a");
}
#[test]
fn an_integration_test_is_named_with_test() {
let exe = Path::new("/w/target/debug/deps/api-5d4aace97df90d4a");
assert_eq!(
rerun_hint(Some("yew-e2e"), Some(exe)),
"cargo test -p yew-e2e --test api -- --last-session"
);
}
#[test]
fn an_example_is_run_with_cargo_run() {
for exe in [
"/w/target/debug/examples/static_page",
"/w/target/debug/examples/static_page-9cde5bb8f39ff32d",
] {
assert_eq!(
rerun_hint(Some("yew-e2e"), Some(Path::new(exe))),
"cargo run -p yew-e2e --example static_page -- --last-session"
);
}
}
#[test]
fn an_unknown_target_reruns_the_executable() {
let exe = Path::new("/w/target/debug/app-e2e");
assert_eq!(
rerun_hint(Some("app"), Some(exe)),
"/w/target/debug/app-e2e --last-session"
);
let exe = Path::new("/w/target/debug/deps/api-5d4aace97df90d4a");
assert_eq!(
rerun_hint(None, Some(exe)),
"/w/target/debug/deps/api-5d4aace97df90d4a --last-session"
);
}
}
#[derive(Parser)]
#[command(name = "e2e", disable_version_flag = true)]
struct Options {
#[arg(value_name = "FILTER")]
filters: Vec<String>,
#[arg(
short = 'j',
long,
alias = "test-threads",
default_value_t = DEFAULT_PARALLEL,
value_parser = clap::value_parser!(u16).range(1..),
)]
parallel: u16,
#[arg(long)]
headed: bool,
#[arg(long, value_name = "BROWSER")]
browser: Option<Engine>,
#[arg(long)]
exact: bool,
#[arg(long)]
list: bool,
#[arg(long)]
allow_empty: bool,
#[arg(long, conflicts_with = "session")]
last_session: bool,
#[arg(long, value_name = "NAME")]
session: Option<String>,
#[arg(long, value_name = "SECONDS", default_value_t = DEFAULT_GRACE)]
grace: u16,
#[arg(long, value_name = "SECONDS", default_value_t = DEFAULT_TIMEOUT)]
timeout: u16,
#[arg(long, value_name = "SECONDS", default_value_t = DEFAULT_LOCK_WAIT)]
lock_wait: u16,
#[arg(long, value_name = "SCOPE", default_value = "target")]
lock: crate::run_lock::Scope,
#[arg(long, hide = true)]
nocapture: bool,
#[arg(long, hide = true)]
show_output: bool,
#[arg(short, long, hide = true)]
quiet: bool,
#[arg(long, hide = true, value_name = "FORMAT")]
format: Option<String>,
}
impl Options {
fn wants(&self, name: &str) -> bool {
if self.filters.is_empty() {
return true;
}
self.filters.iter().any(|filter| {
if self.exact {
name == filter
} else {
name.contains(filter)
}
})
}
}
#[cfg(test)]
mod selection_tests {
use super::*;
#[test]
fn empty_selection_requires_opt_in_except_when_listing() {
for args in [
vec!["e2e", "missing"],
vec!["e2e", "present", "--exact"],
vec!["e2e", "missing", "--allow-empty"],
vec!["e2e", "missing", "--list"],
vec!["e2e", "present", "--list"],
] {
let options = Options::parse_from(&args);
let allowed = options.allow_empty || options.list;
let mut suite = Suite::<Nothing>::new();
suite.add(
"module::present",
|_, _| Box::pin(async { panic!("selection checks must not run a browser test") }),
(),
);
let result = suite.try_run(options);
if allowed {
assert!(result.unwrap(), "{args:?}");
} else {
assert_eq!(
result.unwrap_err().to_string(),
"no tests selected by the filter — this is not a pass; the executable may be stale",
"{args:?}"
);
}
}
}
}
#[macro_export]
macro_rules! harness {
(
$fixture:ty;
$($module:ident :: {
$($test:ident $(($($setup:ident),* $(,)?))?),* $(,)?
}),* $(,)?
) => {
fn main() -> ! {
let mut suite = $crate::harness::Suite::<$fixture>::new();
$(
$(
suite.add(
concat!(stringify!($module), "::", stringify!($test)),
{
fn start<'a>(
driver: &'a mut $crate::TestDriver,
fixture: &'a mut $fixture,
) -> $crate::harness::TestFuture<'a> {
Box::pin($module::$test(driver, fixture))
}
start
},
{
#[allow(unused_mut)]
let mut setup = <<$fixture as $crate::Fixture>::Setup as ::core::default::Default>::default();
$($(setup.$setup = true;)*)?
setup
},
);
)*
)*
suite.run()
}
};
}
#[cfg(test)]
mod tests {
use std::ffi::OsStr;
use super::headed_from;
#[test]
fn the_headed_variable_applies_without_the_flag() {
assert!(!headed_from(false, None));
assert!(headed_from(true, None));
for on in ["1", "true", "yes"] {
assert!(headed_from(false, Some(OsStr::new(on))));
}
for off in ["0", "false"] {
assert!(!headed_from(false, Some(OsStr::new(off))));
assert!(headed_from(true, Some(OsStr::new(off))));
}
}
}