#![allow(deprecated)]
use tempfile::TempDir;
use tree_type::tree_type;
tree_type! {
TestProject {
#[required]
tasks/ as TasksDir,
#[required]
backlog/ {
p1/,
p2/
},
#[required]
config("config.toml") as ConfigFile
}
}
#[test]
fn test_directory_with_custom_type_and_required() {
let tempdir = TempDir::new().unwrap();
let project = TestProject::new(tempdir.path()).unwrap();
let report = project.validate();
assert!(!report.is_ok(), "Should have validation errors");
assert!(
report
.errors
.iter()
.any(|e| e.path == project.tasks().as_path()),
"Should have error for missing tasks/"
);
std::fs::create_dir(project.tasks().as_path()).unwrap();
let report = project.validate();
if !report.is_ok() {
eprintln!("Validation errors after creating tasks/:");
for err in &report.errors {
eprintln!(" - {:?}: {}", err.path, err.message);
}
}
assert!(
!report
.errors
.iter()
.any(|e| e.path == project.tasks().as_path()),
"Should NOT have error for tasks/ after creating it"
);
}
#[test]
fn test_directory_with_children_parent_required_children_optional() {
let tempdir = TempDir::new().unwrap();
let project = TestProject::new(tempdir.path()).unwrap();
let report = project.validate();
assert!(!report.is_ok(), "Should have validation errors");
assert!(
report
.errors
.iter()
.any(|e| e.path == project.backlog().as_path()),
"Should have error for missing backlog/"
);
std::fs::create_dir(project.backlog().as_path()).unwrap();
let report = project.validate();
assert!(
!report
.errors
.iter()
.any(|e| e.path == project.backlog().as_path()),
"Should NOT have error for backlog/ after creating it"
);
assert!(
!report
.errors
.iter()
.any(|e| e.path == project.backlog().p1().as_path()),
"Should NOT have error for optional p1/"
);
assert!(
!report
.errors
.iter()
.any(|e| e.path == project.backlog().p2().as_path()),
"Should NOT have error for optional p2/"
);
}
#[test]
fn test_file_with_custom_type_and_required() {
let tempdir = TempDir::new().unwrap();
let project = TestProject::new(tempdir.path()).unwrap();
let report = project.validate();
assert!(!report.is_ok(), "Should have validation errors");
assert!(
report
.errors
.iter()
.any(|e| e.path == project.config().as_path()),
"Should have error for missing config"
);
std::fs::write(project.config().as_path(), "# config").unwrap();
let report = project.validate();
assert!(
!report
.errors
.iter()
.any(|e| e.path == project.config().as_path()),
"Should NOT have error for config after creating it"
);
}
#[test]
fn test_ensure_creates_only_required_paths() {
let tempdir = TempDir::new().unwrap();
let project = TestProject::new(tempdir.path()).unwrap();
let result = project.ensure();
assert!(result.is_ok());
let report = result.unwrap();
assert!(
!report.is_ok(),
"Validation should fail - config file missing"
);
std::fs::write(project.config().as_path(), "# config").unwrap();
let result = project.ensure();
assert!(result.is_ok());
let report = result.unwrap();
assert!(
report.is_ok(),
"Validation should pass: {:?}",
report.errors
);
assert!(project.tasks().exists(), "Required tasks/ created");
assert!(project.backlog().exists(), "Required backlog/ created");
assert!(project.backlog().p1().exists(), "Optional p1/ also created");
assert!(project.backlog().p2().exists(), "Optional p2/ also created");
assert!(
project.config().exists(),
"Required config created manually"
);
}