From f1729163cffca045241cb078963f83d6b5f32f4f Mon Sep 17 00:00:00 2001 From: Lucien Greathouse Date: Tue, 3 Sep 2019 13:48:27 -0700 Subject: [PATCH] Add foundations for 'rojo serve' tests --- Cargo.lock | 2 + rojo-test/Cargo.toml | 4 +- .../placeholder/default.project.json | 9 ++++ rojo-test/src/build_test.rs | 5 ++- rojo-test/src/lib.rs | 6 +-- rojo-test/src/serve_test.rs | 42 ++++++++++++++++++- rojo-test/src/util.rs | 7 +++- 7 files changed, 65 insertions(+), 10 deletions(-) create mode 100644 rojo-test/serve-tests/placeholder/default.project.json diff --git a/Cargo.lock b/Cargo.lock index 18471fba..13ac387b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1572,7 +1572,9 @@ dependencies = [ name = "rojo-test" version = "0.1.0" dependencies = [ + "env_logger 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)", "insta 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", "paste 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", "reqwest 0.9.20 (registry+https://github.com/rust-lang/crates.io-index)", "rojo 0.5.0", diff --git a/rojo-test/Cargo.toml b/rojo-test/Cargo.toml index fdda8754..d8a95026 100644 --- a/rojo-test/Cargo.toml +++ b/rojo-test/Cargo.toml @@ -10,6 +10,8 @@ insta = "0.10.0" paste = "0.1.5" tempfile = "3.1.0" reqwest = "0.9.20" +env_logger = "0.6.2" +log = "0.4.8" # We execute Rojo via std::process::Command, so depend on it so it's built! -rojo = { path = ".." } \ No newline at end of file +rojo = { path = ".." } diff --git a/rojo-test/serve-tests/placeholder/default.project.json b/rojo-test/serve-tests/placeholder/default.project.json new file mode 100644 index 00000000..01868e2a --- /dev/null +++ b/rojo-test/serve-tests/placeholder/default.project.json @@ -0,0 +1,9 @@ +{ + "name": "placeholder", + "tree": { + "$className": "StringValue", + "$properties": { + "Value": "Hello, world!" + } + } +} \ No newline at end of file diff --git a/rojo-test/src/build_test.rs b/rojo-test/src/build_test.rs index fcfa7a3f..1cb3bfca 100644 --- a/rojo-test/src/build_test.rs +++ b/rojo-test/src/build_test.rs @@ -11,6 +11,7 @@ macro_rules! gen_build_tests { paste::item! { #[test] fn []() { + let _ = env_logger::try_init(); run_build_test(stringify!($test_name)); } } @@ -54,9 +55,9 @@ fn run_build_test(test_name: &str) { let build_test_path = get_build_tests_path(); let working_dir = get_working_dir_path(); - let output_dir = tempdir().expect("couldn't create temporary directory"); - let input_path = build_test_path.join(test_name); + + let output_dir = tempdir().expect("couldn't create temporary directory"); let output_path = output_dir.path().join(format!("{}.rbxmx", test_name)); let exe_path = get_rojo_path(); diff --git a/rojo-test/src/lib.rs b/rojo-test/src/lib.rs index 8e0cd9b6..786d9410 100644 --- a/rojo-test/src/lib.rs +++ b/rojo-test/src/lib.rs @@ -1,7 +1,5 @@ -mod util; +#![cfg(test)] -#[cfg(test)] mod build_test; - -#[cfg(test)] mod serve_test; +mod util; diff --git a/rojo-test/src/serve_test.rs b/rojo-test/src/serve_test.rs index e5b6a9a6..82a089c9 100644 --- a/rojo-test/src/serve_test.rs +++ b/rojo-test/src/serve_test.rs @@ -1 +1,41 @@ -use std::path::Path; \ No newline at end of file +use std::process::Command; + +use crate::util::{get_rojo_path, get_serve_tests_path, get_working_dir_path}; + +#[test] +fn serve_test() { + let _ = env_logger::try_init(); + + let serve_test_path = get_serve_tests_path(); + let working_dir = get_working_dir_path(); + + let input_path = serve_test_path.join("placeholder"); + + let exe_path = get_rojo_path(); + + let mut handle = Command::new(exe_path) + .args(&["serve", input_path.to_str().unwrap(), "--port", "35103"]) + .current_dir(working_dir) + .spawn() + .expect("Couldn't start Rojo"); + + loop { + let mut response = match reqwest::get("http://localhost:35103/api/rojo") { + Ok(res) => res, + Err(err) => { + log::info!("Server error, retrying: {}", err); + std::thread::sleep(std::time::Duration::from_millis(30)); + continue; + } + }; + + let text = response.text().expect("Couldn't get response text"); + + log::info!("Got response body: {}", text); + break; + } + + handle + .kill() + .expect("Rojo server was not running at end of test"); +} diff --git a/rojo-test/src/util.rs b/rojo-test/src/util.rs index d4df13ea..5835010d 100644 --- a/rojo-test/src/util.rs +++ b/rojo-test/src/util.rs @@ -13,7 +13,10 @@ pub fn get_rojo_path() -> PathBuf { pub fn get_working_dir_path() -> PathBuf { let mut manifest_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")); - assert!(manifest_dir.pop(), "Manifest directory did not have a parent"); + assert!( + manifest_dir.pop(), + "Manifest directory did not have a parent" + ); manifest_dir } @@ -25,4 +28,4 @@ pub fn get_build_tests_path() -> PathBuf { pub fn get_serve_tests_path() -> PathBuf { let manifest_dir = Path::new(env!("CARGO_MANIFEST_DIR")); manifest_dir.join("serve-tests") -} \ No newline at end of file +}