Improve serve snapshot test ergonomics

This commit is contained in:
Lucien Greathouse
2019-10-16 17:36:13 -07:00
parent cc68d57f11
commit 3880708e1d
5 changed files with 46 additions and 45 deletions

1
Cargo.lock generated
View File

@@ -1617,6 +1617,7 @@ dependencies = [
"rojo-insta-ext 0.1.0", "rojo-insta-ext 0.1.0",
"serde 1.0.101 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.101 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 1.0.41 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.41 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_yaml 0.8.9 (registry+https://github.com/rust-lang/crates.io-index)",
"tempfile 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "tempfile 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"walkdir 2.2.9 (registry+https://github.com/rust-lang/crates.io-index)", "walkdir 2.2.9 (registry+https://github.com/rust-lang/crates.io-index)",
] ]

View File

@@ -13,8 +13,10 @@ rbx_dom_weak = "1.9.0"
reqwest = "0.9.20" reqwest = "0.9.20"
serde = "1.0.99" serde = "1.0.99"
serde_json = "1.0.40" serde_json = "1.0.40"
serde_yaml = "0.8.9"
tempfile = "3.1.0" tempfile = "3.1.0"
walkdir = "2.2.9" walkdir = "2.2.9"
rojo-insta-ext = { path = "../rojo-insta-ext" } rojo-insta-ext = { path = "../rojo-insta-ext" }
# We execute Rojo via std::process::Command, so depend on it so it's built! # We execute Rojo via std::process::Command, so depend on it so it's built!

View File

@@ -1,6 +1,8 @@
#![cfg(test)] #![cfg(test)]
#[macro_use]
mod serve_util;
mod build_test; mod build_test;
mod serve_test; mod serve_test;
mod serve_util;
mod util; mod util;

View File

