Lumiera  0.pre.03
»edit your freedom«
temp-dir-test.cpp
Go to the documentation of this file.
1 /*
2  TempDir(Test) - verify automated temporary working directory
3 
4  Copyright (C)
5  2024, Hermann Vosseler <Ichthyostega@web.de>
6 
7   **Lumiera** is free software; you can redistribute it and/or modify it
8   under the terms of the GNU General Public License as published by the
9   Free Software Foundation; either version 2 of the License, or (at your
10   option) any later version. See the file COPYING for further details.
11 
12 * *****************************************************************/
13 
19 #include "lib/test/run.hpp"
20 #include "lib/test/test-helper.hpp"
21 #include "lib/test/temp-dir.hpp"
22 
23 #include <fstream>
24 
25 
26 namespace lib {
27 namespace test{
28 namespace test{
29 
30 
31  /***************************************************************/
37  class TempDir_test : public Test
38  {
39  void
40  run (Arg)
41  {
42  simpleUsage();
44  }
45 
46 
47  void
48  simpleUsage()
49  {
50  TempDir temp;
51  auto ff = temp.makeFile();
52  CHECK (fs::exists (ff));
53  CHECK (fs::is_empty (ff));
54 
55  std::ofstream out{ff, std::ios_base::out};
56  auto scree = randStr(55);
57  out << scree << std::endl;
58  out.close();
59 
60  CHECK (fs::is_regular_file (ff));
61  CHECK (not fs::is_empty (ff));
62 
63  std::ifstream in{ff};
64  string readBack;
65  in >> readBack;
66  CHECK (readBack == scree);
67  }
68 
69 
70 
72  void
74  {
75  fs::path d1;
76  fs::path d2;
77  {
78  TempDir tt;
79  d1 = tt;
80  tt.makeFile("huibuh");
81  tt.makeFile("huibuh");
82  tt.makeFile("huibuh");
83  std::ofstream boo{d1 / "huibuh"};
84  boo << "boo";
85  fs::create_directories(d1 / "bug/bear");
86  fs::rename (d1 / "huibuh", d1 / "bug/bear/fray");
87 
88  auto scare = [&]{
89  TempDir tt;
90  d2 = tt;
91  tt.makeFile("Mooo");
92  CHECK (fs::exists(d2 / "Mooo"));
93  CHECK (not fs::is_empty(d2));
94  fs::create_directory(d2 / "Mooo"); // Booom!
95  };
96  CHECK (d2.empty());
97  CHECK (not d1.empty());
98 
99  VERIFY_FAIL ("File exists", scare() );
100  // nested context was cleaned-up after exception
101  CHECK (not fs::exists(d2));
102  CHECK ( fs::exists(d1));
103  CHECK (not d2.empty());
104  CHECK (d1 != d2);
105 
106  boo << "moo";
107  boo.close();
108  CHECK (6 == fs::file_size(d1 / "bug/bear/fray"));
109  // so bottom line: can do filesystem stuff for real...
110  }
111  // All traces are gone...
112  CHECK (not fs::exists(d1));
113  CHECK (not fs::exists(d2));
114  }
115  };
116 
117  LAUNCHER (TempDir_test, "unit common");
118 
119 
120 }}} // namespace lib::test::test
Definition: run.hpp:40
Manage a temporary directory for storage, with automated clean-up.
Implementation namespace for support and library code.
string randStr(size_t len)
create garbage string of given length
Definition: test-helper.cpp:61
Simplistic test class runner.
A collection of frequently used helper functions to support unit testing.
A RAII style temporary directory.
Definition: temp-dir.hpp:54
#define VERIFY_FAIL(FAILURE_MSG, ERRONEOUS_STATEMENT)
Macro to verify that a statement indeed raises a std::exception, which additionally contains some FAI...