Files
rojo/rojo-test/src/serve_test.rs
2019-11-18 18:16:37 -08:00

209 lines
6.7 KiB
Rust

use std::fs;
use insta::assert_yaml_snapshot;
use tempfile::tempdir;
use crate::{internable::InternAndRedact, serve_util::run_serve_test};
#[test]
fn empty() {
run_serve_test("empty", |session, mut redactions| {
let info = session.get_api_rojo().unwrap();
let root_id = info.root_instance_id;
assert_yaml_snapshot!("empty_info", redactions.redacted_yaml(info));
let read_response = session.get_api_read(root_id).unwrap();
assert_yaml_snapshot!(
"empty_all",
read_response.intern_and_redact(&mut redactions, root_id)
);
});
}
#[test]
fn scripts() {
run_serve_test("scripts", |session, mut redactions| {
let info = session.get_api_rojo().unwrap();
let root_id = info.root_instance_id;
assert_yaml_snapshot!("scripts_info", redactions.redacted_yaml(info));
let read_response = session.get_api_read(root_id).unwrap();
assert_yaml_snapshot!(
"scripts_all",
read_response.intern_and_redact(&mut redactions, root_id)
);
fs::write(session.path().join("foo.lua"), "Updated foo!").unwrap();
let subscribe_response = session.get_api_subscribe(0).unwrap();
assert_yaml_snapshot!(
"scripts_subscribe",
subscribe_response.intern_and_redact(&mut redactions, ())
);
let read_response = session.get_api_read(root_id).unwrap();
assert_yaml_snapshot!(
"scripts_all-2",
read_response.intern_and_redact(&mut redactions, root_id)
);
});
}
#[test]
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;
assert_yaml_snapshot!("just_txt_info", redactions.redacted_yaml(info));
let read_response = session.get_api_read(root_id).unwrap();
assert_yaml_snapshot!(
"just_txt_all",
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!
});
}
#[test]
fn add_folder() {
run_serve_test("add_folder", |session, mut redactions| {
let info = session.get_api_rojo().unwrap();
let root_id = info.root_instance_id;
assert_yaml_snapshot!("add_folder_info", redactions.redacted_yaml(info));
let read_response = session.get_api_read(root_id).unwrap();
assert_yaml_snapshot!(
"add_folder_all",
read_response.intern_and_redact(&mut redactions, root_id)
);
fs::create_dir(session.path().join("my-new-folder")).unwrap();
let subscribe_response = session.get_api_subscribe(0).unwrap();
assert_yaml_snapshot!(
"add_folder_subscribe",
subscribe_response.intern_and_redact(&mut redactions, ())
);
let read_response = session.get_api_read(root_id).unwrap();
assert_yaml_snapshot!(
"add_folder_all-2",
read_response.intern_and_redact(&mut redactions, root_id)
);
});
}
#[test]
fn remove_file() {
run_serve_test("remove_file", |session, mut redactions| {
let info = session.get_api_rojo().unwrap();
let root_id = info.root_instance_id;
assert_yaml_snapshot!("remove_file_info", redactions.redacted_yaml(info));
let read_response = session.get_api_read(root_id).unwrap();
assert_yaml_snapshot!(
"remove_file_all",
read_response.intern_and_redact(&mut redactions, root_id)
);
fs::remove_file(session.path().join("hello.txt")).unwrap();
let subscribe_response = session.get_api_subscribe(0).unwrap();
assert_yaml_snapshot!(
"remove_file_subscribe",
subscribe_response.intern_and_redact(&mut redactions, ())
);
let read_response = session.get_api_read(root_id).unwrap();
assert_yaml_snapshot!(
"remove_file_all-2",
read_response.intern_and_redact(&mut redactions, root_id)
);
});
}
#[test]
fn edit_init() {
run_serve_test("edit_init", |session, mut redactions| {
let info = session.get_api_rojo().unwrap();
let root_id = info.root_instance_id;
assert_yaml_snapshot!("edit_init_info", redactions.redacted_yaml(info));
let read_response = session.get_api_read(root_id).unwrap();
assert_yaml_snapshot!(
"edit_init_all",
read_response.intern_and_redact(&mut redactions, root_id)
);
fs::write(session.path().join("init.lua"), b"-- Edited contents").unwrap();
let subscribe_response = session.get_api_subscribe(0).unwrap();
assert_yaml_snapshot!(
"edit_init_subscribe",
subscribe_response.intern_and_redact(&mut redactions, ())
);
let read_response = session.get_api_read(root_id).unwrap();
assert_yaml_snapshot!(
"edit_init_all-2",
read_response.intern_and_redact(&mut redactions, root_id)
);
});
}
#[test]
fn move_folder_of_stuff() {
run_serve_test("move_folder_of_stuff", |session, mut redactions| {
let info = session.get_api_rojo().unwrap();
let root_id = info.root_instance_id;
assert_yaml_snapshot!("move_folder_of_stuff_info", redactions.redacted_yaml(info));
let read_response = session.get_api_read(root_id).unwrap();
assert_yaml_snapshot!(
"move_folder_of_stuff_all",
read_response.intern_and_redact(&mut redactions, root_id)
);
// Create a directory full of stuff we can move in
let src_dir = tempdir().unwrap();
let stuff_path = src_dir.path().join("new-stuff");
fs::create_dir(&stuff_path).unwrap();
// Make a bunch of random files in our stuff folder
for i in 0..10 {
let file_name = stuff_path.join(format!("{}.txt", i));
let file_contents = format!("File #{}", i);
fs::write(file_name, file_contents).unwrap();
}
// We're hoping that this rename gets picked up as one event. This test
// will fail otherwise.
fs::rename(stuff_path, session.path().join("new-stuff")).unwrap();
let subscribe_response = session.get_api_subscribe(0).unwrap();
assert_yaml_snapshot!(
"move_folder_of_stuff_subscribe",
subscribe_response.intern_and_redact(&mut redactions, ())
);
let read_response = session.get_api_read(root_id).unwrap();
assert_yaml_snapshot!(
"move_folder_of_stuff_all-2",
read_response.intern_and_redact(&mut redactions, root_id)
);
});
}