1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 // UNSUPPORTED: c++03
10
11 // <filesystem>
12
13 // uintmax_t remove_all(const path& p);
14 // uintmax_t remove_all(const path& p, error_code& ec) noexcept;
15
16 #include "filesystem_include.h"
17
18 #include "test_macros.h"
19 #include "rapid-cxx-test.h"
20 #include "filesystem_test_helper.h"
21
22 using namespace fs;
23
24 TEST_SUITE(filesystem_remove_all_test_suite)
25
TEST_CASE(test_signatures)26 TEST_CASE(test_signatures)
27 {
28 const path p; ((void)p);
29 std::error_code ec; ((void)ec);
30 ASSERT_SAME_TYPE(decltype(fs::remove_all(p)), std::uintmax_t);
31 ASSERT_SAME_TYPE(decltype(fs::remove_all(p, ec)), std::uintmax_t);
32
33 ASSERT_NOT_NOEXCEPT(fs::remove_all(p));
34 ASSERT_NOT_NOEXCEPT(fs::remove_all(p, ec));
35 }
36
TEST_CASE(test_error_reporting)37 TEST_CASE(test_error_reporting)
38 {
39 auto checkThrow = [](path const& f, const std::error_code& ec)
40 {
41 #ifndef TEST_HAS_NO_EXCEPTIONS
42 try {
43 fs::remove_all(f);
44 return false;
45 } catch (filesystem_error const& err) {
46 return err.path1() == f
47 && err.path2() == ""
48 && err.code() == ec;
49 }
50 #else
51 ((void)f); ((void)ec);
52 return true;
53 #endif
54 };
55 scoped_test_env env;
56 const path non_empty_dir = env.create_dir("dir");
57 env.create_file(non_empty_dir / "file1", 42);
58 const path bad_perms_dir = env.create_dir("bad_dir");
59 const path file_in_bad_dir = env.create_file(bad_perms_dir / "file", 42);
60 permissions(bad_perms_dir, perms::none);
61 const path bad_perms_file = env.create_file("file2", 42);
62 permissions(bad_perms_file, perms::none);
63
64 const path testCases[] = {
65 file_in_bad_dir
66 };
67 const auto BadRet = static_cast<std::uintmax_t>(-1);
68 for (auto& p : testCases) {
69 std::error_code ec;
70
71 TEST_CHECK(fs::remove_all(p, ec) == BadRet);
72 TEST_CHECK(ec);
73 TEST_CHECK(checkThrow(p, ec));
74 }
75
76 // PR#35780
77 const path testCasesNonexistant[] = {
78 "",
79 env.make_env_path("dne")
80 };
81 for (auto &p : testCasesNonexistant) {
82 std::error_code ec;
83
84 TEST_CHECK(fs::remove_all(p, ec) == 0);
85 TEST_CHECK(!ec);
86 }
87 }
88
TEST_CASE(basic_remove_all_test)89 TEST_CASE(basic_remove_all_test)
90 {
91 scoped_test_env env;
92 const path dne = env.make_env_path("dne");
93 const path link = env.create_symlink(dne, "link");
94 const path nested_link = env.make_env_path("nested_link");
95 create_symlink(link, nested_link);
96 const path testCases[] = {
97 env.create_file("file", 42),
98 env.create_dir("empty_dir"),
99 nested_link,
100 link
101 };
102 for (auto& p : testCases) {
103 std::error_code ec = std::make_error_code(std::errc::address_in_use);
104 TEST_CHECK(remove(p, ec));
105 TEST_CHECK(!ec);
106 TEST_CHECK(!exists(symlink_status(p)));
107 }
108 }
109
TEST_CASE(symlink_to_dir)110 TEST_CASE(symlink_to_dir)
111 {
112 scoped_test_env env;
113 const path dir = env.create_dir("dir");
114 const path file = env.create_file(dir / "file", 42);
115 const path link = env.create_directory_symlink(dir, "sym");
116
117 {
118 std::error_code ec = std::make_error_code(std::errc::address_in_use);
119 TEST_CHECK(remove_all(link, ec) == 1);
120 TEST_CHECK(!ec);
121 TEST_CHECK(!exists(symlink_status(link)));
122 TEST_CHECK(exists(dir));
123 TEST_CHECK(exists(file));
124 }
125 }
126
127
TEST_CASE(nested_dir)128 TEST_CASE(nested_dir)
129 {
130 scoped_test_env env;
131 const path dir = env.create_dir("dir");
132 const path dir1 = env.create_dir(dir / "dir1");
133 const path out_of_dir_file = env.create_file("file1", 42);
134 const path all_files[] = {
135 dir, dir1,
136 env.create_file(dir / "file1", 42),
137 env.create_symlink(out_of_dir_file, dir / "sym1"),
138 env.create_file(dir1 / "file2", 42),
139 env.create_directory_symlink(dir, dir1 / "sym2")
140 };
141 const std::size_t expected_count = sizeof(all_files) / sizeof(all_files[0]);
142
143 std::error_code ec = std::make_error_code(std::errc::address_in_use);
144 TEST_CHECK(remove_all(dir, ec) == expected_count);
145 TEST_CHECK(!ec);
146 for (auto const& p : all_files) {
147 TEST_CHECK(!exists(symlink_status(p)));
148 }
149 TEST_CHECK(exists(out_of_dir_file));
150 }
151
152 TEST_SUITE_END()
153