Library: coverage for some filesystem convenience helpers

...created as a byproduct of the TempDir feature,
which in turn is required to do ''any'' meaningful unit-test
of filesystem related functionality.
This commit is contained in:
Fischlurch 2024-03-13 01:52:11 +01:00
parent a6aad5261c
commit 1c2cbd4d47
2 changed files with 66 additions and 67 deletions

View file

@ -29,28 +29,8 @@
#include "lib/test/test-helper.hpp"
#include "lib/test/temp-dir.hpp"
#include "lib/stat/file.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 {
@ -62,7 +42,7 @@ namespace test{
/***************************************************************//**
/********************************************************************//**
* @test verify supplemental helper functions for file-handling support,
* provided to complement the C++ `<filesystem>` library.
* @see file.hpp
@ -82,38 +62,69 @@ namespace test{
simplifiedPermissionAccess()
{
TempDir temp;
SHOW_EXPR(temp)
SHOW_EXPR(fs::path{temp})
fs::path f = temp.makeFile("Lumiera.nix");
SHOW_EXPR(f);
SHOW_EXPR(fs::exists(f));
CHECK (fs::exists(f));
CHECK (f.filename() == "Lumiera.nix");
CHECK (f.parent_path() == temp);
// enforce specific permissions...
fs::permissions(f, fs::perms::owner_read | fs::perms::group_all | fs::perms::others_exec);
CHECK ( fs::has_perm(f, fs::perms::owner_read));
CHECK (not fs::has_perm(f, fs::perms::owner_write));
CHECK (not fs::has_perm(f, fs::perms::owner_exec));
CHECK (not fs::has_perm(f, fs::perms::owner_all));
CHECK ( fs::has_perm(f, fs::perms::group_read));
CHECK ( fs::has_perm(f, fs::perms::group_write));
CHECK ( fs::has_perm(f, fs::perms::group_exec));
CHECK ( fs::has_perm(f, fs::perms::group_all));
CHECK (not fs::has_perm(f, fs::perms::others_read));
CHECK (not fs::has_perm(f, fs::perms::others_write));
CHECK ( fs::has_perm(f, fs::perms::others_exec));
CHECK (not fs::has_perm(f, fs::perms::others_all));
CHECK (not fs::has_perm(f, fs::perms::all));
CHECK ( fs::can_read(f));
CHECK (not fs::can_write(f));
CHECK (not fs::can_exec(f));
// and indeed: we can not write
std::ofstream out{f};
SHOW_EXPR(fs::exists(f));
SHOW_EXPR(fs::status(f).permissions())
SHOW_EXPR(fs::has_perm(temp, fs::perms::owner_read));
SHOW_EXPR(fs::has_perm(temp, fs::perms::owner_write));
SHOW_EXPR(fs::has_perm(temp, fs::perms::owner_exec));
SHOW_EXPR(fs::has_perm(temp, fs::perms::owner_all));
SHOW_EXPR(fs::has_perm(temp, fs::perms::group_read));
SHOW_EXPR(fs::has_perm(temp, fs::perms::group_write));
SHOW_EXPR(fs::has_perm(temp, fs::perms::group_exec));
SHOW_EXPR(fs::has_perm(temp, fs::perms::group_all));
SHOW_EXPR(fs::has_perm(temp, fs::perms::others_read));
SHOW_EXPR(fs::has_perm(temp, fs::perms::others_write));
SHOW_EXPR(fs::has_perm(temp, fs::perms::others_exec));
SHOW_EXPR(fs::has_perm(temp, fs::perms::others_all));
SHOW_EXPR(fs::has_perm(temp, fs::perms::all));
SHOW_EXPR(fs::can_read(temp));
SHOW_EXPR(fs::can_write(temp));
SHOW_EXPR(fs::can_exec(temp));
out << "outch";
out.close();
CHECK (not out.good());
CHECK (0 == fs::file_size(f));
}
/** @test prints "sizeof()" including some type name. */
/** @test verify _consolidated path_
* - retrieves and expands the POSIX home directory
* - transforms into canonical, absolute path
*/
void
homedirectoryExpansion ()
{
fs::path sweetHome{"~"};
CHECK ("~" == sweetHome.generic_string());
CHECK (not sweetHome.empty());
CHECK (not sweetHome.has_parent_path());
CHECK (not sweetHome.is_absolute());
sweetHome = fs::consolidated (sweetHome);
CHECK (not util::startsWith (sweetHome.generic_string(), "~"));
CHECK ( util::startsWith (sweetHome.generic_string(), "/"));
CHECK (not sweetHome.empty());
CHECK ( sweetHome.has_parent_path());
CHECK ( sweetHome.is_absolute());
CHECK (fs::is_directory(sweetHome));
fs::path itFollows = fs::consolidated ("~/it/follows");
CHECK (util::startsWith (itFollows.generic_string(), "/"));
CHECK (util::endsWith (itFollows.generic_string(), "follows"));
CHECK (itFollows.filename() == "follows");
CHECK (itFollows.is_absolute());
CHECK (fs::relative (itFollows, sweetHome) == "it/follows");
}
};

