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

xtask: Simplify fn utils::run_command #471

Merged
merged 1 commit into from
May 14, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
65 changes: 25 additions & 40 deletions xtask/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,13 @@ 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), "host");

do_test(
|| {
run_command(
&["cargo", "check", "--workspace", "--features", "unstable-test"],
"cargo",
&["check", "--workspace", "--features", "unstable-test"],
None,
&env,
)
Expand All @@ -97,14 +98,15 @@ fn test_host(deny_warnings: bool) {
);

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

do_test(
|| {
run_command(
&["cargo", "test", "--workspace", "--features", "unstable-test"],
"cargo",
&["test", "--workspace", "--features", "unstable-test"],
None,
&[],
)
Expand All @@ -115,7 +117,8 @@ fn test_host(deny_warnings: bool) {
do_test(
|| {
run_command(
&["cargo", "test", "--workspace", "--features", "unstable-test"],
"cargo",
&["test", "--workspace", "--features", "unstable-test"],
None,
&[],
)
Expand All @@ -134,22 +137,14 @@ 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, &[]),
"cross",
);
do_test(
|| {
run_command(
&[
"cargo",
"check",
"--target",
target,
"-p",
"defmt",
"--features",
"alloc",
],
"cargo",
&["check", "--target", target, "-p", "defmt", "--features", "alloc"],
None,
&[],
)
Expand All @@ -161,8 +156,8 @@ fn test_cross() {
do_test(
|| {
run_command(
"cargo",
&[
"cargo",
"check",
"--target",
"thumbv6m-none-eabi",
Expand All @@ -182,7 +177,8 @@ fn test_cross() {
do_test(
|| {
run_command(
&["cargo", "check", "--target", "thumbv7em-none-eabi", "--workspace"],
"cargo",
&["check", "--target", "thumbv7em-none-eabi", "--workspace"],
Some("firmware"),
&[],
)
Expand All @@ -193,14 +189,8 @@ 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"),
&[],
)
Expand All @@ -211,14 +201,8 @@ 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"),
&[],
)
Expand Down Expand Up @@ -297,17 +281,18 @@ 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, &[]), "book");

do_test(
|| run_command(&["cargo", "build", "--features", "unstable-test"], None, &[]),
|| run_command("cargo", &["build", "--features", "unstable-test"], None, &[]),
"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"),
&[],
)
Expand All @@ -318,11 +303,11 @@ fn test_book() {

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

do_test(|| run_command(&["cargo", "clippy", "--workspace"], None, &[]), "lint");
do_test(|| run_command("cargo", &["clippy", "--workspace"], None, &[]), "lint");
}
37 changes: 14 additions & 23 deletions xtask/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,34 +24,25 @@ 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: &[(&str, &str)]) -> anyhow::Result<()> {
let mut cmd = Command::new(program);
cmd.args(args).envs(envs.iter().copied());

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