Skip to content

Commit

Permalink
Add a clear build summary at the end of build-all-examples
Browse files Browse the repository at this point in the history
Currently, it's tricky to get an idea of how many successes/failures
there were without combing through the stdout/stderr. This adds a little
summary to the end that looks something like this:

```
Build all examples summary:
  [✓]: /home/mindtree/programming/rust/fuel/sway/examples/fizzbuzz succeeded!
  [✓]: /home/mindtree/programming/rust/fuel/sway/examples/hello_world succeeded!
  [x]: /home/mindtree/programming/rust/fuel/sway/examples/wallet_smart_contract failed!
  [✓]: /home/mindtree/programming/rust/fuel/sway/examples/subcurrency succeeded!
3 successes, 1 failure
```
  • Loading branch information
mitchmindtree committed Feb 11, 2022
1 parent 3fd37fa commit ee5984a
Showing 1 changed file with 38 additions and 6 deletions.
44 changes: 38 additions & 6 deletions examples/build-all-examples/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@
use std::{
fs,
io::{self, Write},
path::Path,
path::{Path, PathBuf},
};

fn main() {
let proj_dir = Path::new(env!("CARGO_MANIFEST_DIR"));
let examples_dir = proj_dir
.parent()
.expect("failed to find examples directory");
let mut failed = false;

// Track discovered projects and whether or not they were successful.
let mut summary: Vec<(PathBuf, bool)> = vec![];

for res in fs::read_dir(examples_dir).expect("failed to walk examples directory") {
let entry = match res {
Ok(entry) => entry,
Expand All @@ -31,14 +34,43 @@ fn main() {
.expect("failed to run `forc build` for example project");

// Print output on failure so we can read it in CI.
if !output.status.success() {
let success = if !output.status.success() {
io::stdout().write_all(&output.stdout).unwrap();
io::stdout().write_all(&output.stderr).unwrap();
failed = true;
false
} else {
true
};

summary.push((path, success));
}

println!("\nBuild all examples summary:");
let mut successes = 0;
for (path, success) in &summary {
let (checkmark, status) = if *success {
("[✓]", "succeeded")
} else {
("[x]", "failed")
};
println!(" {}: {} {}!", checkmark, path.display(), status);
if *success {
successes += 1;
}
}
if failed {
eprintln!("One or more example projects failed to build");
let failures = summary.len() - successes;
let successes_str = if successes == 1 {
"success"
} else {
"successes"
};
let failures_str = if failures == 1 { "failure" } else { "failures" };
println!(
"{} {}, {} {}",
successes, successes_str, failures, failures_str
);

if failures > 0 {
std::process::exit(1);
}
}
Expand Down

0 comments on commit ee5984a

Please sign in to comment.