Files
khadhroony-solana-project/crates/ksp-app-config-desk/unit_tests/bootstrap.rs
2026-08-16 20:15:58 +02:00

118 lines
5.5 KiB
Rust

// file: crates/ksp-app-config-desk/unit_tests/bootstrap.rs
// version: 2
#[test]
fn fallback_logging_is_console_only_and_bounded_to_info() {
let settings = super::fallback_logging_settings();
assert_eq!(settings.default_filter(), ksp_logging_lib::LogFilterLevel::Info);
assert_eq!(settings.span_events(), ksp_logging_lib::SpanEvents::Off);
assert!(settings.files().is_empty());
let console = settings.console();
assert!(console.is_some());
if let std::option::Option::Some(console) = console {
assert!(console.enabled());
assert_eq!(console.output(), ksp_logging_lib::ConsoleOutput::Stderr);
assert_eq!(console.format(), ksp_logging_lib::LogFormat::Human);
}
}
#[test]
fn invalid_logging_source_is_planned_as_transient_fallback_before_subscriber_installation() {
let fixture = BootstrapFixture::new("invalid-source");
assert!(fixture.is_ok());
let fixture = match fixture {
std::result::Result::Ok(value) => value,
std::result::Result::Err(_) => return,
};
let malformed = std::fs::write(fixture.config_root.join(ksp_config_lib::DEFAULT_STD_LOGGING_FILENAME), "{ invalid json");
assert!(malformed.is_ok());
let management = fixture.management();
assert!(management.is_ok());
let management = match management {
std::result::Result::Ok(value) => value,
std::result::Result::Err(_) => return,
};
let plan = super::resolve_logging_startup(&management);
let fallback = matches!(&plan, super::LoggingStartupPlan::Fallback { .. });
assert!(fallback, "invalid Logging source must produce a fallback startup plan");
if let super::LoggingStartupPlan::Fallback { diagnostic, settings, .. } = plan {
assert_eq!(settings.default_filter(), ksp_logging_lib::LogFilterLevel::Info);
assert!(settings.files().is_empty());
assert!(!diagnostic.domain.is_empty());
assert!(!diagnostic.code.is_empty());
assert!(!diagnostic.message.is_empty());
}
}
struct BootstrapFixture {
root: std::path::PathBuf,
config_root: std::path::PathBuf,
schema_root: std::path::PathBuf,
}
impl BootstrapFixture {
fn new(label: &str) -> ksp_core_lib::Result<Self> {
let root = std::env::temp_dir().join(std::format!("ksp-config-desk-{label}-{}", std::process::id()));
let cleanup = std::fs::remove_dir_all(root.as_path());
match cleanup {
std::result::Result::Ok(()) | std::result::Result::Err(_) => {},
}
let config_root = root.join("config");
let schema_root = root.join("schemas");
let create_config = std::fs::create_dir_all(config_root.as_path());
if let std::result::Result::Err(error) = create_config {
return std::result::Result::Err(
ksp_core_lib::Error::new(crate::ERROR_CODE_APP_STATE_INVALID, "Cannot create bootstrap test config root").with_source(error),
);
}
let create_schema = std::fs::create_dir_all(schema_root.as_path());
if let std::result::Result::Err(error) = create_schema {
return std::result::Result::Err(
ksp_core_lib::Error::new(crate::ERROR_CODE_APP_STATE_INVALID, "Cannot create bootstrap test schema root").with_source(error),
);
}
let workspace = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"));
let workspace = match workspace.parent().and_then(std::path::Path::parent) {
std::option::Option::Some(value) => value,
std::option::Option::None => {
return std::result::Result::Err(ksp_core_lib::Error::new(
crate::ERROR_CODE_APP_STATE_INVALID,
"Cannot resolve workspace root for bootstrap test",
));
},
};
let source_schema = workspace.join(ksp_config_lib::DEFAULT_SCHEMA_PATH).join(ksp_config_lib::DEFAULT_STD_LOGGING_SCHEMA_FILENAME);
let destination_schema = schema_root.join(ksp_config_lib::DEFAULT_STD_LOGGING_SCHEMA_FILENAME);
let copied = std::fs::copy(source_schema.as_path(), destination_schema.as_path());
if let std::result::Result::Err(error) = copied {
return std::result::Result::Err(
ksp_core_lib::Error::new(crate::ERROR_CODE_APP_STATE_INVALID, "Cannot copy bootstrap test Logging schema").with_source(error),
);
}
return std::result::Result::Ok(Self { root, config_root, schema_root });
}
fn management(&self) -> ksp_core_lib::Result<ksp_config_lib::ConfigManagement> {
let bootstrap = ksp_config_lib::ConfigBootstrapOptions::from_paths(self.config_root.as_path(), self.schema_root.as_path());
let bootstrap = match bootstrap {
std::result::Result::Ok(value) => value,
std::result::Result::Err(error) => return std::result::Result::Err(error),
};
let registry = ksp_config_lib::ConfigFileRegistry::defaults();
let registry = match registry {
std::result::Result::Ok(value) => value,
std::result::Result::Err(error) => return std::result::Result::Err(error),
};
return std::result::Result::Ok(ksp_config_lib::ConfigManagement::new(ksp_config_lib::ConfigDocumentEngine::new(bootstrap, registry)));
}
}
impl std::ops::Drop for BootstrapFixture {
fn drop(&mut self) {
let cleanup = std::fs::remove_dir_all(self.root.as_path());
match cleanup {
std::result::Result::Ok(()) | std::result::Result::Err(_) => {},
}
}
}