Repository: mitsuhiko/insta
Pull request #87 (2019-10-28 04:54:02)

Use full module path for test names
Closes https://github.com/mitsuhiko/insta/issues/78

Is this the right design? A good case is the `insta__test__embedded.snap` change below. I suspect those cases are rare; though it's a breaking change where they do exist.

I need to adds a whatsnew

mitsuhiko: Something went wrong here

Patch:
diff --git a/src/runtime.rs b/src/runtime.rs
index a7c9b3b..5d3f512 100644
--- a/src/runtime.rs
+++ b/src/runtime.rs
@@ -283,7 +283,7 @@ fn print_changeset(changeset: &Changeset, expr: Option<&str>) {
 }
 
 pub fn get_snapshot_filename(
-    module_name: &str,
+    module_path: &str,
     snapshot_name: &str,
     cargo_workspace: &Path,
     base: &str,
@@ -293,7 +293,7 @@ pub fn get_snapshot_filename(
     Settings::with(|settings| {
         root.join(base.parent().unwrap())
             .join(settings.snapshot_path())
-            .join(format!("{}__{}.snap", module_name, snapshot_name))
+            .join(format!("{}__{}.snap", module_path, snapshot_name))
     })
 }
 
@@ -724,17 +724,17 @@ pub fn assert_snapshot(
     line: u32,
     expr: &str,
 ) -> Result<(), Box<dyn Error>> {
-    let module_name = module_path.rsplit("::").next().unwrap();
+    let module_path = module_path.replace("::", "__");
     let cargo_workspace = get_cargo_workspace(manifest_dir);
 
     let (snapshot_name, snapshot_file, old, pending_snapshots) = match refval {
         ReferenceValue::Named(snapshot_name) => {
             let snapshot_name: Cow<str> = match snapshot_name {
                 Some(snapshot_name) => snapshot_name,
-                None => (generate_snapshot_name_for_thread(module_path).into()),
+                None => (generate_snapshot_name_for_thread(&module_path).into()),
             };
             let snapshot_file =
-                get_snapshot_filename(module_name, &snapshot_name, &cargo_workspace, file);
+                get_snapshot_filename(&module_path, &snapshot_name, &cargo_workspace, file);
             let old = if fs::metadata(&snapshot_file).is_ok() {
                 Some(Snapshot::from_file(&snapshot_file)?)
             } else {
@@ -743,7 +743,7 @@ pub fn assert_snapshot(
             (snapshot_name, Some(snapshot_file), old, None)
         }
         ReferenceValue::Inline(contents) => {
-            let snapshot_name = generate_snapshot_name_for_thread(module_path);
+            let snapshot_name = generate_snapshot_name_for_thread(&module_path);
             let mut filename = cargo_workspace.join(file);
             filename.set_file_name(format!(
                 ".{}.pending-snap",
@@ -757,7 +757,7 @@ pub fn assert_snapshot(
                 Cow::Owned(snapshot_name),
                 None,
                 Some(Snapshot::from_components(
-                    module_name.to_string(),
+                    module_path.to_string(),
                     None,
                     MetaData::default(),
                     SnapshotContents::from_inline(contents),
@@ -769,7 +769,7 @@ pub fn assert_snapshot(
 
     let new_snapshot_contents: SnapshotContents = new_snapshot.into();
     let new = Snapshot::from_components(
-        module_name.to_string(),
+        module_path.to_string(),
         Some(snapshot_name.to_string()),
         MetaData {
             source: Some(path_to_storage(file)),


Test patch:
diff --git a/src/snapshots/test__embedded.snap b/src/snapshots/insta__test__embedded.snap
similarity index 100%
rename from src/snapshots/test__embedded.snap
rename to src/snapshots/insta__test__embedded.snap
diff --git a/tests/snapshots/test_basic__nested__nested_module.snap b/tests/snapshots/test_basic__nested__nested_module.snap
new file mode 100644
index 0000000..8bb5a73
--- /dev/null
+++ b/tests/snapshots/test_basic__nested__nested_module.snap
@@ -0,0 +1,5 @@
+---
+source: tests/test_basic.rs
+expression: "\"aoeu\""
+---
+aoeu
diff --git a/tests/test_basic.rs b/tests/test_basic.rs
index 032d0fb..0138e41 100644
--- a/tests/test_basic.rs
+++ b/tests/test_basic.rs
@@ -39,6 +39,14 @@ fn test_unnamed_json_vector() {
     assert_json_snapshot!(vec![1, 2, 3, 4, 5]);
 }
 
+mod nested {
+    #[test]
+    fn test_nested_module() {
+        use insta::assert_snapshot;
+        assert_snapshot!("aoeu");
+    }
+}
+
 struct TestDisplay;
 
 impl fmt::Display for TestDisplay {