container: root: rewrite throw handler, refactor using common types

- Refactor using common types that were once internal
- Removes ancient macro approaches to C++20 solutions
- Changes `Exception` message type to use std::string
  * std::string_view isn't worthwhile in this context
This commit is contained in:
2025-11-19 16:37:46 -08:00
parent fa91fd02e8
commit ca59169116
11 changed files with 134 additions and 121 deletions

View File

@@ -60,6 +60,9 @@ namespace tests
//! \since docker-finance 1.0.0
namespace unit
{
namespace common = ::dfi::common;
//! \brief Tools utility fixture
//! \since docker-finance 1.0.0
struct Tools : public ::testing::Test, public tests::ToolsFixture
@@ -296,50 +299,41 @@ struct CommonRawFiles : public ::testing::Test,
}
catch (...)
{
THROW(std::runtime_error, "could not generate path");
common::throw_ex<common::type::RuntimeError>("could not generate path");
}
std::ofstream file_1(path_1), file_2(path_2);
THROW_IF(
!file_1.is_open() || !file_2.is_open(),
std::runtime_error,
"could not create file");
common::throw_ex_if<common::type::RuntimeError>(
!file_1.is_open() || !file_2.is_open(), "could not create file");
file_1 << "using my_foo = int;\n";
file_1.close();
file_2 << "using my_bar = char;\n";
file_2.close();
THROW_IF(
file_1.bad() || file_2.bad(),
std::runtime_error,
"could not write to file");
common::throw_ex_if<common::type::RuntimeError>(
file_1.bad() || file_2.bad(), "could not write to file");
}
void TearDown() override
{
THROW_IF(
std::remove(path_1.c_str()),
std::runtime_error,
"could not remove file '" + path_1 + "'");
common::throw_ex_if<common::type::RuntimeError>(
std::remove(path_1.c_str()), "could not remove file '" + path_1 + "'");
THROW_IF(
std::remove(path_2.c_str()),
std::runtime_error,
"could not remove file '" + path_2 + "'");
common::throw_ex_if<common::type::RuntimeError>(
std::remove(path_2.c_str()), "could not remove file '" + path_2 + "'");
}
};
TEST_F(CommonRawFiles, RawLoadSingle)
{
ASSERT_THROW(
::dfi::common::Command::load({path_1 + "should-not-exist"}),
std::runtime_error);
common::Command::load({path_1 + "should-not-exist"}), type::RuntimeError);
gInterpreter->ProcessLine("my_foo foo;", ecode.get());
ASSERT_NE(ecode, nullptr);
EXPECT_NE(*ecode, EErrorCode::kNoError);
ASSERT_NO_THROW(::dfi::common::Command::load(path_1));
ASSERT_NO_THROW(common::Command::load(path_1));
gInterpreter->ProcessLine("my_foo foo;", ecode.get());
ASSERT_NE(ecode, nullptr);
EXPECT_EQ(*ecode, EErrorCode::kNoError);
@@ -348,14 +342,14 @@ TEST_F(CommonRawFiles, RawLoadSingle)
TEST_F(CommonRawFiles, RawLoadMultiple)
{
ASSERT_THROW(
::dfi::common::Command::load(
common::Command::load(
{{path_1 + "should-not-exist"}, {path_2 + "nor-should-this"}}),
std::runtime_error);
type::RuntimeError);
gInterpreter->ProcessLine("my_bar bar;", ecode.get());
ASSERT_NE(ecode, nullptr);
EXPECT_NE(*ecode, EErrorCode::kNoError);
ASSERT_NO_THROW(::dfi::common::Command::load({path_1, path_2}));
ASSERT_NO_THROW(common::Command::load({path_1, path_2}));
gInterpreter->ProcessLine("my_bar bar;", ecode.get());
ASSERT_NE(ecode, nullptr);
EXPECT_EQ(*ecode, EErrorCode::kNoError);
@@ -373,7 +367,7 @@ struct CommonRepoFiles : public ::testing::Test,
TEST_F(CommonRepoFiles, MacroLoad)
{
ASSERT_THROW(
::dfi::macro::load("macro/should-not/exist.C"), std::runtime_error);
::dfi::macro::load("macro/should-not/exist.C"), type::RuntimeError);
gInterpreter->ProcessLine(
"dfi::macro::common::crypto::botan::Hash h;", ecode.get());
ASSERT_NE(ecode, nullptr);
@@ -391,7 +385,7 @@ TEST_F(CommonRepoFiles, MacroLoad)
TEST_F(CommonRepoFiles, PluginLoad)
{
ASSERT_THROW(
::dfi::plugin::load("repo/should-not/exist.cc"), std::runtime_error);
::dfi::plugin::load("repo/should-not/exist.cc"), type::RuntimeError);
gInterpreter->ProcessLine(
"dfi::plugin::my_plugin_namespace::example2();", ecode.get());
ASSERT_NE(ecode, nullptr);
@@ -416,19 +410,19 @@ struct CommonFree : public ::testing::Test, public ::dfi::tests::CommonFixture
TEST_F(CommonFree, env)
{
ASSERT_THROW(::dfi::common::get_env("should-not-exist"), std::runtime_error);
ASSERT_NO_THROW(::dfi::common::get_env("DOCKER_FINANCE_VERSION"));
ASSERT_THROW(common::get_env("should-not-exist"), type::RuntimeError);
ASSERT_NO_THROW(common::get_env("DOCKER_FINANCE_VERSION"));
}
TEST_F(CommonFree, exec)
{
ASSERT_NE(::dfi::common::exec("should-not-exist"), 0);
ASSERT_EQ(::dfi::common::exec("pwd"), 0);
ASSERT_NE(common::exec("should-not-exist"), 0);
ASSERT_EQ(common::exec("pwd"), 0);
}
TEST_F(CommonFree, make_timestamp)
{
ASSERT_EQ(::dfi::common::make_timestamp().size(), 20);
ASSERT_EQ(common::make_timestamp().size(), 20);
}
} // namespace unit