Skip to content

Commit

Permalink
Better missing file errors (#451)
Browse files Browse the repository at this point in the history
  • Loading branch information
max-sixty committed Mar 3, 2024
1 parent efa031c commit 1fc3983
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 9 deletions.
16 changes: 11 additions & 5 deletions cargo-insta/src/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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 => {}
}
Expand Down
12 changes: 9 additions & 3 deletions src/content/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion src/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ impl PendingInlineSnapshot {

#[cfg(feature = "_cargo_insta_internal")]
pub fn load_batch(p: &Path) -> Result<Vec<PendingInlineSnapshot>, Box<dyn Error>> {
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<Self> = contents
.lines()
Expand Down

0 comments on commit 1fc3983

Please sign in to comment.