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

Add xtask option to run a single snapshot test by name #585

Merged
merged 3 commits into from
Sep 24, 2021
Merged
Changes from 1 commit
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
85 changes: 67 additions & 18 deletions xtask/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{process::Command, sync::Mutex};
use std::{process::Command, str::FromStr, sync::Mutex};

use anyhow::{anyhow, Context};
use colored::Colorize;
Expand All @@ -15,6 +15,50 @@ use crate::utils::{

static ALL_ERRORS: Lazy<Mutex<Vec<String>>> = Lazy::new(|| Mutex::new(vec![]));

const ALL_SNAPSHOT_TESTS: [&str; 12] = [
"log",
"bitflags",
"timestamp",
"panic",
"assert",
"assert-eq",
"assert-ne",
"unwrap",
"defmt-test",
"hints",
"hints_inner",
"dbg",
];

#[derive(Debug, StructOpt)]
#[allow(clippy::enum_variant_names)]
enum Snapshot {
All,
Single { name: String },
}

impl FromStr for Snapshot {
type Err = String;

fn from_str(source: &str) -> Result<Self, Self::Err> {
match source {
"all" => Ok(Snapshot::All),
test => {
if ALL_SNAPSHOT_TESTS.contains(&test) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This doesn't work with the conditionally-available alloc test, but that's probably not a big problem

Ok(Self::Single {
name: String::from(test),
})
} else {
Err(format!(
"Specified test '{}' does not exist, available are: {:?} or 'all'",
test, ALL_SNAPSHOT_TESTS
))
}
}
}
}
}

#[derive(Debug, StructOpt)]
struct Options {
#[structopt(subcommand)]
Expand All @@ -40,6 +84,9 @@ enum TestCommand {
/// Overwrite the expected output instead of comparing it.
#[structopt(long)]
overwrite: bool,
/// Runs a single snapshot test in Debug mode
#[structopt(long, default_value = "all")]
single: Snapshot,
jonas-schievink marked this conversation as resolved.
Show resolved Hide resolved
},
}

Expand All @@ -57,11 +104,13 @@ fn main() -> anyhow::Result<()> {
added_targets = Some(targets::install().expect("Error while installing required targets"));
match cmd {
TestCommand::TestCross => test_cross(),
TestCommand::TestSnapshot { overwrite } => test_snapshot(overwrite),
TestCommand::TestSnapshot { overwrite, single } => {
test_snapshot(overwrite, single);
}
TestCommand::TestAll => {
test_host(opt.deny_warnings);
test_cross();
test_snapshot(false);
test_snapshot(false, Snapshot::All);
test_book();
test_lint();
}
Expand Down Expand Up @@ -248,22 +297,22 @@ fn test_cross() {
)
}

fn test_snapshot(overwrite: bool) {
fn test_snapshot(overwrite: bool, snapshot: Snapshot) {
println!("🧪 qemu/snapshot");
let mut tests = vec![
"log",
"bitflags",
"timestamp",
"panic",
"assert",
"assert-eq",
"assert-ne",
"unwrap",
"defmt-test",
"hints",
"hints_inner",
"dbg",
];

match snapshot {
Snapshot::All => test_all_snapshots(overwrite),
Snapshot::Single { name } => {
do_test(
|| test_single_snapshot(&name, "", false, overwrite),
"qemu/snapshot",
);
}
}
}

fn test_all_snapshots(overwrite: bool) {
let mut tests = ALL_SNAPSHOT_TESTS.iter().cloned().collect::<Vec<_>>();

if rustc_is_nightly() {
tests.push("alloc");
Expand Down