Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ rexpect = "0.7"
anyhow = "1.0.102"
clap = {version = "4.6", features = ["derive"]}
crossterm = "0.29.0"
regex = "1.10"
similar = "2.4.0"
22 changes: 20 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use serde::Deserialize;
use serde::{Deserialize, Deserializer};

#[derive(Debug, Deserialize)]
pub struct TestConfig {
Expand All @@ -18,7 +18,25 @@ pub struct ShellInteractions {
#[derive(Debug, Deserialize)]
pub struct CommandTest {
pub command: String,
pub expect: String,
#[serde(deserialize_with = "deserialize_expect")]
pub expect: Vec<String>,
}

fn deserialize_expect<'de, D>(deserializer: D) -> Result<Vec<String>, D::Error>
where
D: Deserializer<'de>,
{
#[derive(Deserialize)]
#[serde(untagged)]
enum StringOrVec {
String(String),
Vec(Vec<String>),
}

match StringOrVec::deserialize(deserializer)? {
StringOrVec::String(s) => Ok(vec![s]),
StringOrVec::Vec(v) => Ok(v),
}
}

#[cfg(test)]
Expand Down
62 changes: 56 additions & 6 deletions src/engine.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use regex::Regex;
use rexpect::spawn;
use similar::{ChangeTag, TextDiff};

use crate::config::TestConfig;
use crate::traits::{Reporter, Runner};
use crate::{RED, RESET};

pub struct QemuRunner;

Expand Down Expand Up @@ -33,12 +36,27 @@ impl Runner for QemuRunner {
anyhow::bail!(e.to_string());
}

if let Err(e) = p.exp_string(&test.expect) {
reporter.on_step_failure(
&test.command,
&format!("Expected '{}' not found", test.expect),
);
anyhow::bail!(e.to_string());
for expect in &test.expect {
if let Err(e) = p.exp_string(expect) {
let fail_msg = match &e {
rexpect::error::Error::Timeout {
expected: exp, got, ..
} => format_diff(exp, got),
rexpect::error::Error::EOF {
expected: exp, got, ..
} => {
format!(
"EOF (OS Crashed)!\n {RED}│{RESET} Expected: '{}'\n {RED}│{RESET} Got: '{}'",
exp,
clean_output(got)
)
}
_ => e.to_string(),
};

reporter.on_step_failure(&test.command, &fail_msg);
anyhow::bail!(e.to_string());
}
}

if let Err(e) = p.exp_string(&config.shell_interactions.prompt) {
Expand All @@ -58,3 +76,35 @@ impl Runner for QemuRunner {
Ok(())
}
}

fn clean_output(raw: &str) -> String {
let mut msg = raw.replace("\r\n", " ↵ ").replace('\n', " ↵ ");

if let Ok(re) = Regex::new(r"\x1b\[[0-9;]*[mK]|\\u\{1b\}\[[0-9;]*[mK]") {
msg = re.replace_all(&msg, "").to_string();
}
msg
}

fn format_diff(expected: &str, got: &str) -> String {
let clean_got = clean_output(got);

let diff = TextDiff::from_chars(expected, &clean_got);
let mut colored_got = String::new();

for change in diff.iter_all_changes() {
match change.tag() {
ChangeTag::Delete => {}
ChangeTag::Insert => {
colored_got.push_str(&format!("\x1b[31m{}\x1b[0m", change.value()));
}
ChangeTag::Equal => {
colored_got.push_str(&format!("\x1b[32m{}\x1b[0m", change.value()));
}
}
}
format!(
"Timeout!\n {RED}│{RESET} Expected: '{}'\n {RED}│{RESET} Got: '{}'",
expected, colored_got
)
}
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@ pub mod config;
pub mod engine;
pub mod reporter;
pub mod traits;

const GREEN: &str = "\x1b[32m";
const RED: &str = "\x1b[31m";
const RESET: &str = "\x1b[0m";
5 changes: 1 addition & 4 deletions src/reporter/console.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
use crate::config::TestConfig;
use crate::traits::Reporter;
use crate::{GREEN, RED, RESET};

pub struct ConsoleReporter;

const GREEN: &str = "\x1b[32m";
const RED: &str = "\x1b[31m";
const RESET: &str = "\x1b[0m";

impl Reporter for ConsoleReporter {
fn on_test_start(&self, config: &TestConfig) {
println!("\n▶ 📼 WALKMAN PLAYING: {}", config.name);
Expand Down
9 changes: 4 additions & 5 deletions src/reporter/tui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crossterm::{
terminal::{Clear, ClearType},
};

use crate::{config::TestConfig, traits::Reporter};
use crate::{RED, RESET, config::TestConfig, traits::Reporter};

#[derive(Debug, PartialEq)]
enum Status {
Expand Down Expand Up @@ -151,7 +151,7 @@ impl TuiReporter {
let _ = execute!(
out,
SetForegroundColor(Color::DarkGrey),
Print(" │ ⌨️ Sending payload...\n"),
Print(" │ 📦 Sending payload...\n"),
Print(" │ ⏳ Waiting for OS response...\n"),
ResetColor
);
Expand All @@ -167,8 +167,7 @@ impl TuiReporter {
);
let _ = execute!(
out,
SetForegroundColor(Color::Red),
Print(format!(" │ Reason: {}\n", reason)),
Print(format!(" {RED}↳ │{RESET} Reason: {}\n", reason)),
ResetColor
);
lines += 1;
Expand Down Expand Up @@ -273,7 +272,7 @@ mod tests {
prompt: "$".to_string(),
tests: vec![CommandTest {
command: "ls".to_string(),
expect: "file".to_string(),
expect: vec!["file".to_string()],
}],
},
}
Expand Down
Loading