Skip to content

Commit

Permalink
bump tree-fs and remove test crates (#1026)
Browse files Browse the repository at this point in the history
* bump tree fs and remove test crates

* remove unused cargo_metadata crate
  • Loading branch information
kaplanelad authored Nov 26, 2024
1 parent c7acac3 commit 1fb0cbf
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 29 deletions.
8 changes: 1 addition & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
2 changes: 1 addition & 1 deletion loco-gen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
5 changes: 2 additions & 3 deletions loco-gen/src/testutil.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use std::{
};

use regex::Regex;
use tempfile::tempdir;

// Define the custom struct to encapsulate file content
pub struct FileContent {
Expand Down Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions src/controller/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -452,14 +452,15 @@ mod tests {
#[tokio::test]
async fn view_response() {
let yaml_content = r"
drop: true
files:
- path: template/test.html
content: |-
- {{foo}}
";

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();
Expand Down Expand Up @@ -547,14 +548,15 @@ mod tests {
#[tokio::test]
async fn builder_view_response() {
let yaml_content = r"
drop: true
files:
- path: template/test.html
content: |-
- {{foo}}
";

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()
Expand Down
3 changes: 2 additions & 1 deletion src/controller/views/engines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ mod tests {
#[test]
fn can_render_view() {
let yaml_content = r"
drop: true
files:
- path: template/test.html
content: |-
Expand All @@ -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"}))
Expand Down
21 changes: 14 additions & 7 deletions src/scheduler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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()
Expand Down
23 changes: 15 additions & 8 deletions tests/controller/middlewares.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
"<h1>404 not found</h1>",
Expand All @@ -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,
Expand Down Expand Up @@ -367,24 +368,30 @@ 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"),
"<h1>fallback response</h1>",
)
.create()
.unwrap()
.join("static_content.html"),
.unwrap(),
)
} else {
None
};

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()
};
Expand All @@ -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, "<h1>fallback response</h1>".to_string());
}

Expand Down

0 comments on commit 1fb0cbf

Please sign in to comment.