Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions include/hyprutils/path/Path.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ namespace Hyprutils::Path {
*/
std::optional<std::string> getXdgConfigHome();

/** Retrieves a data path for storing persistent data based on appName. appName must
* be valid to put inside a path.
*/
std::optional<std::string> 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.
Expand Down
13 changes: 13 additions & 0 deletions src/path/Path.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <hyprutils/path/Path.hpp>
#include <hyprutils/string/VarList.hpp>
#include <cstdlib>
#include <filesystem>
#include <expected>

Expand Down Expand Up @@ -51,6 +52,18 @@ namespace Hyprutils::Path {
return xdgConfigHome;
}

std::optional<std::string> 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::string>;
std::pair<T, T> findConfig(std::string programName) {
return findConfig(programName, "conf");
Expand Down
61 changes: 61 additions & 0 deletions tests/path/Path.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <hyprutils/path/Path.hpp>

#include <cstdlib>
#include <filesystem>
#include <gtest/gtest.h>

Expand Down Expand Up @@ -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<std::string> m_oldHome;
std::optional<std::string> 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());
}
}
Loading