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",
"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_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)",
"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"
serde = "1.0.99"
serde_json = "1.0.40"
serde_yaml = "0.8.9"
tempfile = "3.1.0"
walkdir = "2.2.9"
rojo-insta-ext = { path = "../rojo-insta-ext" }
# We execute Rojo via std::process::Command, so depend on it so it's built!

View File

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

View File

@@ -2,22 +2,18 @@ use std::fs;
use insta::assert_yaml_snapshot;
use crate::serve_util::{intern_read_response, run_serve_test};
use crate::serve_util::{run_serve_test, InternAndRedact};
#[test]
fn empty() {
run_serve_test("empty", |session, mut redactions| {
let info = session.get_api_rojo().unwrap();
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();
intern_read_response(&mut redactions, &read_result, root_id);
let read_result = redactions.redacted_yaml(read_result);
assert_yaml_snapshot!(read_result);
let read_response = session.get_api_read(root_id).unwrap();
assert_yaml_snapshot!(read_response.intern_and_redact(&mut redactions, root_id));
});
}
@@ -27,23 +23,18 @@ fn scripts() {
let info = session.get_api_rojo().unwrap();
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();
intern_read_response(&mut redactions, &read_result, root_id);
let read_result = redactions.redacted_yaml(read_result);
assert_yaml_snapshot!(read_result);
let read_response = session.get_api_read(root_id).unwrap();
assert_yaml_snapshot!(read_response.intern_and_redact(&mut redactions, root_id));
fs::write(session.path().join("foo.lua"), "Updated foo!").unwrap();
let subscribe_result = session.get_api_subscribe(0).unwrap();
let subscribe_result = redactions.redacted_yaml(subscribe_result);
assert_yaml_snapshot!(subscribe_result);
let subscribe_response = session.get_api_subscribe(0).unwrap();
assert_yaml_snapshot!(redactions.redacted_yaml(subscribe_response));
let read_result = session.get_api_read(root_id).unwrap();
let read_result = redactions.redacted_yaml(read_result);
assert_yaml_snapshot!(read_result);
let read_response = session.get_api_read(root_id).unwrap();
assert_yaml_snapshot!(read_response.intern_and_redact(&mut redactions, root_id));
});
}
@@ -51,25 +42,15 @@ fn scripts() {
fn just_txt() {
run_serve_test("just-txt.txt", |session, mut redactions| {
let info = session.get_api_rojo().unwrap();
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();
redactions.intern_iter(read_result.instances.keys().copied());
let read_result = redactions.redacted_yaml(read_result);
assert_yaml_snapshot!(read_result);
let read_response = session.get_api_read(root_id).unwrap();
assert_yaml_snapshot!(read_response.intern_and_redact(&mut redactions, root_id));
fs::write(session.path(), "Changed content!").unwrap();
// 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 serde::Serialize;
use tempfile::{tempdir, TempDir};
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));
}
/// Intern the response to Rojo's read API, doing traversal in a deterministic
/// order.
pub fn intern_read_response(
redactions: &mut RedactionMap,
response: &ReadResponse,
root_id: RbxId,
) {
redactions.intern(root_id);
pub trait Internable<T> {
fn intern(&self, redactions: &mut RedactionMap, extra: T);
}
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() {
intern_read_response(redactions, response, child_id);
let root_instance = self.instances.get(&root_id).unwrap();
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)
}
}