use anyhow::Result;
use crate::core::config::WorkspaceConfig;
pub(crate) fn sync_alef_version_pin(
workspace: &WorkspaceConfig,
config_path: &std::path::Path,
is_build_clean: bool,
) -> Result<()> {
crate::cli::version_pin::check_alef_toml_version(workspace)?;
crate::cli::version_pin::maybe_update_alef_toml_version_pin(
workspace,
config_path,
workspace.auto_update_alef_version,
is_build_clean,
)?;
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
fn workspace_with(pin: &str, auto_update_alef_version: bool) -> WorkspaceConfig {
let toml = format!("alef_version = \"{pin}\"\nauto_update_alef_version = {auto_update_alef_version}\n");
toml::from_str(&toml).expect("valid workspace config")
}
fn write_fixture_alef_toml(dir: &std::path::Path, pin: &str, auto_update_alef_version: bool) -> std::path::PathBuf {
let path = dir.join("alef.toml");
let config = format!(
"[workspace]\nalef_version = \"{pin}\"\nauto_update_alef_version = {auto_update_alef_version}\nlanguages = []\n"
);
std::fs::write(&path, config).expect("write fixture");
path
}
#[test]
fn opted_in_clean_and_newer_rewrites_the_pin() {
let dir = tempfile::tempdir().expect("tempdir");
let path = write_fixture_alef_toml(dir.path(), "0.0.1", true);
let workspace = workspace_with("0.0.1", true);
sync_alef_version_pin(&workspace, &path, true).expect("must not error");
let after = std::fs::read_to_string(&path).expect("read fixture");
assert!(
after.contains(&format!(
"alef_version = \"{}\"",
crate::cli::version_pin::cli_version()
)),
"pin must be rewritten to the running CLI version:\n{after}"
);
}
#[test]
fn opted_out_by_default_does_not_rewrite_even_when_otherwise_eligible() {
let dir = tempfile::tempdir().expect("tempdir");
let path = write_fixture_alef_toml(dir.path(), "0.0.1", false);
let workspace = workspace_with("0.0.1", false);
let before = std::fs::read_to_string(&path).expect("read fixture");
sync_alef_version_pin(&workspace, &path, true).expect("must not error");
assert_eq!(
std::fs::read_to_string(&path).expect("read fixture"),
before,
"auto_update_alef_version defaults to off; the pin must not move without an explicit opt-in"
);
}
#[test]
fn opted_in_but_dirty_build_does_not_rewrite() {
let dir = tempfile::tempdir().expect("tempdir");
let path = write_fixture_alef_toml(dir.path(), "0.0.1", true);
let workspace = workspace_with("0.0.1", true);
let before = std::fs::read_to_string(&path).expect("read fixture");
sync_alef_version_pin(&workspace, &path, false).expect("must not error");
assert_eq!(
std::fs::read_to_string(&path).expect("read fixture"),
before,
"a dirty build must never rewrite the pin, even opted in"
);
}
#[test]
fn every_known_command_arm_calls_sync_alef_version_pin() {
let call_sites: &[(&str, &str)] = &[
("all_commands.rs", include_str!("all_commands.rs")),
("core_commands.rs", include_str!("core_commands.rs")),
("core_commands/generate.rs", include_str!("core_commands/generate.rs")),
];
for (name, source) in call_sites {
let occurrences = source.matches("sync_alef_version_pin(").count();
assert!(
occurrences > 0,
"{name} must call version_pin_sync::sync_alef_version_pin at least once"
);
}
let all_commands_source = call_sites[0].1;
assert_eq!(
all_commands_source.matches("sync_alef_version_pin(").count(),
2,
"Commands::All must sync the pin both after the initial config load and after a \
registry-version-sync reload"
);
}
}