From 2722431ce919d5e780d01bd14b77fbc03d007f07 Mon Sep 17 00:00:00 2001 From: Joel Klinghed Date: Tue, 23 Nov 2021 23:48:26 +0100 Subject: Make config keep the absolute root to load relative files from This means Config::get_path will continue to work even if we change current directory in the process (when the path the config was loaded from was relative). --- src/config.cc | 3 ++- test/test_config.cc | 10 ++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/config.cc b/src/config.cc index 4d7cf52..515c1eb 100644 --- a/src/config.cc +++ b/src/config.cc @@ -128,7 +128,8 @@ std::unique_ptr load(Logger* logger, path.c_str(), strerror(errno)); return nullptr; } - auto root = path.parent_path(); + std::error_code err; + auto root = std::filesystem::absolute(path.parent_path(), err); std::unordered_map data; std::string line; unsigned long num = 0; diff --git a/test/test_config.cc b/test/test_config.cc index d9ddf3a..bcb7b59 100644 --- a/test/test_config.cc +++ b/test/test_config.cc @@ -165,3 +165,13 @@ TEST_F(ConfigTest, path) { EXPECT_EQ("/file", cfg->get_path("absolute", "")); EXPECT_EQ(path().parent_path() / "file", cfg->get_path("relative", "")); } + +TEST_F(ConfigTest, path_when_load_relative) { + write("absolute = /file\n" + "relative = file\n"); + auto cfg = Config::create(logger(), std::filesystem::relative(path())); + ASSERT_TRUE(cfg); + EXPECT_EQ("/file", cfg->get_path("absolute", "")); + EXPECT_EQ(path().parent_path() / "file", + std::filesystem::weakly_canonical(cfg->get_path("relative", ""))); +} -- cgit v1.2.3-70-g09d2