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) Lumiera.org
5  2024, Hermann Vosseler <Ichthyostega@web.de>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21 * *****************************************************/
22 
28 #include "lib/test/run.hpp"
29 #include "lib/test/test-helper.hpp"
30 #include "lib/test/temp-dir.hpp"
31 
32 #include <fstream>
33 
34 
35 namespace lib {
36 namespace test{
37 namespace test{
38 
39 
40  /***************************************************************/
46  class TempDir_test : public Test
47  {
48  void
49  run (Arg)
50  {
51  simpleUsage();
53  }
54 
55 
56  void
57  simpleUsage()
58  {
59  TempDir temp;
60  auto ff = temp.makeFile();
61  CHECK (fs::exists (ff));
62  CHECK (fs::is_empty (ff));
63 
64  std::ofstream out{ff, std::ios_base::out};
65  auto scree = randStr(55);
66  out << scree << std::endl;
67  out.close();
68 
69  CHECK (fs::is_regular_file (ff));
70  CHECK (not fs::is_empty (ff));
71 
72  std::ifstream in{ff};
73  string readBack;
74  in >> readBack;
75  CHECK (readBack == scree);
76  }
77 
78 
79 
81  void
83  {
84  fs::path d1;
85  fs::path d2;
86  {
87  TempDir tt;
88  d1 = tt;
89  tt.makeFile("huibuh");
90  tt.makeFile("huibuh");
91  tt.makeFile("huibuh");
92  std::ofstream boo{d1 / "huibuh"};
93  boo << "boo";
94  fs::create_directories(d1 / "bug/bear");
95  fs::rename (d1 / "huibuh", d1 / "bug/bear/fray");
96 
97  auto scare = [&]{
98  TempDir tt;
99  d2 = tt;
100  tt.makeFile("Mooo");
101  CHECK (fs::exists(d2 / "Mooo"));
102  CHECK (not fs::is_empty(d2));
103  fs::create_directory(d2 / "Mooo"); // Booom!
104  };
105  CHECK (d2.empty());
106  CHECK (not d1.empty());
107 
108  VERIFY_FAIL ("File exists", scare() );
109  // nested context was cleaned-up after exception
110  CHECK (not fs::exists(d2));
111  CHECK ( fs::exists(d1));
112  CHECK (not d2.empty());
113  CHECK (d1 != d2);
114 
115  boo << "moo";
116  boo.close();
117  CHECK (6 == fs::file_size(d1 / "bug/bear/fray"));
118  // so bottom line: can do filesystem stuff for real...
119  }
120  // All traces are gone...
121  CHECK (not fs::exists(d1));
122  CHECK (not fs::exists(d2));
123  }
124  };
125 
126  LAUNCHER (TempDir_test, "unit common");
127 
128 
129 }}} // namespace lib::test::test
Definition: run.hpp:49
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:69
Simple test class runner.
A collection of frequently used helper functions to support unit testing.
A RAII style temporary directory.
Definition: temp-dir.hpp:63
#define VERIFY_FAIL(FAILURE_MSG, ERRONEOUS_STATEMENT)
Macro to verify that a statement indeed raises a std::exception, which additionally contains some FAI...