Skip to content

Commit

Permalink
No commit message
Browse files Browse the repository at this point in the history
  • Loading branch information
max-sixty committed Sep 2, 2024
1 parent a1880f0 commit 40df530
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 113 deletions.
6 changes: 4 additions & 2 deletions cargo-insta/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1061,12 +1061,14 @@ fn pending_snapshots_cmd(cmd: PendingSnapshotsCommand) -> Result<(), Box<dyn Err
let is_inline = snapshot_container.snapshot_file().is_none();
for snapshot_ref in snapshot_container.iter_snapshots() {
if cmd.as_json {
let old_snapshot = snapshot_ref.old.as_ref().map(|x| x.contents_str());
let new_snapshot = snapshot_ref.new.contents_str();
let info = if is_inline {
SnapshotKey::InlineSnapshot {
path: &target_file,
line: snapshot_ref.line.unwrap(),
old_snapshot: snapshot_ref.old.as_ref().map(|x| x.contents_str()),
new_snapshot: snapshot_ref.new.contents_str(),
old_snapshot: old_snapshot.as_deref(),
new_snapshot: &new_snapshot,
expression: snapshot_ref.new.metadata().expression(),
}
} else {
Expand Down
81 changes: 79 additions & 2 deletions cargo-insta/tests/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -843,7 +843,7 @@ fn test_force_update_inline_snapshot_linebreaks() {
"Cargo.toml",
r#"
[package]
name = "force-update-inline-linkbreaks"
name = "force-update-inline-linebreaks"
version = "0.1.0"
edition = "2021"
Expand Down Expand Up @@ -905,7 +905,7 @@ fn test_force_update_inline_snapshot_hashes() {
"Cargo.toml",
r#"
[package]
name = "force-update-inline"
name = "force-update-inline-hashes"
version = "0.1.0"
edition = "2021"
Expand Down Expand Up @@ -947,6 +947,83 @@ fn test_excessive_hashes() {
assert_snapshot!(test_project.diff("src/lib.rs"), @"");
}

#[test]
fn test_inline_snapshot_indent() {
let test_project = TestFiles::new()
.add_file(
"Cargo.toml",
r#"
[package]
name = "inline-indent"
version = "0.1.0"
edition = "2021"
[dependencies]
insta = { path = '$PROJECT_PATH' }
"#
.to_string(),
)
.add_file(
"src/lib.rs",
r#####"
#[test]
fn test_wrong_indent_force() {
insta::assert_snapshot!(r#"
foo
foo
"#, @r#"
foo
foo
"#);
}
"#####
.to_string(),
)
.create_project();

// Confirm the test passes despite the indent
let output = test_project
.cmd()
.args(["test", "--check", "--", "--nocapture"])
.output()
.unwrap();
assert_success(&output);

// Then run the test with --force-update-snapshots and --accept to confirm
// the new snapshot is written
let output = test_project
.cmd()
.args([
"test",
"--force-update-snapshots",
"--accept",
"--",
"--nocapture",
])
.output()
.unwrap();
assert_success(&output);

// https://github.com/mitsuhiko/insta/pull/563 will fix the starting &
// ending newlines
assert_snapshot!(test_project.diff("src/lib.rs"), @r##"
--- Original: src/lib.rs
+++ Updated: src/lib.rs
@@ -5,7 +5,9 @@
foo
foo
"#, @r#"
- foo
- foo
+
+ foo
+ foo
+
"#);
}
"##);
}

#[test]
fn test_hashtag_escape_in_inline_snapshot() {
let test_project = TestFiles::new()
Expand Down
8 changes: 5 additions & 3 deletions insta/src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,17 @@ impl<'a> SnapshotPrinter<'a> {
}

fn print_changeset(&self) {
let old = self.old_snapshot.as_ref().map_or("", |x| x.contents_str());
let old: String = self
.old_snapshot
.map_or("".to_string(), |x| x.contents_str());
let new = self.new_snapshot.contents_str();
let newlines_matter = newlines_matter(old, new);
let newlines_matter = newlines_matter(old.as_str(), new.as_str());

let width = term_width();
let diff = TextDiff::configure()
.algorithm(Algorithm::Patience)
.timeout(Duration::from_millis(500))
.diff_lines(old, new);
.diff_lines(old.as_str(), new.as_str());
print_line(width);

if self.show_info {
Expand Down
14 changes: 4 additions & 10 deletions insta/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,7 @@ impl<'a> SnapshotAssertionContext<'a> {
module_path.replace("::", "__"),
None,
MetaData::default(),
SnapshotKind::Inline,
SnapshotContents::from_inline(contents),
SnapshotContents::new(contents.to_string(), SnapshotKind::Inline),
));
}
};
Expand Down Expand Up @@ -319,12 +318,7 @@ impl<'a> SnapshotAssertionContext<'a> {
}

/// Creates the new snapshot from input values.
pub fn new_snapshot(
&self,
contents: SnapshotContents,
expr: &str,
kind: SnapshotKind,
) -> Snapshot {
pub fn new_snapshot(&self, contents: SnapshotContents, expr: &str) -> Snapshot {
Snapshot::from_components(
self.module_path.replace("::", "__"),
self.snapshot_name.as_ref().map(|x| x.to_string()),
Expand All @@ -343,7 +337,6 @@ impl<'a> SnapshotAssertionContext<'a> {
.and_then(|x| self.localize_path(x))
.map(|x| path_to_storage(&x)),
}),
kind,
contents,
)
}
Expand Down Expand Up @@ -651,7 +644,8 @@ pub fn assert_snapshot(
Some(_) => SnapshotKind::File,
None => SnapshotKind::Inline,
};
let new_snapshot = ctx.new_snapshot(new_snapshot_value.into(), expr, kind);
let new_snapshot =
ctx.new_snapshot(SnapshotContents::new(new_snapshot_value.into(), kind), expr);

// memoize the snapshot file if requested.
if let Some(ref snapshot_file) = ctx.snapshot_file {
Expand Down
Loading

0 comments on commit 40df530

Please sign in to comment.