use std::ffi::OsString;
use std::path::{Path, PathBuf};
pub(crate) const ARTIFACT_DIR_ENV: &str = "KRAB_ARTIFACT_DIR";
pub(crate) const DEFAULT_ARTIFACT_ROOT: &str = ".krab";
pub(crate) const LEGACY_ARTIFACT_ROOT: &str = "internal/audit";
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum ArtifactRootSource {
Env,
LegacyInternalAudit,
Default,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct ArtifactRoot {
pub(crate) path: PathBuf,
pub(crate) source: ArtifactRootSource,
}
pub(crate) fn resolve_artifact_root_in(cwd: &Path, env_value: Option<OsString>) -> ArtifactRoot {
if let Some(value) = env_value.filter(|value| !value.is_empty()) {
return ArtifactRoot {
path: PathBuf::from(value),
source: ArtifactRootSource::Env,
};
}
if cwd.join(LEGACY_ARTIFACT_ROOT).is_dir() {
return ArtifactRoot {
path: PathBuf::from(LEGACY_ARTIFACT_ROOT),
source: ArtifactRootSource::LegacyInternalAudit,
};
}
ArtifactRoot {
path: PathBuf::from(DEFAULT_ARTIFACT_ROOT),
source: ArtifactRootSource::Default,
}
}
pub(crate) fn legacy_fallback_warning() -> String {
format!(
"warning: writing artifacts under `{LEGACY_ARTIFACT_ROOT}/` because that directory exists; \
this fallback is removed in 0.7.0 (the default becomes `{DEFAULT_ARTIFACT_ROOT}/`) — set \
{ARTIFACT_DIR_ENV}={LEGACY_ARTIFACT_ROOT} to keep the current location"
)
}
pub(crate) fn resolve_artifact_root() -> ArtifactRoot {
let root = resolve_artifact_root_in(Path::new("."), std::env::var_os(ARTIFACT_DIR_ENV));
if root.source == ArtifactRootSource::LegacyInternalAudit {
eprintln!("{}", legacy_fallback_warning());
}
root
}
pub(crate) fn default_rehearsal_evidence_path() -> PathBuf {
resolve_artifact_root()
.path
.join("evidence")
.join("rollback-rehearsal-evidence.txt")
}
pub(crate) fn default_release_certify_dir() -> PathBuf {
resolve_artifact_root()
.path
.join("release-certify")
.join("local")
}
#[cfg(test)]
mod tests {
use super::{
legacy_fallback_warning, resolve_artifact_root_in, ArtifactRootSource, ARTIFACT_DIR_ENV,
DEFAULT_ARTIFACT_ROOT, LEGACY_ARTIFACT_ROOT,
};
use std::ffi::OsString;
use std::fs;
use std::path::PathBuf;
#[test]
fn a_plain_directory_defaults_to_dot_krab() {
let dir = tempfile::tempdir().expect("tempdir");
let root = resolve_artifact_root_in(dir.path(), None);
assert_eq!(root.source, ArtifactRootSource::Default);
assert_eq!(root.path, PathBuf::from(DEFAULT_ARTIFACT_ROOT));
}
#[test]
fn an_existing_internal_audit_directory_is_used_as_a_deprecated_fallback() {
let dir = tempfile::tempdir().expect("tempdir");
fs::create_dir_all(dir.path().join("internal/audit")).expect("mkdir");
let root = resolve_artifact_root_in(dir.path(), None);
assert_eq!(root.source, ArtifactRootSource::LegacyInternalAudit);
assert_eq!(root.path, PathBuf::from(LEGACY_ARTIFACT_ROOT));
}
#[test]
fn internal_without_audit_does_not_trigger_the_fallback() {
let dir = tempfile::tempdir().expect("tempdir");
fs::create_dir_all(dir.path().join("internal/plans")).expect("mkdir");
let root = resolve_artifact_root_in(dir.path(), None);
assert_eq!(root.source, ArtifactRootSource::Default);
}
#[test]
fn the_environment_override_wins_over_the_fallback() {
let dir = tempfile::tempdir().expect("tempdir");
fs::create_dir_all(dir.path().join("internal/audit")).expect("mkdir");
let root = resolve_artifact_root_in(dir.path(), Some(OsString::from("build/evidence")));
assert_eq!(root.source, ArtifactRootSource::Env);
assert_eq!(root.path, PathBuf::from("build/evidence"));
}
#[test]
fn an_empty_override_counts_as_unset() {
let dir = tempfile::tempdir().expect("tempdir");
let root = resolve_artifact_root_in(dir.path(), Some(OsString::new()));
assert_eq!(root.source, ArtifactRootSource::Default);
}
#[test]
fn the_fallback_warning_names_the_removal_release_and_the_override() {
let warning = legacy_fallback_warning();
assert!(warning.contains("0.7.0"), "{warning}");
assert!(warning.contains(ARTIFACT_DIR_ENV), "{warning}");
assert!(warning.contains(LEGACY_ARTIFACT_ROOT), "{warning}");
assert_eq!(warning.lines().count(), 1, "{warning}");
}
#[test]
#[serial_test::serial]
fn the_out_defaults_derive_from_the_process_override() {
let previous = std::env::var_os(ARTIFACT_DIR_ENV);
std::env::set_var(ARTIFACT_DIR_ENV, "custom-artifacts");
let rehearsal = super::default_rehearsal_evidence_path();
let certify = super::default_release_certify_dir();
match previous {
Some(value) => std::env::set_var(ARTIFACT_DIR_ENV, value),
None => std::env::remove_var(ARTIFACT_DIR_ENV),
}
assert_eq!(
rehearsal,
PathBuf::from("custom-artifacts/evidence/rollback-rehearsal-evidence.txt")
);
assert_eq!(
certify,
PathBuf::from("custom-artifacts/release-certify/local")
);
}
}