mirror of
https://github.com/mhx/dwarfs.git
synced 2025-09-09 12:28:13 -04:00
feat: add getenv() abstraction to os_access
This commit is contained in:
parent
eb60ed1ea5
commit
ac3248f966
@ -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<std::string> getenv(std::string_view name) const = 0;
|
||||
};
|
||||
} // namespace dwarfs
|
||||
|
@ -24,6 +24,7 @@
|
||||
#include <cstddef>
|
||||
#include <filesystem>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
|
||||
#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<std::string> getenv(std::string_view name) const override;
|
||||
};
|
||||
} // namespace dwarfs
|
||||
|
@ -19,6 +19,8 @@
|
||||
* along with dwarfs. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <cstdlib>
|
||||
|
||||
#include <folly/portability/Unistd.h>
|
||||
|
||||
#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<std::string>
|
||||
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
|
||||
|
@ -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<std::string> 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<fs::path> find_binary(std::string_view name) {
|
||||
auto path_str = std::getenv("PATH");
|
||||
if (!path_str) {
|
||||
|
@ -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<dir_reader>
|
||||
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<std::string> getenv(std::string_view name) const override;
|
||||
|
||||
private:
|
||||
static std::vector<std::string> 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<mock_dirent> root_;
|
||||
size_t ino_{1000000};
|
||||
std::set<std::filesystem::path> access_fail_set_;
|
||||
std::map<std::string, std::string> env_;
|
||||
};
|
||||
|
||||
class script_mock : public script {
|
||||
|
Loading…
x
Reference in New Issue
Block a user