Skip to content

Commit b2147f1

Browse files
committed
Enabled formatting with rustfmt via -f flag
Also added a way to see result of `cargo check` if it fails with `-v` flag
1 parent 5daaac3 commit b2147f1

File tree

3 files changed

+86
-31
lines changed

3 files changed

+86
-31
lines changed

ci/svd2rust-regress/src/errors.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,9 @@
1-
error_chain!{}
1+
use std::path;
2+
error_chain!{
3+
errors {
4+
ProcessFailed(command: String, stderr: Option<path::PathBuf>, stdout: Option<path::PathBuf>) {
5+
description("Process Failed")
6+
display("Process Failed - {}", command)
7+
}
8+
}
9+
}

ci/svd2rust-regress/src/main.rs

Lines changed: 57 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,18 @@ extern crate reqwest;
66
#[macro_use]
77
extern crate structopt;
88

9-
mod tests;
109
mod errors;
1110
mod svd_test;
11+
mod tests;
1212

13-
use std::path::PathBuf;
14-
use structopt::StructOpt;
1513
use rayon::prelude::*;
14+
use std::path::PathBuf;
15+
use std::process::{exit, Command};
1616
use std::sync::atomic::{AtomicBool, Ordering};
17-
use std::process::exit;
1817
use std::time::Instant;
18+
use std::fs::File;
19+
use std::io::Read;
20+
use structopt::StructOpt;
1921

