]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - test/std/experimental/filesystem/fs.op.funcs/fs.op.remove_all/remove_all.pass.cpp
Vendor import of libc++ release_39 branch r276489:
[FreeBSD/FreeBSD.git] / test / std / experimental / filesystem / fs.op.funcs / fs.op.remove_all / remove_all.pass.cpp
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
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_NOEXCEPT(fs::remove_all(p, ec));
37 }
38
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         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         env.make_env_path("dne"),
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         TEST_CHECK(fs::remove_all(p, ec) == BadRet);
73         TEST_CHECK(ec);
74         TEST_CHECK(checkThrow(p, ec));
75     }
76 }
77
78 TEST_CASE(basic_remove_all_test)
79 {
80     scoped_test_env env;
81     const path dne = env.make_env_path("dne");
82     const path link = env.create_symlink(dne, "link");
83     const path nested_link = env.make_env_path("nested_link");
84     create_symlink(link, nested_link);
85     const path testCases[] = {
86         env.create_file("file", 42),
87         env.create_dir("empty_dir"),
88         nested_link,
89         link
90     };
91     for (auto& p : testCases) {
92         std::error_code ec = std::make_error_code(std::errc::address_in_use);
93         TEST_CHECK(remove(p, ec));
94         TEST_CHECK(!ec);
95         TEST_CHECK(!exists(symlink_status(p)));
96     }
97 }
98
99 TEST_CASE(symlink_to_dir)
100 {
101     scoped_test_env env;
102     const path dir = env.create_dir("dir");
103     const path file = env.create_file(dir / "file", 42);
104     const path link = env.create_symlink(dir, "sym");
105
106     {
107         std::error_code ec = std::make_error_code(std::errc::address_in_use);
108         TEST_CHECK(remove_all(link, ec) == 1);
109         TEST_CHECK(!ec);
110         TEST_CHECK(!exists(symlink_status(link)));
111         TEST_CHECK(exists(dir));
112         TEST_CHECK(exists(file));
113     }
114 }
115
116
117 TEST_CASE(nested_dir)
118 {
119     scoped_test_env env;
120     const path dir = env.create_dir("dir");
121     const path dir1 = env.create_dir(dir / "dir1");
122     const path out_of_dir_file = env.create_file("file1", 42);
123     const path all_files[] = {
124         dir, dir1,
125         env.create_file(dir / "file1", 42),
126         env.create_symlink(out_of_dir_file, dir / "sym1"),
127         env.create_file(dir1 / "file2", 42),
128         env.create_symlink(dir, dir1 / "sym2")
129     };
130     const std::size_t expected_count = sizeof(all_files) / sizeof(all_files[0]);
131
132     std::error_code ec = std::make_error_code(std::errc::address_in_use);
133     TEST_CHECK(remove_all(dir, ec) == expected_count);
134     TEST_CHECK(!ec);
135     for (auto const& p : all_files) {
136         TEST_CHECK(!exists(symlink_status(p)));
137     }
138     TEST_CHECK(exists(out_of_dir_file));
139 }
140
141 TEST_SUITE_END()