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
This commit is contained in:
Jalil David Salamé Messina 2024-05-04 15:26:47 +02:00 committed by Ben Wiederhake
parent 3c47f27698
commit ff1a03c284
2 changed files with 36 additions and 2 deletions

View file

@ -111,11 +111,15 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let locked_stdout = stdout.lock(); let locked_stdout = stdout.lock();
let mut buffered_stdout = BufWriter::new(locked_stdout); 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 mut sets_iter = sets.iter().map(|c| c.as_str());
let (set1, set2) = Sequence::solve_set_characters( let (set1, set2) = Sequence::solve_set_characters(
sets_iter.next().unwrap_or_default().as_bytes(), sets_iter.next().unwrap_or_default().as_bytes(),
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. // '*_op' are the operations that need to be applied, in order.

View file

@ -2,7 +2,7 @@
// //
// For the full copyright and license information, please view the LICENSE // For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code. // 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; use crate::common::util::TestScenario;
#[test] #[test]
@ -1334,3 +1334,33 @@ fn check_regression_issue_6163_match() {
.no_stderr() .no_stderr()
.stdout_only("Z\n"); .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");
}