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_iterator
17 // explicit recursive_directory_iterator(const path& p);
18 // recursive_directory_iterator(const path& p, directory_options options);
19 // recursive_directory_iterator(const path& p, error_code& ec) noexcept;
20 // recursive_directory_iterator(const path& p, directory_options options, error_code& ec) noexcept;
23 #include <experimental/filesystem>
24 #include <type_traits>
28 #include "test_macros.h"
29 #include "rapid-cxx-test.hpp"
30 #include "filesystem_test_helper.hpp"
32 using namespace std::experimental::filesystem;
34 using RDI = recursive_directory_iterator;
36 TEST_SUITE(recursive_directory_iterator_constructor_tests)
38 TEST_CASE(test_constructor_signatures)
40 using D = recursive_directory_iterator;
42 // explicit directory_iterator(path const&);
43 static_assert(!std::is_convertible<path, D>::value, "");
44 static_assert(std::is_constructible<D, path>::value, "");
45 static_assert(!std::is_nothrow_constructible<D, path>::value, "");
47 // directory_iterator(path const&, error_code&) noexcept
48 static_assert(std::is_nothrow_constructible<D, path, std::error_code&>::value, "");
50 // directory_iterator(path const&, directory_options);
51 static_assert(std::is_constructible<D, path, directory_options>::value, "");
52 static_assert(!std::is_nothrow_constructible<D, path, directory_options>::value, "");
54 // directory_iterator(path const&, directory_options, error_code&) noexcept
55 static_assert(std::is_nothrow_constructible<D, path, directory_options, std::error_code&>::value, "");
58 TEST_CASE(test_construction_from_bad_path)
61 directory_options opts = directory_options::none;
64 const path testPaths[] = { StaticEnv::DNE, StaticEnv::BadSymlink };
65 for (path const& testPath : testPaths)
70 TEST_CHECK(it == endIt);
73 RDI it(testPath, opts, ec);
75 TEST_CHECK(it == endIt);
78 TEST_CHECK_THROW(filesystem_error, RDI(testPath));
79 TEST_CHECK_THROW(filesystem_error, RDI(testPath, opts));
84 TEST_CASE(access_denied_test_case)
86 using namespace std::experimental::filesystem;
88 path const testDir = env.make_env_path("dir1");
89 path const testFile = testDir / "testFile";
90 env.create_dir(testDir);
91 env.create_file(testFile, 42);
93 // Test that we can iterator over the directory before changing the perms
96 TEST_REQUIRE(it != RDI{});
99 // Change the permissions so we can no longer iterate
100 permissions(testDir, perms::none);
102 // Check that the construction fails when skip_permissions_denied is
108 TEST_CHECK(it == RDI{});
110 // Check that construction does not report an error when
111 // 'skip_permissions_denied' is given.
114 RDI it(testDir, directory_options::skip_permission_denied, ec);
116 TEST_CHECK(it == RDI{});
121 TEST_CASE(access_denied_to_file_test_case)
123 using namespace std::experimental::filesystem;
125 path const testFile = env.make_env_path("file1");
126 env.create_file(testFile, 42);
128 // Change the permissions so we can no longer iterate
129 permissions(testFile, perms::none);
131 // Check that the construction fails when skip_permissions_denied is
135 RDI it(testFile, ec);
137 TEST_CHECK(it == RDI{});
139 // Check that construction still fails when 'skip_permissions_denied' is given
140 // because we tried to open a file and not a directory.
143 RDI it(testFile, directory_options::skip_permission_denied, ec);
145 TEST_CHECK(it == RDI{});
149 TEST_CASE(test_open_on_empty_directory_equals_end)
152 const path testDir = env.make_env_path("dir1");
153 env.create_dir(testDir);
160 TEST_CHECK(it == endIt);
164 TEST_CHECK(it == endIt);
168 TEST_CASE(test_open_on_directory_succeeds)
170 const path testDir = StaticEnv::Dir;
171 std::set<path> dir_contents(std::begin(StaticEnv::DirIterationList),
172 std::end( StaticEnv::DirIterationList));
179 TEST_CHECK(it != endIt);
180 TEST_CHECK(dir_contents.count(*it));
184 TEST_CHECK(it != endIt);
185 TEST_CHECK(dir_contents.count(*it));
189 TEST_CASE(test_open_on_file_fails)
191 const path testFile = StaticEnv::File;
195 RDI it(testFile, ec);
197 TEST_CHECK(it == endIt);
200 TEST_CHECK_THROW(filesystem_error, RDI(testFile));
204 TEST_CASE(test_options_post_conditions)
206 const path goodDir = StaticEnv::Dir;
207 const path badDir = StaticEnv::DNE;
212 RDI it1(goodDir, ec);
214 TEST_CHECK(it1.options() == directory_options::none);
218 TEST_REQUIRE(it2 == RDI{});
222 const directory_options opts = directory_options::skip_permission_denied;
224 RDI it1(goodDir, opts, ec);
226 TEST_CHECK(it1.options() == opts);
228 RDI it2(badDir, opts, ec);
230 TEST_REQUIRE(it2 == RDI{});
234 TEST_CHECK(it.options() == directory_options::none);
237 const directory_options opts = directory_options::follow_directory_symlink;
238 RDI it(goodDir, opts);
239 TEST_CHECK(it.options() == opts);