1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // REQUIRES: can-create-symlinks
10 // UNSUPPORTED: c++03, c++11, c++14
11 // UNSUPPORTED: no-filesystem
12 // UNSUPPORTED: availability-filesystem-missing
16 // uintmax_t remove_all(const path& p);
17 // uintmax_t remove_all(const path& p, error_code& ec) noexcept;
21 #include "test_macros.h"
22 #include "filesystem_test_helper.h"
23 namespace fs
= std::filesystem
;
26 static void test_signatures()
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);
33 ASSERT_NOT_NOEXCEPT(fs::remove_all(p
));
34 ASSERT_NOT_NOEXCEPT(fs::remove_all(p
, ec
));
37 static void test_error_reporting()
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
)
45 #ifndef TEST_HAS_NO_EXCEPTIONS
49 } catch (filesystem_error
const& err
) {
50 return err
.path1() == f
55 ((void)f
); ((void)ec
);
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
);
67 const path testCases
[] = {
70 const auto BadRet
= static_cast<std::uintmax_t>(-1);
71 for (auto& p
: testCases
) {
74 assert(fs::remove_all(p
, ec
) == BadRet
);
76 assert(checkThrow(p
, ec
));
81 const path testCasesNonexistant
[] = {
83 env
.make_env_path("dne")
85 for (auto &p
: testCasesNonexistant
) {
88 assert(fs::remove_all(p
, ec
) == 0);
93 static void basic_remove_all_test()
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"),
106 for (auto& p
: testCases
) {
107 std::error_code ec
= std::make_error_code(std::errc::address_in_use
);
108 assert(remove(p
, ec
));
110 assert(!exists(symlink_status(p
)));
114 static void symlink_to_dir()
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");
122 std::error_code ec
= std::make_error_code(std::errc::address_in_use
);
123 assert(remove_all(link
, ec
) == 1);
125 assert(!exists(symlink_status(link
)));
127 assert(exists(file
));
132 static void nested_dir()
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
[] = {
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")
145 const std::size_t expected_count
= sizeof(all_files
) / sizeof(all_files
[0]);
147 std::error_code ec
= std::make_error_code(std::errc::address_in_use
);
148 assert(remove_all(dir
, ec
) == expected_count
);
150 for (auto const& p
: all_files
) {
151 assert(!exists(symlink_status(p
)));
153 assert(exists(out_of_dir_file
));
156 int main(int, char**) {
158 test_error_reporting();
159 basic_remove_all_test();