Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Binary snapshots #489

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
6e66262
Add support for binary snapshots
lasernoises Apr 26, 2024
0f9a08d
Move file_eq function to utils
lasernoises Apr 29, 2024
0f08023
Fix snapshot comparison whitespace handling
lasernoises Apr 30, 2024
f193ca8
Add basic test for binary snapshots
lasernoises May 2, 2024
5f8de35
Add more tests for file extension of binary snapshots
lasernoises May 2, 2024
6b9884a
Fix file extension change behavior for binary snapshots
lasernoises May 8, 2024
51cb04b
Move &mut for binary snapshot closure into macro
lasernoises May 8, 2024
13fea4a
Merge branch 'master' into binary-snapshots
lasernoises May 27, 2024
c4aad15
Use try_removing_snapshot closure for binary deletion
lasernoises May 27, 2024
6ec72dc
Switch to a temporary .snap._ for binary snapshots before saving
lasernoises May 29, 2024
831427a
Fix handling of binary snapshot paths
lasernoises Jun 5, 2024
fdb9eca
Add cleanup for binary snapshots
lasernoises Jun 5, 2024
4865e6d
Change file content to ascii for binary snapshot tests
lasernoises Jun 5, 2024
36b1cc0
Forbid binary snapshot extensions that could cause conflicts
lasernoises Jun 5, 2024
87306da
Reduce binary snapshot code duplication
lasernoises Jun 6, 2024
ebbd335
Make binary snapshot error handling more constistent with the rest of…
lasernoises Jun 7, 2024
edfabf3
Add doc comment for encode_file_link_escape
lasernoises Jun 13, 2024
86a56d7
Merge remote-tracking branch 'upstream/master' into binary-snapshots
lasernoises Sep 16, 2024
a27eed1
Fix some issues after merge
lasernoises Sep 16, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 7 additions & 11 deletions insta/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -724,17 +724,13 @@ pub fn assert_snapshot(
}
});

let pass = ctx
.old_snapshot
.as_ref()
.map(|x| {
if tool_config.require_full_match() {
x.matches_fully(&new_snapshot)
} else {
x.matches(&new_snapshot)
}
})
.unwrap_or(false);
let pass = ctx.old_snapshot.as_ref().map_or(Ok(false), |x| {
if tool_config.require_full_match() {
x.matches_fully(&new_snapshot)
} else {
x.matches(&new_snapshot)
}
})?;

if pass {
ctx.cleanup_passing()?;
Expand Down
30 changes: 15 additions & 15 deletions insta/src/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -494,8 +494,8 @@ impl Snapshot {
}

/// Snapshot contents match another snapshot's.
pub fn matches(&self, other: &Snapshot) -> bool {
match (self.contents(), other.contents()) {
pub fn matches(&self, other: &Snapshot) -> Result<bool, Box<dyn Error>> {
Ok(match (self.contents(), other.contents()) {
(SnapshotContents::String(this), SnapshotContents::String(other)) => {
trim_content_str(this) == trim_content_str(other)
}
Expand All @@ -510,22 +510,22 @@ impl Snapshot {
},
) => {
if this_ext != other_ext {
return false;
return Ok(false);
}

let mut this = File::open(this).unwrap();
let mut other = File::open(other).unwrap();
let mut this = File::open(this)?;
let mut other = File::open(other)?;
max-sixty marked this conversation as resolved.
Show resolved Hide resolved

file_eq(&mut this, &mut other).unwrap()
file_eq(&mut this, &mut other)?
}
_ => false,
}
})
}

/// Snapshot contents _and_ metadata match another snapshot's.
pub fn matches_fully(&self, other: &Snapshot) -> bool {
self.matches(other)
&& self.metadata.trim_for_persistence() == other.metadata.trim_for_persistence()
pub fn matches_fully(&self, other: &Snapshot) -> Result<bool, Box<dyn Error>> {
Ok(self.matches(other)?
&& self.metadata.trim_for_persistence() == other.metadata.trim_for_persistence())
}

/// The snapshot contents as a &str
Expand Down Expand Up @@ -572,7 +572,7 @@ impl Snapshot {
&& if let SnapshotContents::Binary { .. } = &self.snapshot {
let old_snapshot = Snapshot::from_file(ref_path)?;

self.matches(&old_snapshot)
self.matches(&old_snapshot)?
} else {
true
}
Expand Down Expand Up @@ -638,24 +638,24 @@ impl Snapshot {
}
}

pub fn cleanup_extra_files(snapshot: Option<&Self>, path: &Path) -> Result<(), std::io::Error> {
pub fn cleanup_extra_files(snapshot: Option<&Self>, path: &Path) -> Result<(), Box<dyn Error>> {
let binary_file_name = snapshot.and_then(|snapshot| match &snapshot.contents() {
SnapshotContents::String(_) => None,
SnapshotContents::Binary { path, .. } => Some(path.file_name().unwrap()),
});

let file_name = path.file_name().unwrap();

for entry in path.parent().unwrap().read_dir().unwrap() {
let entry = entry.unwrap();
for entry in path.parent().unwrap().read_dir()? {
let entry = entry?;
let entry_file_name = entry.file_name();
if entry_file_name
.as_encoded_bytes()
.starts_with(file_name.as_encoded_bytes())
&& entry_file_name != file_name
&& binary_file_name.map_or(true, |x| x != entry_file_name)
{
std::fs::remove_file(entry.path()).unwrap();
std::fs::remove_file(entry.path())?;
}
}

Expand Down