Produce an error if `before` or `after` field is not a table

This commit is contained in:
ISSOtm 2021-09-27 21:28:21 +02:00
parent 6b784be616
commit d8ad68c947
1 changed files with 38 additions and 2 deletions

View File

@ -401,7 +401,13 @@ fn determine_preprocessors(config: &Config) -> Result<Vec<Box<dyn Preprocessor>>
|| preprocessor_table.contains_key(name) || preprocessor_table.contains_key(name)
}; };
if let Some(before) = table.get("before").and_then(Value::as_array) { if let Some(before) = table.get("before") {
let before = before.as_array().ok_or_else(|| {
Error::msg(format!(
"Expected preprocessor.{}.before to be an array",
name
))
})?;
for after in before { for after in before {
let after = after.as_str().ok_or_else(|| { let after = after.as_str().ok_or_else(|| {
Error::msg(format!( Error::msg(format!(
@ -416,7 +422,13 @@ fn determine_preprocessors(config: &Config) -> Result<Vec<Box<dyn Preprocessor>>
} }
} }
if let Some(after) = table.get("after").and_then(Value::as_array) { if let Some(after) = table.get("after") {
let after = after.as_array().ok_or_else(|| {
Error::msg(format!(
"Expected preprocessor.{}.after to be an array",
name
))
})?;
for before in after { for before in after {
let before = before.as_str().ok_or_else(|| { let before = before.as_str().ok_or_else(|| {
Error::msg(format!( Error::msg(format!(
@ -630,6 +642,30 @@ mod tests {
assert_eq!(random, "python random.py"); assert_eq!(random, "python random.py");
} }
#[test]
fn preprocessor_before_must_be_array() {
let cfg_str = r#"
[preprocessor.random]
before = 0
"#;
let cfg = Config::from_str(cfg_str).unwrap();
assert!(determine_preprocessors(&cfg).is_err());
}
#[test]
fn preprocessor_after_must_be_array() {
let cfg_str = r#"
[preprocessor.random]
after = 0
"#;
let cfg = Config::from_str(cfg_str).unwrap();
assert!(determine_preprocessors(&cfg).is_err());
}
#[test] #[test]
fn preprocessor_order_is_honored() { fn preprocessor_order_is_honored() {
let cfg_str = r#" let cfg_str = r#"