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 // bool remove(const path& p);
14 // bool remove(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_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(p)), bool);
31     ASSERT_SAME_TYPE(decltype(fs::remove(p, ec)), bool);
32 
33     ASSERT_NOT_NOEXCEPT(fs::remove(p));
34     ASSERT_NOEXCEPT(fs::remove(p, ec));
35 }
36 
TEST_CASE(test_error_reporting)37 TEST_CASE(test_error_reporting)
38 {
39     auto checkThrow = [](path const& f, const std::error_code& ec)
40     {
41 #ifndef TEST_HAS_NO_EXCEPTIONS
42         try {
43             fs::remove(f);
44             return false;
45         } catch (filesystem_error const& err) {
46             return err.path1() == f
47                 && err.path2() == ""
48                 && err.code() == ec;
49         }
50 #else
51         ((void)f); ((void)ec);
52         return true;
53 #endif
54     };
55     scoped_test_env env;
56     const path non_empty_dir = env.create_dir("dir");
57     env.create_file(non_empty_dir / "file1", 42);
58     const path bad_perms_dir = env.create_dir("bad_dir");
59     const path file_in_bad_dir = env.create_file(bad_perms_dir / "file", 42);
60     permissions(bad_perms_dir, perms::none);
61     const path testCases[] = {
62         non_empty_dir,
63 #ifndef TEST_WIN_NO_FILESYSTEM_PERMS_NONE
64         // Windows doesn't support setting perms::none on a directory to
65         // stop it from being accessed. And a fictional file under
66         // GetWindowsInaccessibleDir() doesn't cause fs::remove() to report
67         // errors, it just returns false cleanly.
68         file_in_bad_dir,
69 #endif
70     };
71     for (auto& p : testCases) {
72         std::error_code ec;
73 
74         TEST_CHECK(!fs::remove(p, ec));
75         TEST_CHECK(ec);
76         TEST_CHECK(checkThrow(p, ec));
77     }
78 
79     // PR#35780
80     const path testCasesNonexistant[] = {
81         "",
82         env.make_env_path("dne")
83     };
84 
85     for (auto& p : testCasesNonexistant) {
86         std::error_code ec;
87 
88         TEST_CHECK(!fs::remove(p, ec));
89         TEST_CHECK(!ec);
90     }
91 }
92 
TEST_CASE(basic_remove_test)93 TEST_CASE(basic_remove_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 
114 TEST_SUITE_END()
115