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
57 const path non_empty_dir = env.create_dir("dir");
58 env.create_file(non_empty_dir / "file1", 42);
59 const path bad_perms_dir = env.create_dir("bad_dir");
60 const path file_in_bad_dir = env.create_file(bad_perms_dir / "file", 42);
61 permissions(bad_perms_dir, perms::none);
62 const path bad_perms_file = env.create_file("file2", 42);
63 permissions(bad_perms_file, perms::none);
65 const path testCases[] = {
66 env.make_env_path("dne"),
69 const auto BadRet = static_cast<std::uintmax_t>(-1);
70 for (auto& p : testCases) {
72 TEST_CHECK(fs::remove_all(p, ec) == BadRet);
74 TEST_CHECK(checkThrow(p, ec));
78 TEST_CASE(basic_remove_all_test)
81 const path dne = env.make_env_path("dne");
82 const path link = env.create_symlink(dne, "link");
83 const path nested_link = env.make_env_path("nested_link");
84 create_symlink(link, nested_link);
85 const path testCases[] = {
86 env.create_file("file", 42),
87 env.create_dir("empty_dir"),
91 for (auto& p : testCases) {
92 std::error_code ec = std::make_error_code(std::errc::address_in_use);
93 TEST_CHECK(remove(p, ec));
95 TEST_CHECK(!exists(symlink_status(p)));
99 TEST_CASE(symlink_to_dir)
102 const path dir = env.create_dir("dir");
103 const path file = env.create_file(dir / "file", 42);
104 const path link = env.create_symlink(dir, "sym");
107 std::error_code ec = std::make_error_code(std::errc::address_in_use);
108 TEST_CHECK(remove_all(link, ec) == 1);
110 TEST_CHECK(!exists(symlink_status(link)));
111 TEST_CHECK(exists(dir));
112 TEST_CHECK(exists(file));
117 TEST_CASE(nested_dir)
120 const path dir = env.create_dir("dir");
121 const path dir1 = env.create_dir(dir / "dir1");
122 const path out_of_dir_file = env.create_file("file1", 42);
123 const path all_files[] = {
125 env.create_file(dir / "file1", 42),
126 env.create_symlink(out_of_dir_file, dir / "sym1"),
127 env.create_file(dir1 / "file2", 42),
128 env.create_symlink(dir, dir1 / "sym2")
130 const std::size_t expected_count = sizeof(all_files) / sizeof(all_files[0]);
132 std::error_code ec = std::make_error_code(std::errc::address_in_use);
133 TEST_CHECK(remove_all(dir, ec) == expected_count);
135 for (auto const& p : all_files) {
136 TEST_CHECK(!exists(symlink_status(p)));
138 TEST_CHECK(exists(out_of_dir_file));