Skip to content
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

defmt-test: #[cfg(test)] the #[defmt_test::tests] module #604

Merged
merged 5 commits into from
Oct 19, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 3 additions & 1 deletion firmware/defmt-test/macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,9 @@ fn tests_impl(args: TokenStream, input: TokenStream) -> parse::Result<TokenStrea
}
})
.collect::<Vec<_>>();
Ok(quote!(mod #ident {
Ok(quote!(
#[cfg(test)]
mod #ident {
#(#untouched_tokens)*
// TODO use `cortex-m-rt::entry` here to get the `static mut` transform
#[export_name = "main"]
Expand Down
1 change: 1 addition & 0 deletions firmware/qemu/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[alias]
tt = "-q test --target thumbv7m-none-eabi --test"
rb = "-q run --target thumbv7m-none-eabi --bin"
rrb = "-q run --target thumbv7m-none-eabi --release --bin"

Expand Down
4 changes: 4 additions & 0 deletions firmware/qemu/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ name = "firmware"
publish = false
version = "0.1.0"

[[test]]
name = "defmt-test"
harness = false

[dependencies]
defmt = { path = "../../defmt" }
defmt-semihosting = { path = "../defmt-semihosting" }
Expand Down
6 changes: 6 additions & 0 deletions firmware/qemu/tests/defmt-test.out.new
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
INFO (1/3) running `assert_true`...
INFO (2/3) running `assert_imported_max`...
INFO (3/3) running `assert_eq`...
ERROR panicked at 'assertion failed: `(left == right)`: TODO: write actual tests'
left: `24`
right: `42`
6 changes: 6 additions & 0 deletions firmware/qemu/tests/defmt-test.release.out.new
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
INFO (1/3) running `assert_true`...
INFO (2/3) running `assert_imported_max`...
INFO (3/3) running `assert_eq`...
ERROR panicked at 'assertion failed: `(left == right)`: TODO: write actual tests'
left: `24`
right: `42`
File renamed without changes.
5 changes: 4 additions & 1 deletion xtask/src/backcompat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,12 @@ impl QemuRun {
fn run_snapshot(&self, name: &str) -> anyhow::Result<()> {
println!("{}", name.bold());

let is_test = name.contains("test");
let command = if is_test { "tt" } else { "rb" };

run_silently(
Command::new("cargo")
.args(["-q", "rb", name])
.args(["-q", command, name])
.current_dir(SNAPSHOT_TESTS_DIRECTORY)
.env(RUNNER_ENV_VAR, self.path()),
|| anyhow!("{}", name.to_string()),
Expand Down
12 changes: 9 additions & 3 deletions xtask/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,13 @@ fn test_all_snapshots(overwrite: bool) {
fn test_single_snapshot(name: &str, features: &str, overwrite: bool) -> anyhow::Result<()> {
println!("{}", name.bold());

let mut args = vec!["-q", "rb", name];
let is_test = name.contains("test");

let mut args = if is_test {
vec!["-q", "tt", name]
} else {
vec!["-q", "rb", name]
};

if !features.is_empty() {
args.extend_from_slice(&["--features", features]);
Expand All @@ -346,11 +352,11 @@ fn test_single_snapshot(name: &str, features: &str, overwrite: bool) -> anyhow::
.with_context(|| name.to_string())?;

if overwrite {
overwrite_expected_output(name, actual.as_bytes())?;
overwrite_expected_output(name, actual.as_bytes(), is_test)?;
return Ok(());
}

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

// if anything isn't ChangeTag::Equal, print it and turn on error flag
Expand Down
34 changes: 24 additions & 10 deletions xtask/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
use std::{fs, path::Path, process::Command, str};
use std::{
fs,
path::{Path, PathBuf},
process::Command,
str,
};

use anyhow::{anyhow, Context};
use colored::Colorize;

pub fn load_expected_output(name: &str) -> anyhow::Result<String> {
let file = expected_output_path(name);
let path = Path::new(&file);
pub fn load_expected_output(name: &str, is_test: bool) -> anyhow::Result<String> {
let path = expected_output_path(name, is_test);

fs::read_to_string(path).with_context(|| {
fs::read_to_string(&path).with_context(|| {
format!(
"Failed to load expected output data from {}",
path.to_str().unwrap_or("(non-Unicode path)")
)
})
}

pub fn overwrite_expected_output(name: &str, contents: &[u8]) -> anyhow::Result<()> {
let file = expected_output_path(name);
pub fn overwrite_expected_output(name: &str, contents: &[u8], is_test: bool) -> anyhow::Result<()> {
let file = expected_output_path(name, is_test);
let path = Path::new(&file);

fs::write(path, contents).with_context(|| {
Expand All @@ -27,9 +31,19 @@ pub fn overwrite_expected_output(name: &str, contents: &[u8]) -> anyhow::Result<
})
}

fn expected_output_path(name: &str) -> String {
const BASE: &str = "firmware/qemu/src/bin";
format!("{}/{}.out", BASE, name)
fn expected_output_path(name: &str, is_test: bool) -> PathBuf {
const PROJECT_DIR: &str = "firmware/qemu";

let mut path = PathBuf::from(PROJECT_DIR);
if is_test {
path.push("tests")
} else {
path.push("src");
path.push("bin");
};
path.push(name);
path.set_extension("out");
path
}

/// Execute the [`Command`]. If success return `stdout`, if failure print to `stderr`
Expand Down