Skip to content

Commit

Permalink
Resolve an issue where inline snapshot tests refused to work in docte…
Browse files Browse the repository at this point in the history
…sts (#253)
  • Loading branch information
mitsuhiko committed Jul 30, 2022
1 parent df15039 commit c552ebe
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
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;

0 comments on commit c552ebe

Please sign in to comment.