Skip to content

Commit 2d047ad

Browse files
authored
test(node-bindings): add unit tests for node-bindings utils and refac (alloy-rs#1637)
1 parent 9b0e04d commit 2d047ad

File tree

2 files changed

+65
-24
lines changed

2 files changed

+65
-24
lines changed

crates/node-bindings/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,4 @@ url.workspace = true
3131

3232
[dev-dependencies]
3333
ci_info.workspace = true
34+
tokio = { workspace = true, features = ["macros", "rt-multi-thread"] }

crates/node-bindings/src/utils.rs

+64-24
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,9 @@ pub(crate) fn extract_value<'a>(key: &str, line: &'a str) -> Option<&'a str> {
5959

6060
/// Extracts the endpoint from the given line.
6161
pub(crate) fn extract_endpoint(key: &str, line: &str) -> Option<SocketAddr> {
62-
let val = extract_value(key, line)?;
63-
64-
// Remove the "Some( ... )" wrapper if it exists
65-
let val =
66-
if val.starts_with("Some(") && val.ends_with(')') { &val[5..val.len() - 1] } else { val };
67-
68-
val.parse::<SocketAddr>().ok()
62+
extract_value(key, line)
63+
.map(|val| val.trim_start_matches("Some(").trim_end_matches(')'))
64+
.and_then(|val| val.parse().ok())
6965
}
7066

7167
/// Runs the given closure with a temporary directory.
@@ -90,23 +86,67 @@ where
9086
temp_dir.close().unwrap();
9187
}
9288

93-
#[test]
94-
fn test_extract_http_address() {
95-
let line = "INFO [07-01|13:20:42.774] HTTP server started endpoint=127.0.0.1:8545 auth=false prefix= cors= vhosts=localhost";
96-
assert_eq!(extract_endpoint("endpoint=", line), Some(SocketAddr::from(([127, 0, 0, 1], 8545))));
97-
}
89+
#[cfg(test)]
90+
mod tests {
91+
use super::*;
92+
use std::net::SocketAddr;
9893

99-
#[test]
100-
fn test_extract_udp_address() {
101-
let line = "Updated local ENR enr=Enr { id: Some(\"v4\"), seq: 2, NodeId: 0x04dad428038b4db230fc5298646e137564fc6861662f32bdbf220f31299bdde7, signature: \"416520d69bfd701d95f4b77778970a5c18fa86e4dd4dc0746e80779d986c68605f491c01ef39cd3739fdefc1e3558995ad2f5d325f9e1db795896799e8ee94a3\", IpV4 UDP Socket: Some(0.0.0.0:30303), IpV6 UDP Socket: None, IpV4 TCP Socket: Some(0.0.0.0:30303), IpV6 TCP Socket: None, Other Pairs: [(\"eth\", \"c984fc64ec0483118c30\"), (\"secp256k1\", \"a103aa181e8fd5df651716430f1d4b504b54d353b880256f56aa727beadd1b7a9766\")], .. }";
102-
assert_eq!(
103-
extract_endpoint("IpV4 TCP Socket: ", line),
104-
Some(SocketAddr::from(([0, 0, 0, 0], 30303)))
105-
);
106-
}
94+
#[test]
95+
fn test_extract_value_with_equals() {
96+
let line = "key=value some other text";
97+
assert_eq!(extract_value("key", line), Some("value"));
98+
}
99+
100+
#[test]
101+
fn test_extract_value_with_colon() {
102+
let line = "key: value, more text here";
103+
assert_eq!(extract_value("key", line), Some("value"));
104+
}
105+
106+
#[test]
107+
fn test_extract_value_not_found() {
108+
let line = "unrelated text";
109+
assert_eq!(extract_value("key", line), None);
110+
}
107111

108-
#[test]
109-
fn test_unused_port() {
110-
let port = unused_port();
111-
assert!(port > 0);
112+
#[test]
113+
fn test_extract_http_address() {
114+
let line = "INFO [07-01|13:20:42.774] HTTP server started endpoint=127.0.0.1:8545 auth=false prefix= cors= vhosts=localhost";
115+
assert_eq!(
116+
extract_endpoint("endpoint=", line),
117+
Some(SocketAddr::from(([127, 0, 0, 1], 8545)))
118+
);
119+
}
120+
121+
#[test]
122+
fn test_extract_udp_address() {
123+
let line = "Updated local ENR enr=Enr { id: Some(\"v4\"), seq: 2, NodeId: 0x04dad428038b4db230fc5298646e137564fc6861662f32bdbf220f31299bdde7, signature: \"416520d69bfd701d95f4b77778970a5c18fa86e4dd4dc0746e80779d986c68605f491c01ef39cd3739fdefc1e3558995ad2f5d325f9e1db795896799e8ee94a3\", IpV4 UDP Socket: Some(0.0.0.0:30303), IpV6 UDP Socket: None, IpV4 TCP Socket: Some(0.0.0.0:30303), IpV6 TCP Socket: None, Other Pairs: [(\"eth\", \"c984fc64ec0483118c30\"), (\"secp256k1\", \"a103aa181e8fd5df651716430f1d4b504b54d353b880256f56aa727beadd1b7a9766\")], .. }";
124+
assert_eq!(
125+
extract_endpoint("IpV4 TCP Socket: ", line),
126+
Some(SocketAddr::from(([0, 0, 0, 0], 30303)))
127+
);
128+
}
129+
130+
#[test]
131+
fn test_unused_port() {
132+
let port = unused_port();
133+
assert!(port > 0);
134+
}
135+
136+
#[test]
137+
fn test_run_with_tempdir_sync() {
138+
run_with_tempdir_sync("test_prefix", |path| {
139+
assert!(path.exists(), "Temporary directory should exist");
140+
assert!(path.is_dir(), "Temporary directory should be a directory");
141+
});
142+
}
143+
144+
#[tokio::test]
145+
async fn test_run_with_tempdir_async() {
146+
run_with_tempdir("test_prefix", |path| async move {
147+
assert!(path.exists(), "Temporary directory should exist");
148+
assert!(path.is_dir(), "Temporary directory should be a directory");
149+
})
150+
.await;
151+
}
112152
}

0 commit comments

Comments
 (0)