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