From 13c32cd753652cc4bbad83d1623c3510013f9806 Mon Sep 17 00:00:00 2001 From: Nils Andreas Svee Date: Tue, 28 Apr 2026 23:16:22 +0700 Subject: [PATCH] refactor(cli): restrict test utilities to test module and expand value escaping Move internal test helpers to the test module to clean up the public API and add support for escaping common whitespace characters in output values. --- crates/irtt-cli/src/lib.rs | 41 +++++++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/crates/irtt-cli/src/lib.rs b/crates/irtt-cli/src/lib.rs index faa5e1b..a6a1025 100644 --- a/crates/irtt-cli/src/lib.rs +++ b/crates/irtt-cli/src/lib.rs @@ -2,13 +2,13 @@ use std::{ fmt::Write as _, - net::{IpAddr, Ipv4Addr, SocketAddr}, - time::{Duration, Instant, SystemTime, UNIX_EPOCH}, + net::SocketAddr, + time::{Duration, SystemTime, UNIX_EPOCH}, }; use clap::{Parser, ValueEnum}; use irtt_client::{ - ClientConfig, ClientEvent, ClientTimestamp, NegotiatedParams, OneWayDelaySample, PacketMeta, + ClientConfig, ClientEvent, NegotiatedParams, OneWayDelaySample, PacketMeta, ReceivedStatsSample, RttSample, ServerTiming, SocketConfig, WarningKind, }; use irtt_proto::{Clock, ReceivedStats, StampAt}; @@ -471,26 +471,35 @@ fn warning_kind(kind: WarningKind) -> &'static str { } fn escape_value(value: &str) -> String { - value.replace('\\', "\\\\").replace(' ', "\\s") -} - -pub fn test_timestamp(offset: Duration) -> ClientTimestamp { - ClientTimestamp { - wall: UNIX_EPOCH + offset, - mono: Instant::now() + offset, - } -} - -pub fn test_remote() -> SocketAddr { - SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 2112) + value + .replace('\\', "\\\\") + .replace(' ', "\\s") + .replace('\n', "\\n") + .replace('\r', "\\r") + .replace('\t', "\\t") } #[cfg(test)] mod tests { use super::*; use clap::Parser; - use irtt_client::{PacketMeta, RttSample}; + use irtt_client::{ClientTimestamp, PacketMeta, RttSample}; use irtt_proto::{Params, PROTOCOL_VERSION}; + use std::{ + net::{IpAddr, Ipv4Addr, SocketAddr}, + time::Instant, + }; + + fn test_timestamp(offset: Duration) -> ClientTimestamp { + ClientTimestamp { + wall: UNIX_EPOCH + offset, + mono: Instant::now() + offset, + } + } + + fn test_remote() -> SocketAddr { + SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 2112) + } fn parse(args: &[&str]) -> Result { let mut argv = vec!["irtt-rs"];