Skip to content
Closed
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
1 change: 1 addition & 0 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion examples/memory_management/expected_output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Checkpoint 21
PrintOnDrop(A) has been dropped
Checkpoint 22

thread '<unnamed>' panicked at examples/memory_management/src/lib.rs:30:9:
thread '<unnamed>' ({{re:\d+}}) panicked at examples/memory_management/src/lib.rs:30:9:
consume_and_panic executed with value B
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
PrintOnDrop(B) has been dropped
Expand Down
2 changes: 1 addition & 1 deletion examples/simple/expected_output.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
17
s[2] = 7

thread '<unnamed>' panicked at examples/simple/src/generated.rs:192:39:
thread '<unnamed>' ({{re:\d+}}) panicked at examples/simple/src/generated.rs:192:39:
called `Option::unwrap()` on a `None` value
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
s[4] = Rust panic happened
Expand Down
1 change: 1 addition & 0 deletions xtask/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ publish = false
xshell = "0.2.7"
anyhow = "1.0"
clap = { version = "4.3.12", features = ["derive"] }
regex = "1"
63 changes: 60 additions & 3 deletions xtask/src/ci.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use anyhow::{Context, Result};
use anyhow::{Context, Result, bail};
use regex::Regex;
use std::fs;
use xshell::{Shell, cmd};

fn check_crate(sh: &Shell) -> Result<()> {
Expand Down Expand Up @@ -34,8 +36,9 @@ fn check_examples(sh: &Shell, fix: bool) -> Result<()> {
if fix {
sh.copy_file("./actual_output.txt", "./expected_output.txt")?;
}
cmd!(sh, "diff actual_output.txt expected_output.txt")
.run()
let expected_path = format!("examples/{}/expected_output.txt", example);
let actual_path = format!("examples/{}/actual_output.txt", example);
compare_expected_with_actual(&expected_path, &actual_path)
.with_context(|| format!("Example `{example}` output differs from expected."))?;
cmd!(sh, "cargo fmt --check")
.run()
Expand All @@ -45,6 +48,60 @@ fn check_examples(sh: &Shell, fix: bool) -> Result<()> {
Ok(())
}

/// Compare `expected_path` to `actual_path` line-by-line.
/// Supports one extension in expected:
/// - Inline regex placeholders: `{{re:...}}` which are treated as raw regex
/// segments embedded within an otherwise literal line
fn compare_expected_with_actual(expected_path: &str, actual_path: &str) -> Result<()> {
let expected = fs::read_to_string(expected_path)
.with_context(|| format!("Failed to read {expected_path}"))?;
let actual =
fs::read_to_string(actual_path).with_context(|| format!("Failed to read {actual_path}"))?;

let expected_lines: Vec<&str> = expected.lines().collect();
let actual_lines: Vec<&str> = actual.lines().collect();

if expected_lines.len() != actual_lines.len() {
bail!(
"Line count differs: expected {} lines, actual {} lines",
expected_lines.len(),
actual_lines.len()
);
}

for (idx, (exp, act)) in expected_lines.iter().zip(actual_lines.iter()).enumerate() {
let line_no = idx + 1;
// Escape literals, splice in `{{re:...}}` as raw regex
let mut pattern = String::new();
let mut rest = *exp;
while let Some(start) = rest.find("{{re:") {
let (head, tail) = rest.split_at(start);
pattern.push_str(&regex::escape(head));
if let Some(end) = tail.find("}}") {
let re_body = &tail[5..end]; // after '{{re:' up to before '}}'
pattern.push_str(re_body);
rest = &tail[end + 2..];
} else {
// No closing, treat literally
pattern.push_str(&regex::escape(tail));
rest = "";
}
}
pattern.push_str(&regex::escape(rest));
// Anchor the pattern to the full line
let anchored = format!("^{}$", pattern);
let re = Regex::new(&anchored)
.with_context(|| format!("Invalid constructed regex on expected line {line_no}"))?;
if !re.is_match(act) {
bail!(
"Line {line_no} differs.\n expected: {exp}\n actual: {act}\n pattern: {anchored}"
);
}
}

Ok(())
}

pub fn main(fix: bool) -> Result<()> {
let sh = &Shell::new()?;
println!("Cargo version = {}", cmd!(sh, "cargo --version").read()?);
Expand Down