use jbuild::build::{BuildSystem, BuildWrapper, WrapperType};
use std::fs;
use tempfile::TempDir;
#[test]
fn test_build_system_detection_maven() {
let temp_dir = TempDir::new().unwrap();
assert_eq!(BuildSystem::detect(temp_dir.path()), None);
fs::write(temp_dir.path().join("pom.xml"), "<project></project>").unwrap();
assert_eq!(BuildSystem::detect(temp_dir.path()), Some(BuildSystem::Maven));
let subdir = temp_dir.path().join("subdir");
fs::create_dir_all(&subdir).unwrap();
assert_eq!(BuildSystem::detect(&subdir), Some(BuildSystem::Maven));
}
#[test]
fn test_build_system_detection_gradle() {
let temp_dir = TempDir::new().unwrap();
fs::write(temp_dir.path().join("build.gradle"), "plugins { id 'java' }").unwrap();
assert_eq!(BuildSystem::detect(temp_dir.path()), Some(BuildSystem::Gradle));
fs::remove_file(temp_dir.path().join("build.gradle")).unwrap();
fs::write(temp_dir.path().join("build.gradle.kts"), "plugins { java }").unwrap();
assert_eq!(BuildSystem::detect(temp_dir.path()), Some(BuildSystem::Gradle));
}
#[test]
fn test_build_system_detection_priority() {
let temp_dir = TempDir::new().unwrap();
fs::write(temp_dir.path().join("pom.xml"), "<project></project>").unwrap();
fs::write(temp_dir.path().join("build.gradle"), "plugins { id 'java' }").unwrap();
assert_eq!(BuildSystem::detect(temp_dir.path()), Some(BuildSystem::Maven));
}
#[test]
fn test_build_file_path_generation() {
let temp_dir = TempDir::new().unwrap();
assert_eq!(
BuildSystem::Maven.build_file_path(temp_dir.path()),
temp_dir.path().join("pom.xml")
);
assert_eq!(
BuildSystem::Gradle.build_file_path(temp_dir.path()),
temp_dir.path().join("build.gradle")
);
}
#[test]
fn test_build_file_name_generation() {
assert_eq!(BuildSystem::Maven.build_file_name(), "pom.xml");
assert_eq!(BuildSystem::Gradle.build_file_name(), "build.gradle");
}
#[test]
fn test_maven_wrapper_detection() {
let temp_dir = TempDir::new().unwrap();
assert!(BuildWrapper::detect(temp_dir.path()).is_none());
fs::write(temp_dir.path().join("mvnw"), "#!/bin/bash\necho mvnw").unwrap();
let wrapper = BuildWrapper::detect(temp_dir.path()).unwrap();
assert_eq!(wrapper.wrapper_type, WrapperType::Maven);
assert_eq!(wrapper.script_path, temp_dir.path().join("mvnw"));
}
#[test]
fn test_gradle_wrapper_detection() {
let temp_dir = TempDir::new().unwrap();
fs::write(temp_dir.path().join("gradlew"), "#!/bin/bash\necho gradlew").unwrap();
let wrapper = BuildWrapper::detect(temp_dir.path()).unwrap();
assert_eq!(wrapper.wrapper_type, WrapperType::Gradle);
assert_eq!(wrapper.script_path, temp_dir.path().join("gradlew"));
}
#[test]
fn test_wrapper_detection_priority() {
let temp_dir = TempDir::new().unwrap();
fs::write(temp_dir.path().join("mvnw"), "#!/bin/bash\necho mvnw").unwrap();
fs::write(temp_dir.path().join("gradlew"), "#!/bin/bash\necho gradlew").unwrap();
let wrapper = BuildWrapper::detect(temp_dir.path()).unwrap();
assert_eq!(wrapper.wrapper_type, WrapperType::Gradle);
}
#[test]
fn test_maven_wrapper_properties_parsing() {
let temp_dir = TempDir::new().unwrap();
fs::write(temp_dir.path().join("mvnw"), "#!/bin/bash\necho mvnw").unwrap();
fs::create_dir_all(temp_dir.path().join(".mvn/wrapper")).unwrap();
let properties_content = r#"#Maven Wrapper Properties
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.4/apache-maven-3.9.4-bin.zip
wrapperUrl=https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.2.0/maven-wrapper-3.2.0.jar"#;
fs::write(
temp_dir.path().join(".mvn/wrapper/maven-wrapper.properties"),
properties_content,
).unwrap();
let wrapper = BuildWrapper::detect(temp_dir.path()).unwrap();
assert_eq!(wrapper.get_version(), Some("3.9.4".to_string()));
}
#[test]
fn test_gradle_wrapper_properties_parsing() {
let temp_dir = TempDir::new().unwrap();
fs::write(temp_dir.path().join("gradlew"), "#!/bin/bash\necho gradlew").unwrap();
fs::create_dir_all(temp_dir.path().join("gradle/wrapper")).unwrap();
let properties_content = r#"#Gradle Wrapper Properties
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip
networkTimeout=10000
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists"#;
fs::write(
temp_dir.path().join("gradle/wrapper/gradle-wrapper.properties"),
properties_content,
).unwrap();
let wrapper = BuildWrapper::detect(temp_dir.path()).unwrap();
assert_eq!(wrapper.get_version(), Some("8.4".to_string()));
}
#[test]
fn test_wrapper_version_parsing_formats() {
let test_cases = vec![
("https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.4/apache-maven-3.9.4-bin.zip", Some("3.9.4".to_string())),
("https://services.gradle.org/distributions/gradle-8.4-bin.zip", Some("8.4".to_string())),
("https://services.gradle.org/distributions/gradle-7.6.1-all.zip", Some("7.6.1".to_string())),
("invalid-url", None),
("", None),
];
for (url, expected_version) in test_cases {
let temp_dir = TempDir::new().unwrap();
fs::write(temp_dir.path().join("mvnw"), "#!/bin/bash").unwrap();
fs::create_dir_all(temp_dir.path().join(".mvn/wrapper")).unwrap();
let properties = format!("distributionUrl={url}");
fs::write(
temp_dir.path().join(".mvn/wrapper/maven-wrapper.properties"),
properties,
).unwrap();
let wrapper = BuildWrapper::detect(temp_dir.path()).unwrap();
assert_eq!(wrapper.get_version(), expected_version, "Failed for URL: {url}");
}
}
#[test]
fn test_build_system_default_goals() {
assert_eq!(BuildSystem::Maven.default_goals(), vec!["compile".to_string()]);
assert_eq!(BuildSystem::Gradle.default_goals(), vec!["build".to_string()]);
}
#[test]
fn test_wrapper_script_path_resolution() {
let temp_dir = TempDir::new().unwrap();
fs::write(temp_dir.path().join("mvnw"), "#!/bin/bash").unwrap();
let wrapper = BuildWrapper::detect(temp_dir.path()).unwrap();
assert_eq!(wrapper.script_path.file_name().unwrap(), "mvnw");
fs::remove_file(temp_dir.path().join("mvnw")).unwrap();
fs::write(temp_dir.path().join("gradlew"), "#!/bin/bash").unwrap();
let wrapper = BuildWrapper::detect(temp_dir.path()).unwrap();
assert_eq!(wrapper.script_path.file_name().unwrap(), "gradlew");
}
#[test]
fn test_wrapper_detection_missing_properties() {
let temp_dir = TempDir::new().unwrap();
fs::write(temp_dir.path().join("mvnw"), "#!/bin/bash").unwrap();
let wrapper = BuildWrapper::detect(temp_dir.path()).unwrap();
assert_eq!(wrapper.get_version(), None);
}
#[test]
fn test_build_system_detection_subdirectory() {
let temp_dir = TempDir::new().unwrap();
fs::write(temp_dir.path().join("pom.xml"), "<project></project>").unwrap();
let subdir = temp_dir.path().join("src/main/java/com/example");
fs::create_dir_all(&subdir).unwrap();
assert_eq!(BuildSystem::detect(&subdir), Some(BuildSystem::Maven));
}
#[test]
fn test_build_system_detection_with_symlinks() {
let temp_dir = TempDir::new().unwrap();
let linked_dir = temp_dir.path().join("linked");
let actual_dir = temp_dir.path().join("actual");
fs::create_dir_all(&actual_dir).unwrap();
fs::write(actual_dir.join("pom.xml"), "<project></project>").unwrap();
#[cfg(unix)]
{
use std::os::unix::fs::symlink;
symlink(&actual_dir, &linked_dir).unwrap();
assert_eq!(BuildSystem::detect(&linked_dir), Some(BuildSystem::Maven));
}
}
#[test]
fn test_wrapper_type_enum() {
assert_eq!(WrapperType::Maven as u8, 0);
assert_eq!(WrapperType::Gradle as u8, 1);
assert_eq!(format!("{:?}", WrapperType::Maven), "Maven");
assert_eq!(format!("{:?}", WrapperType::Gradle), "Gradle");
}
#[test]
fn test_build_system_enum() {
assert_eq!(BuildSystem::Maven as u8, 0);
assert_eq!(BuildSystem::Gradle as u8, 1);
assert_eq!(format!("{:?}", BuildSystem::Maven), "Maven");
assert_eq!(format!("{:?}", BuildSystem::Gradle), "Gradle");
}