diff --git a/dropshot/tests/integration-tests/pagination.rs b/dropshot/tests/integration-tests/pagination.rs index e744b2f3..9c7a82a8 100644 --- a/dropshot/tests/integration-tests/pagination.rs +++ b/dropshot/tests/integration-tests/pagination.rs @@ -32,11 +32,13 @@ use serde::Deserialize; use serde::Serialize; use serde::de::DeserializeOwned; use std::collections::BTreeSet; +use std::env::consts::EXE_SUFFIX; use std::env::current_exe; use std::fmt::Debug; use std::net::Ipv4Addr; use std::net::SocketAddr; use std::ops::Bound; +use std::path::PathBuf; use std::sync::LazyLock; use std::sync::atomic::AtomicU16; use std::sync::atomic::Ordering; @@ -783,6 +785,34 @@ impl Drop for ExampleContext { } } +/// Returns the path of the named example program. +fn example_path(name: &str) -> PathBuf { + let test_program = current_exe().expect("failed to find test program"); + + // The test program sits in `deps` or, with Cargo 1.100 and later, in + // `build///out`. The nearest ancestor with an `examples` + // directory is the profile directory. + let examples_dir = test_program + .ancestors() + .skip(1) + .map(|dir| dir.join("examples")) + .find(|dir| dir.is_dir()) + .unwrap_or_else(|| { + panic!( + "no examples directory above test program {}", + test_program.display(), + ) + }); + + let path = examples_dir.join(format!("{name}{EXE_SUFFIX}")); + assert!( + path.is_file(), + "example program {} not found; run `cargo build --examples` first", + path.display(), + ); + path +} + /// For one of the example programs that starts a Dropshot server on localhost /// using a TCP port provided as a command-line argument, this function starts /// the requested example on the requested TCP port and attempts to wait for the @@ -799,15 +829,7 @@ async fn start_example(path: &str, port: u16) -> ExampleContext { ); let log = logctx.log.new(o!()); - let cmd_path = { - let mut my_path = current_exe().expect("failed to find test program"); - my_path.pop(); - assert_eq!(my_path.file_name().unwrap(), "deps"); - my_path.pop(); - my_path.push("examples"); - my_path.push(path); - my_path - }; + let cmd_path = example_path(path); // We redirect stderr to /dev/null to avoid spamming the user's terminal. // It would be better to put this in some log file that we manage similarly