Files
Library/tests/standalone/Configuration/Config.cpp
faumarayandClaude Fable 5 28b94883fd Restructure into umbrella modules (~55 -> 16 targets)
Headers move, CMake targets merge, C++ namespaces stay unchanged:
- Core: Scope, Lazy, Counted, Flags, Hash, Bits, Uuid, TypeList, TypeMap, Variant
- Functional: Pipeline, Either, Result, Retry
- Containers: Ring, Pool, PriorityQueue, LRU, Trie, Graph, DisjointSet, Bitset, Interval
- Math: +Matrix, Numeric, Random, Histogram
- Text: Str, Format, Tokenizer, Diff, Encoding, Semver
- Data: Json, Csv, Ini
- Async: Coro, Concurrency, EventBus, Observable
- Time: +Bench
- Log: Log facade + Logging framework (facade now header-only, breaking
  the Stacktrace<->Log link cycle the merge would have created)
- System: FileSystem + Process
- Configuration: +Argparse
- Unchanged: Stacktrace, Plugin, StateMachine, Testing, TestingMock, Detail

Module system fixes:
- OPTIONAL_DEPS resolution deferred to void_finalize_modules() so it no
  longer depends on the alphabetical discovery order of src/ subdirs
  (Testing -> Text would have been silently dropped).
- Every header-only module now has a tracked module.cmake; previously the
  empty src/<Name> marker dirs were untracked, so a fresh clone would have
  silently skipped those modules.
- VOID_HAS_LOGGING/CONCURRENCY/STR consumers renamed to LOG/ASYNC/TEXT.

Tests move to tests/standalone/<Umbrella>/<Feature>.cpp (Result suite files
prefixed: ResultBasic etc.). Test count verified identical (831) before and
after. README/CLAUDE.md module tables updated.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-12 18:50:02 +03:00

122 lines
3.8 KiB
C++

#include <Void/Configuration/Config.hpp>
#include <Void/Configuration/Env.hpp>
#include <Void/System/TempDir.hpp>
#include <Void/System/Path.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <cstdlib>
#include <string>
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());
}