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)
This commit is contained in:
Fischlurch 2024-03-12 23:46:33 +01:00
parent 18b1d37a3d
commit a6aad5261c
8 changed files with 115 additions and 68 deletions

View file

@ -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 */

View file

@ -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

View file

@ -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()

View file

@ -39,12 +39,8 @@
#include "lib/format-string.hpp"
#include "lib/util.hpp"
//#include <unordered_map>
//#include <iostream>
//#include <vector>
#include <fstream>
#include <string>
//#include <map>
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")
};

View file

@ -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.

View file

@ -20,7 +20,7 @@ return: 0
END
PLANNED "Temporary working directory" TempDir_test <<END
TEST "Temporary working directory" TempDir_test <<END
return: 0
END

View file

@ -28,28 +28,8 @@
#include "lib/test/run.hpp"
#include "lib/test/test-helper.hpp"
#include "lib/test/temp-dir.hpp"
//#include "lib/time/timevalue.hpp"
//#include "lib/error.hpp"
//#include "lib/util-foreach.hpp"
#include "lib/format-cout.hpp"
#include "lib/test/diagnostic-output.hpp"
//#include <boost/algorithm/string.hpp>
#include <fstream>
//#include <functional>
//#include <string>
//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

View file

@ -57236,18 +57236,20 @@
</node>
</node>
</node>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1710170202711" ID="ID_828243870" MODIFIED="1710170421762" TEXT="TempDir : ein tempor&#xe4;res Arbeitsverzeichnis f&#xfc;r Tests">
<icon BUILTIN="flag-yellow"/>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1710170260096" ID="ID_1777987925" MODIFIED="1710170459123" TEXT="Verhalten definiert per TempDir_test">
<arrowlink COLOR="#ad7a88" DESTINATION="ID_992572632" ENDARROW="Default" ENDINCLINATION="-1582;-265;" ID="Arrow_ID_891079113" STARTARROW="None" STARTINCLINATION="1511;114;"/>
<icon BUILTIN="flag-yellow"/>
<node COLOR="#338800" CREATED="1710170202711" ID="ID_828243870" MODIFIED="1710294040890" TEXT="TempDir : ein tempor&#xe4;res Arbeitsverzeichnis f&#xfc;r Tests">
<icon BUILTIN="button_ok"/>
<node COLOR="#338800" CREATED="1710170260096" ID="ID_1777987925" MODIFIED="1710293999811" TEXT="Verhalten definiert per TempDir_test">
<arrowlink COLOR="#7a9cad" DESTINATION="ID_992572632" ENDARROW="Default" ENDINCLINATION="-1582;-265;" ID="Arrow_ID_891079113" STARTARROW="None" STARTINCLINATION="1511;114;"/>
<icon BUILTIN="button_ok"/>
</node>
<node CREATED="1710185830600" ID="ID_1750124828" MODIFIED="1710185837239" TEXT="Spezifikation">
<node CREATED="1710185830600" ID="ID_1750124828" MODIFIED="1710294002587" TEXT="Spezifikation">
<icon BUILTIN="info"/>
<node CREATED="1710185838462" ID="ID_1494325621" MODIFIED="1710185847781" TEXT="modelliert als RAII"/>
<node CREATED="1710185850649" ID="ID_507946689" MODIFIED="1710185923149" TEXT="erzeugt einen zuf&#xe4;lligen Namen"/>
<node CREATED="1710294005525" ID="ID_1879137488" MODIFIED="1710294012655" TEXT="kann zuf&#xe4;llige Dateien erzeugen"/>
</node>
<node BACKGROUND_COLOR="#eef0c5" COLOR="#990000" CREATED="1710185930891" ID="ID_406086033" MODIFIED="1710280179518" TEXT="Implementierung">
<icon BUILTIN="pencil"/>
<node COLOR="#338800" CREATED="1710185930891" FOLDED="true" ID="ID_406086033" MODIFIED="1710294035139" TEXT="Implementierung">
<icon BUILTIN="button_ok"/>
<node CREATED="1710185934990" ID="ID_1561994557" LINK="https://stackoverflow.com/a/58454949" MODIFIED="1710186020053" TEXT="Ausgangspunkt: L&#xf6;sung von Stackoverflow"/>
<node COLOR="#338800" CREATED="1710186086663" ID="ID_206197040" MODIFIED="1710269456216" TEXT="Zufallserzeugung extrahiert">
<arrowlink COLOR="#752d90" DESTINATION="ID_588494337" ENDARROW="Default" ENDINCLINATION="33;-54;" ID="Arrow_ID_353980881" STARTARROW="None" STARTINCLINATION="-193;18;"/>
@ -57373,16 +57375,22 @@
<node CREATED="1710269947604" ID="ID_1418089221" MODIFIED="1710269962399" TEXT="Hilfsfunktion hierf&#xfc;r &#x27f6; file.hpp"/>
</node>
</node>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1710188193120" ID="ID_988204568" MODIFIED="1710188200824" TEXT="Destruktion">
<icon BUILTIN="flag-yellow"/>
<node COLOR="#338800" CREATED="1710188193120" ID="ID_988204568" MODIFIED="1710286037329" TEXT="Destruktion">
<icon BUILTIN="button_ok"/>
<node COLOR="#435e98" CREATED="1710286061761" ID="ID_1188781692" MODIFIED="1710286070923" TEXT="Ha! fs::remove_all(path)">
<icon BUILTIN="ksmiletris"/>
</node>
<node COLOR="#338800" CREATED="1710286073199" ID="ID_674596605" MODIFIED="1710286089427" TEXT="alle Exceptions absorbieren">
<icon BUILTIN="yes"/>
</node>
</node>
<node COLOR="#338800" CREATED="1710280159637" ID="ID_1031094481" MODIFIED="1710280171203" TEXT="Eindeutige Dateien dort erzeugen">
<icon BUILTIN="button_ok"/>
</node>
</node>
<node CREATED="1710269985121" ID="ID_1475343702" MODIFIED="1710269987814" TEXT="Test">
<node BACKGROUND_COLOR="#eef0c5" COLOR="#990000" CREATED="1710269988763" ID="ID_1423252413" LINK="#ID_992572632" MODIFIED="1710270061936" TEXT="Test f&#xfc;r TempDir">
<icon BUILTIN="pencil"/>
<node COLOR="#338800" CREATED="1710269988763" ID="ID_1423252413" LINK="#ID_992572632" MODIFIED="1710293968479" TEXT="Test f&#xfc;r TempDir">
<icon BUILTIN="button_ok"/>
</node>
<node BACKGROUND_COLOR="#fdfdcf" COLOR="#ff0000" CREATED="1710270007654" ID="ID_1049869933" MODIFIED="1710270065664" TEXT="Abdeckung einiger Filesystem-Zusatzfunktionen">
<icon BUILTIN="flag-pink"/>
@ -112062,17 +112070,17 @@ Date:&#160;&#160;&#160;Thu Apr 20 18:53:17 2023 +0200<br/>
<node CREATED="1710167587540" ID="ID_1196727143" MODIFIED="1710167645967" TEXT="group=calculation"/>
<node CREATED="1710167598409" ID="ID_498080750" MODIFIED="1710167615767" TEXT="Mathematik, aber auch Farbr&#xe4;ume und Signalverarbeitung"/>
</node>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1710170186234" ID="ID_992572632" MODIFIED="1710170471944" TEXT="TempDir_test">
<linktarget COLOR="#ad7a88" DESTINATION="ID_992572632" ENDARROW="Default" ENDINCLINATION="-1582;-265;" ID="Arrow_ID_891079113" SOURCE="ID_1777987925" STARTARROW="None" STARTINCLINATION="1511;114;"/>
<icon BUILTIN="flag-yellow"/>
<node BACKGROUND_COLOR="#eef0c5" COLOR="#990000" CREATED="1710170516550" ID="ID_1675475629" MODIFIED="1710175170639" TEXT="simpleUsage">
<icon BUILTIN="pencil"/>
<node COLOR="#338800" CREATED="1710170186234" ID="ID_992572632" MODIFIED="1710293988771" TEXT="TempDir_test">
<linktarget COLOR="#7a9cad" DESTINATION="ID_992572632" ENDARROW="Default" ENDINCLINATION="-1582;-265;" ID="Arrow_ID_891079113" SOURCE="ID_1777987925" STARTARROW="None" STARTINCLINATION="1511;114;"/>
<icon BUILTIN="button_ok"/>
<node COLOR="#338800" CREATED="1710170516550" ID="ID_1675475629" MODIFIED="1710293977320" TEXT="simpleUsage">
<icon BUILTIN="button_ok"/>
<node CREATED="1710170549626" ID="ID_1134291003" MODIFIED="1710170554749" TEXT="RAII-Objekt erstellen"/>
<node CREATED="1710170555509" ID="ID_1842541319" MODIFIED="1710170574082" TEXT="Datei dorhin platzieren"/>
<node CREATED="1710170575118" ID="ID_819300234" MODIFIED="1710170577186" TEXT="reinschreiben"/>
</node>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1710170579669" ID="ID_16645191" MODIFIED="1710170590652" TEXT="verify_Lifecycle">
<icon BUILTIN="flag-yellow"/>
<node COLOR="#338800" CREATED="1710170579669" ID="ID_16645191" MODIFIED="1710293984720" TEXT="verify_Lifecycle">
<icon BUILTIN="button_ok"/>
<node CREATED="1710170593004" ID="ID_1544820358" MODIFIED="1710170600981" TEXT="mehrere RAII-Objekte"/>
<node CREATED="1710170615896" ID="ID_1289416435" MODIFIED="1710170620870" TEXT="haben verschiedene Pfade"/>
<node CREATED="1710170623288" ID="ID_212131047" MODIFIED="1710170629899" TEXT="sind nachher weg"/>