From e8cfbbd67b3dd276e9e2cf13eb3c1933de03bbe4 Mon Sep 17 00:00:00 2001 From: Vaxry Date: Fri, 23 Oct 2026 16:54:04 +0200 Subject: [PATCH] path: add getDataDir --- include/hyprutils/path/Path.hpp | 5 +++ src/path/Path.cpp | 13 +++++++ tests/path/Path.cpp | 61 +++++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+) diff --git a/include/hyprutils/path/Path.hpp b/include/hyprutils/path/Path.hpp index 9b917d8..d60d7e6 100644 --- a/include/hyprutils/path/Path.hpp +++ b/include/hyprutils/path/Path.hpp @@ -44,6 +44,11 @@ namespace Hyprutils::Path { */ std::optional getXdgConfigHome(); + /** Retrieves a data path for storing persistent data based on appName. appName must + * be valid to put inside a path. + */ + std::optional getDataDir(const std::string& appName); + /** Searches for a config according to the XDG Base Directory specification. Returns a pair of the full path to a config and the base path. Returns std::nullopt in case of a non-existent value. diff --git a/src/path/Path.cpp b/src/path/Path.cpp index ae8fa99..eea6457 100644 --- a/src/path/Path.cpp +++ b/src/path/Path.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include @@ -51,6 +52,18 @@ namespace Hyprutils::Path { return xdgConfigHome; } + std::optional getDataDir(const std::string& appName) { + const auto dataHome = std::getenv("XDG_DATA_HOME"); + if (dataHome && std::filesystem::path(dataHome).is_absolute()) + return (std::filesystem::path(dataHome) / appName).string(); + + const auto homeDir = std::getenv("HOME"); + if (!homeDir || !std::filesystem::path(homeDir).is_absolute()) + return std::nullopt; + + return (std::filesystem::path(homeDir) / ".local/share" / appName).string(); + } + using T = std::optional; std::pair findConfig(std::string programName) { return findConfig(programName, "conf"); diff --git a/tests/path/Path.cpp b/tests/path/Path.cpp index 0900ba2..883d50a 100644 --- a/tests/path/Path.cpp +++ b/tests/path/Path.cpp @@ -1,5 +1,6 @@ #include +#include #include #include @@ -58,3 +59,63 @@ TEST(Path, resolvePath_errors) { EXPECT_FALSE(resolvePath("~nohome").has_value()); } + +class DataDirTest : public testing::Test { + protected: + void SetUp() override { + if (const auto homeDir = std::getenv("HOME")) + m_oldHome = homeDir; + if (const auto dataHome = std::getenv("XDG_DATA_HOME")) + m_oldDataHome = dataHome; + + ASSERT_EQ(setenv("HOME", "/home/test", 1), 0); + ASSERT_EQ(unsetenv("XDG_DATA_HOME"), 0); + } + + void TearDown() override { + if (m_oldHome) + setenv("HOME", m_oldHome->c_str(), 1); + else + unsetenv("HOME"); + + if (m_oldDataHome) + setenv("XDG_DATA_HOME", m_oldDataHome->c_str(), 1); + else + unsetenv("XDG_DATA_HOME"); + } + + std::optional m_oldHome; + std::optional m_oldDataHome; +}; + +TEST_F(DataDirTest, UsesXdgDataHome) { + ASSERT_EQ(setenv("XDG_DATA_HOME", "/custom/data", 1), 0); + EXPECT_EQ(getDataDir("hyprpaper").value(), "/custom/data/hyprpaper"); + EXPECT_EQ(getDataDir("hyprland").value(), "/custom/data/hyprland"); + + ASSERT_EQ(setenv("XDG_DATA_HOME", "/other/data/", 1), 0); + ASSERT_EQ(unsetenv("HOME"), 0); + EXPECT_EQ(getDataDir("hyprpaper").value(), "/other/data/hyprpaper"); +} + +TEST_F(DataDirTest, FallsBackToHome) { + EXPECT_EQ(getDataDir("hyprpaper").value(), "/home/test/.local/share/hyprpaper"); + + for (const auto dataHome : {"", "relative/data"}) { + ASSERT_EQ(setenv("XDG_DATA_HOME", dataHome, 1), 0); + EXPECT_EQ(getDataDir("hyprpaper").value(), "/home/test/.local/share/hyprpaper"); + } + + ASSERT_EQ(setenv("HOME", "/home/other/", 1), 0); + EXPECT_EQ(getDataDir("hyprpaper").value(), "/home/other/.local/share/hyprpaper"); +} + +TEST_F(DataDirTest, RequiresAbsoluteBase) { + ASSERT_EQ(unsetenv("HOME"), 0); + EXPECT_FALSE(getDataDir("hyprpaper").has_value()); + + for (const auto homeDir : {"", "relative/home"}) { + ASSERT_EQ(setenv("HOME", homeDir, 1), 0); + EXPECT_FALSE(getDataDir("hyprpaper").has_value()); + } +}