Port SnapshotDir tests to use insta snapshots

This commit is contained in:
Lucien Greathouse
2019-09-30 18:33:46 -07:00
parent 1c6788ea45
commit 95f06d56d8
5 changed files with 83 additions and 20 deletions

View File

@@ -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<S, T>(maybe_path: &Option<T>, serializer: S) -> Result<S::Ok, S::Error>
where
@@ -72,3 +72,35 @@ where
serializer.serialize_str(&output)
}
pub fn serialize_absolute<S, T>(path: T, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
T: AsRef<Path>,
{
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<S, T>(paths: &Vec<T>, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
T: AsRef<Path>,
{
let mut seq = serializer.serialize_seq(Some(paths.len()))?;
for path in paths {
seq.serialize_element(&WithAbsolute(path.as_ref()))?;
}
seq.end()
}

View File

@@ -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<PathBuf>,
/// If this instance was defined in a project file, this is the name from

View File

@@ -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);
}
}

View File

@@ -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: []

View File

@@ -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: []