use super::*;
#[test]
fn test_java_checkstyle_no_cosmetic_checks() {
let mut config = test_config();
config.languages = vec![Language::Java];
let api = test_api();
let all_files = scaffold(&api, &config, &[Language::Java]).unwrap();
let files = language_files(&all_files);
let checkstyle = files.iter().find(|f| f.path.ends_with("checkstyle.xml")).unwrap();
assert!(!checkstyle.content.contains("WhitespaceAfter"));
assert!(!checkstyle.content.contains("WhitespaceAround"));
assert!(!checkstyle.content.contains("GenericWhitespace"));
assert!(!checkstyle.content.contains("EmptyBlock"));
assert!(!checkstyle.content.contains("NeedBraces"));
assert!(!checkstyle.content.contains("MagicNumber"));
assert!(!checkstyle.content.contains("JavadocPackage"));
assert!(checkstyle.content.contains("EqualsHashCode"));
assert!(checkstyle.content.contains("UnusedImports"));
assert!(checkstyle.content.contains("MethodLength"));
assert!(checkstyle.content.contains("LineLength"));
assert!(checkstyle.content.contains("\"200\""));
}
#[test]
fn test_scaffold_java_checkstyle_suppressions_use_config_location() {
let config = test_config();
let api = test_api();
let all_files = scaffold(&api, &config, &[Language::Java]).unwrap();
let files = language_files(&all_files);
let xml = files.iter().find(|f| f.path.ends_with("checkstyle.xml")).unwrap();
assert!(
xml.content.contains(r#"value="checkstyle-suppressions.xml""#),
"checkstyle suppressions path must be relative to project basedir; content:\n{}",
xml.content
);
let properties = files
.iter()
.find(|f| f.path.ends_with("checkstyle.properties"))
.unwrap();
assert!(
properties.content.is_empty(),
"checkstyle properties must be empty (0 bytes) so end-of-file-fixer leaves it untouched on every regen; a lone trailing newline gets stripped back to empty; content:\n{}",
properties.content
);
}
#[test]
fn test_scaffold_java_checkstyle_plugin_excludes_alef_scratch_directory() {
let config = test_config();
let api = test_api();
let all_files = scaffold(&api, &config, &[Language::Java]).unwrap();
let files = language_files(&all_files);
let pom = files.iter().find(|f| f.path.ends_with("pom.xml")).unwrap();
let checkstyle_section = pom
.content
.split("<artifactId>maven-checkstyle-plugin</artifactId>")
.nth(1)
.and_then(|section| section.split("</plugin>").next())
.expect("pom.xml must configure maven-checkstyle-plugin");
assert!(
checkstyle_section.contains("<excludes>") && checkstyle_section.contains(".alef"),
"checkstyle plugin must exclude the .alef/ snippet-validation scratch directory so \
generated snippet scratch sources never fail `mvn compile`; block:\n{checkstyle_section}"
);
}
#[test]
fn test_scaffold_java_javadoc_plugin_documents_only_publishable_sources() {
let config = test_config();
let api = test_api();
let all_files = scaffold(&api, &config, &[Language::Java]).unwrap();
let files = language_files(&all_files);
let pom = files.iter().find(|f| f.path.ends_with("pom.xml")).unwrap();
let javadoc_section = pom
.content
.split("<artifactId>maven-javadoc-plugin</artifactId>")
.nth(1)
.and_then(|section| section.split("</plugin>").next())
.expect("pom.xml must configure maven-javadoc-plugin");
assert!(
javadoc_section.contains("<sourceFileIncludes>"),
"javadoc plugin must restrict which sources it documents, or a basedir sourcepath \
sweeps in src/test/java and fails the build; block:\n{javadoc_section}"
);
let includes = javadoc_section
.split("<sourceFileIncludes>")
.nth(1)
.and_then(|block| block.split("</sourceFileIncludes>").next())
.expect("the <sourceFileIncludes> block must be well-formed");
assert!(
!includes.contains("src/test/java"),
"javadoc must never be pointed at test sources; includes:\n{includes}"
);
assert!(
includes.contains("<sourceFileInclude>src/main/java/**/*.java</sourceFileInclude>"),
"the conventional src/main/java overlay must stay documented; includes:\n{includes}"
);
}
#[test]
fn test_scaffold_java_checkstyle_ignores_alef_scratch_but_still_catches_real_violations() {
if crate::test_support::spawn_from_stable_dir("mvn")
.arg("--version")
.output()
.is_err()
{
return;
}
let _mvn_lock = crate::test_support::RealMvnGuard::acquire();
let maven_repo_local = format!(
"-Dmaven.repo.local={}",
crate::test_support::maven_local_repo_dir().display()
);
let config = test_config();
let api = test_api();
let all_files = scaffold(&api, &config, &[Language::Java]).unwrap();
let files = language_files(&all_files);
let project_dir = tempfile::tempdir().expect("temp project directory");
for name in [
"pom.xml",
"checkstyle.xml",
"checkstyle.properties",
"checkstyle-suppressions.xml",
] {
let file = files
.iter()
.find(|f| f.path.ends_with(name))
.unwrap_or_else(|| panic!("scaffold must emit {name}"));
std::fs::write(project_dir.path().join(name), &file.content).expect("write scaffolded file");
}
let group_id = config.java_group_id();
let source_root = group_id.split('.').next().unwrap_or("dev");
let package_dir = project_dir.path().join(source_root).join("scratchcheck");
std::fs::create_dir_all(&package_dir).expect("package directory");
let long_line = "x".repeat(220);
let real_source = package_dir.join("RealSource.java");
std::fs::write(
&real_source,
format!("package scratchcheck;\n\npublic final class RealSource {{\n // {long_line}\n}}\n"),
)
.expect("write real source");
let real_violation_output = std::process::Command::new("mvn")
.args(["-q", "validate", &maven_repo_local])
.current_dir(project_dir.path())
.output()
.expect("mvn runs");
assert!(
!real_violation_output.status.success(),
"checkstyle must still fail on a genuine violation in a real binding source; stdout:\n{}\nstderr:\n{}",
String::from_utf8_lossy(&real_violation_output.stdout),
String::from_utf8_lossy(&real_violation_output.stderr),
);
std::fs::remove_file(&real_source).expect("remove real violation source");
let scratch_dir = project_dir.path().join(".alef/snippets/sessions/deadbeefcafefeed");
std::fs::create_dir_all(&scratch_dir).expect("scratch session directory");
std::fs::write(
scratch_dir.join("Example.java"),
format!("public final class _TestVisitor {{\n // {long_line}\n}}\n"),
)
.expect("write scratch source");
let scratch_output = std::process::Command::new("mvn")
.args(["-q", "validate", &maven_repo_local])
.current_dir(project_dir.path())
.output()
.expect("mvn runs");
assert!(
scratch_output.status.success(),
"checkstyle must ignore .alef snippet-validation scratch sources; stdout:\n{}\nstderr:\n{}",
String::from_utf8_lossy(&scratch_output.stdout),
String::from_utf8_lossy(&scratch_output.stderr),
);
}