#![cfg(feature = "include-package")]
use hocon::{ParseOptions, Parser, ResolveOptions};
use std::collections::HashMap;
use tempfile::tempdir;
const CHILD_DEFAULT_PLUS_OPTIONAL_FILE_UNSET: &str = r#"
registry {
instance-id = "localhost"
instance-id = ${?GH128_RS_FILE_UNSET}
}
"#;
const CHILD_DEFAULT_PLUS_OPTIONAL_FILE_SET: &str = r#"
registry {
instance-id = "localhost"
instance-id = ${?GH128_RS_FILE_SET}
}
"#;
const CHILD_DEFAULT_PLUS_OPTIONAL_PKG_UNSET: &str = r#"
registry {
instance-id = "localhost"
instance-id = ${?GH128_RS_PKG_UNSET}
}
"#;
const CHILD_DEFAULT_PLUS_OPTIONAL_PKG_SET: &str = r#"
registry {
instance-id = "localhost"
instance-id = ${?GH128_RS_PKG_SET}
}
"#;
const CHILD_DEFAULT_PLUS_OPTIONAL_PKG_DEFERRED: &str = r#"
registry {
instance-id = "localhost"
instance-id = ${?GH128_RS_PKG_DEFERRED}
}
"#;
#[test]
fn issue128_include_file_env_unset_preserves_prior_default() {
let dir = tempdir().unwrap();
let dir_str = dir.path().display().to_string().replace('\\', "/");
std::fs::write(
dir.path().join("child.conf"),
CHILD_DEFAULT_PLUS_OPTIONAL_FILE_UNSET,
)
.unwrap();
let input = format!("include \"{}/child.conf\"\n", dir_str);
let env: HashMap<String, String> = HashMap::new();
let cfg = hocon::parse_with_env(&input, &env).expect("parse must succeed");
let got = cfg
.get_string("registry.instance-id")
.expect("registry.instance-id missing — prior default must remain when ${?ENV} is unset");
assert_eq!(got, "localhost");
}
#[test]
fn issue128_include_file_env_set_applies_env_value() {
let dir = tempdir().unwrap();
let dir_str = dir.path().display().to_string().replace('\\', "/");
std::fs::write(
dir.path().join("child.conf"),
CHILD_DEFAULT_PLUS_OPTIONAL_FILE_SET,
)
.unwrap();
let input = format!("include \"{}/child.conf\"\n", dir_str);
let mut env: HashMap<String, String> = HashMap::new();
env.insert("GH128_RS_FILE_SET".into(), "from-env".into());
let cfg = hocon::parse_with_env(&input, &env).expect("parse must succeed");
let got = cfg
.get_string("registry.instance-id")
.expect("registry.instance-id missing");
assert_eq!(got, "from-env");
}
#[test]
fn issue128_include_package_env_unset_preserves_prior_default() {
let parser = Parser::new().register_package(
"github.com/o3co/rs.hocon/test/issue128-unset",
"reference.conf",
CHILD_DEFAULT_PLUS_OPTIONAL_PKG_UNSET,
);
let env: HashMap<String, String> = HashMap::new();
let cfg = parser
.parse_with_env(
r#"include package("github.com/o3co/rs.hocon/test/issue128-unset", "reference.conf")"#,
&env,
)
.expect("parse must succeed");
let got = cfg
.get_string("registry.instance-id")
.expect("registry.instance-id missing — prior default must remain when ${?ENV} is unset");
assert_eq!(got, "localhost");
}
#[test]
fn issue128_include_package_env_set_applies_env_value() {
let parser = Parser::new().register_package(
"github.com/o3co/rs.hocon/test/issue128-set",
"reference.conf",
CHILD_DEFAULT_PLUS_OPTIONAL_PKG_SET,
);
let mut env: HashMap<String, String> = HashMap::new();
env.insert("GH128_RS_PKG_SET".into(), "from-pkg-env".into());
let cfg = parser
.parse_with_env(
r#"include package("github.com/o3co/rs.hocon/test/issue128-set", "reference.conf")"#,
&env,
)
.expect("parse must succeed");
let got = cfg
.get_string("registry.instance-id")
.expect("registry.instance-id missing");
assert_eq!(got, "from-pkg-env");
}
#[test]
fn issue128_include_package_deferred_env_unset_preserves_prior_default() {
let parser = Parser::new().register_package(
"github.com/o3co/rs.hocon/test/issue128-deferred",
"reference.conf",
CHILD_DEFAULT_PLUS_OPTIONAL_PKG_DEFERRED,
);
let env: HashMap<String, String> = HashMap::new();
let opts = ParseOptions::defaults()
.with_resolve_substitutions(false)
.with_env(env);
let unresolved = parser
.parse_with_options(
r#"include package("github.com/o3co/rs.hocon/test/issue128-deferred", "reference.conf")"#,
opts,
)
.expect("parse must succeed");
let resolved = unresolved
.resolve(ResolveOptions::defaults().with_use_system_environment(false))
.expect("Resolve must succeed");
let got = resolved
.get_string("registry.instance-id")
.expect("registry.instance-id missing — prior default must remain when ${?ENV} is unset");
assert_eq!(got, "localhost");
}