From 95f06d56d86beb51f78768d021ac875f0eaf1861 Mon Sep 17 00:00:00 2001 From: Lucien Greathouse Date: Mon, 30 Sep 2019 18:33:46 -0700 Subject: [PATCH] Port SnapshotDir tests to use insta snapshots --- src/path_serializer.rs | 38 +++++++++++++++++-- src/snapshot/metadata.rs | 3 +- src/snapshot_middleware/dir.rs | 24 ++++-------- .../snapshots/test__empty_folder.snap | 14 +++++++ .../snapshots/test__folder_in_folder.snap | 24 ++++++++++++ 5 files changed, 83 insertions(+), 20 deletions(-) create mode 100644 src/snapshot_middleware/snapshots/test__empty_folder.snap create mode 100644 src/snapshot_middleware/snapshots/test__folder_in_folder.snap diff --git a/src/path_serializer.rs b/src/path_serializer.rs index fefe22e4..e3212077 100644 --- a/src/path_serializer.rs +++ b/src/path_serializer.rs @@ -25,12 +25,12 @@ //! } //! ``` //! -//! **The methods in this module can only handle relative paths, since absolute -//! paths are never portable.** +//! For absolute paths, which are only safe to serialize if they're artificial, +//! use `serialize_absolute`. use std::path::{Component, Path}; -use serde::Serializer; +use serde::{ser::SerializeSeq, Serialize, Serializer}; pub fn serialize_option(maybe_path: &Option, serializer: S) -> Result where @@ -72,3 +72,35 @@ where serializer.serialize_str(&output) } + +pub fn serialize_absolute(path: T, serializer: S) -> Result +where + S: Serializer, + T: AsRef, +{ + let as_str = path + .as_ref() + .as_os_str() + .to_str() + .expect("Invalid Unicode in file path, cannot serialize"); + let replaced = as_str.replace("\\", "/"); + + serializer.serialize_str(&replaced) +} + +#[derive(Serialize)] +struct WithAbsolute<'a>(#[serde(serialize_with = "serialize_absolute")] &'a Path); + +pub fn serialize_vec_absolute(paths: &Vec, serializer: S) -> Result +where + S: Serializer, + T: AsRef, +{ + let mut seq = serializer.serialize_seq(Some(paths.len()))?; + + for path in paths { + seq.serialize_element(&WithAbsolute(path.as_ref()))?; + } + + seq.end() +} diff --git a/src/snapshot/metadata.rs b/src/snapshot/metadata.rs index 0105e34e..f4c4f241 100644 --- a/src/snapshot/metadata.rs +++ b/src/snapshot/metadata.rs @@ -2,7 +2,7 @@ use std::path::PathBuf; use serde::{Deserialize, Serialize}; -use crate::project::ProjectNode; +use crate::{path_serializer, project::ProjectNode}; /// Rojo-specific metadata that can be associated with an instance or a snapshot /// of an instance. @@ -33,6 +33,7 @@ pub struct InstanceMetadata { /// This path is used to make sure that file changes update all instances /// that may need updates. // TODO: Change this to be a SmallVec for performance in common cases? + #[serde(serialize_with = "path_serializer::serialize_vec_absolute")] pub contributing_paths: Vec, /// If this instance was defined in a project file, this is the name from diff --git a/src/snapshot_middleware/dir.rs b/src/snapshot_middleware/dir.rs index a826c43f..04b566c4 100644 --- a/src/snapshot_middleware/dir.rs +++ b/src/snapshot_middleware/dir.rs @@ -4,7 +4,7 @@ use rbx_dom_weak::{RbxId, RbxTree}; use crate::{ imfs::{DirectorySnapshot, Imfs, ImfsEntry, ImfsFetcher, ImfsSnapshot}, - snapshot::InstanceSnapshot, + snapshot::{InstanceMetadata, InstanceSnapshot}, }; use super::{ @@ -43,7 +43,10 @@ impl SnapshotMiddleware for SnapshotDir { Ok(Some(InstanceSnapshot { snapshot_id: None, - metadata: Default::default(), // TODO + metadata: InstanceMetadata { + contributing_paths: vec![entry.path().to_path_buf()], + ..Default::default() + }, name: Cow::Owned(instance_name), class_name: Cow::Borrowed("Folder"), properties: HashMap::new(), @@ -76,6 +79,7 @@ impl SnapshotMiddleware for SnapshotDir { mod test { use super::*; + use insta::assert_yaml_snapshot; use maplit::hashmap; use crate::imfs::{ImfsDebug, NoopFetcher}; @@ -90,10 +94,7 @@ mod test { let entry = imfs.get("/foo").unwrap(); let instance_snapshot = SnapshotDir::from_imfs(&mut imfs, &entry).unwrap().unwrap(); - assert_eq!(instance_snapshot.name, "foo"); - assert_eq!(instance_snapshot.class_name, "Folder"); - assert_eq!(instance_snapshot.properties, HashMap::new()); - assert_eq!(instance_snapshot.children, Vec::new()); + assert_yaml_snapshot!(instance_snapshot); } #[test] @@ -108,15 +109,6 @@ mod test { let entry = imfs.get("/foo").unwrap(); let instance_snapshot = SnapshotDir::from_imfs(&mut imfs, &entry).unwrap().unwrap(); - assert_eq!(instance_snapshot.name, "foo"); - assert_eq!(instance_snapshot.class_name, "Folder"); - assert_eq!(instance_snapshot.properties, HashMap::new()); - assert_eq!(instance_snapshot.children.len(), 1); - - let child = &instance_snapshot.children[0]; - assert_eq!(child.name, "Child"); - assert_eq!(child.class_name, "Folder"); - assert_eq!(child.properties, HashMap::new()); - assert_eq!(child.children, Vec::new()); + assert_yaml_snapshot!(instance_snapshot); } } diff --git a/src/snapshot_middleware/snapshots/test__empty_folder.snap b/src/snapshot_middleware/snapshots/test__empty_folder.snap new file mode 100644 index 00000000..15556287 --- /dev/null +++ b/src/snapshot_middleware/snapshots/test__empty_folder.snap @@ -0,0 +1,14 @@ +--- +source: src/snapshot_middleware/dir.rs +expression: instance_snapshot +--- +snapshot_id: ~ +metadata: + ignore_unknown_instances: false + contributing_paths: + - /foo + project_node: ~ +name: foo +class_name: Folder +properties: {} +children: [] diff --git a/src/snapshot_middleware/snapshots/test__folder_in_folder.snap b/src/snapshot_middleware/snapshots/test__folder_in_folder.snap new file mode 100644 index 00000000..a65151d7 --- /dev/null +++ b/src/snapshot_middleware/snapshots/test__folder_in_folder.snap @@ -0,0 +1,24 @@ +--- +source: src/snapshot_middleware/dir.rs +expression: instance_snapshot +--- +snapshot_id: ~ +metadata: + ignore_unknown_instances: false + contributing_paths: + - /foo + project_node: ~ +name: foo +class_name: Folder +properties: {} +children: + - snapshot_id: ~ + metadata: + ignore_unknown_instances: false + contributing_paths: + - /foo/Child + project_node: ~ + name: Child + class_name: Folder + properties: {} + children: []