From 7073951d67943fa6e360b2bb9728c5e77d2296e0 Mon Sep 17 00:00:00 2001 From: lordofwizard Date: Thu, 17 Oct 2024 19:40:48 +0530 Subject: [PATCH] [ADD] added a function to test whether, tree structure is good or not --- src/build.rs | 134 ++++++++++++++++++++++++++++++++++++++++++ src/config_builder.rs | 11 ++-- 2 files changed, 138 insertions(+), 7 deletions(-) diff --git a/src/build.rs b/src/build.rs index fec1e15..3ce2aca 100644 --- a/src/build.rs +++ b/src/build.rs @@ -1,3 +1,137 @@ +use std::path::Path; pub fn build() { // TODO Build this } + +pub fn check_project_structure() -> bool { + let required_file = "config.toml"; + let required_dirs = vec!["server", "cache", "log", "java"]; + + if !Path::new(required_file).exists() { + return false; + } + + for dir in required_dirs { + if !Path::new(dir).is_dir() { + return false; + } + } + + // All checks passed + true +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn check_project_structure_test() { + std::env::set_current_dir("/tmp").expect("Failed to set current directory"); + std::process::Command::new("mkdir") + .arg("test_project_for_tree_structure1") + .output() + .expect("Failed to create test project directory"); + std::process::Command::new("touch") + .arg("test_project_for_tree_structure1/config.toml") + .output() + .expect("Failed to create config.toml file"); + std::process::Command::new("mkdir") + .arg("test_project_for_tree_structure1/server") + .output() + .expect("Failed to create server directory"); + std::process::Command::new("mkdir") + .arg("test_project_for_tree_structure1/cache") + .output() + .expect("Failed to create cache directory"); + std::process::Command::new("mkdir") + .arg("test_project_for_tree_structure1/log") + .output() + .expect("Failed to create log directory"); + std::process::Command::new("mkdir") + .arg("test_project_for_tree_structure1/java") + .output() + .expect("Failed to create java directory"); + + std::env::set_current_dir("/tmp/test_project_for_tree_structure1").expect("Failed to set current directory to test_project_for_tree_structure1"); + std::env::set_current_dir("/tmp").expect("Failed to set current directory"); + assert_eq!(check_project_structure(), true); + std::process::Command::new("rm") + .arg("-rf") + .arg("/tmp/test_project_for_tree_structure1") + .output() + .expect("Failed to remove test project directory"); + } + #[test] + fn check_project_structure_test_empty() { + std::env::set_current_dir("/tmp").expect("Failed to set current directory"); + std::process::Command::new("mkdir") + .arg("test_project_for_tree_structure2") + .output() + .expect("Failed to create test project directory"); + + std::env::set_current_dir("/tmp/test_project_for_tree_structure2").expect("Failed to set current directory to test_project_for_tree_structure2"); + assert_eq!(check_project_structure(), false); + std::process::Command::new("rm") + .arg("-rf") + .arg("/tmp/test_project_for_tree_structure2") + .output() + .expect("Failed to remove test project directory"); + } + #[test] + fn check_project_structure_test_except_config() { + std::env::set_current_dir("/tmp").expect("Failed to set current directory"); + std::process::Command::new("mkdir") + .arg("test_project_for_tree_structure3") + .output() + .expect("Failed to create test project directory"); + std::process::Command::new("mkdir") + .arg("test_project_for_tree_structure3/server") + .output() + .expect("Failed to create server directory"); + std::process::Command::new("mkdir") + .arg("test_project_for_tree_structure3/cache") + .output() + .expect("Failed to create cache directory"); + std::process::Command::new("mkdir") + .arg("test_project_for_tree_structure3/log") + .output() + .expect("Failed to create log directory"); + std::process::Command::new("mkdir") + .arg("test_project_for_tree_structure3/java") + .output() + .expect("Failed to create java directory"); + + std::env::set_current_dir("/tmp/test_project_for_tree_structure3").expect("Failed to set current directory to test_project_for_tree_structure3"); + assert_eq!(check_project_structure(), false); + std::process::Command::new("rm") + .arg("-rf") + .arg("/tmp/test_project_for_tree_structure3") + .output() + .expect("Failed to remove test project directory"); + } + #[test] + fn check_project_structure_test_few_folders() { + std::env::set_current_dir("/tmp").expect("Failed to set current directory"); + std::process::Command::new("mkdir") + .arg("test_project_for_tree_structure4") + .output() + .expect("Failed to create test project directory"); + std::process::Command::new("touch") + .arg("test_project_for_tree_structure4/config.toml") + .output() + .expect("Failed to create config.toml file"); + std::process::Command::new("mkdir") + .arg("test_project_for_tree_structure4/log") + .output() + .expect("Failed to create log directory"); + + std::env::set_current_dir("/tmp/test_project_for_tree_structure4").expect("Failed to set current directory to test_project_for_tree_structure4"); + assert_eq!(check_project_structure(), false); + std::process::Command::new("rm") + .arg("-rf") + .arg("/tmp/test_project_for_tree_structure4") + .output() + .expect("Failed to remove test project directory"); + } +} diff --git a/src/config_builder.rs b/src/config_builder.rs index a66046e..f055728 100644 --- a/src/config_builder.rs +++ b/src/config_builder.rs @@ -4,7 +4,7 @@ pub fn config_builder(project_name: &str) { write_toml_file(project_name); } -pub fn write_toml_file(project_name: &str){ +pub fn write_toml_file(project_name: &str) { // Call functions to get the latest versions let latest_java_version = latest_java_version(); let latest_minecraft_version = fetch_latest_minecraft_version(); @@ -35,13 +35,10 @@ providor = "vanilla" let file_path = format!("./{}/config.toml", project_name); // Write the formatted TOML content to the file - std::fs::create_dir_all(format!("./{}", project_name)).expect("Project Should Have been properly setuped"); // Ensure the directory exists + std::fs::create_dir_all(format!("./{}", project_name)) + .expect("Project Should Have been properly setuped"); // Ensure the directory exists std::fs::write(file_path, toml_content).expect("Project Should Have been properly setuped"); - } #[cfg(test)] -mod tests { - -} - +mod tests {}