From d8b654a30ca89107547f9add1e28046cef899a74 Mon Sep 17 00:00:00 2001 From: Joel Rosdahl Date: Thu, 3 Aug 2023 13:37:25 +0200 Subject: [PATCH] refactor: Remove dependency on core from util --- src/Config.cpp | 3 ++- src/util/DirEntry.cpp | 1 - src/util/LockFile.cpp | 1 - src/util/environment.cpp | 9 +++++---- src/util/environment.hpp | 7 +++++-- unittest/test_util_environment.cpp | 12 ++++-------- 6 files changed, 16 insertions(+), 17 deletions(-) diff --git a/src/Config.cpp b/src/Config.cpp index 16dfbd5f3..021be4888 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -978,7 +978,8 @@ Config::set_item(const std::string& key, return; } - std::string value = util::expand_environment_variables(unexpanded_value); + std::string value = util::value_or_throw( + util::expand_environment_variables(unexpanded_value)); switch (it->second.item) { case ConfigItem::absolute_paths_in_stderr: diff --git a/src/util/DirEntry.cpp b/src/util/DirEntry.cpp index 80ed955ef..c6335dd15 100644 --- a/src/util/DirEntry.cpp +++ b/src/util/DirEntry.cpp @@ -21,7 +21,6 @@ #include #include #include -#include #include #include diff --git a/src/util/LockFile.cpp b/src/util/LockFile.cpp index f08340cdc..139b17dad 100644 --- a/src/util/LockFile.cpp +++ b/src/util/LockFile.cpp @@ -24,7 +24,6 @@ #include "fmtmacros.hpp" #include -#include #include #include #include diff --git a/src/util/environment.cpp b/src/util/environment.cpp index 336ed252e..57e0470ae 100644 --- a/src/util/environment.cpp +++ b/src/util/environment.cpp @@ -18,13 +18,12 @@ #include "environment.hpp" -#include #include #include namespace util { -std::string +tl::expected expand_environment_variables(const std::string& str) { std::string result; @@ -52,7 +51,8 @@ expand_environment_variables(const std::string& str) ++right; } if (curly && *right != '}') { - throw core::Error(FMT("syntax error: missing '}}' after \"{}\"", left)); + return tl::unexpected( + FMT("syntax error: missing '}}' after \"{}\"", left)); } if (right == left) { // Special case: don't consider a single $ the left of a variable. @@ -62,7 +62,8 @@ expand_environment_variables(const std::string& str) std::string name(left, right - left); const char* value = getenv(name.c_str()); if (!value) { - throw core::Error(FMT("environment variable \"{}\" not set", name)); + return tl::unexpected( + FMT("environment variable \"{}\" not set", name)); } result += value; if (!curly) { diff --git a/src/util/environment.hpp b/src/util/environment.hpp index 29a99de59..c874b2233 100644 --- a/src/util/environment.hpp +++ b/src/util/environment.hpp @@ -18,13 +18,16 @@ #pragma once +#include + #include namespace util { // Expand all instances of $VAR or ${VAR}, where VAR is an environment variable, -// in `str`. Throws `core::Error` if one of the environment variables. -[[nodiscard]] std::string expand_environment_variables(const std::string& str); +// in `str`. +tl::expected +expand_environment_variables(const std::string& str); // Set environment variable `name` to `value`. void setenv(const std::string& name, const std::string& value); diff --git a/unittest/test_util_environment.cpp b/unittest/test_util_environment.cpp index 06654672a..1819ab86b 100644 --- a/unittest/test_util_environment.cpp +++ b/unittest/test_util_environment.cpp @@ -35,14 +35,10 @@ TEST_CASE("util::expand_environment_variables") CHECK(util::expand_environment_variables("x$FOO") == "xbar"); CHECK(util::expand_environment_variables("${FOO}x") == "barx"); - DOCTEST_GCC_SUPPRESS_WARNING_PUSH - DOCTEST_GCC_SUPPRESS_WARNING("-Wunused-result") - CHECK_THROWS_WITH( - (void)util::expand_environment_variables("$surelydoesntexist"), - "environment variable \"surelydoesntexist\" not set"); - CHECK_THROWS_WITH((void)util::expand_environment_variables("${FOO"), - "syntax error: missing '}' after \"FOO\""); - DOCTEST_GCC_SUPPRESS_WARNING_POP + CHECK(util::expand_environment_variables("$surelydoesntexist").error() + == "environment variable \"surelydoesntexist\" not set"); + CHECK(util::expand_environment_variables("${FOO").error() + == "syntax error: missing '}' after \"FOO\""); } TEST_SUITE_END(); -- 2.47.2