Files
Library/tests/standalone/Configuration/Config.cpp
T
faumaray bd5488b15b Windows: qualify Configuration, Crypto and System
- Allowlist Configuration, Crypto and System for the Windows target.
- System: link winmm for the Win32 audio backend; include windows.h before
  mmsystem.h.
- Tests: add a whole-file // @requires-posix gate (CMake path and the VBS
  build.cpp) and mark the POSIX-only System suites; skip helper-based cases
  on Windows; make setenv/unsetenv and getpid portable.
- The POSIX process helper is no longer built on Windows.

Cross-built with x86_64-w64-mingw32 GCC 16.2: all modules and VoidTests.exe
link. Linux System+Testing suite still passes (477 tests, 474 passed).

Refs #65
2026-12-05 15:57:12 +03:00

144 lines
4.1 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>
namespace
{
// setenv/unsetenv are POSIX-only; _putenv_s is the Windows equivalent.
void set_env(const char* name, const char* value)
{
#ifdef _WIN32
_putenv_s(name, value);
#else
::setenv(name, value, 1);
#endif
}
void unset_env(const char* name)
{
#ifdef _WIN32
_putenv_s(name, "");
#else
::unsetenv(name);
#endif
}
} // namespace
Void::Testing::Test get_env_finds_set_variable()
{
set_env("VOID_TEST_VAR_A", "hello");
auto r = Void::Configuration::get_env("VOID_TEST_VAR_A");
Void::Testing::expect(r.has_value());
Void::Testing::expect_eq(*r, std::string{"hello"});
unset_env("VOID_TEST_VAR_A");
}
Void::Testing::Test get_env_returns_error_for_missing()
{
unset_env("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()
{
unset_env("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()
{
set_env("VOID_TEST_VAR_B", "world");
auto val = Void::Configuration::get_env_or("VOID_TEST_VAR_B", "fallback");
Void::Testing::expect_eq(val, std::string{"world"});
unset_env("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()
{
set_env("VOID_TEST_CFG_X", "42");
unset_env("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"});
unset_env("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());
}