From 1083f3cb5db56af9775d975852d14dbacc0848f3 Mon Sep 17 00:00:00 2001 From: jerrrrycho Date: Sun, 8 Nov 2026 04:23:30 +0100 Subject: [PATCH] fix: make the odometer test's scratch directory in the build directory for CI Sonar (S5443) flags the shared temporary directory; the tests now use one in their working directory. Also makes the scratch directory non-copyable and spells out the optional checks Sonar asked about. --- tests/unit/test_odometer.cpp | 7 +++++-- vehicle/Train.cpp | 2 +- vehicle/odometer.cpp | 4 ++-- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/tests/unit/test_odometer.cpp b/tests/unit/test_odometer.cpp index 8297d3167..784fabaab 100644 --- a/tests/unit/test_odometer.cpp +++ b/tests/unit/test_odometer.cpp @@ -24,13 +24,16 @@ odometer::reading run(double const Km, double const Metres) { return reading; } -// a directory of its own for a test, removed afterwards +// a directory of its own for a test, removed afterwards. it's made in the working directory of the tests (the build +// directory), not in the shared temporary one struct scratch_directory { std::filesystem::path path; - explicit scratch_directory(std::string const &Name) : path{std::filesystem::temp_directory_path() / ("eu07_unit_" + Name)} { + explicit scratch_directory(std::string const &Name) : path{std::filesystem::current_path() / ("eu07_unit_" + Name)} { std::filesystem::remove_all(path); std::filesystem::create_directories(path); } + scratch_directory(scratch_directory const &) = delete; + scratch_directory &operator=(scratch_directory const &) = delete; ~scratch_directory() { std::error_code error; std::filesystem::remove_all(path, error); diff --git a/vehicle/Train.cpp b/vehicle/Train.cpp index 5a918809a..c48ef69c6 100644 --- a/vehicle/Train.cpp +++ b/vehicle/Train.cpp @@ -12323,7 +12323,7 @@ bool TTrain::initialize_gauge(cParser &Parser, std::string const &Label, int con gauge.Load(Parser, DynamicObject); // the counter shows the odometer of the vehicle, kept between the sessions (eu07/maszyna#72). its offset // was the state it started each session with, now it is that only for a vehicle with no known state - if (auto const seed{odometer::counter_seed(gauge.offset(), gauge.scale())}) + if (auto const seed{odometer::counter_seed(gauge.offset(), gauge.scale())}; seed.has_value()) mvControlled->Odometer.seed(*seed); gauge.offset(0.f); gauge.AssignDouble(&mvControlled->Odometer.km); diff --git a/vehicle/odometer.cpp b/vehicle/odometer.cpp index 5f2a185d1..9d03462b2 100644 --- a/vehicle/odometer.cpp +++ b/vehicle/odometer.cpp @@ -153,9 +153,9 @@ double guessed_state(std::string const &Vehicle) { reading starting_state(store const &States, std::string const &Vehicle, std::filesystem::path const &Vehicledirectory) { reading result; - if (auto const saved{States.find(Vehicle)}) { + if (auto const saved{States.find(Vehicle)}; saved.has_value()) { result.begin(*saved, origin::saved); - } else if (auto const legacy{legacy_state(Vehicledirectory, Vehicle)}) { + } else if (auto const legacy{legacy_state(Vehicledirectory, Vehicle)}; legacy.has_value()) { result.begin(*legacy, origin::legacy); } else { result.begin(guessed_state(Vehicle), origin::guessed);