mirror of
https://github.com/theryangeary/choose
synced 2024-11-23 03:13:04 +00:00
Handle failure to open input file
This commit is contained in:
parent
dfe4c2cad0
commit
41e060680e
2 changed files with 20 additions and 1 deletions
10
src/main.rs
10
src/main.rs
|
@ -1,5 +1,6 @@
|
|||
use std::fs::File;
|
||||
use std::io::{self, Read, Write};
|
||||
use std::process;
|
||||
use structopt::StructOpt;
|
||||
|
||||
#[macro_use]
|
||||
|
@ -17,7 +18,14 @@ fn main() {
|
|||
let config = Config::new(opt);
|
||||
|
||||
let read = match &config.opt.input {
|
||||
Some(f) => Box::new(File::open(f).expect("Could not open file")) as Box<dyn Read>,
|
||||
Some(f) => match File::open(f) {
|
||||
Ok(fh) => Box::new(fh) as Box<dyn Read>,
|
||||
Err(e) => {
|
||||
eprintln!("Failed to open file: {}", e);
|
||||
// exit code of 3 means failure to open input file
|
||||
process::exit(3);
|
||||
}
|
||||
},
|
||||
None => Box::new(io::stdin()) as Box<dyn Read>,
|
||||
};
|
||||
|
||||
|
|
|
@ -33,6 +33,17 @@ if [ $r -ne 2 ]; then
|
|||
exit 1
|
||||
fi
|
||||
|
||||
file=/tmp/000_file
|
||||
touch $file
|
||||
chmod 000 $file
|
||||
cargo run -- 3 -i $file >&/dev/null
|
||||
r=$?
|
||||
if [ $r -ne 3 ]; then
|
||||
echo "Failed to return 3 on file open error"
|
||||
exit 1
|
||||
fi
|
||||
rm -f $file
|
||||
|
||||
cd $orig_dir
|
||||
|
||||
printf "\033[1;32mAll tests passed\033[0m\n"
|
||||
|
|
Loading…
Reference in a new issue