Skip to content
Draft
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
10 changes: 10 additions & 0 deletions libmamba/include/mamba/util/environment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@ namespace mamba::util
*/
[[nodiscard]] auto user_home_dir() -> std::string;

#ifndef _WIN32
/**
* Return the directory to zsh dotfiles.
*
* This function checks for the `ZDOTDIR` environment variable and falls back to the user's
* home directory if it is not set.
*/
[[nodiscard]] auto zsh_home_dir() -> std::string;
#endif // _WIN32

/**
* Return the current user config directory.
*
Expand Down
3 changes: 3 additions & 0 deletions libmamba/src/core/shell_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1275,6 +1275,7 @@ namespace mamba
}
else if (shell == "zsh")
{
home = util::zsh_home_dir();
fs::u8path zshrc_path = home / ".zshrc";
modify_rc_file(context, zshrc_path, conda_prefix, shell, mamba_exe);
}
Expand Down Expand Up @@ -1349,6 +1350,7 @@ namespace mamba
}
else if (shell == "zsh")
{
home = util::zsh_home_dir();
fs::u8path zshrc_path = home / ".zshrc";
reset_rc_file(context, zshrc_path, shell, mamba_exe);
}
Expand Down Expand Up @@ -1415,6 +1417,7 @@ namespace mamba
}
else if (shell == "zsh")
{
home = util::zsh_home_dir();
config_path = home / ".zshrc";
}
else if (shell == "xonsh")
Expand Down
9 changes: 9 additions & 0 deletions libmamba/src/util/environment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,15 @@ namespace mamba::util
throw std::runtime_error("HOME not set.");
}

auto zsh_home_dir() -> std::string
{
if (auto maybe_zdotdir = get_env("ZDOTDIR").value_or(""); !maybe_zdotdir.empty())
{
return maybe_zdotdir;
}
return user_home_dir();
}

auto user_config_dir() -> std::string
{
if (auto maybe_dir = get_env("XDG_CONFIG_HOME").value_or(""); !maybe_dir.empty())
Expand Down
23 changes: 23 additions & 0 deletions libmamba/tests/src/util/test_environment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,29 @@ namespace
}
}

TEST_CASE("zsh_home_dir", "[mamba::util]")
{
if (on_win)
{
return;
}

const auto restore = mambatests::EnvironmentCleaner();

SECTION("ZDOTDIR set")
{
set_env("ZDOTDIR", "/user/mamba/.zsh");
REQUIRE(zsh_home_dir() == "/user/mamba/.zsh");
}

SECTION("ZDOTDIR not set")
{
unset_env("ZDOTDIR");
set_env("HOME", "/user/mamba");
REQUIRE(zsh_home_dir() == user_home_dir());
}
}

TEST_CASE("user_xdg", "[mamba::util]")
{
const auto restore = mambatests::EnvironmentCleaner();
Expand Down
Loading