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

Resolve an issue where inline snapshot tests refused to work in doctests #253

Merged
merged 1 commit into from
Jul 30, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ All notable changes to insta and cargo-insta are documented here.
## Unrelease

- Added support for nextest. (#242)
- Resolved an issue where inline snapshot tests in doctests refused to
work. (#252)

## 0.17.0

Expand Down
17 changes: 13 additions & 4 deletions src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,15 @@ fn is_doctest(function_name: &str) -> bool {
function_name.starts_with("rust_out::main::_doctest")
}

fn detect_snapshot_name(function_name: &str, module_path: &str) -> Result<String, &'static str> {
fn detect_snapshot_name(
function_name: &str,
module_path: &str,
inline: bool,
) -> Result<String, &'static str> {
let mut name = function_name;

// simplify doctest names
if is_doctest(name) {
if is_doctest(name) && !inline {
panic!("Cannot determine reliable names for snapshot in doctests. Please use explicit names instead.");
}

Expand Down Expand Up @@ -217,7 +221,7 @@ impl<'a> SnapshotAssertionContext<'a> {
ReferenceValue::Named(name) => {
let name = match name {
Some(name) => add_suffix_to_snapshot_name(name),
None => detect_snapshot_name(function_name, module_path)
None => detect_snapshot_name(function_name, module_path, false)
.unwrap()
.into(),
};
Expand All @@ -236,7 +240,7 @@ impl<'a> SnapshotAssertionContext<'a> {
snapshot_file = Some(file);
}
ReferenceValue::Inline(contents) => {
snapshot_name = detect_snapshot_name(function_name, module_path)
snapshot_name = detect_snapshot_name(function_name, module_path, true)
.ok()
.map(Cow::Owned);
let mut pending_file = cargo_workspace.join(assertion_file);
Expand Down Expand Up @@ -510,4 +514,9 @@ pub fn assert_snapshot(
/// ```should_panic
/// insta::assert_yaml_snapshot!(vec![1, 2, 3, 4, 5]);
/// ```
///
/// ```
/// let some_string = "Coucou je suis un joli bug";
/// insta::assert_snapshot!(some_string, @"Coucou je suis un joli bug");
/// ```
const _DOCTEST1: bool = false;