|
| 1 | +#[macro_use] |
| 2 | +extern crate error_chain; |
| 3 | +extern crate inflections; |
| 4 | +extern crate rayon; |
| 5 | +extern crate reqwest; |
| 6 | +#[macro_use] |
| 7 | +extern crate structopt; |
| 8 | + |
| 9 | +mod tests; |
| 10 | +mod errors; |
| 11 | +mod svd_test; |
| 12 | + |
| 13 | +use std::path::PathBuf; |
| 14 | +use structopt::StructOpt; |
| 15 | +use rayon::prelude::*; |
| 16 | +use std::sync::atomic::{AtomicBool, Ordering}; |
| 17 | +use std::process::exit; |
| 18 | +use std::time::Instant; |
| 19 | + |
| 20 | +#[derive(StructOpt, Debug)] |
| 21 | +#[structopt(name = "svd2rust-regress")] |
| 22 | +struct Opt { |
| 23 | + /// Run a long test (it's very long) |
| 24 | + #[structopt(short = "l", long = "long-test")] |
| 25 | + long_test: bool, |
| 26 | + |
| 27 | + /// Path to an `svd2rust` binary, relative or absolute. |
| 28 | + /// Defaults to `target/release/svd2rust[.exe]` of this repository |
| 29 | + /// (which must be already built) |
| 30 | + #[structopt(short = "p", long = "svd2rust-path", parse(from_os_str))] |
| 31 | + bin_path: Option<PathBuf>, |
| 32 | + |
| 33 | + /// Filter by chip name, case sensitive, may be combined with other filters |
| 34 | + #[structopt(short = "c", long = "chip")] |
| 35 | + chip: Option<String>, |
| 36 | + |
| 37 | + /// Filter by manufacturer, case sensitive, may be combined with other filters |
| 38 | + #[structopt(short = "m", long = "manufacturer")] |
| 39 | + mfgr: Option<String>, |
| 40 | + |
| 41 | + /// Filter by architecture, case sensitive, may be combined with other filters |
| 42 | + /// Options are: "CortexM", "RiscV", and "Msp430" |
| 43 | + #[structopt(short = "a", long = "architecture")] |
| 44 | + arch: Option<String>, |
| 45 | + |
| 46 | + /// Include tests expected to fail (will cause a non-zero return code) |
| 47 | + #[structopt(short = "b", long = "bad-tests")] |
| 48 | + bad_tests: bool, |
| 49 | + |
| 50 | + // TODO: Specify smaller subset of tests? Maybe with tags? |
| 51 | + // TODO: Early fail |
| 52 | + // TODO: Compile svd2rust? |
| 53 | +} |
| 54 | + |
| 55 | +/// Validate any assumptions made by this program |
| 56 | +fn validate_tests(tests: &[&tests::TestCase]) { |
| 57 | + use std::collections::HashSet; |
| 58 | + |
| 59 | + let mut fail = false; |
| 60 | + |
| 61 | + // CONDITION 1: All mfgr+chip names must be unique |
| 62 | + let mut uniq = HashSet::new(); |
| 63 | + for t in tests { |
| 64 | + let name = t.name(); |
| 65 | + if !uniq.insert(name.clone()) { |
| 66 | + fail = true; |
| 67 | + eprintln!("{} is not unique!", name); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + if fail { |
| 72 | + panic!("Tests failed validation"); |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +fn main() { |
| 77 | + let opt = Opt::from_args(); |
| 78 | + |
| 79 | + // Validate all test pre-conditions |
| 80 | + validate_tests(tests::TESTS); |
| 81 | + |
| 82 | + // Determine default svd2rust path |
| 83 | + let default_svd2rust_iter = ["..", "..", "..", "..", "target", "release"]; |
| 84 | + |
| 85 | + let default_svd2rust = if cfg!(windows) { |
| 86 | + default_svd2rust_iter.iter().chain(["svd2rust.exe"].iter()) |
| 87 | + } else { |
| 88 | + default_svd2rust_iter.iter().chain(["svd2rust"].iter()) |
| 89 | + }.collect(); |
| 90 | + |
| 91 | + let bin_path = match opt.bin_path { |
| 92 | + Some(ref bp) => bp, |
| 93 | + None => &default_svd2rust, |
| 94 | + }; |
| 95 | + |
| 96 | + // collect enabled tests |
| 97 | + let tests = tests::TESTS |
| 98 | + .iter() |
| 99 | + // Short test? |
| 100 | + .filter(|t| t.should_run(!opt.long_test)) |
| 101 | + // selected architecture? |
| 102 | + .filter(|t| { |
| 103 | + if let Some(ref arch) = opt.arch { |
| 104 | + arch == &format!("{:?}", t.arch) |
| 105 | + } else { |
| 106 | + true |
| 107 | + } |
| 108 | + }) |
| 109 | + // selected manufacturer? |
| 110 | + .filter(|t| { |
| 111 | + if let Some(ref mfgr) = opt.mfgr { |
| 112 | + mfgr == &format!("{:?}", t.mfgr) |
| 113 | + } else { |
| 114 | + true |
| 115 | + } |
| 116 | + }) |
| 117 | + // Specify chip - note: may match multiple |
| 118 | + .filter(|t| { |
| 119 | + if let Some(ref chip) = opt.chip { |
| 120 | + chip == t.chip |
| 121 | + } else { |
| 122 | + true |
| 123 | + } |
| 124 | + }) |
| 125 | + // Run failable tests? |
| 126 | + .filter(|t| opt.bad_tests || t.should_pass) |
| 127 | + .collect::<Vec<_>>(); |
| 128 | + |
| 129 | + let any_fails = AtomicBool::new(false); |
| 130 | + |
| 131 | + // TODO: It would be more efficient to reuse directories, so we don't |
| 132 | + // have to rebuild all the deps crates |
| 133 | + tests.par_iter().for_each(|t| { |
| 134 | + let start = Instant::now(); |
| 135 | + |
| 136 | + match svd_test::test(t, &bin_path) { |
| 137 | + Ok(()) => { |
| 138 | + eprintln!( |
| 139 | + "Passed: {} - {} seconds", |
| 140 | + t.name(), |
| 141 | + start.elapsed().as_secs() |
| 142 | + ); |
| 143 | + } |
| 144 | + Err(e) => { |
| 145 | + any_fails.store(true, Ordering::Release); |
| 146 | + eprintln!( |
| 147 | + "Failed: {} - {} seconds - {:?}", |
| 148 | + t.name(), |
| 149 | + start.elapsed().as_secs(), |
| 150 | + e |
| 151 | + ); |
| 152 | + } |
| 153 | + } |
| 154 | + }); |
| 155 | + |
| 156 | + if any_fails.load(Ordering::Acquire) { |
| 157 | + exit(1); |
| 158 | + } else { |
| 159 | + exit(0); |
| 160 | + } |
| 161 | +} |
0 commit comments