From 124d7870459d4ede0510a2a5b519f3ba15acf339 Mon Sep 17 00:00:00 2001 From: Misakait Date: Sat, 4 Oct 2025 17:30:52 +0800 Subject: [PATCH 1/2] fix(ptx): Correct reference format for stdin --- src/uu/ptx/src/ptx.rs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/uu/ptx/src/ptx.rs b/src/uu/ptx/src/ptx.rs index f3afc58df..111d74d39 100644 --- a/src/uu/ptx/src/ptx.rs +++ b/src/uu/ptx/src/ptx.rs @@ -352,11 +352,15 @@ fn create_word_set(config: &Config, filter: &WordFilter, file_map: &FileMap) -> fn get_reference(config: &Config, word_ref: &WordRef, line: &str, context_reg: &Regex) -> String { if config.auto_ref { - format!( - "{}:{}", - word_ref.filename.maybe_quote(), - word_ref.local_line_nr + 1 - ) + if word_ref.filename == "-" { + format!(":{}", word_ref.local_line_nr + 1) + } else { + format!( + "{}:{}", + word_ref.filename.maybe_quote(), + word_ref.local_line_nr + 1 + ) + } } else if config.input_ref { let (beg, end) = match context_reg.find(line) { Some(x) => (x.start(), x.end()), From e131d31486f982864307da92c48968a320ccabce Mon Sep 17 00:00:00 2001 From: Misakait Date: Sat, 4 Oct 2025 17:44:11 +0800 Subject: [PATCH 2/2] test(ptx): Add regression test for stdin reference format --- tests/by-util/test_ptx.rs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/tests/by-util/test_ptx.rs b/tests/by-util/test_ptx.rs index 7fd40db76..917cd047a 100644 --- a/tests/by-util/test_ptx.rs +++ b/tests/by-util/test_ptx.rs @@ -10,7 +10,25 @@ use uutests::new_ucmd; fn test_invalid_arg() { new_ucmd!().arg("--definitely-invalid").fails_with_code(1); } - +#[test] +fn test_reference_format_for_stdin() { + let input = "Rust is good language"; + let expected_output = concat!( + r#".xx "" "" "Rust is good language" "" ":1""#, + "\n", + r#".xx "" "Rust is" "good language" "" ":1""#, + "\n", + r#".xx "" "Rust" "is good language" "" ":1""#, + "\n", + r#".xx "" "Rust is good" "language" "" ":1""#, + "\n", + ); + new_ucmd!() + .args(&["-G", "-A"]) + .pipe_in(input) + .succeeds() + .stdout_only(expected_output); +} #[test] fn test_tex_format_no_truncation_markers() { let input = "Hello world Rust is a fun language";