use anyhow::Result;
pub(super) fn refuse_unimplemented_verify_flags(compile: bool, lint: bool, lang: Option<&[String]>) -> Result<()> {
let mut requested: Vec<&str> = Vec::new();
if compile {
requested.push("--compile");
}
if lint {
requested.push("--lint");
}
if lang.is_some() {
requested.push("--lang");
}
if requested.is_empty() {
return Ok(());
}
anyhow::bail!(
"`alef verify` does not implement {}. Verification checks freshness of generated \
output only. Run `alef build --lang <langs>` for a compilation check and `alef lint \
--lang <langs>` for a lint check; both accept the language filter. Re-run `alef \
verify` without {} to check freshness.",
requested.join(", "),
requested.join(", "),
)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_plain_verify_is_accepted() {
assert!(refuse_unimplemented_verify_flags(false, false, None).is_ok());
}
#[test]
fn the_deprecated_exit_code_flag_is_not_part_of_the_refusal() {
assert!(refuse_unimplemented_verify_flags(false, false, None).is_ok());
}
#[test]
fn compile_is_refused_rather_than_silently_ignored() {
let error = refuse_unimplemented_verify_flags(true, false, None).expect_err("must refuse");
let message = error.to_string();
assert!(message.contains("--compile"), "names the offending flag: {message}");
assert!(
message.contains("alef build"),
"points at the command that does compile: {message}"
);
}
#[test]
fn lint_is_refused_rather_than_silently_ignored() {
let error = refuse_unimplemented_verify_flags(false, true, None).expect_err("must refuse");
let message = error.to_string();
assert!(message.contains("--lint"), "names the offending flag: {message}");
assert!(
message.contains("alef lint"),
"points at the command that does lint: {message}"
);
}
#[test]
fn lang_is_refused_even_when_the_list_is_empty() {
let error = refuse_unimplemented_verify_flags(false, false, Some(&[])).expect_err("must refuse");
assert!(error.to_string().contains("--lang"), "{error}");
}
#[test]
fn every_requested_flag_is_named_not_just_the_first() {
let languages = vec!["python".to_string()];
let error = refuse_unimplemented_verify_flags(true, true, Some(&languages)).expect_err("must refuse");
let message = error.to_string();
for flag in ["--compile", "--lint", "--lang"] {
assert!(message.contains(flag), "{flag} missing from: {message}");
}
}
}