@@ -2,22 +2,18 @@ use std::fs;
use insta::assert_yaml_snapshot; use insta::assert_yaml_snapshot;
use crate::serve_util::{intern_read_response, run_serve_test}; use crate::serve_util::{run_serve_test, InternAndRedact};
#[test] #[test]
fn empty() { fn empty() {
run_serve_test("empty", |session, mut redactions| { run_serve_test("empty", |session, mut redactions| {
let info = session.get_api_rojo().unwrap(); let info = session.get_api_rojo().unwrap();
let root_id = info.root_instance_id; let root_id = info.root_instance_id;
let info = redactions.redacted_yaml(info); assert_yaml_snapshot!(redactions.redacted_yaml(info));
assert_yaml_snapshot!(info);
let read_result = session.get_api_read(root_id).unwrap(); let read_response = session.get_api_read(root_id).unwrap();
intern_read_response(&mut redactions, &read_result, root_id); assert_yaml_snapshot!(read_response.intern_and_redact(&mut redactions, root_id));
let read_result = redactions.redacted_yaml(read_result);
assert_yaml_snapshot!(read_result);
}); });
} }
@@ -27,23 +23,18 @@ fn scripts() {
let info = session.get_api_rojo().unwrap(); let info = session.get_api_rojo().unwrap();
let root_id = info.root_instance_id; let root_id = info.root_instance_id;
let info = redactions.redacted_yaml(info); assert_yaml_snapshot!(redactions.redacted_yaml(info));
assert_yaml_snapshot!(info);
let read_result = session.get_api_read(root_id).unwrap(); let read_response = session.get_api_read(root_id).unwrap();
intern_read_response(&mut redactions, &read_result, root_id); assert_yaml_snapshot!(read_response.intern_and_redact(&mut redactions, root_id));
let read_result = redactions.redacted_yaml(read_result);
assert_yaml_snapshot!(read_result);
fs::write(session.path().join("foo.lua"), "Updated foo!").unwrap(); fs::write(session.path().join("foo.lua"), "Updated foo!").unwrap();
let subscribe_result = session.get_api_subscribe(0).unwrap(); let subscribe_response = session.get_api_subscribe(0).unwrap();
let subscribe_result = redactions.redacted_yaml(subscribe_result); assert_yaml_snapshot!(redactions.redacted_yaml(subscribe_response));
assert_yaml_snapshot!(subscribe_result);
let read_result = session.get_api_read(root_id).unwrap(); let read_response = session.get_api_read(root_id).unwrap();
let read_result = redactions.redacted_yaml(read_result); assert_yaml_snapshot!(read_response.intern_and_redact(&mut redactions, root_id));
assert_yaml_snapshot!(read_result);
}); });
} }
@@ -51,25 +42,15 @@ fn scripts() {
fn just_txt() { fn just_txt() {
run_serve_test("just-txt.txt", |session, mut redactions| { run_serve_test("just-txt.txt", |session, mut redactions| {
let info = session.get_api_rojo().unwrap(); let info = session.get_api_rojo().unwrap();
let root_id = info.root_instance_id; let root_id = info.root_instance_id;
let info = redactions.redacted_yaml(info);
assert_yaml_snapshot!(info); assert_yaml_snapshot!(redactions.redacted_yaml(info));
let read_result = session.get_api_read(root_id).unwrap(); let read_response = session.get_api_read(root_id).unwrap();
redactions.intern_iter(read_result.instances.keys().copied()); assert_yaml_snapshot!(read_response.intern_and_redact(&mut redactions, root_id));
let read_result = redactions.redacted_yaml(read_result);
assert_yaml_snapshot!(read_result);
fs::write(session.path(), "Changed content!").unwrap(); fs::write(session.path(), "Changed content!").unwrap();
// TODO: Directly served files currently don't trigger changed events! // TODO: Directly served files currently don't trigger changed events!
// let subscribe_result = session.get_api_subscribe(0).unwrap();
// let subscribe_result = redactions.redacted_yaml(subscribe_result);
// assert_yaml_snapshot!(subscribe_result);
}); });
} }

View File

@@ -6,6 +6,7 @@ use std::{
}; };
use rbx_dom_weak::RbxId; use rbx_dom_weak::RbxId;
use serde::Serialize;
use tempfile::{tempdir, TempDir}; use tempfile::{tempdir, TempDir};
use librojo::web_interface::{ReadResponse, ServerInfoResponse, SubscribeResponse}; use librojo::web_interface::{ReadResponse, ServerInfoResponse, SubscribeResponse};
@@ -35,19 +36,33 @@ pub fn run_serve_test(test_name: &str, callback: impl FnOnce(TestServeSession, R
settings.bind(move || callback(session, redactions)); settings.bind(move || callback(session, redactions));
} }
/// Intern the response to Rojo's read API, doing traversal in a deterministic pub trait Internable<T> {
/// order. fn intern(&self, redactions: &mut RedactionMap, extra: T);
pub fn intern_read_response( }
redactions: &mut RedactionMap,
response: &ReadResponse,
root_id: RbxId,
) {
redactions.intern(root_id);
let root_instance = response.instances.get(&root_id).unwrap(); impl Internable<RbxId> for ReadResponse<'_> {
fn intern(&self, redactions: &mut RedactionMap, root_id: RbxId) {
redactions.intern(root_id);
for &child_id in root_instance.children.iter() { let root_instance = self.instances.get(&root_id).unwrap();
intern_read_response(redactions, response, child_id);
for &child_id in root_instance.children.iter() {
self.intern(redactions, child_id);
}
}
}
pub trait InternAndRedact<T> {
fn intern_and_redact(&self, redactions: &mut RedactionMap, extra: T) -> serde_yaml::Value;
}
impl<I, T> InternAndRedact<T> for I
where
I: Serialize + Internable<T>,
{
fn intern_and_redact(&self, redactions: &mut RedactionMap, extra: T) -> serde_yaml::Value {
self.intern(redactions, extra);
redactions.redacted_yaml(self)
} }
} }