Skip to content

Commit

Permalink
Add other ELFs and .out files
Browse files Browse the repository at this point in the history
- Other examples which don't require features are now in the xtask dir
- Added new functions to better align with how the current snapshot tests work
  • Loading branch information
BenFordTytherington committed Dec 13, 2024
1 parent ebbeee4 commit 942dea3
Show file tree
Hide file tree
Showing 25 changed files with 79 additions and 51 deletions.
11 changes: 11 additions & 0 deletions firmware/qemu/write_output.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash

bins=("assert" "assert-eq" "assert-ne" "bitflags" "dbg" "hints" "hints_inner" "log" "panic" "panic_info" "timestamp" "unwrap")

echo "Generating output ..."

for value in "${bins[@]}"; do
command="DEFMT_LOG=trace cargo -q run --features no-decode --manifest-path ../../qemu-run/Cargo.toml ../target/thumbv7m-none-eabi/debug/$value > ~/defmt/xtask/output_files/$value.out"
echo "$command"
eval "$command"
done
Binary file added xtask/output_files/assert-eq.out
Binary file not shown.
Binary file added xtask/output_files/assert-ne.out
Binary file not shown.
Binary file added xtask/output_files/assert.out
Binary file not shown.
Binary file added xtask/output_files/bitflags.out
Binary file not shown.
Binary file added xtask/output_files/dbg.out
Binary file not shown.
Binary file added xtask/output_files/hints.out
Binary file not shown.
Empty file.
Binary file added xtask/output_files/panic.out
Binary file not shown.
Binary file added xtask/output_files/panic_info.out
Binary file not shown.
Binary file added xtask/output_files/timestamp.out
Binary file not shown.
Binary file added xtask/output_files/unwrap.out
Binary file not shown.
Binary file added xtask/snapshot_elfs/assert
Binary file not shown.
Binary file added xtask/snapshot_elfs/assert-eq
Binary file not shown.
Binary file added xtask/snapshot_elfs/assert-ne
Binary file not shown.
Binary file added xtask/snapshot_elfs/bitflags
Binary file not shown.
Binary file added xtask/snapshot_elfs/dbg
Binary file not shown.
Binary file added xtask/snapshot_elfs/hints
Binary file not shown.
Binary file added xtask/snapshot_elfs/hints_inner
Binary file not shown.
Binary file added xtask/snapshot_elfs/panic
Binary file not shown.
Binary file added xtask/snapshot_elfs/panic_info
Binary file not shown.
Binary file added xtask/snapshot_elfs/timestamp
Binary file not shown.
Binary file added xtask/snapshot_elfs/unwrap
Binary file not shown.
7 changes: 5 additions & 2 deletions xtask/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ enum TestCommand {
/// Runs a single snapshot test in Debug mode
single: Option<Snapshot>,
},
TestPrintSnapshot,
TestPrintSnapshot {
/// Runs a single snapshot test in Debug mode
single: Option<Snapshot>,
},
}

fn main() -> anyhow::Result<()> {
Expand Down Expand Up @@ -80,7 +83,7 @@ fn main() -> anyhow::Result<()> {
test_book();
test_lint();
}
TestCommand::TestPrintSnapshot => test_print_snapshot("log"),
TestCommand::TestPrintSnapshot { single } => test_print_snapshot(single),
_ => unreachable!("get handled in outer `match`"),
}
}
Expand Down
112 changes: 63 additions & 49 deletions xtask/src/print_snapshot.rs
Original file line number Diff line number Diff line change
@@ -1,59 +1,73 @@
use crate::do_test;
use crate::snapshot::{all_snapshot_tests, Snapshot};
use crate::utils::{load_expected_output, run_capturing_stdout};
use anyhow::{anyhow, Context};
use colored::Colorize;
use similar::{ChangeTag, TextDiff};
use std::process::{Command, Stdio};

pub fn test_print_snapshot(name: &str) {
do_test(
|| {
println!("{}", name.bold());

let frame_path = format!("xtask/output_files/{}.out", name);
let elf_path = format!("xtask/snapshot_elfs/{}", name);

let frames = Command::new("cat")
.arg(frame_path)
.stdout(Stdio::piped())
.spawn()
.unwrap();

let actual = run_capturing_stdout(
Command::new("defmt-print")
.arg("-e")
.arg(elf_path)
.arg("--log-format")
.arg("{L:4} {s}")
.stdin(Stdio::from(frames.stdout.unwrap())),
)
.with_context(|| name.to_string())?;

let expected = load_expected_output(name, false)?;
let diff = TextDiff::from_lines(&expected, &actual);

// if anything isn't ChangeTag::Equal, print it and turn on error flag
let mut actual_matches_expected = true;
for op in diff.ops() {
for change in diff.iter_changes(op) {
let styled_change = match change.tag() {
ChangeTag::Delete => Some(("-".bold().red(), change.to_string().red())),
ChangeTag::Insert => Some(("+".bold().green(), change.to_string().green())),
ChangeTag::Equal => None,
};
if let Some((sign, change)) = styled_change {
actual_matches_expected = false;
eprint!("{sign}{change}");
}
}
}
pub fn test_print_snapshot(snapshot: Option<Snapshot>) {
match snapshot {
None => test_all_print_snapshots(),
Some(snapshot) => {
do_test(
|| test_single_print_snapshot(snapshot.name()),
"qemu/snapshot_print",
);
}
}
}

if actual_matches_expected {
Ok(())
} else {
Err(anyhow!("{}", name))
}
},
"qemu/print_snapshot",
pub fn test_all_print_snapshots() {
for test in all_snapshot_tests() {
do_test(|| test_single_print_snapshot(test), "qemu/snapshot_print");
}
}

pub fn test_single_print_snapshot(name: &str) -> anyhow::Result<()> {
println!("{}", name.bold());

let frame_path = format!("xtask/output_files/{}.out", name);
let elf_path = format!("xtask/snapshot_elfs/{}", name);

let frames = Command::new("cat")
.arg(frame_path)
.stdout(Stdio::piped())
.spawn()
.unwrap();

let actual = run_capturing_stdout(
Command::new("defmt-print")
.arg("-e")
.arg(elf_path)
.arg("--log-format")
.arg("{L:4} {s}")
.stdin(Stdio::from(frames.stdout.unwrap())),
)
.with_context(|| name.to_string())?;

let expected = load_expected_output(name, false)?;
let diff = TextDiff::from_lines(&expected, &actual);

// if anything isn't ChangeTag::Equal, print it and turn on error flag
let mut actual_matches_expected = true;
for op in diff.ops() {
for change in diff.iter_changes(op) {
let styled_change = match change.tag() {
ChangeTag::Delete => Some(("-".bold().red(), change.to_string().red())),
ChangeTag::Insert => Some(("+".bold().green(), change.to_string().green())),
ChangeTag::Equal => None,
};
if let Some((sign, change)) = styled_change {
actual_matches_expected = false;
eprint!("{sign}{change}");
}
}
}

if actual_matches_expected {
Ok(())
} else {
Err(anyhow!("{}", name))
}
}

0 comments on commit 942dea3

Please sign in to comment.