|
| 1 | +// Tests for volk config path resolution (XDG preference, legacy fallback) |
| 2 | +#include <gtest/gtest.h> |
| 3 | +#include <volk/volk_prefs.h> |
| 4 | + |
| 5 | +#include <filesystem> |
| 6 | +#include <fstream> |
| 7 | +#include <string> |
| 8 | +#include <string_view> |
| 9 | +#include <cstdlib> |
| 10 | +#include <random> |
| 11 | +#include <sstream> |
| 12 | + |
| 13 | +#if defined(_WIN32) |
| 14 | +static void set_env(std::string_view name, const char* value) |
| 15 | +{ |
| 16 | + std::string n(name); |
| 17 | + if (value) |
| 18 | + _putenv_s(n.c_str(), value); |
| 19 | + else |
| 20 | + _putenv_s(n.c_str(), ""); |
| 21 | +} |
| 22 | +#else |
| 23 | +static void set_env(std::string_view name, const char* value) |
| 24 | +{ |
| 25 | + std::string n(name); |
| 26 | + if (value) |
| 27 | + setenv(n.c_str(), value, 1); |
| 28 | + else |
| 29 | + unsetenv(n.c_str()); |
| 30 | +} |
| 31 | +#endif |
| 32 | + |
| 33 | +static std::string volk_config_path_read() |
| 34 | +{ |
| 35 | + char buf[512] = {0}; |
| 36 | + volk_get_config_path(buf, true); |
| 37 | + return std::string(buf); |
| 38 | +} |
| 39 | + |
| 40 | +static std::string volk_config_path_write() |
| 41 | +{ |
| 42 | + char buf[512] = {0}; |
| 43 | + volk_get_config_path(buf, false); |
| 44 | + return std::string(buf); |
| 45 | +} |
| 46 | + |
| 47 | +// Helper to generate unique temp directory names |
| 48 | +static std::filesystem::path unique_temp_path(const char* prefix) |
| 49 | +{ |
| 50 | + std::random_device rd; |
| 51 | + std::mt19937 gen(rd()); |
| 52 | + std::uniform_int_distribution<> distrib(100000, 999999); |
| 53 | + std::ostringstream oss; |
| 54 | + oss << prefix << "_" << distrib(gen); |
| 55 | + return std::filesystem::temp_directory_path() / oss.str(); |
| 56 | +} |
| 57 | + |
| 58 | +TEST(VolkPaths, OverrideEnv) |
| 59 | +{ |
| 60 | + namespace fs = std::filesystem; |
| 61 | + fs::path tmp = unique_temp_path("volk_test_override"); |
| 62 | + fs::create_directories(tmp / "volk"); |
| 63 | + std::ofstream(tmp / "volk" / "volk_config") << "test"; |
| 64 | + |
| 65 | + set_env("VOLK_CONFIGPATH", tmp.string().c_str()); |
| 66 | + |
| 67 | + std::string expect = (tmp / "volk" / "volk_config").generic_string(); |
| 68 | + std::string got = volk_config_path_read(); |
| 69 | + EXPECT_EQ(expect, got); |
| 70 | + |
| 71 | + // cleanup |
| 72 | + set_env("VOLK_CONFIGPATH", nullptr); |
| 73 | + fs::remove_all(tmp); |
| 74 | +} |
| 75 | + |
| 76 | +TEST(VolkPaths, XDGPreference) |
| 77 | +{ |
| 78 | + namespace fs = std::filesystem; |
| 79 | + fs::path tmp = unique_temp_path("volk_test_xdg"); |
| 80 | + fs::create_directories(tmp / "volk"); |
| 81 | + std::ofstream(tmp / "volk" / "volk_config") << "test"; |
| 82 | + |
| 83 | + set_env("VOLK_CONFIGPATH", nullptr); |
| 84 | + set_env("XDG_CONFIG_HOME", tmp.string().c_str()); |
| 85 | + |
| 86 | + std::string expect = (tmp / "volk" / "volk_config").generic_string(); |
| 87 | + std::string got = volk_config_path_read(); |
| 88 | + EXPECT_EQ(expect, got); |
| 89 | + |
| 90 | + set_env("XDG_CONFIG_HOME", nullptr); |
| 91 | + fs::remove_all(tmp); |
| 92 | +} |
| 93 | + |
| 94 | +TEST(VolkPaths, HomeDotConfigFallback) |
| 95 | +{ |
| 96 | + namespace fs = std::filesystem; |
| 97 | + fs::path tmp = unique_temp_path("volk_test_homecfg"); |
| 98 | + fs::create_directories(tmp / ".config" / "volk"); |
| 99 | + std::ofstream(tmp / ".config" / "volk" / "volk_config") << "test"; |
| 100 | + |
| 101 | + set_env("VOLK_CONFIGPATH", nullptr); |
| 102 | + set_env("XDG_CONFIG_HOME", nullptr); |
| 103 | + set_env("HOME", tmp.string().c_str()); |
| 104 | + |
| 105 | + std::string expect = (tmp / ".config" / "volk" / "volk_config").generic_string(); |
| 106 | + std::string got = volk_config_path_read(); |
| 107 | + EXPECT_EQ(expect, got); |
| 108 | + |
| 109 | + set_env("HOME", nullptr); |
| 110 | + fs::remove_all(tmp); |
| 111 | +} |
| 112 | + |
| 113 | +TEST(VolkPaths, LegacyFallback) |
| 114 | +{ |
| 115 | + namespace fs = std::filesystem; |
| 116 | + fs::path tmp = unique_temp_path("volk_test_legacy"); |
| 117 | + fs::create_directories(tmp / ".volk"); |
| 118 | + std::ofstream(tmp / ".volk" / "volk_config") << "test"; |
| 119 | + |
| 120 | + set_env("VOLK_CONFIGPATH", nullptr); |
| 121 | + set_env("XDG_CONFIG_HOME", nullptr); |
| 122 | + set_env("HOME", tmp.string().c_str()); |
| 123 | + |
| 124 | + // Ensure .config/volk does not exist to force legacy path selection |
| 125 | + fs::remove_all(tmp / ".config"); |
| 126 | + |
| 127 | + std::string expect = (tmp / ".volk" / "volk_config").generic_string(); |
| 128 | + std::string got = volk_config_path_read(); |
| 129 | + EXPECT_EQ(expect, got); |
| 130 | + |
| 131 | + set_env("HOME", nullptr); |
| 132 | + fs::remove_all(tmp); |
| 133 | +} |
| 134 | + |
| 135 | +TEST(VolkPaths, WriteCreatesXDGDir) |
| 136 | +{ |
| 137 | + namespace fs = std::filesystem; |
| 138 | + fs::path tmp = unique_temp_path("volk_test_write"); |
| 139 | + |
| 140 | + set_env("VOLK_CONFIGPATH", nullptr); |
| 141 | + set_env("XDG_CONFIG_HOME", tmp.string().c_str()); |
| 142 | + |
| 143 | + // Ensure directory does not exist yet |
| 144 | + fs::remove_all(tmp); |
| 145 | + EXPECT_FALSE(fs::exists(tmp / "volk")); |
| 146 | + |
| 147 | + // Call write-mode; this should create the XDG/volk directory |
| 148 | + volk_config_path_write(); |
| 149 | + EXPECT_TRUE(fs::exists(tmp / "volk")); |
| 150 | + |
| 151 | + set_env("XDG_CONFIG_HOME", nullptr); |
| 152 | + fs::remove_all(tmp); |
| 153 | +} |
0 commit comments