pub use trusty_common::palace_id::{
derive_palace_id, owner_repo_from_git_remote, palace_override_from_env, parent_dir_slug,
PALACE_OVERRIDE_ENV,
};
pub fn follow_palace_alias(slug: String) -> String {
let Ok(registry_dir) = trusty_common::palace_alias::default_palace_registry_dir() else {
return slug;
};
trusty_common::palace_alias::alias_target_if_absent(®istry_dir, &slug).unwrap_or(slug)
}
#[cfg(test)]
mod tests {
use super::*;
use trusty_common::palace_alias::PalaceAliasStore;
fn with_data_dir<T>(dir: &std::path::Path, f: impl FnOnce() -> T) -> T {
unsafe { std::env::set_var(trusty_common::DATA_DIR_OVERRIDE_ENV, dir) };
let out = f();
unsafe { std::env::remove_var(trusty_common::DATA_DIR_OVERRIDE_ENV) };
out
}
fn seed_palace(registry_dir: &std::path::Path, id: &str) {
let dir = registry_dir.join(id);
std::fs::create_dir_all(&dir).unwrap();
std::fs::write(dir.join("palace.json"), b"{}").unwrap();
}
#[tokio::test]
async fn follow_palace_alias_names_the_target() {
let _guard = crate::commands::env_test_lock().lock().await;
let tmp = tempfile::tempdir().unwrap();
with_data_dir(tmp.path(), || {
let registry = trusty_common::palace_alias::default_palace_registry_dir().unwrap();
seed_palace(®istry, "trusty-tools");
PalaceAliasStore::register_alias(®istry, "bobmatnyc-trusty-tools", "trusty-tools")
.unwrap();
assert_eq!(
follow_palace_alias("bobmatnyc-trusty-tools".to_string()),
"trusty-tools"
);
});
}
#[tokio::test]
async fn follow_palace_alias_leaves_a_real_palace_alone() {
let _guard = crate::commands::env_test_lock().lock().await;
let tmp = tempfile::tempdir().unwrap();
with_data_dir(tmp.path(), || {
let registry = trusty_common::palace_alias::default_palace_registry_dir().unwrap();
seed_palace(®istry, "real-palace");
seed_palace(®istry, "other-palace");
PalaceAliasStore::register_alias(®istry, "real-palace", "other-palace").unwrap();
assert_eq!(
follow_palace_alias("real-palace".to_string()),
"real-palace"
);
assert_eq!(
follow_palace_alias("never-aliased".to_string()),
"never-aliased"
);
});
}
}