1 //===----------------------------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // UNSUPPORTED: c++98, c++03
12 // <experimental/filesystem>
14 // class directory_entry
16 // file_status status() const;
17 // file_status status(error_code const&) const noexcept;
19 #include "filesystem_include.hpp"
20 #include <type_traits>
23 #include "filesystem_test_helper.hpp"
24 #include "rapid-cxx-test.hpp"
26 TEST_SUITE(directory_entry_obs_testsuite)
30 directory_entry p("dne");
33 TEST_CASE(signatures) {
35 const directory_entry e = {};
37 #define TEST_FUNC(name) \
38 static_assert(std::is_same<decltype(e.name()), bool>::value, \
39 "wrong return type"); \
40 static_assert(noexcept(e.name()) == false, "should not be noexcept"); \
41 static_assert(std::is_same<decltype(e.name(ec)), bool>::value, \
42 "wrong return type"); \
43 static_assert(noexcept(e.name(ec)) == true, "should be noexcept")
46 TEST_FUNC(is_block_file);
47 TEST_FUNC(is_character_file);
48 TEST_FUNC(is_directory);
51 TEST_FUNC(is_regular_file);
53 TEST_FUNC(is_symlink);
58 TEST_CASE(test_without_ec) {
60 using fs::directory_entry;
61 using fs::file_status;
65 path f = env.create_file("foo", 42);
66 path d = env.create_dir("dir");
67 path fifo = env.create_fifo("fifo");
68 path hl = env.create_hardlink("foo", "hl");
69 for (auto p : {hl, f, d, fifo}) {
71 file_status st = status(p);
72 file_status sym_st = symlink_status(p);
74 TEST_REQUIRE(e.exists());
75 TEST_REQUIRE(!exists(p));
76 TEST_CHECK(e.exists() == exists(st));
77 TEST_CHECK(e.is_block_file() == is_block_file(st));
78 TEST_CHECK(e.is_character_file() == is_character_file(st));
79 TEST_CHECK(e.is_directory() == is_directory(st));
80 TEST_CHECK(e.is_fifo() == is_fifo(st));
81 TEST_CHECK(e.is_other() == is_other(st));
82 TEST_CHECK(e.is_regular_file() == is_regular_file(st));
83 TEST_CHECK(e.is_socket() == is_socket(st));
84 TEST_CHECK(e.is_symlink() == is_symlink(sym_st));
88 TEST_CASE(test_with_ec) {
90 using fs::directory_entry;
91 using fs::file_status;
95 path f = env.create_file("foo", 42);
96 path d = env.create_dir("dir");
97 path fifo = env.create_fifo("fifo");
98 path hl = env.create_hardlink("foo", "hl");
99 for (auto p : {hl, f, d, fifo}) {
100 directory_entry e(p);
101 std::error_code status_ec = GetTestEC();
102 std::error_code sym_status_ec = GetTestEC(1);
103 file_status st = status(p, status_ec);
104 file_status sym_st = symlink_status(p, sym_status_ec);
106 std::error_code ec = GetTestEC(2);
107 auto CheckEC = [&](std::error_code const& other_ec) {
108 bool res = ec == other_ec;
113 TEST_REQUIRE(e.exists(ec));
114 TEST_CHECK(CheckEC(status_ec));
115 TEST_REQUIRE(!exists(p));
117 TEST_CHECK(e.exists(ec) == exists(st));
118 TEST_CHECK(CheckEC(status_ec));
120 TEST_CHECK(e.is_block_file(ec) == is_block_file(st));
121 TEST_CHECK(CheckEC(status_ec));
123 TEST_CHECK(e.is_character_file(ec) == is_character_file(st));
124 TEST_CHECK(CheckEC(status_ec));
126 TEST_CHECK(e.is_directory(ec) == is_directory(st));
127 TEST_CHECK(CheckEC(status_ec));
129 TEST_CHECK(e.is_fifo(ec) == is_fifo(st));
130 TEST_CHECK(CheckEC(status_ec));
132 TEST_CHECK(e.is_other(ec) == is_other(st));
133 TEST_CHECK(CheckEC(status_ec));
135 TEST_CHECK(e.is_regular_file(ec) == is_regular_file(st));
136 TEST_CHECK(CheckEC(status_ec));
138 TEST_CHECK(e.is_socket(ec) == is_socket(st));
139 TEST_CHECK(CheckEC(status_ec));
141 TEST_CHECK(e.is_symlink(ec) == is_symlink(sym_st));
142 TEST_CHECK(CheckEC(sym_status_ec));
146 TEST_CASE(test_with_ec_dne) {
148 using fs::directory_entry;
149 using fs::file_status;
152 for (auto p : {StaticEnv::DNE, StaticEnv::BadSymlink}) {
154 directory_entry e(p);
155 std::error_code status_ec = GetTestEC();
156 std::error_code sym_status_ec = GetTestEC(1);
157 file_status st = status(p, status_ec);
158 file_status sym_st = symlink_status(p, sym_status_ec);
159 std::error_code ec = GetTestEC(2);
160 auto CheckEC = [&](std::error_code const& other_ec) {
161 bool res = ec == other_ec;
166 TEST_CHECK(e.exists(ec) == exists(st));
167 TEST_CHECK(CheckEC(status_ec));
169 TEST_CHECK(e.is_block_file(ec) == is_block_file(st));
170 TEST_CHECK(CheckEC(status_ec));
172 TEST_CHECK(e.is_character_file(ec) == is_character_file(st));
173 TEST_CHECK(CheckEC(status_ec));
175 TEST_CHECK(e.is_directory(ec) == is_directory(st));
176 TEST_CHECK(CheckEC(status_ec));
178 TEST_CHECK(e.is_fifo(ec) == is_fifo(st));
179 TEST_CHECK(CheckEC(status_ec));
181 TEST_CHECK(e.is_other(ec) == is_other(st));
182 TEST_CHECK(CheckEC(status_ec));
184 TEST_CHECK(e.is_regular_file(ec) == is_regular_file(st));
185 TEST_CHECK(CheckEC(status_ec));
187 TEST_CHECK(e.is_socket(ec) == is_socket(st));
188 TEST_CHECK(CheckEC(status_ec));
190 TEST_CHECK(e.is_symlink(ec) == is_symlink(sym_st));
191 TEST_CHECK(CheckEC(sym_status_ec));
195 TEST_CASE(test_with_ec_cannot_resolve) {
197 using fs::directory_entry;
198 using fs::file_status;
202 const path dir = env.create_dir("dir");
203 const path file = env.create_file("dir/file", 42);
204 const path file_out_of_dir = env.create_file("file2", 99);
205 const path sym = env.create_symlink("file2", "dir/sym");
207 perms old_perms = fs::status(dir).permissions();
209 for (auto p : {file, sym}) {
210 permissions(dir, old_perms);
211 directory_entry e(p);
213 permissions(dir, perms::none);
214 std::error_code dummy_ec;
216 TEST_REQUIRE(dummy_ec);
218 std::error_code status_ec = GetTestEC();
219 std::error_code sym_status_ec = GetTestEC(1);
220 file_status st = status(p, status_ec);
221 file_status sym_st = symlink_status(p, sym_status_ec);
222 std::error_code ec = GetTestEC(2);
223 auto CheckEC = [&](std::error_code const& other_ec) {
224 bool res = ec == other_ec;
229 TEST_CHECK(e.exists(ec) == exists(st));
230 TEST_CHECK(CheckEC(status_ec));
232 TEST_CHECK(e.is_block_file(ec) == is_block_file(st));
233 TEST_CHECK(CheckEC(status_ec));
235 TEST_CHECK(e.is_character_file(ec) == is_character_file(st));
236 TEST_CHECK(CheckEC(status_ec));
238 TEST_CHECK(e.is_directory(ec) == is_directory(st));
239 TEST_CHECK(CheckEC(status_ec));
241 TEST_CHECK(e.is_fifo(ec) == is_fifo(st));
242 TEST_CHECK(CheckEC(status_ec));
244 TEST_CHECK(e.is_other(ec) == is_other(st));
245 TEST_CHECK(CheckEC(status_ec));
247 TEST_CHECK(e.is_regular_file(ec) == is_regular_file(st));
248 TEST_CHECK(CheckEC(status_ec));
250 TEST_CHECK(e.is_socket(ec) == is_socket(st));
251 TEST_CHECK(CheckEC(status_ec));
253 TEST_CHECK(e.is_symlink(ec) == is_symlink(sym_st));
254 TEST_CHECK(CheckEC(sym_status_ec));