From a6aad5261c67a3959e4467a408404237aa29d963 Mon Sep 17 00:00:00 2001 From: Ichthyostega Date: Tue, 12 Mar 2024 23:46:33 +0100 Subject: [PATCH] Library: complete and verify temp-dir helper verify also that clean-up happens in case of exceptions thrown; as an aside, add Macro to check for ''any'' exception and match on something in the message (as opposed to just a Lumiera Exception) --- src/include/logging.h | 3 +- src/lib/error-exception.cpp | 10 ++-- src/lib/error.hpp | 6 +-- src/lib/test/temp-dir.hpp | 15 +++--- src/lib/test/test-helper.hpp | 31 ++++++++++-- tests/00support.tests | 2 +- tests/library/test/temp-dir-test.cpp | 70 +++++++++++++++++----------- wiki/thinkPad.ichthyo.mm | 46 ++++++++++-------- 8 files changed, 115 insertions(+), 68 deletions(-) diff --git a/src/include/logging.h b/src/include/logging.h index 48d582851..fe25167ed 100644 --- a/src/include/logging.h +++ b/src/include/logging.h @@ -129,7 +129,6 @@ NOBUG_CPP_DEFINE_FLAG_PARENT ( progress, logging); NOBUG_CPP_DEFINE_FLAG_PARENT ( main, progress); /** progress log for the vault layer */ NOBUG_CPP_DEFINE_FLAG_PARENT ( vault, progress); -NOBUG_CPP_DEFINE_FLAG_PARENT ( file, vault); //opening/closing files etc NOBUG_CPP_DEFINE_FLAG_PARENT ( mmap, vault); //mmap errors NOBUG_CPP_DEFINE_FLAG_PARENT ( thread, vault); //starting/stopping threads NOBUG_CPP_DEFINE_FLAG_PARENT ( threads, thread); @@ -151,7 +150,7 @@ NOBUG_CPP_DEFINE_FLAG_PARENT ( play, steam); NOBUG_CPP_DEFINE_FLAG_PARENT ( stage, progress); /** progress log for the support lib */ NOBUG_CPP_DEFINE_FLAG_PARENT ( library, progress); -NOBUG_CPP_DEFINE_FLAG_PARENT ( resourcecollector, library); +NOBUG_CPP_DEFINE_FLAG_PARENT ( filesys, library); //filesystem operations, opening/closing files... /** progress log for the common lib */ NOBUG_CPP_DEFINE_FLAG_PARENT ( common, progress); /** progress log, config subsystem */ diff --git a/src/lib/error-exception.cpp b/src/lib/error-exception.cpp index 573025bcc..5033772e4 100644 --- a/src/lib/error-exception.cpp +++ b/src/lib/error-exception.cpp @@ -107,7 +107,7 @@ namespace lumiera { { } - Error::Error (std::exception const& cause, + Error::Error (std::exception const& cause, string description, lumiera_err const id) noexcept : std::exception{} , id_{id} @@ -123,8 +123,8 @@ namespace lumiera { /** Description of the problem, including the internal char constant * in accordance to Lumiera's error identification scheme. - * If a root cause can be obtained, this will be included in the - * generated output as well. + * If a root cause can be obtained, this will be included + * in the generated output as well. */ CStr Error::what() const noexcept @@ -135,11 +135,11 @@ namespace lumiera { if (!isnil (desc_)) what_ += " ("+desc_+")."; if (!isnil (cause_)) what_ += string(" -- caused by: ") + cause_; } - return what_.c_str(); + return what_.c_str(); } - /** @internal get at the description message of the + /** @internal get at the description message of the * first exception encountered in a chain of exceptions */ const string diff --git a/src/lib/error.hpp b/src/lib/error.hpp index 29c538b1d..0091e7ff7 100644 --- a/src/lib/error.hpp +++ b/src/lib/error.hpp @@ -62,8 +62,8 @@ namespace lumiera { /** * Interface and Base definition for all Lumiera Exceptions. - * Provides common operations for getting an diagnostic message - * and to obtaining the _root cause_ message, i.e. the message + * Provides common operations for getting a diagnostic message + * and to obtain the _root cause_ message, i.e. the message * from the first exception encountered in a chain of exceptions. */ class Error @@ -234,7 +234,7 @@ namespace lumiera { * Check the lumiera error state, which maybe was set by C-code. * @throw Errorflag exception to signal an detected lumiera error * @note specific error code and information is enclosed in - * the raised exception; the error state is \em not cleared. + * the raised exception; the error state is _not cleared_. */ inline void throwOnError() diff --git a/src/lib/test/temp-dir.hpp b/src/lib/test/temp-dir.hpp index fb0ba9b20..56d8a5d2e 100644 --- a/src/lib/test/temp-dir.hpp +++ b/src/lib/test/temp-dir.hpp @@ -39,12 +39,8 @@ #include "lib/format-string.hpp" #include "lib/util.hpp" -//#include -//#include -//#include #include #include -//#include namespace lib { @@ -76,7 +72,8 @@ namespace test{ ~TempDir() { - destroyTempDirectory(); + if (fs::exists (loc_)) + destroyTempDirectory(); } @@ -145,9 +142,11 @@ namespace test{ void destroyTempDirectory() - { - UNIMPLEMENTED ("destroy"); - } + try { + fs::remove_all (loc_); + ENSURE (not fs::exists(loc_)); + } + ERROR_LOG_AND_IGNORE (filesys, "TempDir clean-up") }; diff --git a/src/lib/test/test-helper.hpp b/src/lib/test/test-helper.hpp index 7ad2e97d9..bfb31f7bb 100644 --- a/src/lib/test/test-helper.hpp +++ b/src/lib/test/test-helper.hpp @@ -337,16 +337,16 @@ operator""_expect (const char* lit, size_t siz) /* === test helper macros === */ /** - * Macro to verify a statement indeed raises an exception. + * Macro to verify that a statement indeed raises an exception. * If no exception is thrown, the #NOTREACHED macro will trigger - * an assertion failure. In case of an exception, the #lumiera_error + * an assertion failure. In case of exception, the #lumiera_error * state is checked, cleared and verified. */ #define VERIFY_ERROR(ERROR_ID, ERRONEOUS_STATEMENT) \ try \ { \ ERRONEOUS_STATEMENT ; \ - NOTREACHED("expected '%s' failure in: %s", \ + NOTREACHED("expected »%s« failure in: %s", \ #ERROR_ID, #ERRONEOUS_STATEMENT); \ } \ catch (lumiera::Error& ex) \ @@ -362,6 +362,31 @@ operator""_expect (const char* lit, size_t siz) lumiera_error(); \ } +/** + * Macro to verify that a statement indeed raises a std::exception, + * which additionally contains some FAILURE_MSG in its description. + */ +#define VERIFY_FAIL(FAILURE_MSG, ERRONEOUS_STATEMENT) \ + try \ + { \ + ERRONEOUS_STATEMENT ; \ + NOTREACHED("expected »%s«-failure in: %s" \ + , FAILURE_MSG, #ERRONEOUS_STATEMENT);\ + } \ + catch (std::exception& sex) \ + { \ + CHECK (util::contains (sex.what(), FAILURE_MSG) \ + ,"expected failure with »%s« -- but got: %s" \ + ,FAILURE_MSG, sex.what()); \ + lumiera_error(); \ + } \ + catch (...) \ + { \ + NOTREACHED("expected »%s«-failure, " \ + "yet something scary happened instead...", \ + FAILURE_MSG); \ + } + /** * Macro to mark the current test function in STDOUT. diff --git a/tests/00support.tests b/tests/00support.tests index 24da0687d..796a510a4 100644 --- a/tests/00support.tests +++ b/tests/00support.tests @@ -20,7 +20,7 @@ return: 0 END -PLANNED "Temporary working directory" TempDir_test < #include -//#include -//#include - -//using util::for_each; -//using lumiera::Error; -//using lumiera::LUMIERA_ERROR_EXCEPTION; -//using lumiera::error::LUMIERA_ERROR_ASSERTION; -//using lib::time::TimeVar; -//using lib::time::Time; - -//using boost::algorithm::is_lower; -//using boost::algorithm::is_digit; -//using std::function; -//using std::string; namespace lib { @@ -57,11 +37,9 @@ namespace test{ namespace test{ - - /***************************************************************//** - * @test validate proper working of a temporary working directory, - * with automatic name allocation and clean-up. + * @test validate proper behaviour of a temporary working directory, + * including automatic name allocation and clean-up. * @see temp-dir.hpp * @see DataCSV_test usage example */ @@ -85,7 +63,7 @@ namespace test{ std::ofstream out{ff, std::ios_base::out}; auto scree = randStr(55); - out << scree << endl; + out << scree << std::endl; out.close(); CHECK (fs::is_regular_file (ff)); @@ -99,10 +77,49 @@ namespace test{ - /** @test prints "sizeof()" including some type name. */ + /** @test automatic clean-up even in case of errors. */ void verify_Lifecycle () { + fs::path d1; + fs::path d2; + { + TempDir tt; + d1 = tt; + tt.makeFile("huibuh"); + tt.makeFile("huibuh"); + tt.makeFile("huibuh"); + std::ofstream boo{d1 / "huibuh"}; + boo << "boo"; + fs::create_directories(d1 / "bug/bear"); + fs::rename (d1 / "huibuh", d1 / "bug/bear/fray"); + + auto scare = [&]{ + TempDir tt; + d2 = tt; + tt.makeFile("Mooo"); + CHECK (fs::exists(d2 / "Mooo")); + CHECK (not fs::is_empty(d2)); + fs::create_directory(d2 / "Mooo"); // Booom! + }; + CHECK (d2.empty()); + CHECK (not d1.empty()); + + VERIFY_FAIL ("File exists", scare() ); + // nested context was cleaned-up after exception + CHECK (not fs::exists(d2)); + CHECK ( fs::exists(d1)); + CHECK (not d2.empty()); + CHECK (d1 != d2); + + boo << "moo"; + boo.close(); + CHECK (6 == fs::file_size(d1 / "bug/bear/fray")); + // so bottom line: can do filesystem stuff for real... + } + // All traces are gone... + CHECK (not fs::exists(d1)); + CHECK (not fs::exists(d2)); } }; @@ -110,4 +127,3 @@ namespace test{ }}} // namespace lib::test::test - diff --git a/wiki/thinkPad.ichthyo.mm b/wiki/thinkPad.ichthyo.mm index 29a85b08a..26fbea655 100644 --- a/wiki/thinkPad.ichthyo.mm +++ b/wiki/thinkPad.ichthyo.mm @@ -57236,18 +57236,20 @@ - - - - - + + + + + - + + + - - + + @@ -57373,16 +57375,22 @@ - - + + + + + + + + - - + + @@ -112062,17 +112070,17 @@ Date:   Thu Apr 20 18:53:17 2023 +0200
- - - - - + + + + + - - + +