Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit tests: Fix problematic nasty hack in run_tests #1950

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 10 additions & 31 deletions test/libdnf5/run_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.


#include "../shared/base_test_case.hpp"
#include "../shared/test_logger.hpp"

#include <cppunit/BriefTestProgressListener.h>
#include <cppunit/CompilerOutputter.h>
Expand All @@ -35,19 +36,6 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.
#include <memory>


// HACK: CppUnit doesn't give access to the actual test case it is running. The
// given pointer is an instance of CppUnit::TestCaller, which has the test case
// itself as a private member with no getter.
//
// Here we mimic the structure of CppUnit::TestCaller and make the pointer
// accessible.
class HackTestCaller : CppUnit::TestCase {
public:
bool m_ownFixture; // unused
CppUnit::TestCase * m_fixture; // our test case class
};


class TimingListener : public CppUnit::TestListener {
public:
void startTest(CppUnit::Test *) override { start = std::chrono::high_resolution_clock::now(); }
Expand All @@ -62,28 +50,19 @@ class TimingListener : public CppUnit::TestListener {
std::chrono::high_resolution_clock::time_point start = std::chrono::high_resolution_clock::from_time_t(0);
};


class LogCaptureListener : public CppUnit::TestListener {
public:
void startTest(CppUnit::Test * t) override {
auto * f = reinterpret_cast<HackTestCaller *>(t);
auto * tc = dynamic_cast<BaseTestCase *>(f->m_fixture);

if (tc) {
tc->base.get_logger()->add_logger(std::make_unique<libdnf5::MemoryBufferLogger>(10000, 256));
}
void startTest(CppUnit::Test *) override {
// Global test_logger is used. Clear it befor starting new test.
test_logger.clear();
}

void addFailure(const CppUnit::TestFailure & failure) override {
auto * f = reinterpret_cast<HackTestCaller *>(failure.failedTest());
auto * tc = dynamic_cast<BaseTestCase *>(f->m_fixture);

if (tc) {
std::cout << std::endl << "Dnf log:" << std::endl;
libdnf5::StdCStreamLogger cout_logger(std::cout);
dynamic_cast<libdnf5::MemoryBufferLogger *>(tc->base.get_logger()->get_logger(0))
->write_to_logger(cout_logger);
std::cout << std::endl << std::flush;
}
void addFailure(const CppUnit::TestFailure &) override {
std::cout << std::endl << "Dnf log:" << std::endl;
libdnf5::StdCStreamLogger cout_logger(std::cout);
test_logger.write_to_logger(cout_logger);
std::cout << std::endl;
}
};

Expand Down
4 changes: 4 additions & 0 deletions test/shared/base_test_case.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.
#include "base_test_case.hpp"

#include "base/base_impl.hpp"
#include "logger_redirector.hpp"
#include "private_accessor.hpp"
#include "test_logger.hpp"
#include "utils.hpp"
#include "utils/string.hpp"

Expand Down Expand Up @@ -222,6 +224,8 @@ libdnf5::rpm::Package BaseTestCase::first_query_pkg(libdnf5::rpm::PackageQuery &
void BaseTestCase::setUp() {
TestCaseFixture::setUp();

base.get_logger()->add_logger(std::make_unique<LoggerRedirector>(test_logger));

// TODO we could use get_preconfigured_base() for this now, but that would
// need changing the `base` member to a unique_ptr
temp_dir = std::make_unique<libdnf5::utils::fs::TempDir>("libdnf5_unittest");
Expand Down
43 changes: 43 additions & 0 deletions test/shared/logger_redirector.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
Copyright Contributors to the libdnf project.

This file is part of libdnf: https://github.com/rpm-software-management/libdnf/

Libdnf is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.

Libdnf is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with libdnf. If not, see <https://www.gnu.org/licenses/>.
*/


#ifndef TEST_LIBDNF5_LOGGER_REDIRECTOR_HPP
#define TEST_LIBDNF5_LOGGER_REDIRECTOR_HPP

#include <libdnf5/logger/logger.hpp>

// LoggerRedirector is used in tests to route logging from libdnf5::Base to the global logger.
class LoggerRedirector : public libdnf5::Logger {
public:
LoggerRedirector(libdnf5::Logger & target_logger) : target_logger{target_logger} {}

void write(
const std::chrono::time_point<std::chrono::system_clock> & time,
pid_t pid,
Level level,
const std::string & message) noexcept override {
target_logger.write(time, pid, level, message);
}

private:
libdnf5::Logger & target_logger;
};

#endif // TEST_LIBDNF5_LOGGER_REDIRECTOR_HPP
23 changes: 23 additions & 0 deletions test/shared/test_logger.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
Copyright Contributors to the libdnf project.

This file is part of libdnf: https://github.com/rpm-software-management/libdnf/

Libdnf is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.

Libdnf is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with libdnf. If not, see <https://www.gnu.org/licenses/>.
*/


#include "test_logger.hpp"

libdnf5::MemoryBufferLogger test_logger(10000, 256);
30 changes: 30 additions & 0 deletions test/shared/test_logger.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
Copyright Contributors to the libdnf project.

This file is part of libdnf: https://github.com/rpm-software-management/libdnf/

Libdnf is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.

Libdnf is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with libdnf. If not, see <https://www.gnu.org/licenses/>.
*/


#ifndef TEST_LIBDNF5_TEST_LOGGER_HPP
#define TEST_LIBDNF5_TEST_LOGGER_HPP

#include <libdnf5/logger/memory_buffer_logger.hpp>

// Global logger used in many tests.
// Logging from libdnf5::Base is routed to it using LoggerRedirector.
extern libdnf5::MemoryBufferLogger test_logger;

#endif // TEST_LIBDNF5_TEST_LOGGER_HPP
Loading