diff --git a/Cargo.toml b/Cargo.toml index e65bc9adc..2d9793f89 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -181,16 +181,10 @@ features = [ features = ["testing"] [dev-dependencies] -cargo_metadata = "0.18.1" loco-rs = { path = ".", features = ["testing"] } rstest = "0.21.0" insta = { version = "1.34.0", features = ["redactions", "yaml", "filters"] } -tree-fs = { version = "0.1.0" } +tree-fs = { version = "0.2.1" } reqwest = { version = "0.12.7" } serial_test = "3.1.1" tower = { workspace = true, features = ["util"] } - -# generator tests -tempfile = "3" -duct_sh = { version = "0.13.7" } -syn = { version = "2", features = ["full"] } diff --git a/loco-gen/Cargo.toml b/loco-gen/Cargo.toml index eb47efa16..6c976536c 100644 --- a/loco-gen/Cargo.toml +++ b/loco-gen/Cargo.toml @@ -27,5 +27,5 @@ dialoguer = "0.11" duct = "0.13" [dev-dependencies] -tempfile = "3" +tree-fs = { version = "0.2.1" } syn = { version = "2", features = ["full"] } diff --git a/loco-gen/src/testutil.rs b/loco-gen/src/testutil.rs index 44b2a1e77..6b88b48b8 100644 --- a/loco-gen/src/testutil.rs +++ b/loco-gen/src/testutil.rs @@ -11,7 +11,6 @@ use std::{ }; use regex::Regex; -use tempfile::tempdir; // Define the custom struct to encapsulate file content pub struct FileContent { @@ -249,8 +248,8 @@ where let previous = env::current_dir()?; // Get the current directory println!("Current directory: {previous:?}"); - let temp_dir = tempdir()?; // Create a temporary directory - let current = temp_dir.path(); + let tree_fs = tree_fs::TreeBuilder::default().drop(true).create()?; // Create a temporary directory + let current = &tree_fs.root; println!("Temporary directory: {current:?}"); env::set_current_dir(current)?; // Set the current directory to the temp directory diff --git a/src/controller/format.rs b/src/controller/format.rs index 113222767..5d10900fe 100644 --- a/src/controller/format.rs +++ b/src/controller/format.rs @@ -452,6 +452,7 @@ mod tests { #[tokio::test] async fn view_response() { let yaml_content = r" + drop: true files: - path: template/test.html content: |- @@ -459,7 +460,7 @@ mod tests { "; let tree_res = tree_fs::from_yaml_str(yaml_content).unwrap(); - let v = TeraView::from_custom_dir(&tree_res).unwrap(); + let v = TeraView::from_custom_dir(&tree_res.root).unwrap(); assert_debug_snapshot!(view(&v, "template/none.html", serde_json::json!({}))); let response = view(&v, "template/test.html", serde_json::json!({"foo": "loco"})).unwrap(); @@ -547,6 +548,7 @@ mod tests { #[tokio::test] async fn builder_view_response() { let yaml_content = r" + drop: true files: - path: template/test.html content: |- @@ -554,7 +556,7 @@ mod tests { "; let tree_res = tree_fs::from_yaml_str(yaml_content).unwrap(); - let v = TeraView::from_custom_dir(&tree_res).unwrap(); + let v = TeraView::from_custom_dir(&tree_res.root).unwrap(); assert_debug_snapshot!(view(&v, "template/none.html", serde_json::json!({}))); let response = render() diff --git a/src/controller/views/engines.rs b/src/controller/views/engines.rs index be83b7d1e..6967bc676 100644 --- a/src/controller/views/engines.rs +++ b/src/controller/views/engines.rs @@ -95,6 +95,7 @@ mod tests { #[test] fn can_render_view() { let yaml_content = r" + drop: true files: - path: template/test.html content: |- @@ -105,7 +106,7 @@ mod tests { "; let tree_res = tree_fs::from_yaml_str(yaml_content).unwrap(); - let v = TeraView::from_custom_dir(&tree_res).unwrap(); + let v = TeraView::from_custom_dir(&tree_res.root).unwrap(); assert_eq!( v.render("template/test.html", json!({"foo": "foo-txt"})) diff --git a/src/scheduler.rs b/src/scheduler.rs index 367e04b77..12b64e493 100644 --- a/src/scheduler.rs +++ b/src/scheduler.rs @@ -453,21 +453,22 @@ mod tests { pub async fn can_run() { let mut scheduler = get_scheduler_from_config().unwrap(); - let path = tree_fs::Tree::default() + let tree_fs = tree_fs::TreeBuilder::default() + .drop(true) .add("scheduler.txt", "") .add("scheduler2.txt", "") .create() .unwrap(); assert_eq!( - std::fs::read_to_string(path.join("scheduler.txt")) + std::fs::read_to_string(tree_fs.root.join("scheduler.txt")) .unwrap() .lines() .count(), 0 ); assert_eq!( - std::fs::read_to_string(path.join("scheduler2.txt")) + std::fs::read_to_string(tree_fs.root.join("scheduler2.txt")) .unwrap() .lines() .count(), @@ -478,7 +479,10 @@ mod tests { ( "test".to_string(), Job { - run: format!("echo loco >> {}", path.join("scheduler.txt").display()), + run: format!( + "echo loco >> {}", + tree_fs.root.join("scheduler.txt").display() + ), shell: true, cron: "run every 1 second".to_string(), tags: None, @@ -488,7 +492,10 @@ mod tests { ( "test_2".to_string(), Job { - run: format!("echo loco >> {}", path.join("scheduler2.txt").display()), + run: format!( + "echo loco >> {}", + tree_fs.root.join("scheduler2.txt").display() + ), shell: true, cron: "* * * * * ? *".to_string(), tags: None, @@ -505,14 +512,14 @@ mod tests { handle.abort(); assert!( - std::fs::read_to_string(path.join("scheduler.txt")) + std::fs::read_to_string(tree_fs.root.join("scheduler.txt")) .unwrap() .lines() .count() >= 4 ); assert!( - std::fs::read_to_string(path.join("scheduler2.txt")) + std::fs::read_to_string(tree_fs.root.join("scheduler2.txt")) .unwrap() .lines() .count() diff --git a/tests/controller/middlewares.rs b/tests/controller/middlewares.rs index e68ded28a..4743ba427 100644 --- a/tests/controller/middlewares.rs +++ b/tests/controller/middlewares.rs @@ -253,7 +253,8 @@ async fn static_assets() { configure_insta!(); let base_static_assets_path = PathBuf::from("assets").join("static"); - let static_asset_path = tree_fs::Tree::default() + let static_asset_path = tree_fs::TreeBuilder::default() + .drop(true) .add( base_static_assets_path.join("404.html"), "

404 not found

", @@ -266,7 +267,7 @@ async fn static_assets() { .expect("create static tree file"); let mut ctx: AppContext = tests_cfg::app::get_app_context().await; - let base_static_path = static_asset_path.join(base_static_assets_path); + let base_static_path = static_asset_path.root.join(base_static_assets_path); ctx.config.server.middlewares.static_assets = Some(middleware::static_assets::StaticAssets { enable: true, must_exist: true, @@ -367,16 +368,16 @@ async fn fallback( ) { let mut ctx: AppContext = tests_cfg::app::get_app_context().await; - let file = if file { + let maybe_file = if file { Some( - tree_fs::Tree::default() + tree_fs::TreeBuilder::default() + .drop(true) .add( PathBuf::from("static_content.html"), "

fallback response

", ) .create() - .unwrap() - .join("static_content.html"), + .unwrap(), ) } else { None @@ -384,7 +385,13 @@ async fn fallback( let mut fallback_config = middleware::fallback::Fallback { enable: true, - file: file.clone().map(|f| f.display().to_string()), + file: maybe_file.as_ref().map(|tree_fs| { + tree_fs + .root + .join("static_content.html") + .display() + .to_string() + }), not_found: not_found.clone(), ..Default::default() }; @@ -408,7 +415,7 @@ async fn fallback( } let response_text = res.text().await.expect("response text"); - if file.is_some() { + if maybe_file.is_some() { assert_eq!(response_text, "

fallback response

".to_string()); }