Skip to content

Commit 6426bc7

Browse files
sort: write empty file when input is empty (#11958)
* adds implementations for this behavior in both threaded and wasi * adds test for this behavior Co-authored-by: Chad Brewbaker <crb002@gmail.com>
1 parent 52e1bc1 commit 6426bc7

3 files changed

Lines changed: 21 additions & 1 deletion

File tree

src/uu/sort/src/ext_sort/threaded.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use std::cmp::Ordering;
1010
use std::fs::File;
1111
use std::io::{Read, Write, stderr};
12+
use std::iter;
1213
use std::path::PathBuf;
1314
use std::sync::mpsc::{Receiver, SyncSender};
1415
use std::thread;
@@ -175,7 +176,8 @@ fn reader_writer<
175176
}
176177
}
177178
ReadResult::EmptyInput => {
178-
// don't output anything
179+
// output empty too, as coreutils does
180+
print_sorted(iter::empty<&[u8]>(), settings, output)?;
179181
}
180182
}
181183
Ok(())

src/uu/sort/src/ext_sort/wasi.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
99
use std::cmp::Ordering;
1010
use std::io::Read;
11+
use std::iter;
1112

1213
use itertools::Itertools;
1314
use uucore::error::UResult;
@@ -33,9 +34,13 @@ pub fn ext_sort(
3334
for file in files {
3435
file?.read_to_end(&mut input)?;
3536
}
37+
3638
if input.is_empty() {
39+
// empty files are sorted to empty like in coreutils
40+
print_sorted(iter::empty<&[u8]>(), settings, output)?;
3741
return Ok(());
3842
}
43+
3944
let mut chunk = Chunk::try_new(input, |buffer| {
4045
Ok::<_, Box<dyn uucore::error::UError>>(chunks::parse_into_chunk(
4146
buffer, separator, settings,

tests/by-util/test_sort.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2960,4 +2960,17 @@ e f 5436 down data path1 path2 path3 path4 path5\n";
29602960
.stdout_is(input);
29612961
}
29622962

2963+
#[test]
2964+
fn test_empty_input_empty_output() {
2965+
// check for inconsistency #11958
2966+
let input = "test test test";
2967+
let (at, mut ucmd) = at_and_ucmd!();
2968+
2969+
at.touch("file");
2970+
at.append("file", input);
2971+
2972+
ucmd.args(&["-o", "file"]).pipe_in("").succeeds();
2973+
assert_eq!(at.read("file"), "");
2974+
}
2975+
29632976
/* spell-checker: enable */

0 commit comments

Comments
 (0)