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 scoped_test_env env;
40 // Windows doesn't support setting perms::none to trigger failures
41 // reading directories.
42 #ifndef TEST_WIN_NO_FILESYSTEM_PERMS_NONE
43 auto checkThrow = [](path const& f, const std::error_code& ec)
44 {
45 #ifndef TEST_HAS_NO_EXCEPTIONS
46 try {
47 fs::remove_all(f);
48 return false;
49 } catch (filesystem_error const& err) {
50 return err.path1() == f
51 && err.path2() == ""
52 && err.code() == ec;
53 }
54 #else
55 ((void)f); ((void)ec);
56 return true;
57 #endif
58 };
59 const path non_empty_dir = env.create_dir("dir");
60 env.create_file(non_empty_dir / "file1", 42);
61 const path bad_perms_dir = env.create_dir("bad_dir");
62 const path file_in_bad_dir = env.create_file(bad_perms_dir / "file", 42);
63 permissions(bad_perms_dir, perms::none);
64 const path bad_perms_file = env.create_file("file2", 42);
65 permissions(bad_perms_file, perms::none);
66
67 const path testCases[] = {
68 file_in_bad_dir
69 };
70 const auto BadRet = static_cast<std::uintmax_t>(-1);
71 for (auto& p : testCases) {
72 std::error_code ec;
73
74 TEST_CHECK(fs::remove_all(p, ec) == BadRet);
75 TEST_CHECK(ec);
76 TEST_CHECK(checkThrow(p, ec));
77 }
78 #endif
79
80 // PR#35780
81 const path testCasesNonexistant[] = {
82 "",
83 env.make_env_path("dne")
84 };
85 for (auto &p : testCasesNonexistant) {
86 std::error_code ec;
87
88 TEST_CHECK(fs::remove_all(p, ec) == 0);
89 TEST_CHECK(!ec);
90 }
91 }
92
TEST_CASE(basic_remove_all_test)93 TEST_CASE(basic_remove_all_test)
94 {
95 scoped_test_env env;
96 const path dne = env.make_env_path("dne");
97 const path link = env.create_symlink(dne, "link");
98 const path nested_link = env.make_env_path("nested_link");
99 create_symlink(link, nested_link);
100 const path testCases[] = {
101 env.create_file("file", 42),
102 env.create_dir("empty_dir"),
103 nested_link,
104 link
105 };
106 for (auto& p : testCases) {
107 std::error_code ec = std::make_error_code(std::errc::address_in_use);
108 TEST_CHECK(remove(p, ec));
109 TEST_CHECK(!ec);
110 TEST_CHECK(!exists(symlink_status(p)));
111 }
112 }
113
TEST_CASE(symlink_to_dir)114 TEST_CASE(symlink_to_dir)
115 {
116 scoped_test_env env;
117 const path dir = env.create_dir("dir");
118 const path file = env.create_file(dir / "file", 42);
119 const path link = env.create_directory_symlink(dir, "sym");
120
121 {
122 std::error_code ec = std::make_error_code(std::errc::address_in_use);
123 TEST_CHECK(remove_all(link, ec) == 1);
124 TEST_CHECK(!ec);
125 TEST_CHECK(!exists(symlink_status(link)));
126 TEST_CHECK(exists(dir));
127 TEST_CHECK(exists(file));
128 }
129 }
130
131
TEST_CASE(nested_dir)132 TEST_CASE(nested_dir)
133 {
134 scoped_test_env env;
135 const path dir = env.create_dir("dir");
136 const path dir1 = env.create_dir(dir / "dir1");
137 const path out_of_dir_file = env.create_file("file1", 42);
138 const path all_files[] = {
139 dir, dir1,
140 env.create_file(dir / "file1", 42),
141 env.create_symlink(out_of_dir_file, dir / "sym1"),
142 env.create_file(dir1 / "file2", 42),
143 env.create_directory_symlink(dir, dir1 / "sym2")
144 };
145 const std::size_t expected_count = sizeof(all_files) / sizeof(all_files[0]);
146
147 std::error_code ec = std::make_error_code(std::errc::address_in_use);
148 TEST_CHECK(remove_all(dir, ec) == expected_count);
149 TEST_CHECK(!ec);
150 for (auto const& p : all_files) {
151 TEST_CHECK(!exists(symlink_status(p)));
152 }
153 TEST_CHECK(exists(out_of_dir_file));
154 }
155
156 TEST_SUITE_END()
157