Lumiera  0.pre.03
»edit your freedom«
file-support-test.cpp
Go to the documentation of this file.
1 /*
2  FileSupport(Test) - verify additional filesystem helpers
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 #include "lib/stat/file.hpp"
23 
24 #include <fstream>
25 
26 
27 namespace lib {
28 namespace stat{
29 namespace test{
30 
31  using lib::test::TempDir;
32 
33 
34 
35 
36  /********************************************************************/
42  class FileSupport_test : public Test
43  {
44  void
45  run (Arg)
46  {
47  simplifiedPermissionAccess();
49  }
50 
51 
52  void
53  simplifiedPermissionAccess()
54  {
55  TempDir temp;
56  fs::path f = temp.makeFile("Lumiera.nix");
57  CHECK (fs::exists(f));
58  CHECK (f.filename() == "Lumiera.nix");
59  CHECK (f.parent_path() == temp);
60 
61  // enforce specific permissions...
62  fs::permissions(f, fs::perms::owner_read | fs::perms::group_all | fs::perms::others_exec);
63 
64  CHECK ( fs::has_perm(f, fs::perms::owner_read));
65  CHECK (not fs::has_perm(f, fs::perms::owner_write));
66  CHECK (not fs::has_perm(f, fs::perms::owner_exec));
67  CHECK (not fs::has_perm(f, fs::perms::owner_all));
68  CHECK ( fs::has_perm(f, fs::perms::group_read));
69  CHECK ( fs::has_perm(f, fs::perms::group_write));
70  CHECK ( fs::has_perm(f, fs::perms::group_exec));
71  CHECK ( fs::has_perm(f, fs::perms::group_all));
72  CHECK (not fs::has_perm(f, fs::perms::others_read));
73  CHECK (not fs::has_perm(f, fs::perms::others_write));
74  CHECK ( fs::has_perm(f, fs::perms::others_exec));
75  CHECK (not fs::has_perm(f, fs::perms::others_all));
76  CHECK (not fs::has_perm(f, fs::perms::all));
77  CHECK ( fs::can_read(f));
78  CHECK (not fs::can_write(f));
79  CHECK (not fs::can_exec(f));
80 
81  // and indeed: we can not write
82  std::ofstream out{f};
83  out << "outch";
84  out.close();
85  CHECK (not out.good());
86  CHECK (0 == fs::file_size(f));
87  }
88 
89 
90 
95  void
97  {
98  fs::path sweetHome{"~"};
99  CHECK ("~" == sweetHome.generic_string());
100  CHECK (not sweetHome.empty());
101  CHECK (not sweetHome.has_parent_path());
102  CHECK (not sweetHome.is_absolute());
103 
104  sweetHome = fs::consolidated (sweetHome);
105  CHECK (not util::startsWith (sweetHome.generic_string(), "~"));
106  CHECK ( util::startsWith (sweetHome.generic_string(), "/"));
107  CHECK (not sweetHome.empty());
108  CHECK ( sweetHome.has_parent_path());
109  CHECK ( sweetHome.is_absolute());
110  CHECK (fs::is_directory(sweetHome));
111 
112  fs::path itFollows = fs::consolidated ("~/it/follows");
113  CHECK (util::startsWith (itFollows.generic_string(), "/"));
114  CHECK (util::endsWith (itFollows.generic_string(), "follows"));
115  CHECK (itFollows.filename() == "follows");
116  CHECK (itFollows.is_absolute());
117 
118  CHECK (fs::relative (itFollows, sweetHome) == "it/follows");
119  }
120  };
121 
122  LAUNCHER (FileSupport_test, "unit common");
123 
124 
125 }}} // namespace lib::stat::test
126 
Definition: run.hpp:40
Includes the C++ Filesystem library and provides some convenience helpers.
Manage a temporary directory for storage, with automated clean-up.
Implementation namespace for support and library code.
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