Skip to content

Commit

Permalink
[ADD] added a function to test whether, tree structure is good or not
Browse files Browse the repository at this point in the history
  • Loading branch information
lordofwizard committed Oct 17, 2024
1 parent f3ea095 commit 7073951
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 7 deletions.
134 changes: 134 additions & 0 deletions src/build.rs
Original file line number Diff line number Diff line change
@@ -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");
}
}
11 changes: 4 additions & 7 deletions src/config_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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 {}

0 comments on commit 7073951

Please sign in to comment.