diff --git a/cargo-insta/src/container.rs b/cargo-insta/src/container.rs index 83c8fc5c..95fc20d6 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 ed7bf603..dcc23096 100644 --- a/src/content/mod.rs +++ b/src/content/mod.rs @@ -24,19 +24,24 @@ 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 { + use Error::*; match self { - Self::FailedParsingYaml(p) => { + FailedParsingYaml(p) => { f.write_str(format!("Failed parsing the YAML from {:?}", p.display()).as_str()) } - Self::UnexpectedDataType => { - f.write_str("The present data type wasn't what was expected") - } + 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"), + MissingField => f.write_str("A required field was missing"), + #[cfg(feature = "_cargo_insta_internal")] + 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 602ab721..ebbd6b3a 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 59c7d869..806fad58 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| crate::content::Error::FileIo(e, p.to_path_buf()))?; let mut rv: Vec = contents .lines()