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