Fix formatting of YAML merge key error messages

This commit is contained in:
Oliver Hamlet
2025-03-25 22:02:34 +00:00
parent d6781f6a32
commit 415242b299
2 changed files with 48 additions and 1 deletions
+2 -1
View File
@@ -347,12 +347,13 @@ impl std::fmt::Display for YamlMergeKeyError {
let yaml = to_yaml(&self.value);
if saphyr::YamlEmitter::new(&mut output).dump(&yaml).is_ok() {
// The emitter starts the dumped YAML with ---\n, so strip that.
write!(
f,
"invalid YAML merge key value at line {} column {}: {}",
self.value.span.start.line(),
self.value.span.start.col(),
output
output.get(4..).unwrap_or_default()
)
} else {
write!(
+46
View File
@@ -72,3 +72,49 @@ fn merge_hashes(
}
hash1
}
#[cfg(test)]
mod tests {
use super::*;
mod process_merge_keys {
use crate::metadata::parse;
use super::*;
#[test]
fn should_error_if_merge_key_value_has_a_single_value_that_is_not_a_hash() {
let yaml = parse(
"
- &anchor1 test
- <<: *anchor1
value: test-value-2",
);
let error_message = process_merge_keys(yaml).unwrap_err().to_string();
assert_eq!(
"invalid YAML merge key value at line 3 column 6: test",
error_message
);
}
#[test]
fn should_error_if_merge_key_value_is_an_array_of_non_hash_values() {
let yaml = parse(
"
- &anchor1 {key: test-key}
- &anchor2 test
- <<: [*anchor1, *anchor2]
value: test-value-2",
);
let error_message = process_merge_keys(yaml).unwrap_err().to_string();
assert_eq!(
"invalid YAML merge key value at line 4 column 17: test",
error_message
);
}
}
}