join: flush stdout before final error message

This commit is contained in:
Justin Tracey 2022-02-07 22:08:37 -05:00
parent bf67c5d981
commit b873d46ca0
2 changed files with 17 additions and 21 deletions

View file

@ -28,7 +28,7 @@ static NAME: &str = "join";
#[derive(Debug)]
enum JoinError {
IOError(std::io::Error),
UnorderedInput,
UnorderedInput(String),
}
impl UError for JoinError {
@ -43,7 +43,7 @@ impl Display for JoinError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
JoinError::IOError(e) => write!(f, "io error: {}", e),
JoinError::UnorderedInput => Ok(()),
JoinError::UnorderedInput(e) => f.write_str(e),
}
}
}
@ -538,24 +538,22 @@ impl<'a> State<'a> {
let diff = input.compare(self.get_current_key(), line.get_field(self.key));
if diff == Ordering::Greater {
if input.check_order == CheckOrder::Enabled
|| (self.has_unpaired && !self.has_failed)
{
eprintln!(
"{}: {}:{}: is not sorted: {}",
uucore::execution_phrase(),
self.file_name.maybe_quote(),
self.line_num,
String::from_utf8_lossy(&line.string)
);
self.has_failed = true;
}
if diff == Ordering::Greater
&& (input.check_order == CheckOrder::Enabled
|| (self.has_unpaired && !self.has_failed))
{
let err_msg = format!(
"{}:{}: is not sorted: {}",
self.file_name.maybe_quote(),
self.line_num,
String::from_utf8_lossy(&line.string)
);
// This is fatal if the check is enabled.
if input.check_order == CheckOrder::Enabled {
return Err(JoinError::UnorderedInput);
return Err(JoinError::UnorderedInput(err_msg));
}
eprintln!("{}: {}", uucore::execution_phrase(), err_msg);
self.has_failed = true;
}
Ok(Some(line))

View file

@ -339,8 +339,7 @@ fn wrong_line_order() {
.fails()
.stdout_does_not_contain("7 g f 4 fg")
.stderr_is(&format!(
"{0} {1}: fields_4.txt:5: is not sorted: 11 g 5 gh",
ts.bin_path.to_string_lossy(),
"{0}: fields_4.txt:5: is not sorted: 11 g 5 gh",
ts.util_name
));
}
@ -366,8 +365,7 @@ fn both_files_wrong_line_order() {
.fails()
.stdout_does_not_contain("5 e 3 ef")
.stderr_is(&format!(
"{0} {1}: fields_5.txt:4: is not sorted: 3",
ts.bin_path.to_string_lossy(),
"{0}: fields_5.txt:4: is not sorted: 3",
ts.util_name
));
}