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 // uintmax_t remove_all(const path& p);
15 // uintmax_t remove_all(const path& p, error_code& ec) noexcept;
17 #include <experimental/filesystem>
19 #include "test_macros.h"
20 #include "rapid-cxx-test.hpp"
21 #include "filesystem_test_helper.hpp"
23 using namespace std::experimental::filesystem;
24 namespace fs = std::experimental::filesystem;
26 TEST_SUITE(filesystem_remove_all_test_suite)
28 TEST_CASE(test_signatures)
30 const path p; ((void)p);
31 std::error_code ec; ((void)ec);
32 ASSERT_SAME_TYPE(decltype(fs::remove_all(p)), std::uintmax_t);
33 ASSERT_SAME_TYPE(decltype(fs::remove_all(p, ec)), std::uintmax_t);
35 ASSERT_NOT_NOEXCEPT(fs::remove_all(p));
36 ASSERT_NOEXCEPT(fs::remove_all(p, ec));
39 TEST_CASE(test_error_reporting)
41 auto checkThrow = [](path const& f, const std::error_code& ec)
43 #ifndef TEST_HAS_NO_EXCEPTIONS
47 } catch (filesystem_error const& err) {
48 return err.path1() == f
53 ((void)f); ((void)ec);
58 const path non_empty_dir = env.create_dir("dir");
59 env.create_file(non_empty_dir / "file1", 42);
60 const path bad_perms_dir = env.create_dir("bad_dir");
61 const path file_in_bad_dir = env.create_file(bad_perms_dir / "file", 42);
62 permissions(bad_perms_dir, perms::none);
63 const path bad_perms_file = env.create_file("file2", 42);
64 permissions(bad_perms_file, perms::none);
66 const path testCases[] = {
67 env.make_env_path("dne"),
70 const auto BadRet = static_cast<std::uintmax_t>(-1);
71 for (auto& p : testCases) {
73 TEST_CHECK(fs::remove_all(p, ec) == BadRet);
75 TEST_CHECK(checkThrow(p, ec));
79 TEST_CASE(basic_remove_all_test)
82 const path dne = env.make_env_path("dne");
83 const path link = env.create_symlink(dne, "link");
84 const path nested_link = env.make_env_path("nested_link");
85 create_symlink(link, nested_link);
86 const path testCases[] = {
87 env.create_file("file", 42),
88 env.create_dir("empty_dir"),
92 for (auto& p : testCases) {
93 std::error_code ec = std::make_error_code(std::errc::address_in_use);
94 TEST_CHECK(remove(p, ec));
96 TEST_CHECK(!exists(symlink_status(p)));
100 TEST_CASE(symlink_to_dir)
103 const path dir = env.create_dir("dir");
104 const path file = env.create_file(dir / "file", 42);
105 const path link = env.create_symlink(dir, "sym");
108 std::error_code ec = std::make_error_code(std::errc::address_in_use);
109 TEST_CHECK(remove_all(link, ec) == 1);
111 TEST_CHECK(!exists(symlink_status(link)));
112 TEST_CHECK(exists(dir));
113 TEST_CHECK(exists(file));
118 TEST_CASE(nested_dir)
121 const path dir = env.create_dir("dir");
122 const path dir1 = env.create_dir(dir / "dir1");
123 const path out_of_dir_file = env.create_file("file1", 42);
124 const path all_files[] = {
126 env.create_file(dir / "file1", 42),
127 env.create_symlink(out_of_dir_file, dir / "sym1"),
128 env.create_file(dir1 / "file2", 42),
129 env.create_symlink(dir, dir1 / "sym2")
131 const std::size_t expected_count = sizeof(all_files) / sizeof(all_files[0]);
133 std::error_code ec = std::make_error_code(std::errc::address_in_use);
134 TEST_CHECK(remove_all(dir, ec) == expected_count);
136 for (auto const& p : all_files) {
137 TEST_CHECK(!exists(symlink_status(p)));
139 TEST_CHECK(exists(out_of_dir_file));