icinga2/test/test-ctest.hpp
Johannes Schmidt e11aad842a Discover Boost test cases automatically after build
This adds a global fixture that can parse an additional argument to
the test executables (`--generate_ctest_config`). When run by
CMake during build, this generates a CTest script containing all
the tests and their properties.

An additional decorator, that defines CTest properties for a test case
or suite that will be added to the tests during config generation.

This version needs no hacks, no huge CMake scripts, just a bit of
additional C++ code that iterates over all test-cases and collects
the information CTest needs.

One caveat is still that this does not work with cross-compilation,
which probably isn't an issue to begin with, but there are also ways
to fix that if necessary.
2025-08-19 09:24:42 +02:00

58 lines
1.5 KiB
C++

/* Icinga 2 | (c) 2025 Icinga GmbH | GPLv2+ */
#pragma once
#include <BoostTestTargetConfig.h>
#include <boost/filesystem/path.hpp>
#include <boost/test/tree/traverse.hpp>
#include <fstream>
namespace icinga {
/**
* A boost test decorator to set additional ctest properties for the test.
*/
class CTestProperties : public boost::unit_test::decorator::base
{
public:
explicit CTestProperties(std::string props) : m_Props(std::move(props)) {}
std::string m_Props;
private:
/**
* Doesn't do anything to the case/suite it's applied to.
*
* However this gets added to the list so we can later find it by iterating the
* decorators and dynamic_casting them to this type and get the m_props value.
*/
void apply(boost::unit_test::test_unit& tu) override {}
[[nodiscard]] boost::unit_test::decorator::base_ptr clone() const override;
};
class CTestFileGenerator : public boost::unit_test::test_tree_visitor
{
public:
CTestFileGenerator(const boost::filesystem::path& testexe, const boost::filesystem::path& outfile);
void visit(boost::unit_test::test_case const& test) override;
bool test_suite_start(boost::unit_test::test_suite const& suite) override;
void test_suite_finish(boost::unit_test::test_suite const& suite) override;
private:
static std::string LabelsToProp(std::vector<std::string> labels);
std::ofstream m_Fp;
boost::filesystem::path m_WorkDir;
boost::filesystem::path m_TestExe;
std::vector<std::vector<std::string>> m_PropsStack;
};
struct CTestFileGeneratorFixture
{
CTestFileGeneratorFixture();
};
} // namespace icinga