Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/uu/sort/src/merge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ use std::{
};

use compare::Compare;
use uucore::display::Quotable;
use uucore::error::{FromIo, UResult};
use uucore::translate;

use crate::{
GlobalSettings, Output, SortError,
Expand Down Expand Up @@ -304,8 +306,14 @@ struct FileMerger<'a> {
impl FileMerger<'_> {
/// Write the merged contents to the output file.
fn write_all(self, settings: &GlobalSettings, output: Output) -> UResult<()> {
let output_name = output
.as_output_name()
.unwrap_or(OsStr::new("standard output"))
.to_owned();
let ctx = || translate!("sort-error-write-failed", "output" => output_name.maybe_quote());
let mut out = output.into_write();
self.write_all_to(settings, &mut out)
self.write_all_to(settings, &mut out)?;
out.flush().map_err_context(ctx)
}

fn write_all_to(mut self, settings: &GlobalSettings, out: &mut impl Write) -> UResult<()> {
Expand Down
16 changes: 16 additions & 0 deletions tests/by-util/test_sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1182,6 +1182,22 @@ fn test_merge_write_error_does_not_panic() {
}
}

#[test]
#[cfg(target_os = "linux")]
fn test_merge_flush_error_is_reported() {
use std::fs::File;

let ts = TestScenario::new("sort");
ts.fixtures.write("input.txt", "line\n");

let dev_full = File::create("/dev/full").expect("Failed to open /dev/full");
ts.ucmd()
.args(&["-m", "input.txt"])
.set_stdout(dev_full)
.fails()
.stderr_contains("No space left on device");
}

#[test]
fn test_merge_unique() {
new_ucmd!()
Expand Down
Loading