diff --git a/.gitignore b/.gitignore index 9656d14a..8850fc7f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ target **/*.rs.bk .vscode/*.log +.aider* +.env diff --git a/cargo-insta/tests/functional/main.rs b/cargo-insta/tests/functional/main.rs index 67438f93..59856fc5 100644 --- a/cargo-insta/tests/functional/main.rs +++ b/cargo-insta/tests/functional/main.rs @@ -359,13 +359,12 @@ Hello, world! assert_snapshot!(test_current_insta.diff("src/snapshots/test_force_update_current__force_update.snap"), @r#" --- Original: src/snapshots/test_force_update_current__force_update.snap +++ Updated: src/snapshots/test_force_update_current__force_update.snap - @@ -1,8 +1,6 @@ + @@ -1,8 +1,5 @@ - --- source: src/lib.rs -expression: +expression: "\"Hello, world!\"" - +snapshot_kind: text --- Hello, world! - @@ -763,6 +762,63 @@ Hidden snapshot ); } +#[test] +fn test_snapshot_kind_behavior() { + let test_project = TestFiles::new() + .add_cargo_toml("test_snapshot_kind") + .add_file( + "src/lib.rs", + r#" +#[test] +fn test_snapshots() { + insta::assert_snapshot!("new snapshot"); + insta::assert_snapshot!("existing snapshot"); +} +"# + .to_string(), + ) + .add_file( + "src/snapshots/test_snapshot_kind__existing.snap", + r#"--- +source: src/lib.rs +expression: "\"existing snapshot\"" +snapshot_kind: text +--- +existing snapshot +"# + .to_string(), + ) + .create_project(); + + // Run the test with --accept to create the new snapshot + let output = test_project + .insta_cmd() + .args(["test", "--accept"]) + .output() + .unwrap(); + + assert!(output.status.success()); + + // Verify the new snapshot was created without snapshot_kind + let new_snapshot = std::fs::read_to_string( + test_project + .workspace_dir + .join("src/snapshots/test_snapshot_kind__snapshots.snap"), + ) + .unwrap(); + + assert!(!new_snapshot.contains("snapshot_kind:")); + + // Verify both snapshots work with --require-full-match + let output = test_project + .insta_cmd() + .args(["test", "--require-full-match"]) + .output() + .unwrap(); + + assert!(output.status.success()); +} + #[test] fn test_ignored_snapshots() { let test_project = TestFiles::new() diff --git a/cargo-insta/tests/functional/workspace.rs b/cargo-insta/tests/functional/workspace.rs index 0b4841e7..b3976fed 100644 --- a/cargo-insta/tests/functional/workspace.rs +++ b/cargo-insta/tests/functional/workspace.rs @@ -576,7 +576,6 @@ fn test_hello() { --- source: "../tests/lib.rs" expression: hello() - snapshot_kind: text --- Hello, world! "#); diff --git a/insta/src/env.rs b/insta/src/env.rs index 3d59f3fb..11f04d2c 100644 --- a/insta/src/env.rs +++ b/insta/src/env.rs @@ -4,7 +4,7 @@ use std::path::{Path, PathBuf}; use std::sync::{Arc, Mutex}; use std::{env, fmt, fs}; -use crate::utils::{get_cargo, is_ci}; +use crate::utils::is_ci; use crate::{ content::{yaml, Content}, elog, @@ -42,6 +42,7 @@ impl TestRunner { /// Fall back to `cargo test` if `cargo nextest` isn't installed and /// `test_runner_fallback` is true pub fn resolve_fallback(&self, test_runner_fallback: bool) -> &TestRunner { + use crate::_cargo_insta_support::get_cargo; if self == &TestRunner::Nextest && test_runner_fallback && std::process::Command::new(get_cargo()) diff --git a/insta/src/snapshot.rs b/insta/src/snapshot.rs index ec80602e..9275f458 100644 --- a/insta/src/snapshot.rs +++ b/insta/src/snapshot.rs @@ -291,15 +291,13 @@ impl MetaData { fields.push(("input_file", Content::from(input_file))); } - let snapshot_type = Content::from(match self.snapshot_kind { - SnapshotKind::Text => "text", + match self.snapshot_kind { + SnapshotKind::Text => {} SnapshotKind::Binary { ref extension } => { fields.push(("extension", Content::from(extension.clone()))); - "binary" + fields.push(("snapshot_kind", Content::from("binary"))); } - }); - - fields.push(("snapshot_kind", snapshot_type)); + } Content::Struct("MetaData", fields) } diff --git a/insta/src/utils.rs b/insta/src/utils.rs index 8ee58b14..f800252e 100644 --- a/insta/src/utils.rs +++ b/insta/src/utils.rs @@ -108,6 +108,7 @@ pub fn format_rust_expression(value: &str) -> Cow<'_, str> { Cow::Borrowed(value) } +#[cfg(feature = "_cargo_insta_internal")] pub fn get_cargo() -> std::ffi::OsString { let cargo = env::var_os("CARGO"); let cargo = cargo