1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
10 // UNSUPPORTED: no-filesystem
11 // UNSUPPORTED: availability-filesystem-missing
15 // bool remove(const path& p);
16 // bool remove(const path& p, error_code& ec) noexcept;
18 #include "filesystem_include.h"
20 #include "test_macros.h"
21 #include "filesystem_test_helper.h"
25 static void test_signatures()
27 const path p
; ((void)p
);
28 std::error_code ec
; ((void)ec
);
29 ASSERT_SAME_TYPE(decltype(fs::remove(p
)), bool);
30 ASSERT_SAME_TYPE(decltype(fs::remove(p
, ec
)), bool);
32 ASSERT_NOT_NOEXCEPT(fs::remove(p
));
33 ASSERT_NOEXCEPT(fs::remove(p
, ec
));
36 static void test_error_reporting()
38 auto checkThrow
= [](path
const& f
, const std::error_code
& ec
)
40 #ifndef TEST_HAS_NO_EXCEPTIONS
44 } catch (filesystem_error
const& err
) {
45 return err
.path1() == f
50 ((void)f
); ((void)ec
);
55 const path non_empty_dir
= env
.create_dir("dir");
56 env
.create_file(non_empty_dir
/ "file1", 42);
57 const path bad_perms_dir
= env
.create_dir("bad_dir");
58 const path file_in_bad_dir
= env
.create_file(bad_perms_dir
/ "file", 42);
59 permissions(bad_perms_dir
, perms::none
);
60 const path testCases
[] = {
62 #ifndef TEST_WIN_NO_FILESYSTEM_PERMS_NONE
63 // Windows doesn't support setting perms::none on a directory to
64 // stop it from being accessed. And a fictional file under
65 // GetWindowsInaccessibleDir() doesn't cause fs::remove() to report
66 // errors, it just returns false cleanly.
70 for (auto& p
: testCases
) {
73 assert(!fs::remove(p
, ec
));
75 assert(checkThrow(p
, ec
));
79 const path testCasesNonexistant
[] = {
81 env
.make_env_path("dne")
84 for (auto& p
: testCasesNonexistant
) {
87 assert(!fs::remove(p
, ec
));
92 static void basic_remove_test()
95 const path dne
= env
.make_env_path("dne");
96 const path link
= env
.create_symlink(dne
, "link");
97 const path nested_link
= env
.make_env_path("nested_link");
98 create_symlink(link
, nested_link
);
99 const path testCases
[] = {
100 env
.create_file("file", 42),
101 env
.create_dir("empty_dir"),
105 for (auto& p
: testCases
) {
106 std::error_code ec
= std::make_error_code(std::errc::address_in_use
);
107 assert(remove(p
, ec
));
109 assert(!exists(symlink_status(p
)));
113 int main(int, char**) {
115 test_error_reporting();