Skip to content

Commit c947a81

Browse files
committed
sort: write empty file when input is empty (#11958)
* adds implementations for this behavior in both threaded and wasi * adds test for this behavior
1 parent 52e1bc1 commit c947a81

3 files changed

Lines changed: 17 additions & 1 deletion

File tree

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,8 @@ fn reader_writer<
175175
}
176176
}
177177
ReadResult::EmptyInput => {
178-
// don't output anything
178+
// output empty too, as coreutils does
179+
print_sorted(vec![].iter(), settings, output)?;
179180
}
180181
}
181182
Ok(())

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ pub fn ext_sort(
3434
file?.read_to_end(&mut input)?;
3535
}
3636
if input.is_empty() {
37+
// empty files are sorted to empty like in coreutils
38+
print_sorted(chunk.lines().iter(), settings, output)?;
3739
return Ok(());
3840
}
3941
let mut chunk = Chunk::try_new(input, |buffer| {

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)