diff --git a/include/dwarfs/os_access.h b/include/dwarfs/os_access.h index d33fffef..97584312 100644 --- a/include/dwarfs/os_access.h +++ b/include/dwarfs/os_access.h @@ -53,5 +53,6 @@ class os_access { virtual std::filesystem::path canonical(std::filesystem::path const& path) const = 0; virtual std::filesystem::path current_path() const = 0; + virtual std::optional getenv(std::string_view name) const = 0; }; } // namespace dwarfs diff --git a/include/dwarfs/os_access_generic.h b/include/dwarfs/os_access_generic.h index 01998aa4..9750e9bb 100644 --- a/include/dwarfs/os_access_generic.h +++ b/include/dwarfs/os_access_generic.h @@ -24,6 +24,7 @@ #include #include #include +#include #include #include "dwarfs/os_access.h" @@ -45,5 +46,6 @@ class os_access_generic : public os_access { std::filesystem::path canonical(std::filesystem::path const& path) const override; std::filesystem::path current_path() const override; + std::optional getenv(std::string_view name) const override; }; } // namespace dwarfs diff --git a/src/dwarfs/os_access_generic.cpp b/src/dwarfs/os_access_generic.cpp index 371de4b2..fd68391a 100644 --- a/src/dwarfs/os_access_generic.cpp +++ b/src/dwarfs/os_access_generic.cpp @@ -19,6 +19,8 @@ * along with dwarfs. If not, see . */ +#include + #include #include "dwarfs/mmap.h" @@ -84,4 +86,13 @@ fs::path os_access_generic::canonical(fs::path const& path) const { fs::path os_access_generic::current_path() const { return fs::current_path(); } +std::optional +os_access_generic::getenv(std::string_view name) const { + std::string name_str(name); + if (auto value = std::getenv(name_str.c_str())) { + return value; + } + return std::nullopt; +} + } // namespace dwarfs diff --git a/test/test_helpers.cpp b/test/test_helpers.cpp index a4e669e3..9493b949 100644 --- a/test/test_helpers.cpp +++ b/test/test_helpers.cpp @@ -459,6 +459,17 @@ std::filesystem::path os_access_mock::current_path() const { return root_->name; } +void os_access_mock::setenv(std::string name, std::string value) { + env_[std::move(name)] = std::move(value); +} + +std::optional os_access_mock::getenv(std::string_view name) const { + if (auto it = env_.find(std::string(name)); it != env_.end()) { + return it->second; + } + return std::nullopt; +} + std::optional find_binary(std::string_view name) { auto path_str = std::getenv("PATH"); if (!path_str) { diff --git a/test/test_helpers.h b/test/test_helpers.h index a205f784..92e7de54 100644 --- a/test/test_helpers.h +++ b/test/test_helpers.h @@ -91,6 +91,8 @@ class os_access_mock : public os_access { void set_access_fail(std::filesystem::path const& path); + void setenv(std::string name, std::string value); + std::unique_ptr opendir(std::filesystem::path const& path) const override; @@ -108,6 +110,8 @@ class os_access_mock : public os_access { std::filesystem::path current_path() const override; + std::optional getenv(std::string_view name) const override; + private: static std::vector splitpath(std::filesystem::path const& path); struct mock_dirent* find(std::filesystem::path const& path) const; @@ -118,6 +122,7 @@ class os_access_mock : public os_access { std::unique_ptr root_; size_t ino_{1000000}; std::set access_fail_set_; + std::map env_; }; class script_mock : public script {