From 48c4b57c972e311dfc87cdf1bf01cdb4d4868901 Mon Sep 17 00:00:00 2001 From: Ben Wiederhake Date: Fri, 23 Feb 2024 02:59:32 +0100 Subject: [PATCH 1/2] tr: require second string argument when deleting and squeezing --- src/uu/tr/src/tr.rs | 10 ++++++++++ tests/by-util/test_tr.rs | 9 +++++++++ 2 files changed, 19 insertions(+) diff --git a/src/uu/tr/src/tr.rs b/src/uu/tr/src/tr.rs index 47747abac..cbf32c43e 100644 --- a/src/uu/tr/src/tr.rs +++ b/src/uu/tr/src/tr.rs @@ -64,6 +64,16 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { )); } + if delete_flag & squeeze_flag && sets_len < 2 { + return Err(UUsageError::new( + 1, + format!( + "missing operand after {}\nTwo strings must be given when deleting and squeezing.", + sets[0].quote() + ), + )); + } + if sets_len > 1 { let start = "extra operand"; if delete_flag && !squeeze_flag { diff --git a/tests/by-util/test_tr.rs b/tests/by-util/test_tr.rs index f1601c15b..e61a51650 100644 --- a/tests/by-util/test_tr.rs +++ b/tests/by-util/test_tr.rs @@ -163,6 +163,15 @@ fn test_translate_and_squeeze_multiple_lines() { .stdout_is("yaay\nyaay"); // spell-checker:disable-line } +#[test] +fn test_delete_and_squeeze_one_set() { + new_ucmd!() + .args(&["-ds", "a-z"]) + .fails() + .stderr_contains("missing operand after 'a-z'") + .stderr_contains("Two strings must be given when deleting and squeezing."); +} + #[test] fn test_delete_and_squeeze() { new_ucmd!() From 617f3a8b6f1c343c97560a70cd081ea2924822b3 Mon Sep 17 00:00:00 2001 From: Ben Wiederhake Date: Fri, 23 Feb 2024 03:12:30 +0100 Subject: [PATCH 2/2] tr: when deleting and squeezing, never complement set2 --- src/uu/tr/src/tr.rs | 2 +- tests/by-util/test_tr.rs | 11 ++++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/uu/tr/src/tr.rs b/src/uu/tr/src/tr.rs index cbf32c43e..57acb7fac 100644 --- a/src/uu/tr/src/tr.rs +++ b/src/uu/tr/src/tr.rs @@ -127,7 +127,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { } { let mut squeeze_reader = BufReader::new(delete_buffer.as_bytes()); - let op = SqueezeOperation::new(set2, complement_flag); + let op = SqueezeOperation::new(set2, false); translate_input(&mut squeeze_reader, &mut buffered_stdout, op); } } else { diff --git a/tests/by-util/test_tr.rs b/tests/by-util/test_tr.rs index e61a51650..bf589a5c5 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 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 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 use crate::common::util::TestScenario; #[test] @@ -190,6 +190,15 @@ fn test_delete_and_squeeze_complement() { .stdout_is("abc"); } +#[test] +fn test_delete_and_squeeze_complement_squeeze_set2() { + new_ucmd!() + .args(&["-dsc", "abX", "XYZ"]) + .pipe_in("abbbcdddXXXYYY") + .succeeds() + .stdout_is("abbbX"); +} + #[test] fn test_set1_longer_than_set2() { new_ucmd!()