Skip to content

Commit

Permalink
testdeps feature to pull in hootbin when needed
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
algesten committed Feb 16, 2024
1 parent 4ea0a0a commit 2812bb7
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"] }
Expand Down
46 changes: 27 additions & 19 deletions src/testserver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 2812bb7

Please sign in to comment.