From 2dcc4af2babe4cd2df3dcea2b75169e603fabeba Mon Sep 17 00:00:00 2001 From: MilesCranmer Date: Mon, 15 Apr 2024 13:23:06 +0100 Subject: [PATCH] feat: better error when no record found --- src/lib.rs | 4 ++-- src/record.rs | 11 +++++++---- tests/integration_tests.rs | 17 +++++++++++++++++ 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 438e0ff..444ddab 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -69,7 +69,7 @@ pub fn run(cli: Args, mode: impl util::TestingMode, stream: &mut impl Write) -> // the graves_to_exhume. if cli.seance && record.open().is_ok() { let gravepath = util::join_absolute(graveyard, dunce::canonicalize(cwd)?); - for grave in record.seance(&gravepath) { + for grave in record.seance(&gravepath)? { graves_to_exhume.push(grave); } } @@ -112,7 +112,7 @@ pub fn run(cli: Args, mode: impl util::TestingMode, stream: &mut impl Write) -> if cli.seance { let gravepath = util::join_absolute(graveyard, dunce::canonicalize(cwd)?); - for grave in record.seance(&gravepath) { + for grave in record.seance(&gravepath)? { writeln!(stream, "{}", grave.display())?; } return Ok(()); diff --git a/src/record.rs b/src/record.rs index 8664476..df86664 100644 --- a/src/record.rs +++ b/src/record.rs @@ -123,13 +123,16 @@ impl Record { } /// Returns an iterator over all graves in the record that are under gravepath - pub fn seance<'a>(&'a self, gravepath: &'a PathBuf) -> impl Iterator + 'a { - let record_file = self.open().unwrap(); - BufReader::new(record_file) + pub fn seance<'a>( + &'a self, + gravepath: &'a PathBuf, + ) -> io::Result + 'a> { + let record_file = self.open()?; + Ok(BufReader::new(record_file) .lines() .map_while(Result::ok) .map(|line| PathBuf::from(RecordItem::new(&line).dest)) - .filter(move |d| d.starts_with(gravepath)) + .filter(move |d| d.starts_with(gravepath))) } /// Write deletion history to record diff --git a/tests/integration_tests.rs b/tests/integration_tests.rs index 69cc21f..1dad390 100644 --- a/tests/integration_tests.rs +++ b/tests/integration_tests.rs @@ -699,3 +699,20 @@ fn test_graveyard_subcommand(#[values(false, true)] seance: bool) { .success() .stdout(expected_str); } + +#[rstest] +fn read_empty_record() { + let _env_lock = aquire_lock(); + let test_env = TestEnv::new(); + let cwd = env::current_dir().unwrap(); + let record = record::Record::new(&test_env.graveyard); + let gravepath = &util::join_absolute(&test_env.graveyard, dunce::canonicalize(cwd).unwrap()); + let result = record.seance(gravepath); + assert!(result.is_err()); + if let Err(e) = result { + assert_eq!(e.kind(), ErrorKind::NotFound); + assert_eq!(e.to_string(), "Failed to read record!"); + } else { + panic!("Expected an error"); + } +}