diff --git a/src/uu/comm/src/comm.rs b/src/uu/comm/src/comm.rs index 56af42fd9..2f800da8a 100644 --- a/src/uu/comm/src/comm.rs +++ b/src/uu/comm/src/comm.rs @@ -11,6 +11,8 @@ use std::cmp::Ordering; use std::fs::File; use std::io::{self, stdin, BufRead, BufReader, Stdin}; use std::path::Path; +use uucore::error::FromIo; +use uucore::error::UResult; use uucore::InvalidEncodingHandling; use clap::{crate_version, App, Arg, ArgMatches}; @@ -128,20 +130,21 @@ fn open_file(name: &str) -> io::Result { } } -pub fn uumain(args: impl uucore::Args) -> i32 { +#[uucore_procs::gen_uumain] +pub fn uumain(args: impl uucore::Args) -> UResult<()> { let usage = usage(); let args = args .collect_str(InvalidEncodingHandling::ConvertLossy) .accept_any(); let matches = uu_app().usage(&usage[..]).get_matches_from(args); - - let mut f1 = open_file(matches.value_of(options::FILE_1).unwrap()).unwrap(); - let mut f2 = open_file(matches.value_of(options::FILE_2).unwrap()).unwrap(); + let filename1 = matches.value_of(options::FILE_1).unwrap(); + let filename2 = matches.value_of(options::FILE_2).unwrap(); + let mut f1 = open_file(filename1).map_err_context(|| filename1.to_string())?; + let mut f2 = open_file(filename2).map_err_context(|| filename2.to_string())?; comm(&mut f1, &mut f2, &matches); - - 0 + Ok(()) } pub fn uu_app() -> App<'static, 'static> { diff --git a/tests/by-util/test_comm.rs b/tests/by-util/test_comm.rs index 01cdcc985..d5b72b1e9 100644 --- a/tests/by-util/test_comm.rs +++ b/tests/by-util/test_comm.rs @@ -170,3 +170,11 @@ fn no_arguments() { fn one_argument() { new_ucmd!().arg("a").fails().no_stdout().no_stderr(); } + +#[test] +fn test_no_such_file() { + new_ucmd!() + .args(&["bogus_file_1", "bogus_file_2"]) + .fails() + .stderr_only("comm: bogus_file_1: No such file or directory"); +}