feat: add publisher metadata (#3080)
This commit is contained in:
135
tests/tests_log_checker.h
Normal file
135
tests/tests_log_checker.h
Normal file
@@ -0,0 +1,135 @@
|
||||
/**
|
||||
* @file tests/tests_log_checker.h
|
||||
* @brief Utility functions to check log file contents.
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
#include <fstream>
|
||||
#include <regex>
|
||||
#include <string>
|
||||
|
||||
#include <src/logging.h>
|
||||
|
||||
namespace log_checker {
|
||||
|
||||
/**
|
||||
* @brief Remove the timestamp prefix from a log line.
|
||||
* @param line The log line.
|
||||
* @return The log line without the timestamp prefix.
|
||||
*/
|
||||
inline std::string
|
||||
remove_timestamp_prefix(const std::string &line) {
|
||||
static const std::regex timestamp_regex(R"(\[\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3}\]: )");
|
||||
return std::regex_replace(line, timestamp_regex, "");
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Check if a log file contains a line that starts with the given string.
|
||||
* @param log_file Path to the log file.
|
||||
* @param start_str The string that the line should start with.
|
||||
* @return True if such a line is found, false otherwise.
|
||||
*/
|
||||
inline bool
|
||||
line_starts_with(const std::string &log_file, const std::string_view &start_str) {
|
||||
logging::log_flush();
|
||||
|
||||
std::ifstream input(log_file);
|
||||
if (!input.is_open()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (std::string line; std::getline(input, line);) {
|
||||
line = remove_timestamp_prefix(line);
|
||||
if (line.rfind(start_str, 0) == 0) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Check if a log file contains a line that ends with the given string.
|
||||
* @param log_file Path to the log file.
|
||||
* @param end_str The string that the line should end with.
|
||||
* @return True if such a line is found, false otherwise.
|
||||
*/
|
||||
inline bool
|
||||
line_ends_with(const std::string &log_file, const std::string_view &end_str) {
|
||||
logging::log_flush();
|
||||
|
||||
std::ifstream input(log_file);
|
||||
if (!input.is_open()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (std::string line; std::getline(input, line);) {
|
||||
line = remove_timestamp_prefix(line);
|
||||
if (line.size() >= end_str.size() &&
|
||||
line.compare(line.size() - end_str.size(), end_str.size(), end_str) == 0) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Check if a log file contains a line that equals the given string.
|
||||
* @param log_file Path to the log file.
|
||||
* @param str The string that the line should equal.
|
||||
* @return True if such a line is found, false otherwise.
|
||||
*/
|
||||
inline bool
|
||||
line_equals(const std::string &log_file, const std::string_view &str) {
|
||||
logging::log_flush();
|
||||
|
||||
std::ifstream input(log_file);
|
||||
if (!input.is_open()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (std::string line; std::getline(input, line);) {
|
||||
line = remove_timestamp_prefix(line);
|
||||
if (line == str) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Check if a log file contains a line that contains the given substring.
|
||||
* @param log_file Path to the log file.
|
||||
* @param substr The substring to search for.
|
||||
* @param case_insensitive Whether the search should be case-insensitive.
|
||||
* @return True if such a line is found, false otherwise.
|
||||
*/
|
||||
inline bool
|
||||
line_contains(const std::string &log_file, const std::string_view &substr, bool case_insensitive = false) {
|
||||
logging::log_flush();
|
||||
|
||||
std::ifstream input(log_file);
|
||||
if (!input.is_open()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string search_str(substr);
|
||||
if (case_insensitive) {
|
||||
// sonarcloud complains about this, but the solution doesn't work for macOS-12
|
||||
std::transform(search_str.begin(), search_str.end(), search_str.begin(), ::tolower);
|
||||
}
|
||||
|
||||
for (std::string line; std::getline(input, line);) {
|
||||
line = remove_timestamp_prefix(line);
|
||||
if (case_insensitive) {
|
||||
// sonarcloud complains about this, but the solution doesn't work for macOS-12
|
||||
std::transform(line.begin(), line.end(), line.begin(), ::tolower);
|
||||
}
|
||||
if (line.find(search_str) != std::string::npos) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
} // namespace log_checker
|
||||
18
tests/unit/test_entry_handler.cpp
Normal file
18
tests/unit/test_entry_handler.cpp
Normal file
@@ -0,0 +1,18 @@
|
||||
/**
|
||||
* @file tests/unit/test_entry_handler.cpp
|
||||
* @brief Test src/entry_handler.*.
|
||||
*/
|
||||
#include <src/entry_handler.h>
|
||||
|
||||
#include "../tests_common.h"
|
||||
#include "../tests_log_checker.h"
|
||||
|
||||
TEST(EntryHandlerTests, LogPublisherDataTest) {
|
||||
// call log_publisher_data
|
||||
log_publisher_data();
|
||||
|
||||
// check if specific log messages exist
|
||||
ASSERT_TRUE(log_checker::line_starts_with("test_sunshine.log", "Info: Package Publisher: "));
|
||||
ASSERT_TRUE(log_checker::line_starts_with("test_sunshine.log", "Info: Publisher Website: "));
|
||||
ASSERT_TRUE(log_checker::line_starts_with("test_sunshine.log", "Info: Get support: "));
|
||||
}
|
||||
@@ -5,8 +5,8 @@
|
||||
#include <src/logging.h>
|
||||
|
||||
#include "../tests_common.h"
|
||||
#include "../tests_log_checker.h"
|
||||
|
||||
#include <fstream>
|
||||
#include <random>
|
||||
|
||||
namespace {
|
||||
@@ -40,25 +40,5 @@ TEST_P(LogLevelsTest, PutMessage) {
|
||||
auto test_message = std::to_string(rand_gen()) + std::to_string(rand_gen());
|
||||
BOOST_LOG(logger) << test_message;
|
||||
|
||||
// Flush logger and search for the message in the log file
|
||||
|
||||
logging::log_flush();
|
||||
|
||||
std::ifstream input(log_file);
|
||||
ASSERT_TRUE(input.is_open());
|
||||
|
||||
bool found = false;
|
||||
for (std::string line; std::getline(input, line);) {
|
||||
if (line.find(test_message) != std::string::npos) {
|
||||
// Assume that logger may change the case of log level label
|
||||
std::transform(line.begin(), line.end(), line.begin(),
|
||||
[](char c) { return std::tolower(c); });
|
||||
|
||||
if (line.find(label) != std::string::npos) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
ASSERT_TRUE(found);
|
||||
ASSERT_TRUE(log_checker::line_contains(log_file, test_message));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user