From 2812bb7b7c842ad407540638eb28df9d117357c5 Mon Sep 17 00:00:00 2001 From: Martin Algesten Date: Fri, 16 Feb 2024 07:06:44 +0100 Subject: [PATCH] testdeps feature to pull in hootbin when needed This PR makes it so tests must run using the `testdeps` feature. That means hootbin can be an optional dependency and thus not being a forced dependency on our users. --- .github/workflows/test.yml | 2 +- Cargo.toml | 4 +++- src/testserver.rs | 46 ++++++++++++++++++++++---------------- test.sh | 4 ++-- 4 files changed, 33 insertions(+), 23 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 08fa3c4a..02d4a1ef 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -97,7 +97,7 @@ jobs: uses: actions-rs/cargo@v1 with: command: test - args: --no-default-features --features "${{ matrix.tls }} ${{ matrix.feature }}" + args: --no-default-features --features "testdeps ${{ matrix.tls }} ${{ matrix.feature }}" cargo-deny: diff --git a/Cargo.toml b/Cargo.toml index 2f4b68ed..2bdd3ec9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -36,6 +36,8 @@ http-interop = ["dep:http-02"] # http-crate is for http crate version 1.0 (full release) http-crate = ["dep:http"] proxy-from-env = [] +# Doc tests require hootbin. +testdeps = ["dep:hootbin"] [dependencies] base64 = "0.21" @@ -60,7 +62,7 @@ http-02 = { package = "http", version = "0.2", optional = true } http = { version = "1.0", optional = true } # This can't be in dev-dependencies due to doc tests. -hootbin = { version = "0.1.1" } +hootbin = { version = "0.1.1", optional = true } [dev-dependencies] serde = { version = "1", features = ["derive"] } diff --git a/src/testserver.rs b/src/testserver.rs index fd844efc..293caf68 100644 --- a/src/testserver.rs +++ b/src/testserver.rs @@ -8,31 +8,39 @@ use std::time::Duration; use crate::{Agent, AgentBuilder}; +#[cfg(not(feature = "testdeps"))] +fn test_server_handler(_stream: TcpStream) -> io::Result<()> { + Ok(()) +} + +#[cfg(feature = "testdeps")] +fn test_server_handler(stream: TcpStream) -> io::Result<()> { + use hootbin::serve_single; + let o = stream.try_clone().expect("TcpStream to be clonable"); + let i = stream; + match serve_single(i, o, "https://hootbin.test/") { + Ok(()) => {} + Err(e) => { + if let hootbin::Error::Io(ioe) = &e { + if ioe.kind() == io::ErrorKind::UnexpectedEof { + // accept this. the pre-connect below is always erroring. + return Ok(()); + } + } + + println!("TestServer error: {:?}", e); + } + }; + Ok(()) +} + // An agent to be installed by default for tests and doctests, such // that all hostnames resolve to a TestServer on localhost. pub(crate) fn test_agent() -> Agent { #[cfg(test)] let _ = env_logger::try_init(); - let testserver = TestServer::new(|stream: TcpStream| -> io::Result<()> { - use hootbin::serve_single; - let o = stream.try_clone().expect("TcpStream to be clonable"); - let i = stream; - match serve_single(i, o, "https://hootbin.test/") { - Ok(()) => {} - Err(e) => { - if let hootbin::Error::Io(ioe) = &e { - if ioe.kind() == io::ErrorKind::UnexpectedEof { - // accept this. the pre-connect below is always erroring. - return Ok(()); - } - } - - println!("TestServer error: {:?}", e); - } - }; - Ok(()) - }); + let testserver = TestServer::new(test_server_handler); // Slightly tricky thing here: we want to make sure the TestServer lives // as long as the agent. This is accomplished by `move`ing it into the // closure, which becomes owned by the agent. diff --git a/test.sh b/test.sh index d5fa3df0..4bfa42f2 100755 --- a/test.sh +++ b/test.sh @@ -5,8 +5,8 @@ export RUST_BACKTRACE=1 export RUSTFLAGS="-D dead_code -D unused-variables -D unused" for feature in "" tls json charset cookies socks-proxy "tls native-certs" native-tls gzip brotli http-interop http-crate; do - if ! cargo test --no-default-features --features "${feature}" ; then - echo Command failed: cargo test --no-default-features --features \"${feature}\" + if ! cargo test --no-default-features --features "testdeps ${feature}" ; then + echo Command failed: cargo test --no-default-features --features \"testdeps ${feature}\" exit 1 fi done