#include #include #include #include #include #include #include #include Void::Testing::Test get_env_finds_set_variable() { ::setenv("VOID_TEST_VAR_A", "hello", 1); auto r = Void::Configuration::get_env("VOID_TEST_VAR_A"); Void::Testing::expect(r.has_value()); Void::Testing::expect_eq(*r, std::string{"hello"}); ::unsetenv("VOID_TEST_VAR_A"); } Void::Testing::Test get_env_returns_error_for_missing() { ::unsetenv("VOID_TEST_VAR_MISSING_XYZ"); auto r = Void::Configuration::get_env("VOID_TEST_VAR_MISSING_XYZ"); Void::Testing::expect(!r.has_value()); } Void::Testing::Test get_env_or_returns_fallback_when_absent() { ::unsetenv("VOID_TEST_VAR_MISSING_XYZ"); auto val = Void::Configuration::get_env_or("VOID_TEST_VAR_MISSING_XYZ", "default"); Void::Testing::expect_eq(val, std::string{"default"}); } Void::Testing::Test get_env_or_returns_value_when_present() { ::setenv("VOID_TEST_VAR_B", "world", 1); auto val = Void::Configuration::get_env_or("VOID_TEST_VAR_B", "fallback"); Void::Testing::expect_eq(val, std::string{"world"}); ::unsetenv("VOID_TEST_VAR_B"); } Void::Testing::Test config_set_and_get() { Void::Configuration::Config cfg; cfg.set("key", "value"); auto r = cfg.get("key"); Void::Testing::expect(r.has_value()); Void::Testing::expect_eq(std::string{*r}, std::string{"value"}); } Void::Testing::Test config_get_missing_key_fails() { Void::Configuration::Config cfg; auto r = cfg.get("nonexistent"); Void::Testing::expect(!r.has_value()); } Void::Testing::Test config_get_or_returns_fallback() { Void::Configuration::Config cfg; auto val = cfg.get_or("missing", "fb"); Void::Testing::expect_eq(std::string{val}, std::string{"fb"}); } Void::Testing::Test config_from_env_reads_listed_keys() { ::setenv("VOID_TEST_CFG_X", "42", 1); ::unsetenv("VOID_TEST_CFG_Y"); auto cfg = Void::Configuration::Config::from_env({"VOID_TEST_CFG_X", "VOID_TEST_CFG_Y"}); Void::Testing::expect_eq(cfg.size(), std::size_t{1}); Void::Testing::expect_eq(std::string{cfg.get_or("VOID_TEST_CFG_X")}, std::string{"42"}); ::unsetenv("VOID_TEST_CFG_X"); } Void::Testing::Test config_from_file_parses_key_value() { Void::FileSystem::TempDir tmp; auto path = tmp / "cfg.env"; (void)Void::FileSystem::write_file(path, "HOST=localhost\nPORT=5432\n"); auto r = Void::Configuration::Config::from_file(path); Void::Testing::expect(r.has_value()); Void::Testing::expect_eq(std::string{r->get_or("HOST")}, std::string{"localhost"}); Void::Testing::expect_eq(std::string{r->get_or("PORT")}, std::string{"5432"}); } Void::Testing::Test config_from_file_skips_comments_and_blank_lines() { Void::FileSystem::TempDir tmp; auto path = tmp / "cfg.env"; (void)Void::FileSystem::write_file(path, "# this is a comment\n\nFOO=bar\n"); auto r = Void::Configuration::Config::from_file(path); Void::Testing::expect(r.has_value()); Void::Testing::expect_eq(r->size(), std::size_t{1}); Void::Testing::expect_eq(std::string{r->get_or("FOO")}, std::string{"bar"}); } Void::Testing::Test config_overlay_later_wins_on_conflict() { Void::Configuration::Config base; base.set("A", "from_base"); base.set("B", "only_base"); Void::Configuration::Config top; top.set("A", "from_top"); top.set("C", "only_top"); base.overlay(top); Void::Testing::expect_eq(std::string{base.get_or("A")}, std::string{"from_top"}); Void::Testing::expect_eq(std::string{base.get_or("B")}, std::string{"only_base"}); Void::Testing::expect_eq(std::string{base.get_or("C")}, std::string{"only_top"}); } Void::Testing::Test config_from_file_missing_path_fails() { auto r = Void::Configuration::Config::from_file("/no/such/file.env"); Void::Testing::expect(!r.has_value()); }