Skip to content

Commit

Permalink
Added improved doctest support (#243)
Browse files Browse the repository at this point in the history
  • Loading branch information
mitsuhiko committed Jul 21, 2022
1 parent 1fff8b0 commit a8d516e
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -8,6 +8,7 @@ All notable changes to insta and cargo-insta are documented here.
quiet flag. This works around limitations with custom test harnesses
such as cucumber.
- Update RON to 0.7.1.
- Added improved support for running insta from doctests. (#243)

## 1.15.0

Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Expand Up @@ -36,7 +36,7 @@
//!
//! # Writing Tests
//!
//! ```no_run
//! ```
//! use insta::assert_debug_snapshot;
//!
//! #[test]
Expand Down
48 changes: 43 additions & 5 deletions src/runtime.rs
Expand Up @@ -75,11 +75,20 @@ pub enum ReferenceValue<'a> {
Inline(&'a str),
}

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> {
let name = Cow::Borrowed(function_name);
let mut name = function_name;

// simplify doctest names
if is_doctest(name) {
name = "unnamed_doctest";
}

// clean test name first
let mut name = name.rsplit("::").next().unwrap();
name = name.rsplit("::").next().unwrap();
let mut test_prefixed = false;
if name.starts_with("test_") {
name = &name[5..];
Expand Down Expand Up @@ -135,7 +144,9 @@ fn add_suffix_to_snapshot_name(name: Cow<'_, str>) -> Cow<'_, str> {
}

fn get_snapshot_filename(
function_name: &str,
module_path: &str,
assertion_file: &str,
snapshot_name: &str,
cargo_workspace: &Path,
base: &str,
Expand All @@ -149,7 +160,20 @@ fn get_snapshot_filename(
use std::fmt::Write;
let mut f = String::new();
if settings.prepend_module_to_snapshot() {
write!(&mut f, "{}__", module_path.replace("::", "__")).unwrap();
if is_doctest(function_name) {
write!(
&mut f,
"doctest_{}__",
Path::new(assertion_file)
.file_name()
.unwrap()
.to_string_lossy()
.replace('.', "_")
)
.unwrap();
} else {
write!(&mut f, "{}__", module_path.replace("::", "__")).unwrap();
}
}
write!(
&mut f,
Expand Down Expand Up @@ -197,8 +221,14 @@ impl<'a> SnapshotAssertionContext<'a> {
.unwrap()
.into(),
};
let file =
get_snapshot_filename(module_path, &name, &cargo_workspace, assertion_file);
let file = get_snapshot_filename(
function_name,
module_path,
assertion_file,
&name,
&cargo_workspace,
assertion_file,
);
if fs::metadata(&file).is_ok() {
old_snapshot = Some(Snapshot::from_file(&file)?);
}
Expand Down Expand Up @@ -456,3 +486,11 @@ pub fn assert_snapshot(

Ok(())
}

/// Test snapshots in doctests.
///
/// ```
/// insta::assert_yaml_snapshot!(vec![1, 2, 3]);
/// insta::assert_yaml_snapshot!("named", vec![1, 2, 3, 4, 5]);
/// ```
const _DOCTEST: bool = false;
10 changes: 10 additions & 0 deletions src/snapshots/doctest_runtime_rs__named.snap
@@ -0,0 +1,10 @@
---
source: src/runtime.rs
expression: "vec![1, 2, 3, 4, 5]"
---
- 1
- 2
- 3
- 4
- 5

8 changes: 8 additions & 0 deletions src/snapshots/doctest_runtime_rs__unnamed_doctest.snap
@@ -0,0 +1,8 @@
---
source: src/runtime.rs
expression: "vec![1, 2, 3]"
---
- 1
- 2
- 3

0 comments on commit a8d516e

Please sign in to comment.