From e54c9bebe2fdde7bed248d91a74eb7a239f590f7 Mon Sep 17 00:00:00 2001 From: Ben Wiederhake Date: Thu, 15 Feb 2024 23:09:44 +0100 Subject: [PATCH] shuf: Treat empty file as zero elements instead of one emptystring --- src/uu/shuf/src/shuf.rs | 7 +++++++ tests/by-util/test_shuf.rs | 7 +++++++ 2 files changed, 14 insertions(+) diff --git a/src/uu/shuf/src/shuf.rs b/src/uu/shuf/src/shuf.rs index de302435cb6..d7ce8049d6b 100644 --- a/src/uu/shuf/src/shuf.rs +++ b/src/uu/shuf/src/shuf.rs @@ -195,6 +195,13 @@ fn read_input_file(filename: &str) -> UResult> { } fn find_seps(data: &mut Vec<&[u8]>, sep: u8) { + // Special case: If data is empty (and does not even contain a single 'sep' + // to indicate the presence of the empty element), then behave as if the input contained no elements at all. + if data.len() == 1 && data[0].is_empty() { + data.clear(); + return; + } + // need to use for loop so we don't borrow the vector as we modify it in place // basic idea: // * We don't care about the order of the result. This lets us slice the slices diff --git a/tests/by-util/test_shuf.rs b/tests/by-util/test_shuf.rs index 13df0fa483f..c506bc51a7f 100644 --- a/tests/by-util/test_shuf.rs +++ b/tests/by-util/test_shuf.rs @@ -48,6 +48,13 @@ fn test_zero_termination() { assert_eq!(result_seq, input_seq, "Output is not a permutation"); } +#[test] +fn test_empty_input() { + let result = new_ucmd!().pipe_in(vec![]).succeeds(); + result.no_stderr(); + result.no_stdout(); +} + #[test] fn test_echo() { let input_seq = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10];