use tempfile::tempdir;
fn setup() -> (tempfile::TempDir, String) {
let dir = tempdir().unwrap();
std::fs::write(dir.path().join("exists.conf"), "x = 1\n").unwrap();
let dir_str = dir.path().display().to_string().replace('\\', "/");
(dir, dir_str)
}
#[test]
fn issue149_existing_file_include_keeps_trailing_field() {
let (_d, dir) = setup();
let input = format!(
"aa = {{include file(\"{}/exists.conf\"), b = \"ww\"}}\n",
dir
);
let cfg = hocon::parse(&input).unwrap();
assert_eq!(cfg.get_i64("aa.x").unwrap(), 1, "included content lost");
assert_eq!(
cfg.get_string("aa.b").unwrap(),
"ww",
"trailing field swallowed"
);
}
#[test]
fn issue149_missing_file_include_keeps_trailing_field() {
let (_d, dir) = setup();
let input = format!("aa = {{include file(\"{}/nope.conf\"), b = \"ww\"}}\n", dir);
let cfg = hocon::parse(&input).unwrap();
assert_eq!(
cfg.get_string("aa.b").unwrap(),
"ww",
"trailing field swallowed"
);
}
#[test]
fn issue149_spaced_parens_keep_trailing_field() {
let (_d, dir) = setup();
let input = format!(
"aa = {{include required( file( \"{}/exists.conf\" ) ), b = \"ww\"}}\n",
dir
);
let cfg = hocon::parse(&input).unwrap();
assert_eq!(cfg.get_i64("aa.x").unwrap(), 1);
assert_eq!(
cfg.get_string("aa.b").unwrap(),
"ww",
"trailing field swallowed"
);
}
#[test]
fn issue149_required_file_include_keeps_trailing_field() {
let (_d, dir) = setup();
let input = format!(
"aa = {{include required(file(\"{}/exists.conf\")), b = \"ww\"}}\n",
dir
);
let cfg = hocon::parse(&input).unwrap();
assert_eq!(cfg.get_i64("aa.x").unwrap(), 1);
assert_eq!(
cfg.get_string("aa.b").unwrap(),
"ww",
"trailing field swallowed"
);
}
#[test]
fn issue149_fused_junk_after_paren_is_parse_error() {
let (_d, dir) = setup();
let input = format!(
"aa = {{include file(\"{}/exists.conf\")junk, b = \"ww\"}}\n",
dir
);
assert!(
hocon::parse(&input).is_err(),
"fused junk after ')' must be a parse error, not silently swallowed"
);
}
#[test]
fn issue149_missing_close_paren_is_parse_error() {
let (_d, dir) = setup();
let input = format!(
"aa = {{include file(\"{}/exists.conf\", b = \"ww\"}}\n",
dir
);
assert!(
hocon::parse(&input).is_err(),
"missing ')' after include file path must be a parse error"
);
}
#[test]
fn issue149_bare_include_keeps_trailing_field() {
let (_d, dir) = setup();
let input = format!("aa = {{include \"{}/nope.conf\", b = \"ww\"}}\n", dir);
let cfg = hocon::parse(&input).unwrap();
assert_eq!(cfg.get_string("aa.b").unwrap(), "ww");
}
#[test]
fn issue149_field_before_file_include_still_survives() {
let (_d, dir) = setup();
let input = format!("aa = {{b = \"ww\", include file(\"{}/nope.conf\")}}\n", dir);
let cfg = hocon::parse(&input).unwrap();
assert_eq!(cfg.get_string("aa.b").unwrap(), "ww");
}