From ff1a03c284e14acc18186d3761ab26e17b0b0224 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jalil=20David=20Salam=C3=A9=20Messina?= Date: Sat, 4 May 2024 15:26:47 +0200 Subject: [PATCH] tr: don't truncate when not translating An additional issue was found while reviewing #6340, check [this thread][1]. A summary is: - `tr` ignores the `-t`/`--truncate-set1` flag when not translating - Not translating is defined as `-d` was passed, or one set was passed. [1]: https://github.com/uutils/coreutils/pull/6340#discussion_r1590007053 --- src/uu/tr/src/tr.rs | 6 +++++- tests/by-util/test_tr.rs | 32 +++++++++++++++++++++++++++++++- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/uu/tr/src/tr.rs b/src/uu/tr/src/tr.rs index 4a182e555..4d9322829 100644 --- a/src/uu/tr/src/tr.rs +++ b/src/uu/tr/src/tr.rs @@ -111,11 +111,15 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { let locked_stdout = stdout.lock(); let mut buffered_stdout = BufWriter::new(locked_stdout); + // According to the man page: translating only happens if deleting or if a second set is given + let translating = !delete_flag && sets.len() > 1; let mut sets_iter = sets.iter().map(|c| c.as_str()); let (set1, set2) = Sequence::solve_set_characters( sets_iter.next().unwrap_or_default().as_bytes(), sets_iter.next().unwrap_or_default().as_bytes(), - truncate_set1_flag, + complement_flag, + // if we are not translating then we don't truncate set1 + truncate_set1_flag && translating, )?; // '*_op' are the operations that need to be applied, in order. diff --git a/tests/by-util/test_tr.rs b/tests/by-util/test_tr.rs index 77e69112e..21c5c9bb4 100644 --- a/tests/by-util/test_tr.rs +++ b/tests/by-util/test_tr.rs @@ -2,7 +2,7 @@ // // For the full copyright and license information, please view the LICENSE // file that was distributed with this source code. -// spell-checker:ignore aabbaa aabbcc aabc abbb abbbcddd abcc abcdefabcdef abcdefghijk abcdefghijklmn abcdefghijklmnop ABCDEFGHIJKLMNOPQRS abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ ABCDEFZZ abcxyz ABCXYZ abcxyzabcxyz ABCXYZABCXYZ acbdef alnum amzamz AMZXAMZ bbbd cclass cefgm cntrl compl dabcdef dncase Gzabcdefg PQRST upcase wxyzz xdigit XXXYYY xycde xyyye xyyz xyzzzzxyzzzz ZABCDEF Zamz Cdefghijkl Cdefghijklmn +// spell-checker:ignore aabbaa aabbcc aabc abbb abbbcddd abcc abcdefabcdef abcdefghijk abcdefghijklmn abcdefghijklmnop ABCDEFGHIJKLMNOPQRS abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ ABCDEFZZ abcxyz ABCXYZ abcxyzabcxyz ABCXYZABCXYZ acbdef alnum amzamz AMZXAMZ bbbd cclass cefgm cntrl compl dabcdef dncase Gzabcdefg PQRST upcase wxyzz xdigit XXXYYY xycde xyyye xyyz xyzzzzxyzzzz ZABCDEF Zamz Cdefghijkl Cdefghijklmn asdfqqwweerr qwerr asdfqwer qwer aassddffqwer asdfqwer use crate::common::util::TestScenario; #[test] @@ -1334,3 +1334,33 @@ fn check_regression_issue_6163_match() { .no_stderr() .stdout_only("Z\n"); } + +#[test] +fn check_ignore_truncate_when_deleting_and_squeezing() { + new_ucmd!() + .args(&["-dts", "asdf", "qwe"]) + .pipe_in("asdfqqwweerr\n") + .succeeds() + .no_stderr() + .stdout_only("qwerr\n"); +} + +#[test] +fn check_ignore_truncate_when_deleting() { + new_ucmd!() + .args(&["-dt", "asdf"]) + .pipe_in("asdfqwer\n") + .succeeds() + .no_stderr() + .stdout_only("qwer\n"); +} + +#[test] +fn check_ignore_truncate_when_squeezing() { + new_ucmd!() + .args(&["-ts", "asdf"]) + .pipe_in("aassddffqwer\n") + .succeeds() + .no_stderr() + .stdout_only("asdfqwer\n"); +}