Skip to content

Commit

Permalink
xtask: Simplify fn utils::run_command
Browse files Browse the repository at this point in the history
  • Loading branch information
Urhengulas committed May 14, 2021
1 parent 5ad4384 commit 07e8309
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 72 deletions.
96 changes: 47 additions & 49 deletions xtask/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,30 +83,42 @@ fn test_host(deny_warnings: bool) {
vec![]
};

do_test(|| run_command(&["cargo", "check", "--workspace"], None, &env), "host");
do_test(
|| run_command("cargo", &["check", "--workspace"], None, env.clone()),
"host",
);

do_test(
|| {
run_command(
&["cargo", "check", "--workspace", "--features", "unstable-test"],
"cargo",
&["check", "--workspace", "--features", "unstable-test"],
None,
&env,
env.clone(),
)
},
"host",
);

do_test(
|| run_command(&["cargo", "check", "--workspace", "--features", "alloc"], None, &env),
|| {
run_command(
"cargo",
&["check", "--workspace", "--features", "alloc"],
None,
env.clone(),
)
},
"host",
);

do_test(
|| {
run_command(
&["cargo", "test", "--workspace", "--features", "unstable-test"],
"cargo",
&["test", "--workspace", "--features", "unstable-test"],
None,
&[],
vec![],
)
},
"host",
Expand All @@ -115,9 +127,10 @@ fn test_host(deny_warnings: bool) {
do_test(
|| {
run_command(
&["cargo", "test", "--workspace", "--features", "unstable-test"],
"cargo",
&["test", "--workspace", "--features", "unstable-test"],
None,
&[],
vec![],
)
},
"host",
Expand All @@ -134,24 +147,16 @@ fn test_cross() {

for target in &targets {
do_test(
|| run_command(&["cargo", "check", "--target", target, "-p", "defmt"], None, &[]),
|| run_command("cargo", &["check", "--target", target, "-p", "defmt"], None, vec![]),
"cross",
);
do_test(
|| {
run_command(
&[
"cargo",
"check",
"--target",
target,
"-p",
"defmt",
"--features",
"alloc",
],
"cargo",
&["check", "--target", target, "-p", "defmt", "--features", "alloc"],
None,
&[],
vec![],
)
},
"cross",
Expand All @@ -161,8 +166,8 @@ fn test_cross() {
do_test(
|| {
run_command(
"cargo",
&[
"cargo",
"check",
"--target",
"thumbv6m-none-eabi",
Expand All @@ -173,7 +178,7 @@ fn test_cross() {
"firmware",
],
Some("firmware"),
&[],
vec![],
)
},
"cross",
Expand All @@ -182,9 +187,10 @@ fn test_cross() {
do_test(
|| {
run_command(
&["cargo", "check", "--target", "thumbv7em-none-eabi", "--workspace"],
"cargo",
&["check", "--target", "thumbv7em-none-eabi", "--workspace"],
Some("firmware"),
&[],
vec![],
)
},
"cross",
Expand All @@ -193,16 +199,10 @@ fn test_cross() {
do_test(
|| {
run_command(
&[
"cargo",
"check",
"--target",
"thumbv6m-none-eabi",
"--features",
"print-defmt",
],
"cargo",
&["check", "--target", "thumbv6m-none-eabi", "--features", "print-defmt"],
Some("firmware/panic-probe"),
&[],
vec![],
)
},
"cross",
Expand All @@ -211,16 +211,10 @@ fn test_cross() {
do_test(
|| {
run_command(
&[
"cargo",
"check",
"--target",
"thumbv6m-none-eabi",
"--features",
"print-rtt",
],
"cargo",
&["check", "--target", "thumbv6m-none-eabi", "--features", "print-rtt"],
Some("firmware/panic-probe"),
&[],
vec![],
)
},
"cross",
Expand Down Expand Up @@ -297,19 +291,20 @@ fn test_single_snapshot(name: &str, features: &str, release_mode: bool) -> anyho

fn test_book() {
println!("🧪 book");
do_test(|| run_command(&["cargo", "clean"], None, &[]), "book");
do_test(|| run_command("cargo", &["clean"], None, vec![]), "book");

do_test(
|| run_command(&["cargo", "build", "--features", "unstable-test"], None, &[]),
|| run_command("cargo", &["build", "--features", "unstable-test"], None, vec![]),
"book",
);

do_test(
|| {
run_command(
&["mdbook", "test", "-L", "../target/debug", "-L", "../target/debug/deps"],
"mdbook",
&["test", "-L", "../target/debug", "-L", "../target/debug/deps"],
Some("book"),
&[],
vec![],
)
},
"book",
Expand All @@ -318,11 +313,14 @@ fn test_book() {

fn test_lint() {
println!("🧪 lint");
do_test(|| run_command(&["cargo", "clean"], None, &[]), "lint");
do_test(|| run_command("cargo", &["clean"], None, vec![]), "lint");
do_test(
|| run_command(&["cargo", "fmt", "--all", "--", "--check"], None, &[]),
|| run_command("cargo", &["fmt", "--all", "--", "--check"], None, vec![]),
"lint",
);

do_test(|| run_command(&["cargo", "clippy", "--workspace"], None, &[]), "lint");
do_test(
|| run_command("cargo", &["clippy", "--workspace"], None, vec![]),
"lint",
);
}
42 changes: 19 additions & 23 deletions xtask/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,34 +24,30 @@ pub fn run_capturing_stdout(cmd: &mut Command) -> anyhow::Result<String> {
Ok(str::from_utf8(&stdout)?.to_string())
}

pub fn run_command(cmd_and_args: &[&str], cwd: Option<&str>, env: &[(&str, &str)]) -> anyhow::Result<()> {
let cmd_and_args = Vec::from(cmd_and_args);
let mut cmd = &mut Command::new(cmd_and_args[0]);
if cmd_and_args.len() > 1 {
cmd.args(&cmd_and_args[1..]);
}

for (k, v) in env {
cmd.env(k, v);
}

let cwd_s = if let Some(path_ref) = cwd {
cmd = cmd.current_dir(path_ref);
format!("CWD:{} ", path_ref)
pub fn run_command(
program: &str,
args: &[&str],
cwd: Option<&str>,
envs: Vec<(&str, &str)>,
) -> anyhow::Result<()> {
let mut cmd = Command::new(program);
cmd.args(args).envs(envs);

let cwd = if let Some(path) = cwd {
cmd.current_dir(path);
format!("{}$ ", path)
} else {
"".to_string()
};

let cmdline = cmd_and_args.join(" ");
println!("🏃 {}{}", cwd_s, cmdline);
let cmdline = format!("{}{} {}", cwd, program, args.join(" "));
println!("🏃 {}", cmdline);

cmd.status()
.map_err(|e| anyhow!("could not run '{}{}': {}", cwd_s, cmdline, e))
.and_then(|exit_status| {
if exit_status.success() {
Ok(())
} else {
Err(anyhow!("'{}' did not finish successfully: {}", cmdline, exit_status))
}
.map_err(|e| anyhow!("could not run '{}': {}", cmdline, e))
.and_then(|exit_status| match exit_status.success() {
true => Ok(()),
false => Err(anyhow!("'{}' did not finish successfully: {}", cmdline, exit_status)),
})
}

Expand Down

0 comments on commit 07e8309

Please sign in to comment.