@@ -59,13 +59,9 @@ pub(crate) fn extract_value<'a>(key: &str, line: &'a str) -> Option<&'a str> {
59
59
60
60
/// Extracts the endpoint from the given line.
61
61
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 ( ) )
69
65
}
70
66
71
67
/// Runs the given closure with a temporary directory.
@@ -90,23 +86,67 @@ where
90
86
temp_dir. close ( ) . unwrap ( ) ;
91
87
}
92
88
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 ;
98
93
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
+ }
107
111
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
+ }
112
152
}
0 commit comments