From 8de5943f34e3dd47dc0f387b32406e9a0b92b9e2 Mon Sep 17 00:00:00 2001 From: jasonwitty Date: Sun, 24 Aug 2025 12:15:32 -0700 Subject: [PATCH] test(agent): move inline port parsing test to tests/port_parse.rs --- socktop_agent/src/main.rs | 43 +------------------------------ socktop_agent/tests/port_parse.rs | 40 ++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 42 deletions(-) create mode 100644 socktop_agent/tests/port_parse.rs diff --git a/socktop_agent/src/main.rs b/socktop_agent/src/main.rs index 3cb8cb3..2f82b3f 100644 --- a/socktop_agent/src/main.rs +++ b/socktop_agent/src/main.rs @@ -98,45 +98,4 @@ async fn main() -> anyhow::Result<()> { Ok(()) } -#[cfg(test)] -mod tests_cli_agent { - // Local helper for testing port parsing - fn parse_port>(args: I, default_port: u16) -> u16 { - let mut it = args.into_iter(); - let _ = it.next(); // prog - let mut long: Option = None; - let mut short: Option = None; - while let Some(a) = it.next() { - match a.as_str() { - "--port" => long = it.next(), - "-p" => short = it.next(), - _ if a.starts_with("--port=") => { - if let Some((_, v)) = a.split_once('=') { - long = Some(v.to_string()); - } - } - _ => {} - } - } - long.or(short) - .and_then(|s| s.parse::().ok()) - .unwrap_or(default_port) - } - - #[test] - fn port_long_short_and_assign() { - assert_eq!( - parse_port(vec!["agent".into(), "--port".into(), "9001".into()], 8443), - 9001 - ); - assert_eq!( - parse_port(vec!["agent".into(), "-p".into(), "9002".into()], 8443), - 9002 - ); - assert_eq!( - parse_port(vec!["agent".into(), "--port=9003".into()], 8443), - 9003 - ); - assert_eq!(parse_port(vec!["agent".into()], 8443), 8443); - } -} +// Unit tests for CLI parsing moved to `tests/port_parse.rs`. diff --git a/socktop_agent/tests/port_parse.rs b/socktop_agent/tests/port_parse.rs new file mode 100644 index 0000000..acc28e9 --- /dev/null +++ b/socktop_agent/tests/port_parse.rs @@ -0,0 +1,40 @@ +//! Unit test for port parsing logic moved out of `main.rs`. + +fn parse_port>(args: I, default_port: u16) -> u16 { + let mut it = args.into_iter(); + let _ = it.next(); // program name + let mut long: Option = None; + let mut short: Option = None; + while let Some(a) = it.next() { + match a.as_str() { + "--port" => long = it.next(), + "-p" => short = it.next(), + _ if a.starts_with("--port=") => { + if let Some((_, v)) = a.split_once('=') { + long = Some(v.to_string()); + } + } + _ => {} + } + } + long.or(short) + .and_then(|s| s.parse::().ok()) + .unwrap_or(default_port) +} + +#[test] +fn port_long_short_and_assign() { + assert_eq!( + parse_port(vec!["agent".into(), "--port".into(), "9001".into()], 8443), + 9001 + ); + assert_eq!( + parse_port(vec!["agent".into(), "-p".into(), "9002".into()], 8443), + 9002 + ); + assert_eq!( + parse_port(vec!["agent".into(), "--port=9003".into()], 8443), + 9003 + ); + assert_eq!(parse_port(vec!["agent".into()], 8443), 8443); +}