Skip to content

Commit

Permalink
Merge pull request #6011 from BenWiederhake/dev-shuf-repeat-zero-items
Browse files Browse the repository at this point in the history
shuf: Refuse repeating zero lines
  • Loading branch information
cakebaker authored Feb 27, 2024
2 parents ae7d03c + 6834b59 commit aa0d15d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
7 changes: 3 additions & 4 deletions src/uu/shuf/src/shuf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
29 changes: 29 additions & 0 deletions tests/by-util/test_shuf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

0 comments on commit aa0d15d

Please sign in to comment.