Skip to content

Commit

Permalink
fix: improve local addr output and start fixing cli tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed May 15, 2023
1 parent 3a292e5 commit f76d650
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 17 deletions.
7 changes: 5 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -752,8 +752,11 @@ async fn provide(
builder.keypair(keypair).spawn().await?
};

println!("Listening address: {:#?}", provider.local_address()?);
println!("Local endpoints: {:#?}", provider.local_endpoints().await?);
let eps = provider.local_endpoints().await?;
println!("Listening addresses:");
for ep in eps {
println!(" {}", ep.addr);
}
println!("PeerID: {}", provider.peer_id());
println!();
Ok(provider)
Expand Down
49 changes: 34 additions & 15 deletions tests/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ fn cli_provide_addresses() -> Result<()> {
let get_output = cmd.output()?;
let stdout = String::from_utf8(get_output.stdout).unwrap();
assert!(get_output.status.success());
assert_eq!(stdout, "Listening addresses: [127.0.0.1:4333]\n");
assert!(stdout.starts_with("Listening addresses: [127.0.0.1:4333"));

let _provider = make_provider(&path, &input, home, Some("0.0.0.0:4333"), Some(RPC_PORT))?;
let mut cmd = Command::new(iroh_bin());
Expand Down Expand Up @@ -368,7 +368,10 @@ fn test_provide_get_loop(path: &Path, input: Input, output: Output) -> Result<()

// test get stderr output
let get_output = cmd.output()?;
// std::io::copy(&mut std::io::Cursor::new(&get_output.stderr), &mut std::io::stderr())?;
std::io::copy(
&mut std::io::Cursor::new(&get_output.stderr),
&mut std::io::stderr(),
)?;
assert!(get_output.status.success());

// test output
Expand Down Expand Up @@ -462,11 +465,12 @@ fn match_provide_output<T: Read>(

let mut caps = assert_matches_line![
reader,
r"Listening address: [\d.:]*"; 1,
r"Listening addresses:"; 1i64,
r"^ \S+"; -1i64,
r"PeerID: [_\w\d-]*"; 1,
r""; 1,
r"Adding .*"; 1,
r"- \S*: \d*.?\d*? ?[BKMGT]i?B?"; num_blobs,
r"- \S*: \d*.?\d*? ?[BKMGT]i?B?"; num_blobs as i64,
r"Total: [_\w\d-]*"; 1,
r""; 1,
r"Collection: [\da-z]{59}"; 1,
Expand Down Expand Up @@ -497,22 +501,37 @@ fn match_provide_output<T: Read>(
macro_rules! assert_matches_line {
( $x:expr, $( $z:expr;$a:expr ),* ) => {
{
let mut lines = $x.lines();
let mut lines = $x.lines().peekable();
let mut caps = Vec::new();
$(
let rx = regex::Regex::new($z)?;
for _ in 0..$a {
let line = lines.next().context("Unexpected end of stderr reader")??;
if let Some(cap) = rx.captures(line.trim()) {
for i in 0..cap.len() {
if let Some(capture_group) = cap.get(i) {
caps.push(capture_group.as_str().to_string());
let rx = regex::Regex::new($z)?;
let mut num_matches = 0;
loop {
if $a > 0 && num_matches == $a as usize {
break;
}

if let Some(Ok(line)) = lines.peek() {
if let Some(cap) = rx.captures(line) {
for i in 0..cap.len() {
if let Some(capture_group) = cap.get(i) {
caps.push(capture_group.as_str().to_string());
}
}

num_matches += 1;
} else {
break;
}
}
let _ = lines.next().context("Unexpected end of stderr reader")?;
}
if $a == -1 {
assert!(num_matches > 0, "no matches found");
} else {
anyhow::bail!(format!("no match found\nexpected match for '{}'\ngot '{line}'", $z));
};
}
assert_eq!(num_matches, $a as usize, "invalid number of matches");
}

)*
caps
}
Expand Down

0 comments on commit f76d650

Please sign in to comment.