2022
#[derive(StructOpt, Debug)]
2123
#[structopt(name = "svd2rust-regress")]
@@ -47,6 +49,18 @@ struct Opt {
4749
#[structopt(short = "b", long = "bad-tests")]
4850
bad_tests: bool,
4951

52+
/// Enable formatting with `rustfmt`
53+
#[structopt(short = "f", long = "format")]
54+
format: bool,
55+
56+
/// Path to an `rustfmt` binary, relative or absolute.
57+
/// Defaults to `$(rustup which rustfmt)`
58+
#[structopt(long = "rustfmt_bin_path", parse(from_os_str))]
59+
rustfmt_bin_path: Option<PathBuf>,
60+
61+
/// Use verbose output
62+
#[structopt(long = "verbose", short = "v", parse(from_occurrences))]
63+
verbose: u8,
5064
// TODO: Specify smaller subset of tests? Maybe with tags?
5165
// TODO: Early fail
5266
// TODO: Compile svd2rust?
@@ -93,6 +107,27 @@ fn main() {
93107
None => &default_svd2rust,
94108
};
95109

110+
let default_rustfmt: Option<PathBuf> = if let Some(v) = Command::new("rustup")
111+
.args(&["which", "rustfmt"])
112+
.output()
113+
.ok()
114+
.map(|v| v.stdout)
115+
{
116+
Some(String::from_utf8_lossy(&v).into_owned().trim().into())
117+
} else {
118+
None
119+
}; // FIXME: capture error and assure exit is 0
120+
121+
let rustfmt_bin_path = match (&opt.rustfmt_bin_path, opt.format) {
122+
(_, false) => None,
123+
(&Some(ref path), true) => Some(path),
124+
(&None, true) => {
125+
if default_rustfmt.is_none() {
126+
eprintln!("Warning: No rustfmt found, formatting will be skipped")
127+
}
128+
default_rustfmt.as_ref()
129+
}
130+
};
96131
// collect enabled tests
97132
let tests = tests::TESTS
98133
.iter()
@@ -133,8 +168,9 @@ fn main() {
133168
tests.par_iter().for_each(|t| {
134169
let start = Instant::now();
135170

136-
match svd_test::test(t, &bin_path) {
171+
match svd_test::test(t, &bin_path, rustfmt_bin_path) {
137172
Ok(()) => {
173+
// TODO: If verbosity is > 1, print every logged stderr
138174
eprintln!(
139175
"Passed: {} - {} seconds",
140176
t.name(),
@@ -143,11 +179,25 @@ fn main() {
143179
}
144180
Err(e) => {
145181
any_fails.store(true, Ordering::Release);
182+
let additional_info = if opt.verbose > 0 {
183+
match e.kind() {
184+
&errors::ErrorKind::ProcessFailed(ref command, _, ref stderr) if command == "cargo check" => {
185+
let mut buf = String::new();
186+
// Unwrap is safe
187+
File::open(stderr.as_ref().unwrap()).expect("Couldn't open file").read_to_string(&mut buf).expect("Couldn't read file to string");
188+
buf
189+
}
190+
_ => "".into()
191+
}
192+
} else {
193+
"".into()
194+
};
146195
eprintln!(
147-
"Failed: {} - {} seconds - {:?}",
196+
"Failed: {} - {} seconds - {}{}",
148197
t.name(),
149198
start.elapsed().as_secs(),
150-
e
199+
e,
200+
additional_info,
151201
);
152202
}
153203
}

ci/svd2rust-regress/src/svd_test.rs

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use errors::*;
2-
use std::io::prelude::*;
2+
use reqwest;
33
use std::fs::{remove_dir_all, File, OpenOptions};
4+
use std::io::prelude::*;
5+
use std::path::PathBuf;
46
use std::process::{Command, Output};
57
use tests::TestCase;
6-
use reqwest;
7-
use std::path::PathBuf;
88

99
static CRATES_ALL: &[&str] = &["bare-metal = \"0.1.0\"", "vcell = \"0.1.0\""];
1010
static CRATES_MSP430: &[&str] = &["msp430 = \"0.1.0\""];
@@ -50,25 +50,25 @@ impl CommandHelper for Output {
5050
stdout: Option<&PathBuf>,
5151
stderr: Option<&PathBuf>,
5252
) -> Result<()> {
53-
if cant_fail && !self.status.success() {
54-
bail!(format!("Process Failed! - {}", name))
55-
}
56-
5753
if let Some(out) = stdout {
5854
let out_payload = String::from_utf8_lossy(&self.stdout);
5955
file_helper(&out_payload, out)?;
60-
}
56+
};
6157

6258
if let Some(err) = stderr {
6359
let err_payload = String::from_utf8_lossy(&self.stderr);
6460
file_helper(&err_payload, err)?;
61+
};
62+
63+
if cant_fail && !self.status.success() {
64+
return Err(ErrorKind::ProcessFailed(name.into(), stdout.cloned(), stderr.cloned()).into())
6565
}
6666

6767
Ok(())
6868
}
6969
}
7070

71-
pub fn test(t: &TestCase, bin_path: &PathBuf) -> Result<()> {
71+
pub fn test(t: &TestCase, bin_path: &PathBuf, rustfmt_bin_path: Option<&PathBuf>) -> Result<()> {
7272
let user = match ::std::env::var("USER") {
7373
Ok(val) => val,
7474
Err(_) => "rusttester".into(),
@@ -135,10 +135,8 @@ pub fn test(t: &TestCase, bin_path: &PathBuf) -> Result<()> {
135135
&RiscV => "riscv",
136136
};
137137
Command::new(bin_path)
138-
.arg("-i")
139-
.arg(&chip_svd)
140-
.arg("--target")
141-
.arg(target)
138+
.args(&["-i", &chip_svd])
139+
.args(&["--target", &target])
142140
.current_dir(&chip_dir)
143141
.output()
144142
.chain_err(|| "failed to execute process")?
@@ -149,16 +147,15 @@ pub fn test(t: &TestCase, bin_path: &PathBuf) -> Result<()> {
149147
Some(&svd2rust_err_file),
150148
)?;
151149

152-
// TODO: rustfmt < 0.4.0 seems to mangle some doc comments. Omit until this is fixed
153-
// Run `cargo fmt`, capturing stderr to a log file
154-
// let cargo_fmt_err_file = path_helper_base(&chip_dir, &["cargo-fmt.err.log"]);
155-
// Command::new("cargo")
156-
// .arg("fmt")
157-
// .current_dir(&chip_dir)
158-
// .output()
159-
// .chain_err(|| "failed to format")?
160-
// .capture_outputs(false, "cargo fmt", None, Some(&cargo_fmt_err_file))?;
161-
150+
if let Some(rustfmt_bin_path) = rustfmt_bin_path {
151+
// Run `cargo fmt`, capturing stderr to a log file
152+
let fmt_err_file = path_helper_base(&chip_dir, &["rustfmt.err.log"]);
153+
Command::new(rustfmt_bin_path)
154+
.arg(lib_rs_file)
155+
.output()
156+
.chain_err(|| "failed to format")?
157+
.capture_outputs(false, "rustfmt", None, Some(&fmt_err_file))?;
158+
}
162159
// Run `cargo check`, capturing stderr to a log file
163160
let cargo_check_err_file = path_helper_base(&chip_dir, &["cargo-check.err.log"]);
164161
Command::new("cargo")

0 commit comments

Comments
 (0)