it: Add runner and common tests

Still needs some work, but the idea should be clear.

Signed-off-by: John Nunley <dev@notgull.net>
This commit is contained in:
John Nunley
2024-03-03 21:54:23 -08:00
parent b3333b47e1
commit 25fab64f6e
8 changed files with 121 additions and 12 deletions

View File

@@ -1,7 +1,7 @@
//! A testing framework that can be run remotely.
pub mod stream;
pub mod remote;
pub mod stream;
pub mod user;
use serde::{Deserialize, Serialize};
@@ -23,7 +23,7 @@ pub use inventory as __inventory;
macro_rules! main {
($handler:expr) => {
fn main() {
$crate::__entry($harness)
$crate::__entry($handler)
}
};
}
@@ -62,7 +62,7 @@ pub struct __TestStart {
impl __TestStart {
/// Create a new test start.
#[doc(hidden)]
pub fn __new(name: &'static str, func: fn(&mut Harness)) -> Self {
pub const fn __new(name: &'static str, func: fn(&mut Harness)) -> Self {
Self { name, func }
}
}
@@ -290,6 +290,12 @@ pub trait TestHandler {
fn handle_test(&mut self, event: TestEvent);
}
impl<T: TestHandler + ?Sized> TestHandler for Box<T> {
fn handle_test(&mut self, event: TestEvent) {
(**self).handle_test(event)
}
}
/// An event produced by the test harness.
#[derive(Debug, Serialize, Deserialize)]
pub struct TestEvent {

View File

@@ -1,8 +1,8 @@
//! Create a test handler that can be run remotely.
use crate::TestHandler;
use crate::stream::WriteHandler;
use crate::user::UserHandler;
use crate::TestHandler;
use std::env;
use std::net::TcpStream;
@@ -11,16 +11,17 @@ use std::net::TcpStream;
pub fn handler() -> Box<dyn TestHandler + Send + 'static> {
// If GUI_TEST_UNIX_STREAM is enabled, use that as a Unix stream.
#[cfg(unix)]
if let Some(stream_path) = env::var_os("GUI_TEST_UNIX_STREAM")
.filter(|s| !s.is_empty()) {
let stream = std::os::unix::net::UnixStream::connect(stream_path).expect("unable to connect to gui-test handler");
if let Some(stream_path) = env::var_os("GUI_TEST_UNIX_STREAM").filter(|s| !s.is_empty()) {
let stream = std::os::unix::net::UnixStream::connect(stream_path)
.expect("unable to connect to gui-test handler");
return Box::new(WriteHandler::new(stream));
}
// If GUI_TEST_TCP_STREAM is enabled, use that as a TCP stream.
if let Some(tcp_ip) = env::var("GUI_TEST_TCP_STREAM")
.ok()
.filter(|s| !s.is_empty()) {
.filter(|s| !s.is_empty())
{
let stream = TcpStream::connect(tcp_ip).unwrap();
return Box::new(WriteHandler::new(stream));
}

View File

@@ -1,5 +1,5 @@
//! Write events to an output stream.
//!
//!
//! The format is as follows:
//! - First 8 bytes: big-endian length of payload.
//! - Next {len} bytes: JSON payload to deserialize from.
@@ -16,9 +16,7 @@ pub struct WriteHandler<W> {
impl<W: Write> WriteHandler<W> {
/// Create a new write handler.
pub fn new(writer: W) -> Self {
Self {
writer
}
Self { writer }
}
}