1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
|
/*
* Copyright (C) 2019-2025 Matthias Klumpp <matthias@tenstral.net>
*
* SPDX-License-Identifier: LGPL-3.0-or-later
*/
#define CATCH_CONFIG_MAIN
#include <catch2/catch_all.hpp>
#include <fstream>
#include <filesystem>
#include <regex>
#include <cstdlib>
#include <format>
#include <chrono>
#include <thread>
#include <memory>
#include <vector>
#include "logging.h"
#include "reportgenerator.h"
#include "datastore.h"
#include "config.h"
#include "backends/interfaces.h"
#include "backends/dummy/dummypkg.h"
#include "result.h"
#include "hintregistry.h"
using namespace ASGenerator;
static struct TestSetup {
TestSetup()
{
setVerbose(true);
}
} testSetup;
// Test fixture for report generator tests
class ReportGeneratorTestFixture
{
public:
ReportGeneratorTestFixture()
{
// Create temporary directories for testing
m_tempDir = fs::temp_directory_path() / "asgen_test"
/ std::to_string(std::chrono::steady_clock::now().time_since_epoch().count());
fs::create_directories(m_tempDir);
m_dbDir = m_tempDir / "db";
m_htmlDir = m_tempDir / "html";
m_mediaDir = m_tempDir / "media";
fs::create_directories(m_dbDir);
fs::create_directories(m_htmlDir);
fs::create_directories(m_mediaDir);
// Use the default templates
m_templateDir = Utils::getDataPath("templates/default");
// Create a test configuration file and load configuration
auto configFile = createTestConfig();
Config::get().loadFromFile(configFile.string(), m_tempDir.string(), (m_tempDir / "data").string());
// Initialize datastore
m_dstore = std::make_unique<DataStore>();
m_dstore->open(m_dbDir.string(), m_mediaDir.string());
// Load the hints registry to avoid hint tag errors
loadHintsRegistry();
// Create report generator
m_reportGen = std::make_unique<ReportGenerator>(m_dstore.get());
}
~ReportGeneratorTestFixture()
{
m_reportGen.reset();
m_dstore.reset();
// Clean up temporary directory
std::error_code ec;
fs::remove_all(m_tempDir, ec);
}
protected:
std::vector<std::shared_ptr<Package>> createTestPackages()
{
std::vector<std::shared_ptr<Package>> packages;
auto pkg1 = std::make_shared<DummyPackage>("testpkg1", "1.0.0", "amd64");
pkg1->setMaintainer("Test Maintainer <test@example.com>");
pkg1->setFilename("testpkg1_1.0.0_amd64.deb");
packages.push_back(std::move(pkg1));
auto pkg2 = std::make_shared<DummyPackage>("testpkg2", "2.0.0", "amd64");
pkg2->setMaintainer("Another Maintainer <another@example.com>");
pkg2->setFilename("testpkg2_2.0.0_amd64.deb");
packages.push_back(std::move(pkg2));
auto pkg3 = std::make_shared<DummyPackage>("testpkg3", "1.5.0", "riscv64");
pkg3->setMaintainer("Test Maintainer <test@example.com>");
pkg3->setFilename("testpkg3_1.5.0_i386.deb");
packages.push_back(std::move(pkg3));
return packages;
}
void addTestData()
{
// Add some test metadata to the datastore
m_dstore->setMetadata(DataType::YAML, "test.gcid.1", R"(
Type: desktop-application
ID: test.app.1
Name:
C: Test Application 1
Summary:
C: A test application
)");
// Add some test hints for testpkg1
m_dstore->setHints("testpkg1/1.0.0/amd64", R"({
"hints": {
"test.app.1": [
{
"tag": "missing-desktop-file",
"vars": {
"filename": "test.desktop"
}
}
]
}
})");
// Add test hints for testpkg2 so it gets processed by preprocessInformation
m_dstore->setHints("testpkg2/2.0.0/amd64", R"({
"hints": {
"test.app.2": [
{
"tag": "icon-not-found",
"vars": {
"icon_fname": "test-icon.png"
}
}
]
}
})");
}
fs::path createTestConfig()
{
// Create a minimal test configuration
auto configFile = m_tempDir / "test-config.json";
std::ofstream configStream(configFile);
configStream << R"({
"ProjectName": "Test Project",
"ArchiveRoot": "/tmp/archive",
"WorkspaceDir": ")"
<< m_tempDir.string() << R"(",
"MediaBaseUrl": "https://example.com/media",
"HtmlBaseUrl": "https://example.com/html",
"TemplateDir": ")"
<< m_templateDir.string() << R"(",
"ExportDirs": {
"Html": ")" << m_htmlDir.string()
<< R"(",
"Media": ")" << m_mediaDir.string()
<< R"("
},
"Backend": "dummy",
"Suites": {
"testsuite": {
"sections": ["main"],
"architectures": ["amd64", "i386"]
}
}
})";
configStream.close();
return configFile;
}
protected:
fs::path m_tempDir;
fs::path m_dbDir;
fs::path m_htmlDir;
fs::path m_mediaDir;
fs::path m_templateDir;
std::unique_ptr<DataStore> m_dstore;
std::unique_ptr<ReportGenerator> m_reportGen;
};
TEST_CASE_METHOD(ReportGeneratorTestFixture, "ReportGenerator::preprocessInformation")
{
addTestData();
auto packages = createTestPackages();
SECTION("Data preprocessing")
{
auto dsum = m_reportGen->preprocessInformation("testsuite", "main", packages);
REQUIRE(!dsum.pkgSummaries.empty());
REQUIRE(!dsum.hintEntries.empty());
// Check that we have the expected maintainer
REQUIRE(dsum.pkgSummaries.count("Test Maintainer <test@example.com>") > 0);
REQUIRE(dsum.pkgSummaries.count("Another Maintainer <another@example.com>") > 0);
// Check hints are processed
REQUIRE(dsum.hintEntries.count("testpkg1") > 0);
REQUIRE(dsum.hintEntries.count("testpkg2") > 0);
}
}
TEST_CASE_METHOD(ReportGeneratorTestFixture, "ReportGenerator::renderPage")
{
SECTION("Basic page rendering")
{
inja::json context;
context["suite"] = "testsuite";
context["section"] = "main";
// Add the suites array that the main template expects
inja::json suites = inja::json::array();
inja::json suite;
suite["suite"] = "testsuite";
suites.push_back(suite);
context["suites"] = suites;
// Add empty oldsuites array
context["oldsuites"] = inja::json::array();
REQUIRE_NOTHROW(m_reportGen->renderPage("main", "test_main", context));
// Check that the file was created
auto outputFile = m_htmlDir / "test_main.html";
REQUIRE(fs::exists(outputFile));
// Read and verify basic content
std::ifstream file(outputFile);
std::string content((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
REQUIRE(content.find("Test Project") != std::string::npos);
}
SECTION("Page rendering with complex context")
{
inja::json context;
context["suite"] = "testsuite";
context["section"] = "main";
context["package_name"] = "testpkg";
inja::json entries = inja::json::array();
inja::json entry;
entry["component_id"] = "test.app.1";
entry["has_errors"] = true;
entry["has_warnings"] = false;
entry["has_infos"] = false;
// Add the architectures field that the template expects
inja::json architectures = inja::json::array();
inja::json arch;
arch["arch"] = "amd64";
architectures.push_back(arch);
entry["architectures"] = architectures;
inja::json errors = inja::json::array();
inja::json error;
error["error_tag"] = "test-error";
error["error_description"] = "Test error description";
errors.push_back(error);
entry["errors"] = errors;
entries.push_back(entry);
context["entries"] = entries;
REQUIRE_NOTHROW(m_reportGen->renderPage("issues_page", "test_issues", context));
auto outputFile = m_htmlDir / "test_issues.html";
REQUIRE(fs::exists(outputFile));
}
}
TEST_CASE_METHOD(ReportGeneratorTestFixture, "ReportGenerator Statistics")
{
SECTION("Export statistics")
{
// Add some test statistics first
std::unordered_map<std::string, std::variant<std::int64_t, std::string, double>> statsData = {
{"suite", std::string("testsuite")},
{"section", std::string("main") },
{"totalInfos", std::int64_t(5) },
{"totalWarnings", std::int64_t(3) },
{"totalErrors", std::int64_t(1) },
{"totalMetadata", std::int64_t(10) }
};
m_dstore->addStatistics(statsData);
REQUIRE_NOTHROW(m_reportGen->exportStatistics());
auto statsFile = m_htmlDir / "statistics.json";
REQUIRE(fs::exists(statsFile));
std::ifstream file(statsFile);
std::string content((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
REQUIRE(content.find("testsuite") != std::string::npos);
REQUIRE(content.find("main") != std::string::npos);
REQUIRE(content.find("errors") != std::string::npos);
REQUIRE(content.find("warnings") != std::string::npos);
REQUIRE(content.find("infos") != std::string::npos);
REQUIRE(content.find("metadata") != std::string::npos);
// Verify the actual numeric values are present
REQUIRE(content.find(",1]") != std::string::npos); // totalErrors: 1
REQUIRE(content.find(",3]") != std::string::npos); // totalWarnings: 3
REQUIRE(content.find(",5]") != std::string::npos); // totalInfos: 5
REQUIRE(content.find(",10]") != std::string::npos); // totalMetadata: 10
}
}
TEST_CASE_METHOD(ReportGeneratorTestFixture, "ReportGenerator render pages with mock data")
{
SECTION("Process packages for suite/section")
{
auto packages = createTestPackages();
REQUIRE_NOTHROW(m_reportGen->processFor("testsuite", "main", packages));
// Check that the section directory structure was created
auto sectionDir = m_htmlDir / "testsuite" / "main";
REQUIRE(fs::exists(sectionDir));
}
SECTION("Render pages with hint entries")
{
ReportGenerator::DataSummary dsum;
// Create mock hint entry
ReportGenerator::HintEntry hentry;
hentry.identifier = "test.component.1";
hentry.archs = {"amd64", "i386"};
hentry.errors = {
{"error-tag", "Error message"}
};
hentry.warnings = {
{"warning-tag", "Warning message"}
};
hentry.infos = {
{"info-tag", "Info message"}
};
dsum.hintEntries["testpkg1"]["test.component.1"] = std::move(hentry);
// Create mock package summary
ReportGenerator::PkgSummary summary;
summary.pkgname = "testpkg1";
summary.errorCount = 1;
summary.warningCount = 1;
summary.infoCount = 1;
dsum.pkgSummaries["Test Maintainer"]["testpkg1"] = std::move(summary);
REQUIRE_NOTHROW(m_reportGen->renderPagesFor("testsuite", "main", dsum));
// Check that issue pages were created
auto issuesIndex = m_htmlDir / "testsuite" / "main" / "issues" / "index.html";
REQUIRE(fs::exists(issuesIndex));
auto issuesPage = m_htmlDir / "testsuite" / "main" / "issues" / "testpkg1.html";
REQUIRE(fs::exists(issuesPage));
// Verify content
std::ifstream file(issuesPage);
std::string content((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
REQUIRE(content.find("test.component.1") != std::string::npos);
REQUIRE(content.find("Error message") != std::string::npos);
REQUIRE(content.find("Warning message") != std::string::npos);
REQUIRE(content.find("Info message") != std::string::npos);
}
SECTION("Render pages with metadata entries")
{
ReportGenerator::DataSummary dsum;
// Create mock metadata entry
ReportGenerator::MetadataEntry mentry;
mentry.kind = AS_COMPONENT_KIND_DESKTOP_APP;
mentry.identifier = "test.app.1";
mentry.archs = {"amd64"};
mentry.data = "Type: desktop-application\nID: test.app.1\n";
mentry.iconName = "test-icon.png";
dsum.mdataEntries["testpkg1"]["1.0.0"]["test.gcid.1"] = std::move(mentry);
// Create mock package summary with components
ReportGenerator::PkgSummary summary;
summary.pkgname = "testpkg1";
summary.cpts = {"test.app.1 - 1.0.0"};
dsum.pkgSummaries["Test Maintainer"]["testpkg1"] = std::move(summary);
REQUIRE_NOTHROW(m_reportGen->renderPagesFor("testsuite", "main", dsum));
// Check that metainfo pages were created
auto metainfoIndex = m_htmlDir / "testsuite" / "main" / "metainfo" / "index.html";
REQUIRE(fs::exists(metainfoIndex));
auto metainfoPage = m_htmlDir / "testsuite" / "main" / "metainfo" / "testpkg1.html";
REQUIRE(fs::exists(metainfoPage));
// Verify content
std::ifstream file(metainfoPage);
std::string content((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
REQUIRE(content.find("test.app.1 - 1.0.0") != std::string::npos);
REQUIRE(content.find("Type: desktop-application") != std::string::npos);
}
SECTION("Render section index page")
{
ReportGenerator::DataSummary dsum;
dsum.totalMetadata = 10;
dsum.totalInfos = 5;
dsum.totalWarnings = 3;
dsum.totalErrors = 1;
REQUIRE_NOTHROW(m_reportGen->renderPagesFor("testsuite", "main", dsum));
auto sectionIndex = m_htmlDir / "testsuite" / "main" / "index.html";
REQUIRE(fs::exists(sectionIndex));
// Verify statistics are rendered
std::ifstream file(sectionIndex);
std::string content((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
// Check for text that's actually in the section_page.html template
REQUIRE(
content.find("valid components")
!= std::string::npos); // From the template: "{{metainfo_count}} valid components"
REQUIRE(content.find("errors") != std::string::npos); // From the template
REQUIRE(content.find("warnings") != std::string::npos); // From the template
}
SECTION("Update index pages")
{
REQUIRE_NOTHROW(m_reportGen->updateIndexPages());
// Check that main index was created
auto mainIndex = m_htmlDir / "index.html";
REQUIRE(fs::exists(mainIndex));
// Verify content contains expected elements
std::ifstream file(mainIndex);
std::string content((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
// Check for text that's actually in the templates
REQUIRE(content.find("Generated by") != std::string::npos); // From base.html footer
REQUIRE(content.find("appstream-generator") != std::string::npos); // From base.html footer
}
}
|