Lumiera 0.pre.04
»edit your freedom«
Loading...
Searching...
No Matches
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"
21#include "lib/test/temp-dir.hpp"
22#include "lib/file.hpp"
23
24#include <fstream>
25
26
27namespace lib {
28namespace stat{
29namespace test{
30
32
33
34
35
36 /********************************************************************/
42 class FileSupport_test : public Test
43 {
44 void
50
51
52 void
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
A RAII style temporary directory.
Definition temp-dir.hpp:56
fs::path makeFile(string name="")
Definition temp-dir.hpp:77
Includes the C++ Filesystem library and provides some convenience helpers.
Implementation namespace for support and library code.
bool can_exec(fs::path const &p)
Definition file.hpp:93
bool can_write(fs::path const &p)
Definition file.hpp:87
bool has_perm(fs::path const &p, fs::perms permissionMask)
check if the denoted path p has at least the given permissions
Definition file.hpp:74
fs::path consolidated(fs::path rawPath)
resolves symlinks, ~ (Unix home dir) and relative specs
Definition file.hpp:61
bool can_read(fs::path const &p)
check if the owner has read permissions on the denoted file or directory
Definition file.hpp:81
Test runner and basic definitions for tests.
bool startsWith(string const &str, string const &prefix)
check if string starts with a given prefix
Definition util.hpp:185
bool endsWith(string const &str, string const &suffix)
check if string ends with the given suffix
Definition util.hpp:198
Simplistic test class runner.
#define LAUNCHER(_TEST_CLASS_, _GROUPS_)
Definition run.hpp:116
Manage a temporary directory for storage, with automated clean-up.
A collection of frequently used helper functions to support unit testing.