From cf68f02d2f40a698c4ac905db96dceca697c9df6 Mon Sep 17 00:00:00 2001 From: Aaron Fiore Date: Mon, 17 Nov 2025 20:35:52 -0800 Subject: [PATCH] container: root: test: utility: add tests for common --- container/src/root/test/common/utility.hh | 13 ++ container/src/root/test/unit/utility.hh | 156 ++++++++++++++++++++++ 2 files changed, 169 insertions(+) diff --git a/container/src/root/test/common/utility.hh b/container/src/root/test/common/utility.hh index 572877d..e845446 100644 --- a/container/src/root/test/common/utility.hh +++ b/container/src/root/test/common/utility.hh @@ -25,9 +25,11 @@ #include #include +#include #include #include +#include "../../common/utility.hh" #include "../../src/utility.hh" //! \namespace dfi @@ -93,6 +95,17 @@ struct ByteFixture t_byte byte; }; + +//! \brief Common fixture +//! \note Not a 'common' fixture but rather a fixture for 'common' +//! \since docker-finance 1.1.0 +struct CommonFixture +{ + protected: + using EErrorCode = ::TInterpreter::EErrorCode; + std::unique_ptr ecode; + CommonFixture() { ecode = std::make_unique(); } +}; } // namespace tests } // namespace dfi diff --git a/container/src/root/test/unit/utility.hh b/container/src/root/test/unit/utility.hh index 4cca4e4..e6f2dee 100644 --- a/container/src/root/test/unit/utility.hh +++ b/container/src/root/test/unit/utility.hh @@ -28,8 +28,11 @@ #include #include #include +#include #include #include +#include +#include #include #include #include @@ -275,6 +278,159 @@ TEST_F(ByteTransform, unordered_set) ASSERT_EQ(byte.encode(one), two); ASSERT_EQ(byte.decode(two), one); } + +//! \brief Common fixture (raw files) +//! \note Not a 'common' fixture but rather a fixture for 'common' +//! \since docker-finance 1.1.0 +struct CommonRawFiles : public ::testing::Test, + public ::dfi::tests::CommonFixture +{ + std::string path_1, path_2; + + void SetUp() override + { + try + { + path_1 = std::tmpnam(nullptr); + path_2 = std::tmpnam(nullptr); + } + catch (...) + { + THROW(std::runtime_error, "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"); + + 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"); + } + + void TearDown() override + { + THROW_IF( + std::remove(path_1.c_str()), + std::runtime_error, + "could not remove file '" + path_1 + "'"); + + THROW_IF( + std::remove(path_2.c_str()), + std::runtime_error, + "could not remove file '" + path_2 + "'"); + } +}; + +TEST_F(CommonRawFiles, RawLoadSingle) +{ + ASSERT_THROW( + ::dfi::common::Command::load({path_1 + "should-not-exist"}), + std::runtime_error); + 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)); + gInterpreter->ProcessLine("my_foo foo;", ecode.get()); + ASSERT_NE(ecode, nullptr); + EXPECT_EQ(*ecode, EErrorCode::kNoError); +} + +TEST_F(CommonRawFiles, RawLoadMultiple) +{ + ASSERT_THROW( + ::dfi::common::Command::load( + {{path_1 + "should-not-exist"}, {path_2 + "nor-should-this"}}), + std::runtime_error); + 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})); + gInterpreter->ProcessLine("my_bar bar;", ecode.get()); + ASSERT_NE(ecode, nullptr); + EXPECT_EQ(*ecode, EErrorCode::kNoError); +} +// TODO(afiore): RawUnload + +//! \brief Common fixture (repo files) +//! \note Not a 'common' fixture but rather a fixture for 'common' +//! \since docker-finance 1.1.0 +struct CommonRepoFiles : public ::testing::Test, + public ::dfi::tests::CommonFixture +{ +}; + +TEST_F(CommonRepoFiles, MacroLoad) +{ + ASSERT_THROW( + ::dfi::macro::load("macro/should-not/exist.C"), std::runtime_error); + gInterpreter->ProcessLine( + "dfi::macro::common::crypto::botan::Hash h;", ecode.get()); + ASSERT_NE(ecode, nullptr); + EXPECT_NE(*ecode, EErrorCode::kNoError); + + ASSERT_NO_THROW(::dfi::macro::load("crypto/hash.C")); + gInterpreter->ProcessLine( + "dfi::macro::common::crypto::botan::Hash h;", ecode.get()); + ASSERT_NE(ecode, nullptr); + EXPECT_EQ(*ecode, EErrorCode::kNoError); + // TODO(afiore): multiple load +} +// TODO(afiore): MacroUnload + +TEST_F(CommonRepoFiles, PluginLoad) +{ + ASSERT_THROW( + ::dfi::plugin::load("repo/should-not/exist.cc"), std::runtime_error); + gInterpreter->ProcessLine( + "dfi::plugin::my_plugin_namespace::example2();", ecode.get()); + ASSERT_NE(ecode, nullptr); + EXPECT_NE(*ecode, EErrorCode::kNoError); + + ASSERT_NO_THROW(::dfi::plugin::load("repo/example.cc")); + gInterpreter->ProcessLine( + "dfi::plugin::my_plugin_namespace::example2();", ecode.get()); + ASSERT_NE(ecode, nullptr); + EXPECT_EQ(*ecode, EErrorCode::kNoError); + + // TODO(afiore): multiple load +} +// TODO(afiore): PluginUnload + +//! \brief Common fixture (free functions) +//! \note Not a 'common' fixture but rather a fixture for 'common' +//! \since docker-finance 1.1.0 +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")); +} + +TEST_F(CommonFree, exec) +{ + ASSERT_NE(::dfi::common::exec("should-not-exist"), 0); + ASSERT_EQ(::dfi::common::exec("pwd"), 0); +} + +TEST_F(CommonFree, make_timestamp) +{ + ASSERT_EQ(::dfi::common::make_timestamp().size(), 20); +} + } // namespace unit } // namespace tests } // namespace dfi