From 1fc3983fbcf400c018bd633149d1615ae0fbe5e4 Mon Sep 17 00:00:00 2001 From: Maximilian Roos <5635139+max-sixty@users.noreply.github.com> Date: Sun, 3 Mar 2024 08:47:13 -0800 Subject: [PATCH] Better missing file errors (#451) --- cargo-insta/src/container.rs | 16 +++++++++++----- src/content/mod.rs | 12 +++++++++--- src/lib.rs | 1 + src/snapshot.rs | 3 ++- 4 files changed, 23 insertions(+), 9 deletions(-) diff --git a/cargo-insta/src/container.rs b/cargo-insta/src/container.rs index 83c8fc5..95fc20d 100644 --- a/cargo-insta/src/container.rs +++ b/cargo-insta/src/container.rs @@ -3,7 +3,7 @@ use std::fs; use std::path::{Path, PathBuf}; use insta::Snapshot; -use insta::_cargo_insta_support::PendingInlineSnapshot; +use insta::_cargo_insta_support::{ContentError, PendingInlineSnapshot}; use crate::inline::FilePatcher; @@ -113,7 +113,8 @@ impl SnapshotContainer { // The runtime code will issue something like this: // PendingInlineSnapshot::new(None, None, line).save(pending_snapshots)?; if !have_new { - fs::remove_file(&snapshot_path)?; + fs::remove_file(&snapshot_path) + .map_err(|e| ContentError::FileIo(e, snapshot_path.to_path_buf()))?; } rv @@ -192,7 +193,8 @@ impl SnapshotContainer { if did_skip { PendingInlineSnapshot::save_batch(&self.snapshot_path, &new_pending)?; } else { - fs::remove_file(&self.snapshot_path)?; + fs::remove_file(&self.snapshot_path) + .map_err(|e| ContentError::FileIo(e, self.snapshot_path.to_path_buf()))?; } } else { // should only be one or this is weird @@ -201,10 +203,14 @@ impl SnapshotContainer { Operation::Accept => { let snapshot = Snapshot::from_file(&self.snapshot_path)?; snapshot.save(&self.target_path)?; - fs::remove_file(&self.snapshot_path)?; + fs::remove_file(&self.snapshot_path).map_err(|e| { + ContentError::FileIo(e, self.snapshot_path.to_path_buf()) + })?; } Operation::Reject => { - fs::remove_file(&self.snapshot_path)?; + fs::remove_file(&self.snapshot_path).map_err(|e| { + ContentError::FileIo(e, self.snapshot_path.to_path_buf()) + })?; } Operation::Skip => {} } diff --git a/src/content/mod.rs b/src/content/mod.rs index ed7bf60..8fe5e14 100644 --- a/src/content/mod.rs +++ b/src/content/mod.rs @@ -24,19 +24,25 @@ pub enum Error { UnexpectedDataType, #[cfg(feature = "_cargo_insta_internal")] MissingField, + #[cfg(feature = "_cargo_insta_internal")] + FileIo(std::io::Error, std::path::PathBuf), } impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { - Self::FailedParsingYaml(p) => { + Error::FailedParsingYaml(p) => { f.write_str(format!("Failed parsing the YAML from {:?}", p.display()).as_str()) } - Self::UnexpectedDataType => { + Error::UnexpectedDataType => { f.write_str("The present data type wasn't what was expected") } #[cfg(feature = "_cargo_insta_internal")] - Self::MissingField => f.write_str("A required field was missing"), + Error::MissingField => f.write_str("A required field was missing"), + #[cfg(feature = "_cargo_insta_internal")] + Error::FileIo(e, p) => { + f.write_str(format!("File error for {:?}: {}", p.display(), e).as_str()) + } } } } diff --git a/src/lib.rs b/src/lib.rs index 602ab72..ebbd6b3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -293,6 +293,7 @@ pub mod internals { #[cfg(feature = "_cargo_insta_internal")] pub mod _cargo_insta_support { pub use crate::{ + content::Error as ContentError, env::{ Error as ToolConfigError, OutputBehavior, SnapshotUpdate, TestRunner, ToolConfig, UnreferencedSnapshots, diff --git a/src/snapshot.rs b/src/snapshot.rs index 59c7d86..f14df10 100644 --- a/src/snapshot.rs +++ b/src/snapshot.rs @@ -39,7 +39,8 @@ impl PendingInlineSnapshot { #[cfg(feature = "_cargo_insta_internal")] pub fn load_batch(p: &Path) -> Result, Box> { - let contents = fs::read_to_string(p)?; + let contents = + fs::read_to_string(p).map_err(|e| content::Error::FileIo(e, p.to_path_buf()))?; let mut rv: Vec = contents .lines()