lumiera_documentation/tests/lib/test/temp-dir-test.cpp
Ichthyostega 3caa5cfb03 Tests: reorganise test folders
This resolves a long-standing issue:
we could not use proper include paths for test-helper code that was defined alongside the tests.

Since some test-helper code must now be relocated into the »Vault«-Layer,
I took the opportunity to finally resolve this annoyance. The solution is two-fold
 * introduce a dedicated test-support library
 * remove the quirks that were necessary to support some plain-C tests
Together, this allows to raise the include-directory by one level,
and then to adjust all folder names so that they match the namespace
2026-03-07 00:56:19 +01:00

119 lines
3.3 KiB
C++
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
TempDir(Test) - verify automated temporary working directory
Copyright (C)
2024, Hermann Vosseler <Ichthyostega@web.de>
  **Lumiera** is free software; you can redistribute it and/or modify it
  under the terms of the GNU General Public License as published by the
  Free Software Foundation; either version 2 of the License, or (at your
  option) any later version. See the file COPYING for further details.
* *****************************************************************/
/** @file temp-dir-test.cpp
** unit test \ref TempDir_test
*/
#include "test/run.hpp"
#include "test/temp-dir.hpp"
#include "test/test-helper.hpp"
#include <fstream>
namespace test{
namespace test{
/***************************************************************//**
* @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
*/
class TempDir_test : public Test
{
void
run (Arg)
{
simpleUsage();
verify_Lifecycle();
}
void
simpleUsage()
{
TempDir temp;
auto ff = temp.makeFile();
CHECK (fs::exists (ff));
CHECK (fs::is_empty (ff));
std::ofstream out{ff, std::ios_base::out};
auto scree = randStr(55);
out << scree << std::endl;
out.close();
CHECK (fs::is_regular_file (ff));
CHECK (not fs::is_empty (ff));
std::ifstream in{ff};
string readBack;
in >> readBack;
CHECK (readBack == scree);
}
/** @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));
}
};
LAUNCHER (TempDir_test, "unit common");
}} // namespace test::test