Skip to content

Add print snapshot tests #910

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 4 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
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
3 changes: 3 additions & 0 deletions qemu-run/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ version = "0.0.0"
[dependencies]
anyhow = "1"
defmt-decoder = { version = "=0.4.0", path = "../decoder" }

[features]
no-decode = []
10 changes: 7 additions & 3 deletions qemu-run/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

use std::{
env, fs,
io::Read as _,
io::{self, Read as _, Write},
process::{self, Command, Stdio},
};

Expand Down Expand Up @@ -70,8 +70,12 @@ fn notmain() -> Result<Option<i32>, anyhow::Error> {
let exit_code;
loop {
let n = stdout.read(&mut readbuf)?;
decoder.received(&readbuf[..n]);
decode(&mut *decoder)?;
if cfg!(feature = "no-decode") {
io::stdout().write_all(&readbuf[..n])?;
} else {
decoder.received(&readbuf[..n]);
decode(&mut *decoder)?;
}

if let Some(status) = child.0.try_wait()? {
exit_code = status.code();
Expand Down
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.
Binary file added xtask/output_files/hints_inner.out
Binary file not shown.
Binary file added xtask/output_files/log.out
Binary file not shown.
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/log
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: 7 additions & 0 deletions xtask/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod backcompat;
mod print_snapshot;
mod snapshot;
mod targets;
mod utils;
Expand All @@ -9,6 +10,7 @@ use anyhow::anyhow;
use clap::{Parser, Subcommand};
use utils::rustc_is_msrv;

use crate::print_snapshot::test_print_snapshot;
use crate::{
snapshot::{test_snapshot, Snapshot},
utils::{run_capturing_stdout, run_command},
Expand Down Expand Up @@ -48,6 +50,10 @@ enum TestCommand {
/// Runs a single snapshot test in Debug mode
single: Option<Snapshot>,
},
TestPrintSnapshot {
/// Runs a single snapshot test in Debug mode
single: Option<Snapshot>,
},
}

fn main() -> anyhow::Result<()> {
Expand Down Expand Up @@ -77,6 +83,7 @@ fn main() -> anyhow::Result<()> {
test_book();
test_lint();
}
TestCommand::TestPrintSnapshot { single } => test_print_snapshot(single),
_ => unreachable!("get handled in outer `match`"),
}
}
Expand Down
73 changes: 73 additions & 0 deletions xtask/src/print_snapshot.rs
Original file line number Diff line number Diff line change
@@ -0,0 +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(snapshot: Option<Snapshot>) {
match snapshot {
None => test_all_print_snapshots(),
Some(snapshot) => {
do_test(
|| test_single_print_snapshot(snapshot.name()),
"qemu/snapshot_print",
);
}
}
}

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())?;
Copy link
Member

@Urhengulas Urhengulas Jan 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jonathanpallant Clippy refers to the Command in line 33.

I think it is correct without .wait, because it pipes the output from cat into defmt-print. So we do not want to wait until cat exited before executing run_capturing_stdout.

Iiuc the pipe also makes sure cat does not exit before we execute run_capturing_stdout.

So we just need to allow it.

Suggested change
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())?;
#[allow(
clippy::zombie_processes,
reason = "no wait needed, because we pipe into defmt-print"
)]
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())?;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you could also bypass the issue by not spawning cat and instead doing std::fs::read() on the file and passing the bytes to the stdin of the defmt-print process.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, that sounds simpler actually.


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))
}
}
Loading