18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-27 23:57:09 +01:00
Files
maszyna/tests/unit/test_log_files.cpp
jerrrrycho 8c8d5ff5cc Keep the shared log.txt and errors.txt when the run writes its own logs (multiplelogs) (#341)
* fix: keep the shared log.txt and errors.txt when the run writes multiplelogs

Every start deleted log.txt and errors.txt in the game folder before the
settings were read, so a run with multiplelogs (each scenario test) wiped
the logs of the player's last ordinary game although it writes its own
files in logs/.

The shared files are now deleted after the settings, and only without
multiplelogs, where the run is about to write them (an errors.txt of an
earlier run must still not stay behind a run without errors). The log
service keeps what is logged until then, so an early message can't open
and empty log.txt before multiplelogs is known. The file names come from
one place (utilities/logfiles), covered by unit tests.

* refactor(log): the wait for the log file names without Sonar issues

The log service skips a pass with an early continue instead of wrapping
the writing in an if, so the writing keeps its nesting. No change in
behaviour.

* fix(log): the node bank preview tool opens its log files after the settings

The log service holds every message until OpenLogFiles() is called, and
only the game called it. The preview tool kept its whole log in memory
and wrote it only at the end, so a crash or a killed run lost it.
2026-11-25 06:14:43 +01:00

36 lines
1.5 KiB
C++

#include "stdafx.h"
#include "doctest.h"
#include "utilities/logfiles.h"
#include <algorithm>
// the files the log is written to, and which of them a run deletes when it starts
TEST_CASE("log files: without multiplelogs the run writes the shared log.txt and errors.txt")
{
CHECK(log_file_name(log_file::log, false, "td", "20261112_1200123") == "log.txt");
CHECK(log_file_name(log_file::errors, false, "td", "20261112_1200123") == "errors.txt");
}
TEST_CASE("log files: with multiplelogs the run writes its own in logs/, named after the scenery and the time")
{
CHECK(log_file_name(log_file::log, true, "autotest_case", "20261112_1200123") == "logs/log (autotest_case) 20261112_1200123.txt");
CHECK(log_file_name(log_file::errors, true, "autotest_case", "20261112_1200123") == "logs/errors (autotest_case) 20261112_1200123.txt");
}
TEST_CASE("log files: without multiplelogs the start deletes the shared files it is going to write")
{
// errors.txt is made at the first error only: one of the run before must not stay behind a run without errors
auto const stale{stale_log_files(false)};
CHECK(stale.size() == 2);
for (auto const kind : {log_file::log, log_file::errors})
CHECK(std::ranges::count(stale, log_file_name(kind, false, "td", "20261112_1200123")) == 1);
}
TEST_CASE("log files: with multiplelogs the start keeps the shared log.txt and errors.txt of the last ordinary run")
{
// the scenario tests run with multiplelogs; the logs of the player's last game in the game folder stay
CHECK(stale_log_files(true).empty());
}