From 6834b593ee08e9286de5ba9e90e4cbb6d67db452 Mon Sep 17 00:00:00 2001 From: Ben Wiederhake Date: Sat, 24 Feb 2024 22:10:22 +0100 Subject: [PATCH] shuf: refuse repeating zero lines This was a GNU behavior bug: ```console $ LC_ALL=C shuf -er shuf: no lines to repeat [$? = 1] $ cargo run shuf -er # old, bad (unexpected success) $ cargo run shuf -er # new shuf: no lines to repeat [$? = 1] ``` --- src/uu/shuf/src/shuf.rs | 7 +++---- tests/by-util/test_shuf.rs | 29 +++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src/uu/shuf/src/shuf.rs b/src/uu/shuf/src/shuf.rs index a5456e1845d..9ee04826b48 100644 --- a/src/uu/shuf/src/shuf.rs +++ b/src/uu/shuf/src/shuf.rs @@ -435,11 +435,10 @@ fn shuf_exec(input: &mut impl Shufable, opts: Options) -> UResult<()> { None => WrappedRng::RngDefault(rand::thread_rng()), }; - if input.is_empty() { - return Ok(()); - } - if opts.repeat { + if input.is_empty() { + return Err(USimpleError::new(1, "no lines to repeat")); + } for _ in 0..opts.head_count { let r = input.choose(&mut rng); diff --git a/tests/by-util/test_shuf.rs b/tests/by-util/test_shuf.rs index 76d9b322070..7b0af7c944c 100644 --- a/tests/by-util/test_shuf.rs +++ b/tests/by-util/test_shuf.rs @@ -624,3 +624,32 @@ fn test_shuf_multiple_input_line_count() { .count(); assert_eq!(result_count, 5, "Output should have 5 items"); } + +#[test] +#[ignore = "known issue"] +fn test_shuf_repeat_empty_range() { + new_ucmd!() + .arg("-ri4-3") + .fails() + .no_stdout() + .stderr_only("shuf: no lines to repeat\n"); +} + +#[test] +fn test_shuf_repeat_empty_echo() { + new_ucmd!() + .arg("-re") + .fails() + .no_stdout() + .stderr_only("shuf: no lines to repeat\n"); +} + +#[test] +fn test_shuf_repeat_empty_input() { + new_ucmd!() + .arg("-r") + .pipe_in("") + .fails() + .no_stdout() + .stderr_only("shuf: no lines to repeat\n"); +}