use lsofrs::cli::Args;
use std::process::Command;
fn lsofrs_bin() -> Command {
Command::new(env!("CARGO_BIN_EXE_lsofrs"))
}
#[test]
#[allow(non_snake_case)]
fn test_field_output_some_when_dash_F_supplied_none_otherwise() {
let args_none = Args::parse_from(["lsofrs"]);
assert!(
args_none.field_output.is_none(),
"field_output must be None without -F"
);
let args_some = Args::parse_from(["lsofrs", "-F", "pcn"]);
assert_eq!(
args_some.field_output.as_deref(),
Some("pcn"),
"field_output must capture the raw -F arg verbatim"
);
}
#[test]
fn test_field_output_preserves_arbitrary_char_order_no_canonicalisation() {
let args = Args::parse_from(["lsofrs", "-F", "ntpcfu"]);
assert_eq!(
args.field_output.as_deref(),
Some("ntpcfu"),
"-F arg must be preserved verbatim, no reordering at the Args layer"
);
}
#[test]
#[allow(non_snake_case)]
fn test_dash_P_and_dash_n_are_independent_booleans() {
let p_only = Args::parse_from(["lsofrs", "-P"]);
assert!(p_only.no_port_lookup, "-P must set no_port_lookup");
assert!(
!p_only.no_host_lookup,
"-P alone must NOT set no_host_lookup"
);
let n_only = Args::parse_from(["lsofrs", "-n"]);
assert!(n_only.no_host_lookup, "-n must set no_host_lookup");
assert!(
!n_only.no_port_lookup,
"-n alone must NOT set no_port_lookup"
);
let both = Args::parse_from(["lsofrs", "-n", "-P"]);
assert!(
both.no_host_lookup && both.no_port_lookup,
"-n -P sets both"
);
}
#[test]
fn test_field_output_p_only_lines_start_with_p_marker() {
let pid = std::process::id().to_string();
let out = lsofrs_bin()
.args(["-F", "p", "-p", &pid])
.output()
.expect("spawn lsofrs -F p");
assert!(
out.status.success(),
"-F p must exit 0; stderr: {}",
String::from_utf8_lossy(&out.stderr)
);
let stdout = String::from_utf8_lossy(&out.stdout).into_owned();
let nonblank: Vec<&str> = stdout.lines().filter(|l| !l.is_empty()).collect();
assert!(
!nonblank.is_empty(),
"-F p -p <self> must produce at least one record line"
);
for line in &nonblank {
assert!(
line.starts_with('p'),
"-F p produced a line not starting with 'p' marker: {line:?}"
);
for unwanted in ['c', 'u', 'f', 'n', 't'] {
assert!(
!line.starts_with(unwanted),
"-F p produced a line with stray {unwanted:?} marker: {line:?}"
);
}
}
}
#[test]
fn test_field_output_t_only_lines_start_with_t_marker() {
let pid = std::process::id().to_string();
let out = lsofrs_bin()
.args(["-F", "t", "-p", &pid])
.output()
.expect("spawn lsofrs -F t");
assert!(
out.status.success(),
"-F t must exit 0; stderr: {}",
String::from_utf8_lossy(&out.stderr)
);
let stdout = String::from_utf8_lossy(&out.stdout).into_owned();
let nonblank: Vec<&str> = stdout.lines().filter(|l| !l.is_empty()).collect();
assert!(
!nonblank.is_empty(),
"-F t -p <self> must produce at least one type record"
);
for line in &nonblank {
assert!(
line.starts_with('t'),
"-F t produced a line not starting with 't' marker: {line:?}"
);
}
}
#[test]
#[allow(non_snake_case)]
fn test_dash_P_standalone_with_p_self_exits_zero() {
let pid = std::process::id().to_string();
let out = lsofrs_bin()
.args(["-P", "-p", &pid])
.output()
.expect("spawn lsofrs -P -p <self>");
assert!(
out.status.success(),
"-P -p <self> must exit 0; stderr: {}",
String::from_utf8_lossy(&out.stderr)
);
}