Skip to content

Commit

Permalink
feat: better error when no record found
Browse files Browse the repository at this point in the history
  • Loading branch information
MilesCranmer committed Apr 15, 2024
1 parent 18644b3 commit 2dcc4af
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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(());
Expand Down
11 changes: 7 additions & 4 deletions src/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Item = PathBuf> + 'a {
let record_file = self.open().unwrap();
BufReader::new(record_file)
pub fn seance<'a>(
&'a self,
gravepath: &'a PathBuf,
) -> io::Result<impl Iterator<Item = PathBuf> + '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
Expand Down
17 changes: 17 additions & 0 deletions tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}

0 comments on commit 2dcc4af

Please sign in to comment.