View file

@ -57236,7 +57236,7 @@
</node>
</node>
</node>
<node COLOR="#338800" CREATED="1710170202711" ID="ID_828243870" MODIFIED="1710294040890" TEXT="TempDir : ein tempor&#xe4;res Arbeitsverzeichnis f&#xfc;r Tests">
<node COLOR="#338800" CREATED="1710170202711" FOLDED="true" 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;"/>
@ -57261,9 +57261,7 @@
<icon BUILTIN="messagebox_warning"/>
<node CREATED="1710262728032" ID="ID_1183825932" LINK="https://en.cppreference.com/w/cpp/filesystem/create_directory" MODIFIED="1710262791786">
<richcontent TYPE="NODE"><html>
<head>
</head>
<head/>
<body>
<p>
Verstehe die Doku so: wenn directory existiert &#10233; <b>kein</b>&#160;Fehler
@ -57275,9 +57273,7 @@
<node CREATED="1710262820691" ID="ID_715729723" MODIFIED="1710262853435" TEXT="dieses erzeugt Fehler EEXIST"/>
<node CREATED="1710262854071" ID="ID_1948805301" MODIFIED="1710262901444" TEXT="wobei aber dieser Fall die sonstige Invariante bricht">
<richcontent TYPE="NOTE"><html>
<head>
</head>
<head/>
<body>
if -1 is returned, no directory shall be created.
</body>
@ -57321,9 +57317,7 @@
<node CREATED="1710269523471" ID="ID_970699149" MODIFIED="1710269534326" TEXT="man k&#xf6;nnte POSIX getpid() verwenden..."/>
<node CREATED="1710269534846" ID="ID_705812768" MODIFIED="1710269721624" TEXT="wozu....? welches Risiko?">
<richcontent TYPE="NOTE"><html>
<head>
</head>
<head/>
<body>
<p>
ja man k&#246;nnte.... aber welches Risiko soll damit adressiert werden (da es ja nun doch zus&#228;tzliche Kosten in der Form von Nonportabilit&#228;t beinhaltet)? Immerhin ist die ganze Sache mit den Zufallszahlen sowiso mehr oder weniger professionelle Paranoia, denn 2^64 ~ 10^19 ist schon eine Menge
@ -57339,9 +57333,7 @@
<icon BUILTIN="idea"/>
<node CREATED="1710269741378" ID="ID_1044233602" LINK="#ID_1334461494" MODIFIED="1710269822240">
<richcontent TYPE="NODE"><html>
<head>
</head>
<head/>
<body>
<p>
die gestern implementierte Zufalls-Sequenz <font face="Monospaced" color="#2216ba">entropyGen.u64()</font>
@ -57356,9 +57348,7 @@
</node>
<node COLOR="#338800" CREATED="1710269892325" ID="ID_226025075" MODIFIED="1710269926842">
<richcontent TYPE="NODE"><html>
<head>
</head>
<head/>
<body>
<p>
R&#252;ckgabewert von create_directory
@ -57388,12 +57378,12 @@
<icon BUILTIN="button_ok"/>
</node>
</node>
<node CREATED="1710269985121" ID="ID_1475343702" MODIFIED="1710269987814" TEXT="Test">
<node COLOR="#435e98" CREATED="1710269985121" ID="ID_1475343702" MODIFIED="1710297951960" TEXT="Test">
<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"/>
<node COLOR="#338800" CREATED="1710270007654" ID="ID_1049869933" MODIFIED="1710297951154" TEXT="Abdeckung einiger Filesystem-Zusatzfunktionen">
<icon BUILTIN="button_ok"/>
</node>
</node>
</node>
@ -112073,14 +112063,12 @@ Date:&#160;&#160;&#160;Thu Apr 20 18:53:17 2023 +0200<br/>
<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 COLOR="#435e98" CREATED="1710170516550" FOLDED="true" ID="ID_1675475629" MODIFIED="1710298091028" TEXT="simpleUsage">
<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 COLOR="#338800" CREATED="1710170579669" ID="ID_16645191" MODIFIED="1710293984720" TEXT="verify_Lifecycle">
<icon BUILTIN="button_ok"/>
<node COLOR="#435e98" CREATED="1710170579669" FOLDED="true" ID="ID_16645191" MODIFIED="1710298091028" TEXT="verify_Lifecycle">
<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"/>