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 
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 
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         file_in_bad_dir,
64     };
65     for (auto& p : testCases) {
66         std::error_code ec;
67 
68         TEST_CHECK(!fs::remove(p, ec));
69         TEST_CHECK(ec);
70         TEST_CHECK(checkThrow(p, ec));
71     }
72 
73     // PR#35780
74     const path testCasesNonexistant[] = {
75         "",
76         env.make_env_path("dne")
77     };
78 
79     for (auto& p : testCasesNonexistant) {
80         std::error_code ec;
81 
82         TEST_CHECK(!fs::remove(p, ec));
83         TEST_CHECK(!ec);
84     }
85 }
86 
87 TEST_CASE(basic_remove_test)
88 {
89     scoped_test_env env;
90     const path dne = env.make_env_path("dne");
91     const path link = env.create_symlink(dne, "link");
92     const path nested_link = env.make_env_path("nested_link");
93     create_symlink(link, nested_link);
94     const path testCases[] = {
95         env.create_file("file", 42),
96         env.create_dir("empty_dir"),
97         nested_link,
98         link
99     };
100     for (auto& p : testCases) {
101         std::error_code ec = std::make_error_code(std::errc::address_in_use);
102         TEST_CHECK(remove(p, ec));
103         TEST_CHECK(!ec);
104         TEST_CHECK(!exists(symlink_status(p)));
105     }
106 }
107 
108 TEST_SUITE_END()
109