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.
This commit is contained in:
Nils Andreas Svee committed 2026-04-28 23:16:22 +07:00
1 parent 729d884047
commit 13c32cd753
1 file changed
+25 -16
+25 -16
View File
@@ -2,13 +2,13 @@
use std::{ use std::{
fmt::Write as _, fmt::Write as _,
net::{IpAddr, Ipv4Addr, SocketAddr}, net::SocketAddr,
time::{Duration, Instant, SystemTime, UNIX_EPOCH}, time::{Duration, SystemTime, UNIX_EPOCH},
}; };
use clap::{Parser, ValueEnum}; use clap::{Parser, ValueEnum};
use irtt_client::{ use irtt_client::{
ClientConfig, ClientEvent, ClientTimestamp, NegotiatedParams, OneWayDelaySample, PacketMeta, ClientConfig, ClientEvent, NegotiatedParams, OneWayDelaySample, PacketMeta,
ReceivedStatsSample, RttSample, ServerTiming, SocketConfig, WarningKind, ReceivedStatsSample, RttSample, ServerTiming, SocketConfig, WarningKind,
}; };
use irtt_proto::{Clock, ReceivedStats, StampAt}; use irtt_proto::{Clock, ReceivedStats, StampAt};
@@ -471,26 +471,35 @@ fn warning_kind(kind: WarningKind) -> &'static str {
} }
fn escape_value(value: &str) -> String { fn escape_value(value: &str) -> String {
value.replace('\\', "\\\\").replace(' ', "\\s") value
} .replace('\\', "\\\\")
.replace(' ', "\\s")
pub fn test_timestamp(offset: Duration) -> ClientTimestamp { .replace('\n', "\\n")
ClientTimestamp { .replace('\r', "\\r")
wall: UNIX_EPOCH + offset, .replace('\t', "\\t")
mono: Instant::now() + offset,
}
}
pub fn test_remote() -> SocketAddr {
SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 2112)
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use clap::Parser; use clap::Parser;
use irtt_client::{PacketMeta, RttSample}; use irtt_client::{ClientTimestamp, PacketMeta, RttSample};
use irtt_proto::{Params, PROTOCOL_VERSION}; 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<CliArgs, clap::Error> { fn parse(args: &[&str]) -> Result<CliArgs, clap::Error> {
let mut argv = vec!["irtt-rs"]; let mut argv = vec!["irtt-rs"];