Skip to content

Commit

Permalink
test(iroh-cli): Improve bao_store_migration test logging (#2483)
Browse files Browse the repository at this point in the history
## Description

We didn't get good logs on the `cli_bao_store_migration` test so far.
Here's what it's output was in a failure case:
```
--- STDOUT:              iroh-cli::cli cli_bao_store_migration ---

running 1 test
|Iroh is running
|Node ID: cnzuojj4sbo2ulpqoo2riyrdsn2j4pnp3b5bgrjubsft72nutlca
iroh started up.
test cli_bao_store_migration ... FAILED

failures:

failures:
    cli_bao_store_migration

test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 12 filtered out; finished in 2.71s


--- STDERR:              iroh-cli::cli cli_bao_store_migration ---
thread 'cli_bao_store_migration' panicked at iroh-cli/tests/cli.rs:394:5:
assertion `left == right` failed
  left: ""
 right: "4yny3v7anmzzsajv2amm3nxpqd2owfw4dqnjwq6anv7nj2djmt2q (0 B)\n"
 ```
 
 This was only capturing stdout from one of the four iroh commands.
 
 This PR changes that. Now stdout and stderr are captured by all iroh commands in the `cli_bao_store_migration` test.

## Breaking Changes

None

## Notes & open questions

I've also switched `PathBuf::to_string_lossy()` calls to be `PathBuf::display()` calls.

## Change checklist

- [x] Self-review.
- ~~[ ] Documentation updates following the [style guide](https://rust-lang.github.io/rfcs/1574-more-api-documentation-conventions.html#appendix-a-full-conventions-text), if relevant.~~
- ~~[ ] Tests if relevant.~~
- ~~[ ] All breaking changes documented.~~
  • Loading branch information
matheus23 committed Jul 16, 2024
1 parent 50a8b5c commit d17ffa3
Showing 1 changed file with 23 additions and 18 deletions.
41 changes: 23 additions & 18 deletions iroh-cli/tests/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::net::SocketAddr;
use std::path::{Path, PathBuf};
use std::str::FromStr;

use anyhow::{Context, Result};
use anyhow::{ensure, Context, Result};
use bao_tree::blake3;
use duct::{cmd, ReaderHandle};
use iroh::{
Expand Down Expand Up @@ -352,9 +352,20 @@ fn run_cli(
let output = cmd(iroh_bin(), args)
.env_remove("RUST_LOG")
.env("IROH_DATA_DIR", iroh_data_dir)
// .stderr_file(std::io::stderr().as_raw_fd()) // for debug output
.stderr_capture()
.stdout_capture()
.unchecked()
.run()?;

// checking the output first, so you can still view any logging
println!("STDOUT: {}", String::from_utf8_lossy(&output.stdout));
println!("STDERR: {}", String::from_utf8_lossy(&output.stderr));

ensure!(
output.status.success(),
"iroh command failed. See STDERR output above."
);

let text = String::from_utf8(output.stdout)?;
Ok(text)
}
Expand Down Expand Up @@ -746,11 +757,9 @@ fn test_provide_get_loop(input: Input, output: Output) -> Result<()> {
drop(provider);

// checking the output first, so you can still view any logging
println!("STDOUT: {:?}", std::str::from_utf8(&get_output.stdout),);
println!(
"STDERR: {}",
std::str::from_utf8(&get_output.stderr).unwrap()
);
println!("STDOUT: {}", String::from_utf8_lossy(&get_output.stdout));
println!("STDERR: {}", String::from_utf8_lossy(&get_output.stderr));

match_get_stderr(get_output.stderr)?;
assert!(get_output.status.success());

Expand Down Expand Up @@ -1049,16 +1058,12 @@ fn copy_dir_all(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> anyhow::Result<
std::fs::create_dir_all(dst)?;
let mut len = 0;
for entry in std::fs::read_dir(src)? {
let entry = entry.with_context(|| {
format!(
"failed to read directory entry in `{}`",
src.to_string_lossy()
)
})?;
let entry = entry
.with_context(|| format!("failed to read directory entry in `{}`", src.display()))?;
let ty = entry.file_type().with_context(|| {
format!(
"failed to get file type for file `{}`",
entry.path().to_string_lossy()
entry.path().display()
)
})?;
let src = entry.path();
Expand All @@ -1067,16 +1072,16 @@ fn copy_dir_all(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> anyhow::Result<
len += copy_dir_all(&src, &dst).with_context(|| {
format!(
"failed to copy directory `{}` to `{}`",
src.to_string_lossy(),
dst.to_string_lossy()
src.display(),
dst.display()
)
})?;
} else {
std::fs::copy(&src, &dst).with_context(|| {
format!(
"failed to copy file `{}` to `{}`",
src.to_string_lossy(),
dst.to_string_lossy()
src.display(),
dst.display()
)
})?;
len += 1;
Expand Down

0 comments on commit d17ffa3

Please sign in to comment.