container: root: test: utility: add tests for common

This commit is contained in:
2025-11-17 20:35:52 -08:00
parent 0d4b293b2d
commit cf68f02d2f
2 changed files with 169 additions and 0 deletions

View File

@@ -25,9 +25,11 @@
#include <cstdint>
#include <limits>
#include <memory>
#include <type_traits>
#include <vector>
#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<EErrorCode> ecode;
CommonFixture() { ecode = std::make_unique<EErrorCode>(); }
};
} // namespace tests
} // namespace dfi

View File

@@ -28,8 +28,11 @@
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <deque>
#include <forward_list>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <set>
@@ -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