container: root: common: utility: refactor/add tests

This commit is contained in:
2025-12-02 15:11:04 -08:00
parent ea20413107
commit d156f62fa7
2 changed files with 133 additions and 76 deletions

View File

@@ -102,9 +102,6 @@ struct ByteFixture
struct CommonFixture
{
protected:
using EErrorCode = ::TInterpreter::EErrorCode;
std::unique_ptr<EErrorCode> ecode;
CommonFixture() { ecode = std::make_unique<EErrorCode>(); }
};
} // namespace tests
} // namespace dfi

View File

@@ -282,10 +282,63 @@ TEST_F(ByteTransform, unordered_set)
ASSERT_EQ(byte.decode(two), one);
}
//! \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, Line)
{
ASSERT_NO_THROW(common::line(""));
ASSERT_THROW(common::line("a bar without a foo"), type::RuntimeError);
ASSERT_NO_THROW(common::line("#define FOO 1"));
}
TEST_F(CommonFree, add_include_dir)
{
ASSERT_THROW(
common::add_include_dir("/should-not-exist"), type::RuntimeError);
ASSERT_NO_THROW(common::add_include_dir("/usr/include"));
}
TEST_F(CommonFree, add_linked_lib)
{
ASSERT_THROW(
common::add_linked_lib("/should-not-exist.so"), type::RuntimeError);
ASSERT_NO_THROW(common::add_linked_lib("/usr/lib/LLVMgold.so"));
}
TEST_F(CommonFree, remove_linked_lib)
{
ASSERT_THROW(
common::remove_linked_lib("/should-not-exist.so"), type::RuntimeError);
ASSERT_NO_THROW(common::add_linked_lib("/usr/lib/LLVMgold.so"));
ASSERT_NO_THROW(common::remove_linked_lib("/usr/lib/LLVMgold.so"));
}
TEST_F(CommonFree, get_env)
{
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(common::exec("should-not-exist"), 0);
ASSERT_EQ(common::exec("pwd"), 0);
}
TEST_F(CommonFree, make_timestamp)
{
ASSERT_EQ(common::make_timestamp().size(), 20);
}
//! \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,
struct CommonFreeFiles : public ::testing::Test,
public ::dfi::tests::CommonFixture
{
std::string path_1, path_2;
@@ -325,107 +378,114 @@ struct CommonRawFiles : public ::testing::Test,
}
};
TEST_F(CommonRawFiles, RawLoadSingle)
TEST_F(CommonFreeFiles, LoadSingle)
{
ASSERT_THROW(
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_THROW(common::load({path_1 + "should-not-exist"}), type::RuntimeError);
ASSERT_THROW(common::line("my_foo foo;"), type::RuntimeError);
ASSERT_NO_THROW(common::Command::load(path_1));
gInterpreter->ProcessLine("my_foo foo;", ecode.get());
ASSERT_NE(ecode, nullptr);
EXPECT_EQ(*ecode, EErrorCode::kNoError);
ASSERT_NO_THROW(common::load(path_1));
ASSERT_NO_THROW(common::line("my_foo foo;"));
}
TEST_F(CommonRawFiles, RawLoadMultiple)
TEST_F(CommonFreeFiles, LoadMultiple)
{
ASSERT_THROW(
common::Command::load(
common::load(
{{path_1 + "should-not-exist"}, {path_2 + "nor-should-this"}}),
type::RuntimeError);
gInterpreter->ProcessLine("my_bar bar;", ecode.get());
ASSERT_NE(ecode, nullptr);
EXPECT_NE(*ecode, EErrorCode::kNoError);
ASSERT_THROW(common::line("my_bar bar;"), type::RuntimeError);
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);
ASSERT_NO_THROW(common::load({path_1, path_2}));
ASSERT_NO_THROW(common::line("my_bar bar;"));
}
// TODO(afiore): RawUnload
//! \brief Common fixture (repo files)
//! \note Not a 'common' fixture but rather a fixture for 'common'
TEST_F(CommonFreeFiles, UnloadSingle)
{
ASSERT_NO_THROW(common::load(path_1));
ASSERT_NO_THROW(common::line("my_foo foo;"));
ASSERT_NO_THROW(common::unload(path_1));
ASSERT_THROW(common::line("my_foo foo;"), type::RuntimeError);
}
TEST_F(CommonFreeFiles, UnloadMultiple)
{
ASSERT_NO_THROW(common::load({path_1, path_2}));
ASSERT_NO_THROW(common::line("my_foo foo;"));
ASSERT_NO_THROW(common::line("my_bar bar;"));
ASSERT_NO_THROW(common::unload({path_1, path_2}));
ASSERT_THROW(common::line("my_foo foo;"), type::RuntimeError);
ASSERT_THROW(common::line("my_bar bar;"), type::RuntimeError);
}
TEST_F(CommonFreeFiles, ReloadSingle)
{
ASSERT_NO_THROW(common::load(path_1));
ASSERT_NO_THROW(common::line("my_foo foo;"));
ASSERT_NO_THROW(common::reload(path_1));
ASSERT_NO_THROW(common::line("my_foo foo;"));
}
TEST_F(CommonFreeFiles, ReloadMultiple)
{
ASSERT_NO_THROW(common::load({path_1, path_2}));
ASSERT_NO_THROW(common::line("my_foo foo;"));
ASSERT_NO_THROW(common::line("my_bar bar;"));
ASSERT_NO_THROW(common::reload({path_1, path_2}));
ASSERT_NO_THROW(common::line("my_foo foo;"));
ASSERT_NO_THROW(common::line("my_bar bar;"));
}
//! \brief MacroFreeFiles fixture
//! \since docker-finance 1.1.0
struct CommonRepoFiles : public ::testing::Test,
//! \todo Move to Macro tests
struct MacroFreeFiles : public ::testing::Test,
public ::dfi::tests::CommonFixture
{
};
TEST_F(CommonRepoFiles, MacroLoad)
TEST_F(MacroFreeFiles, LoadSingle)
{
ASSERT_THROW(
::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);
EXPECT_NE(*ecode, EErrorCode::kNoError);
ASSERT_THROW(
common::line("dfi::macro::common::crypto::botan::Hash h;"),
type::RuntimeError);
// TODO(afiore): macro loading should not need to be prepended with "macro/"
// (see TODO in common impl regarding plugins-like functionality)
ASSERT_NO_THROW(::dfi::macro::load("macro/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
ASSERT_NO_THROW(common::line("dfi::macro::common::crypto::botan::Hash h;"));
}
// TODO(afiore): MacroUnload
// TODO(afiore): multiple load
// TODO(afiore): unload, reload
TEST_F(CommonRepoFiles, PluginLoad)
//! \brief PluginFreeFiles fixture
//! \since docker-finance 1.1.0
//! \todo Move to Plugin tests
struct PluginFreeFiles : public ::testing::Test,
public ::dfi::tests::CommonFixture
{
// NOTE: custom plugin bind-mount is read-only
};
TEST_F(PluginFreeFiles, LoadSingle)
{
ASSERT_THROW(
::dfi::plugin::load("repo/should-not/exist.cc"), type::RuntimeError);
gInterpreter->ProcessLine(
"dfi::plugin::my_plugin_namespace::example2();", ecode.get());
ASSERT_NE(ecode, nullptr);
EXPECT_NE(*ecode, EErrorCode::kNoError);
ASSERT_THROW(
common::line("dfi::plugin::my_plugin_namespace::example2();"),
type::RuntimeError);
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(common::get_env("should-not-exist"), type::RuntimeError);
ASSERT_NO_THROW(common::get_env("DOCKER_FINANCE_VERSION"));
}
TEST_F(CommonFree, exec)
{
ASSERT_NE(common::exec("should-not-exist"), 0);
ASSERT_EQ(common::exec("pwd"), 0);
}
TEST_F(CommonFree, make_timestamp)
{
ASSERT_EQ(common::make_timestamp().size(), 20);
ASSERT_NO_THROW(
common::line("dfi::plugin::my_plugin_namespace::example2();"));
}
// TODO(afiore): multiple load
// TODO(afiore): unload, reload
} // namespace unit
} // namespace tests