#![allow(deprecated)]
use std::path::PathBuf;
use tempfile::TempDir;
use tree_type::ValidatorResult;
use tree_type::tree_type;
#[allow(dead_code)]
fn check_toml(config: &TestProjectConfig) -> ValidatorResult {
if config.as_path().extension().and_then(|s| s.to_str()) == Some("toml") {
ValidatorResult {
errors: vec![],
warnings: vec![],
}
} else {
ValidatorResult {
errors: vec!["File must have .toml extension".to_string()],
warnings: vec![],
}
}
}
tree_type! {
TestProject {
#[required]
src/,
#[optional]
cache/,
#[required]
#[validate(check_toml)]
config("config.toml")
}
}
#[test]
fn test_required_optional_parsing() {
let project = TestProject::new(PathBuf::from("/test")).unwrap();
let _src = project.src();
let _cache = project.cache();
let _config = project.config();
}
#[test]
fn test_validate_with_attributes() {
let project = TestProject::new(PathBuf::from("/test")).unwrap();
let report = project.validate();
assert!(!report.errors.is_empty());
}
#[test]
fn test_validator_function_called() {
let tempdir = TempDir::new().unwrap();
let project = TestProject::new(tempdir.path()).unwrap();
project.setup().unwrap();
std::fs::write(project.config().as_path(), "# config").unwrap();
let report = project.validate();
assert!(
report.is_ok(),
"Validation should pass: {:?}",
report.errors
);
}
#[test]
fn test_ensure_only_creates_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 passes (custom validator doesn't check existence)"
);
assert!(project.src().exists(), "Required src/ created by setup()");
assert!(
project.cache().exists(),
"Optional cache/ also created by setup()"
);
assert!(
!project.config().exists(),
"Config file not created (no default)"
);
}