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

Remove some methods from SnapshotContents #640

Merged
merged 1 commit into from
Oct 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
16 changes: 10 additions & 6 deletions cargo-insta/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ use std::{env, fs};
use std::{io, process};

use console::{set_colors_enabled, style, Key, Term};
use insta::Snapshot;
use insta::_cargo_insta_support::{
is_ci, SnapshotPrinter, SnapshotUpdate, TestRunner, ToolConfig, UnreferencedSnapshots,
};
use insta::{internals::SnapshotContents, Snapshot};
use itertools::Itertools;
use semver::Version;
use serde::Serialize;
Expand Down Expand Up @@ -1140,11 +1140,15 @@ 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_string().unwrap());
let new_snapshot = snapshot_ref.new.contents_string().unwrap();
let old_snapshot = snapshot_ref.old.as_ref().map(|x| match x.contents() {
SnapshotContents::Text(x) => x.to_string(),
_ => unreachable!(),
});
let new_snapshot = match snapshot_ref.new.contents() {
SnapshotContents::Text(x) => x.to_string(),
_ => unreachable!(),
};

let info = if is_inline {
SnapshotKey::InlineSnapshot {
path: &target_file,
Expand Down
7 changes: 5 additions & 2 deletions cargo-insta/src/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use std::error::Error;
use std::fs;
use std::path::{Path, PathBuf};

use insta::Snapshot;
pub(crate) use insta::TextSnapshotKind;
use insta::_cargo_insta_support::{ContentError, PendingInlineSnapshot};
use insta::{internals::SnapshotContents, Snapshot};

use crate::inline::FilePatcher;

Expand Down Expand Up @@ -186,7 +186,10 @@ impl SnapshotContainer {
Operation::Accept => {
patcher.set_new_content(
idx,
snapshot.new.contents().as_string_contents().unwrap(),
match snapshot.new.contents() {
SnapshotContents::Text(c) => c,
_ => unreachable!(),
},
);
did_accept = true;
}
Expand Down
19 changes: 2 additions & 17 deletions insta/src/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -542,20 +542,12 @@ impl Snapshot {
}
}

/// The normalized snapshot contents as a String
pub fn contents_string(&self) -> Option<String> {
match self.contents() {
SnapshotContents::Text(contents) => Some(contents.normalize()),
SnapshotContents::Binary(_) => None,
}
}

fn serialize_snapshot(&self, md: &MetaData) -> String {
let mut buf = yaml::to_string(&md.as_content());
buf.push_str("---\n");

if let Some(ref contents_str) = self.contents_string() {
buf.push_str(contents_str);
if let SnapshotContents::Text(ref contents) = self.snapshot {
buf.push_str(&contents.to_string());
buf.push('\n');
}

Expand Down Expand Up @@ -636,13 +628,6 @@ impl SnapshotContents {
pub fn is_binary(&self) -> bool {
matches!(self, SnapshotContents::Binary(_))
}

pub fn as_string_contents(&self) -> Option<&TextSnapshotContents> {
match self {
SnapshotContents::Text(contents) => Some(contents),
SnapshotContents::Binary(_) => None,
}
}
}

impl TextSnapshotContents {
Expand Down
Loading