From b632cf43baec53c21f5573b596373770d5807a0b Mon Sep 17 00:00:00 2001 From: Jacob Marshall Date: Mon, 17 Feb 2025 07:40:03 +0000 Subject: [PATCH] refactor: apply RustRover lint suggestions (#569) --- crates/tuono_internal/tests/utils.rs | 2 +- crates/tuono_lib/src/payload.rs | 2 +- crates/tuono_lib/tests/server_test.rs | 10 +- crates/tuono_lib/tests/utils/mock_server.rs | 105 +++++++++++++++++++ crates/tuono_lib/tests/utils/mod.rs | 2 +- crates/tuono_lib/tests/utils/utils.rs | 106 -------------------- 6 files changed, 113 insertions(+), 114 deletions(-) create mode 100644 crates/tuono_lib/tests/utils/mock_server.rs diff --git a/crates/tuono_internal/tests/utils.rs b/crates/tuono_internal/tests/utils.rs index da591559..3a537f93 100644 --- a/crates/tuono_internal/tests/utils.rs +++ b/crates/tuono_internal/tests/utils.rs @@ -43,7 +43,7 @@ impl TempTuonoProject { impl Drop for TempTuonoProject { fn drop(&mut self) { // Set back the current dir in the previous state - env::set_current_dir(self.original_dir.to_owned()) + env::set_current_dir(&self.original_dir) .expect("Failed to restore the original directory."); } } diff --git a/crates/tuono_lib/src/payload.rs b/crates/tuono_lib/src/payload.rs index 80d963e7..b8cfaf39 100644 --- a/crates/tuono_lib/src/payload.rs +++ b/crates/tuono_lib/src/payload.rs @@ -166,7 +166,7 @@ mod tests { use crate::manifest::BundleInfo; - fn prepare_payload<'a>(uri: Option<&'a str>, mode: Mode) -> Payload<'a> { + fn prepare_payload(uri: Option<&str>, mode: Mode) -> Payload { let mut manifest_mock = HashMap::new(); manifest_mock.insert( "client-main".to_string(), diff --git a/crates/tuono_lib/tests/server_test.rs b/crates/tuono_lib/tests/server_test.rs index 5835e2d7..4ccba5d4 100644 --- a/crates/tuono_lib/tests/server_test.rs +++ b/crates/tuono_lib/tests/server_test.rs @@ -1,5 +1,5 @@ mod utils; -use crate::utils::utils::MockTuonoServer; +use crate::utils::mock_server::MockTuonoServer; use serial_test::serial; #[tokio::test] @@ -15,7 +15,7 @@ async fn api_endpoint_work() { let server_url = format!("http://{}:{}", &app.address, &app.port); let response = client - .get(&format!("{server_url}/health_check")) + .get(format!("{server_url}/health_check")) .send() .await .expect("Failed to execute request."); @@ -38,7 +38,7 @@ async fn not_found_route() { let server_url = format!("http://{}:{}", &app.address, &app.port); let response = client - .get(&format!("{server_url}/not-found")) + .get(format!("{server_url}/not-found")) .send() .await .expect("Failed to execute request."); @@ -64,7 +64,7 @@ async fn index_html_route() { let server_url = format!("http://{}:{}", &app.address, &app.port); let response = client - .get(&format!("{server_url}/")) + .get(format!("{server_url}/")) .send() .await .expect("Failed to execute request."); @@ -91,7 +91,7 @@ async fn api_route_route() { let server_url = format!("http://{}:{}", &app.address, &app.port); let response = client - .get(&format!("{server_url}/tuono/data")) + .get(format!("{server_url}/tuono/data")) .send() .await .expect("Failed to execute request."); diff --git a/crates/tuono_lib/tests/utils/mock_server.rs b/crates/tuono_lib/tests/utils/mock_server.rs new file mode 100644 index 00000000..ee904363 --- /dev/null +++ b/crates/tuono_lib/tests/utils/mock_server.rs @@ -0,0 +1,105 @@ +use fs_extra::dir::create_all; +use std::fs::File; +use std::io::Write; +use std::path::PathBuf; +use std::{env, fs}; +use tempfile::{tempdir, TempDir}; +use tuono_lib::axum::routing::get; +use tuono_lib::{axum::Router, tuono_internal_init_v8_platform, Mode, Server}; + +use crate::utils::health_check::get_tuono_internal_api as health_check; +use crate::utils::route as html_route; +use crate::utils::route::tuono_internal_api as route_api; + +use std::sync::Once; + +static INIT: Once = Once::new(); + +fn init_v8() { + INIT.call_once(|| { + tuono_internal_init_v8_platform(); + }) +} + +fn add_file_with_content<'a>(path: &'a str, content: &'a str) { + let path = PathBuf::from(path); + create_all( + path.parent().expect("File path does not have any parent"), + false, + ) + .unwrap_or_else(|_| { + panic!( + "Failed to create parent file directories: {}", + path.display() + ) + }); + + let mut file = File::create(path).expect("Failed to create the file"); + file.write_all(content.as_bytes()) + .expect("Failed to write into the file"); +} + +#[derive(Debug)] +pub struct MockTuonoServer { + pub address: String, + pub port: u16, + original_dir: PathBuf, + #[allow(dead_code)] + // Required for dropping the temp_dir when this struct drops + temp_dir: TempDir, +} + +impl MockTuonoServer { + pub async fn spawn() -> Self { + init_v8(); + let original_dir = env::current_dir().expect("Failed to read current_dir"); + let temp_dir = tempdir().expect("Failed to create temp_dir"); + + let react_prod_build = fs::read_to_string("./tests/assets/fake_react_build.js") + .expect("Failed to read fake_react_build.js"); + + env::set_current_dir(temp_dir.path()).expect("Failed to change current dir into temp_dir"); + + add_file_with_content( + "./.tuono/config/config.json", + r#"{"server": {"host": "127.0.0.1", "port": 0}}"#, + ); + + add_file_with_content("./out/server/prod-server.js", react_prod_build.as_str()); + + add_file_with_content( + "./out/client/.vite/manifest.json", + r#"{"client-main.tsx": { "file": "assets/index.js", "name": "index", "src": "index.tsx", "isEntry": true,"dynamicImports": [],"css": []}}"#, + ); + + let router = Router::new() + .route("/", get(html_route::tuono_internal_route)) + .route("/tuono/data", get(html_route::tuono_internal_api)) + .route("/health_check", get(health_check)) + .route("/route-api", get(route_api)); + + let server = Server::init(router, Mode::Prod).await; + + let socket = server + .listener + .local_addr() + .expect("Failed to extract test server socket"); + + _ = tokio::spawn(server.start()); + + MockTuonoServer { + address: socket.ip().to_string(), + port: socket.port(), + original_dir, + temp_dir, + } + } +} + +impl Drop for MockTuonoServer { + fn drop(&mut self) { + // Set back the current dir in the previous state + env::set_current_dir(&self.original_dir) + .expect("Failed to restore the original directory."); + } +} diff --git a/crates/tuono_lib/tests/utils/mod.rs b/crates/tuono_lib/tests/utils/mod.rs index 62e8bbf3..93dc2e9d 100644 --- a/crates/tuono_lib/tests/utils/mod.rs +++ b/crates/tuono_lib/tests/utils/mod.rs @@ -1,3 +1,3 @@ pub mod health_check; +pub mod mock_server; pub mod route; -pub mod utils; diff --git a/crates/tuono_lib/tests/utils/utils.rs b/crates/tuono_lib/tests/utils/utils.rs index 7f7a7872..e69de29b 100644 --- a/crates/tuono_lib/tests/utils/utils.rs +++ b/crates/tuono_lib/tests/utils/utils.rs @@ -1,106 +0,0 @@ -use fs_extra::dir::create_all; -use std::fs::File; -use std::io::Write; -use std::path::PathBuf; -use std::{env, fs}; -use tempfile::{tempdir, TempDir}; -use tuono_lib::axum::routing::get; -use tuono_lib::{axum::Router, tuono_internal_init_v8_platform, Mode, Server}; - -use crate::utils::health_check::get_tuono_internal_api as health_check; -use crate::utils::route as html_route; -use crate::utils::route::tuono_internal_api as route_api; - -use std::sync::Once; - -static INIT: Once = Once::new(); - -fn init_v8() { - INIT.call_once(|| { - tuono_internal_init_v8_platform(); - }) -} - -fn add_file_with_content<'a>(path: &'a str, content: &'a str) { - let path = PathBuf::from(path); - create_all( - path.parent().expect("File path does not have any parent"), - false, - ) - .expect( - format!( - "Failed to create parent file directories: {}", - path.display() - ) - .as_str(), - ); - - let mut file = File::create(path).expect("Failed to create the file"); - file.write_all(content.as_bytes()) - .expect("Failed to write into the file"); -} - -#[derive(Debug)] -pub struct MockTuonoServer { - pub address: String, - pub port: u16, - original_dir: PathBuf, - #[allow(dead_code)] - // Required for dropping the temp_dir when this struct drops - temp_dir: TempDir, -} - -impl MockTuonoServer { - pub async fn spawn() -> Self { - init_v8(); - let original_dir = env::current_dir().expect("Failed to read current_dir"); - let temp_dir = tempdir().expect("Failed to create temp_dir"); - - let react_prod_build = fs::read_to_string("./tests/assets/fake_react_build.js") - .expect("Failed to read fake_react_build.js"); - - env::set_current_dir(temp_dir.path()).expect("Failed to change current dir into temp_dir"); - - add_file_with_content( - "./.tuono/config/config.json", - r#"{"server": {"host": "127.0.0.1", "port": 0}}"#, - ); - - add_file_with_content("./out/server/prod-server.js", react_prod_build.as_str()); - - add_file_with_content( - "./out/client/.vite/manifest.json", - r#"{"client-main.tsx": { "file": "assets/index.js", "name": "index", "src": "index.tsx", "isEntry": true,"dynamicImports": [],"css": []}}"#, - ); - - let router = Router::new() - .route("/", get(html_route::tuono_internal_route)) - .route("/tuono/data", get(html_route::tuono_internal_api)) - .route("/health_check", get(health_check)) - .route("/route-api", get(route_api)); - - let server = Server::init(router, Mode::Prod).await; - - let socket = server - .listener - .local_addr() - .expect("Failed to extract test server socket"); - - let _ = tokio::spawn(server.start()); - - MockTuonoServer { - address: socket.ip().to_string(), - port: socket.port(), - original_dir, - temp_dir, - } - } -} - -impl Drop for MockTuonoServer { - fn drop(&mut self) { - // Set back the current dir in the previous state - env::set_current_dir(self.original_dir.to_owned()) - .expect("Failed to restore the original directory."); - } -}