icedwright 0.0.1

The icedwright test harness: run an iced 0.14 program headlessly on virtual time
Documentation
//! The watchdog: an `update` that never returns cannot be interrupted on the test thread, so
//! the process aborts with a report. The hanging harness runs in a child process, the test
//! binary itself, with a one-second budget.

use std::process::{Command, Output};
use std::sync::mpsc;
use std::time::Duration;

use iced::widget::text;
use iced::{Element, Theme};
use icedwright::prelude::*;

/// Set in the child, which runs the harness that hangs.
const CHILD: &str = "ICEDWRIGHT_WATCHDOG_CHILD";

#[derive(Debug, Clone)]
enum Message {
    Block,
}

/// Holds a channel that nobody ever sends on.
struct Stuck {
    receiver: mpsc::Receiver<()>,
    _sender: mpsc::Sender<()>,
}

impl Stuck {
    fn new() -> Self {
        let (sender, receiver) = mpsc::channel();
        Self {
            receiver,
            _sender: sender,
        }
    }

    fn update(&mut self, Message::Block: Message) {
        let _ = self.receiver.recv();
    }

    fn view(&self) -> Element<'_, Message> {
        text("stuck").into()
    }
}

fn hang() -> icedwright::Result {
    let mut h = Harness::new(
        iced::application(Stuck::new, Stuck::update, Stuck::view).theme(Theme::Light),
    )?;
    h.dispatch(Message::Block)?;
    Err(icedwright::Error::other(std::io::Error::other(
        "update returned from a channel nobody sends on",
    )))
}

/// Runs the child and waits for it to exit, for at most 30 seconds.
fn run_child(test: &str) -> Option<Output> {
    let exe = std::env::current_exe().ok()?;
    let child = Command::new(exe)
        .args([test, "--exact", "--nocapture", "--test-threads=1"])
        .env(CHILD, "1")
        .env("ICEDWRIGHT_WATCHDOG", "1s")
        .env("ICEDWRIGHT_TRACE", "off")
        .stdout(std::process::Stdio::piped())
        .stderr(std::process::Stdio::piped())
        .spawn()
        .ok()?;
    let (sender, receiver) = mpsc::channel();
    std::thread::spawn(move || {
        let _ = sender.send(child.wait_with_output());
    });
    receiver.recv_timeout(Duration::from_secs(30)).ok()?.ok()
}

// milestone: Core
#[test]
fn an_update_that_never_returns_aborts_the_process_with_a_report() -> icedwright::Result {
    if std::env::var_os(CHILD).is_some() {
        return hang();
    }
    let output = run_child("an_update_that_never_returns_aborts_the_process_with_a_report")
        .ok_or_else(|| {
            icedwright::Error::other(std::io::Error::other("the child did not exit within 30 s"))
        })?;
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(!output.status.success(), "the child passed:\n{stderr}");
    #[cfg(unix)]
    {
        use std::os::unix::process::ExitStatusExt as _;
        assert_eq!(output.status.signal(), Some(6), "not an abort:\n{stderr}");
    }
    for part in [
        "harness aborted: app call did not return",
        "app call: update",
        "last message: Block",
        "tests/watchdog.rs:",
        "pending tasks:",
    ] {
        assert!(stderr.contains(part), "missing {part:?} in:\n{stderr}");
    }
    Ok(())
}