v0.1.3-pre.008
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
// file: crates/ksp-config-lib/unit_tests/document.rs
|
||||
// version: 1
|
||||
// version: 2
|
||||
|
||||
#[test]
|
||||
fn committed_logging_document_passes_registered_schema_and_semantic_validation() {
|
||||
@@ -143,6 +143,85 @@ fn schema_valid_logging_document_can_still_fail_ksp_semantics() {
|
||||
cleanup_fixture(&fixture);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn duplicate_profile_ids_are_semantically_invalid() {
|
||||
let fixture = fixture_roots("duplicate-profile-id");
|
||||
let workspace = workspace_root();
|
||||
let schema_source = std::fs::read_to_string(workspace.join("config/schemas/std.logging.schema.json"));
|
||||
assert!(schema_source.is_ok(), "committed Logging schema should be readable: {schema_source:?}");
|
||||
if let std::result::Result::Ok(schema_source) = schema_source {
|
||||
let source = valid_logging_source_with_profiles("first", "first", "first");
|
||||
let prepared = prepare_fixture(&fixture, std::option::Option::Some(source.as_str()), schema_source.as_str());
|
||||
assert!(prepared.is_ok(), "duplicate profile fixture should be writable: {prepared:?}");
|
||||
if prepared.is_ok() {
|
||||
let result = load_fixture(&fixture);
|
||||
assert_error_code(result, crate::ERROR_CODE_DOCUMENT_SEMANTIC_INVALID);
|
||||
}
|
||||
}
|
||||
cleanup_fixture(&fixture);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn missing_default_profile_target_is_semantically_invalid() {
|
||||
let fixture = fixture_roots("missing-default-profile");
|
||||
let workspace = workspace_root();
|
||||
let schema_source = std::fs::read_to_string(workspace.join("config/schemas/std.logging.schema.json"));
|
||||
assert!(schema_source.is_ok(), "committed Logging schema should be readable: {schema_source:?}");
|
||||
if let std::result::Result::Ok(schema_source) = schema_source {
|
||||
let source = valid_logging_source_with_profiles("missing", "first", "second");
|
||||
let prepared = prepare_fixture(&fixture, std::option::Option::Some(source.as_str()), schema_source.as_str());
|
||||
assert!(prepared.is_ok(), "missing default profile fixture should be writable: {prepared:?}");
|
||||
if prepared.is_ok() {
|
||||
let result = load_fixture(&fixture);
|
||||
assert_error_code(result, crate::ERROR_CODE_DOCUMENT_SEMANTIC_INVALID);
|
||||
}
|
||||
}
|
||||
cleanup_fixture(&fixture);
|
||||
}
|
||||
|
||||
fn valid_logging_source_with_profiles(default_profile: &str, first_profile: &str, second_profile: &str) -> String {
|
||||
return format!(
|
||||
r#"{{
|
||||
"format_version": 1,
|
||||
"logs_directory": "logs",
|
||||
"default_profile": "{default_profile}",
|
||||
"profiles": [
|
||||
{},
|
||||
{}
|
||||
]
|
||||
}}"#,
|
||||
valid_logging_profile(first_profile, "file.first"),
|
||||
valid_logging_profile(second_profile, "file.second")
|
||||
);
|
||||
}
|
||||
|
||||
fn valid_logging_profile(profile_id: &str, output_id: &str) -> String {
|
||||
return format!(
|
||||
r#"{{
|
||||
"profile_id": "{profile_id}",
|
||||
"default_filter": "info",
|
||||
"span_events": "off",
|
||||
"console": {{
|
||||
"enabled": false,
|
||||
"output": "stderr",
|
||||
"ansi": false,
|
||||
"format": "human",
|
||||
"filter": {{"level": "trace", "targets": ["*"], "domains": ["*"]}}
|
||||
}},
|
||||
"files": [{{
|
||||
"output_id": "{output_id}",
|
||||
"enabled": true,
|
||||
"path": "output.log",
|
||||
"rotation": "daily",
|
||||
"format": "human",
|
||||
"ansi": false,
|
||||
"filter": {{"level": "info", "targets": ["*"], "domains": ["*"]}}
|
||||
}}],
|
||||
"target_filters": []
|
||||
}}"#
|
||||
);
|
||||
}
|
||||
|
||||
fn load_fixture(fixture: &FixtureRoots) -> ksp_core_lib::Result<super::ConfigJsonDocument> {
|
||||
let bootstrap = crate::ConfigBootstrapOptions::from_paths(fixture.config.as_path(), fixture.schemas.as_path());
|
||||
let bootstrap = match bootstrap {
|
||||
@@ -212,7 +291,7 @@ struct FixtureRoots {
|
||||
|
||||
fn fixture_roots(name: &str) -> FixtureRoots {
|
||||
let mut root = std::env::temp_dir();
|
||||
root.push(format!("ksp-config-lib-pre007-{name}-{}", std::process::id()));
|
||||
root.push(format!("ksp-config-lib-pre008-{name}-{}", std::process::id()));
|
||||
return FixtureRoots { config: root.join("config"), schemas: root.join("schemas"), root };
|
||||
}
|
||||
|
||||
|
||||
70
crates/ksp-config-lib/unit_tests/profile.rs
Normal file
70
crates/ksp-config-lib/unit_tests/profile.rs
Normal file
@@ -0,0 +1,70 @@
|
||||
// file: crates/ksp-config-lib/unit_tests/profile.rs
|
||||
// version: 1
|
||||
|
||||
#[test]
|
||||
fn committed_default_profile_resolves_globals_profile_and_provenance() {
|
||||
let engine = committed_engine();
|
||||
let file_id = crate::ConfigFileId::new(crate::FILE_ID_STD_LOGGING);
|
||||
assert!(engine.is_ok(), "committed Config engine should be constructible: {engine:?}");
|
||||
assert!(file_id.is_ok(), "logging file_id should be valid: {file_id:?}");
|
||||
if let (std::result::Result::Ok(engine), std::result::Result::Ok(file_id)) = (engine, file_id) {
|
||||
let resolved = engine.load_resolved_profile(&file_id, std::option::Option::None);
|
||||
assert!(resolved.is_ok(), "default profile should resolve: {resolved:?}");
|
||||
if let std::result::Result::Ok(resolved) = resolved {
|
||||
assert_eq!(resolved.profile_id(), "local_dev");
|
||||
assert_eq!(resolved.selection_source(), super::ConfigProfileSelectionSource::DefaultProfile);
|
||||
assert_eq!(resolved.globals().get("logs_directory").and_then(serde_json::Value::as_str), std::option::Option::Some("${KSP_LOGS_DIRECTORY:-logs}"));
|
||||
assert_eq!(resolved.profile().get("default_filter").and_then(serde_json::Value::as_str), std::option::Option::Some("warn"));
|
||||
assert_eq!(resolved.effective().get("default_filter").and_then(serde_json::Value::as_str), std::option::Option::Some("warn"));
|
||||
assert_eq!(resolved.origin("logs_directory"), std::option::Option::Some(super::ConfigValueOrigin::Global));
|
||||
assert_eq!(resolved.origin("default_filter"), std::option::Option::Some(super::ConfigValueOrigin::Profile));
|
||||
assert!(!resolved.effective().contains_key("default_profile"));
|
||||
assert!(!resolved.effective().contains_key("profiles"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn explicit_profile_selection_is_distinct_from_default_selection() {
|
||||
let engine = committed_engine();
|
||||
let file_id = crate::ConfigFileId::new(crate::FILE_ID_STD_LOGGING);
|
||||
assert!(engine.is_ok(), "committed Config engine should be constructible: {engine:?}");
|
||||
assert!(file_id.is_ok(), "logging file_id should be valid: {file_id:?}");
|
||||
if let (std::result::Result::Ok(engine), std::result::Result::Ok(file_id)) = (engine, file_id) {
|
||||
let resolved = engine.load_resolved_profile(&file_id, std::option::Option::Some("local_dev"));
|
||||
assert!(resolved.is_ok(), "explicit committed profile should resolve: {resolved:?}");
|
||||
if let std::result::Result::Ok(resolved) = resolved {
|
||||
assert_eq!(resolved.profile_id(), "local_dev");
|
||||
assert_eq!(resolved.selection_source(), super::ConfigProfileSelectionSource::Explicit);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unknown_explicit_profile_has_distinct_error_code() {
|
||||
let engine = committed_engine();
|
||||
let file_id = crate::ConfigFileId::new(crate::FILE_ID_STD_LOGGING);
|
||||
assert!(engine.is_ok(), "committed Config engine should be constructible: {engine:?}");
|
||||
assert!(file_id.is_ok(), "logging file_id should be valid: {file_id:?}");
|
||||
if let (std::result::Result::Ok(engine), std::result::Result::Ok(file_id)) = (engine, file_id) {
|
||||
let result = engine.load_resolved_profile(&file_id, std::option::Option::Some("does-not-exist"));
|
||||
assert!(result.is_err(), "unknown explicit profile must be rejected: {result:?}");
|
||||
if let std::result::Result::Err(error) = result {
|
||||
assert_eq!(error.code(), crate::ERROR_CODE_PROFILE_NOT_FOUND);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn committed_engine() -> ksp_core_lib::Result<crate::ConfigDocumentEngine> {
|
||||
let workspace = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../..");
|
||||
let bootstrap = crate::ConfigBootstrapOptions::from_paths(workspace.join("config"), workspace.join("config/schemas"));
|
||||
let bootstrap = match bootstrap {
|
||||
std::result::Result::Ok(value) => value,
|
||||
std::result::Result::Err(error) => return std::result::Result::Err(error),
|
||||
};
|
||||
let registry = crate::ConfigFileRegistry::defaults();
|
||||
return match registry {
|
||||
std::result::Result::Ok(value) => std::result::Result::Ok(crate::ConfigDocumentEngine::new(bootstrap, value)),
|
||||
std::result::Result::Err(error) => std::result::Result::Err(error),
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user