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// REQUIRES: can-create-symlinks
10// UNSUPPORTED: c++03, c++11, c++14
11// UNSUPPORTED: no-filesystem
12// UNSUPPORTED: availability-filesystem-missing
13
14// <filesystem>
15
16// bool remove(const path& p);
17// bool remove(const path& p, error_code& ec) noexcept;
18
19#include <filesystem>
20
21#include "test_macros.h"
22#include "filesystem_test_helper.h"
23namespace fs = std::filesystem;
24using namespace fs;
25
26static void 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: p));
34 ASSERT_NOEXCEPT(fs::remove(p: p, ec&: ec));
35}
36
37static void 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(p: 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(p: bad_perms_dir, prms: 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 assert(!fs::remove(p, ec));
75 assert(ec);
76 assert(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 assert(!fs::remove(p, ec));
89 assert(!ec);
90 }
91}
92
93static void 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(to: link, new_symlink: 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 assert(remove(p, ec));
109 assert(!ec);
110 assert(!exists(symlink_status(p)));
111 }
112}
113
114int main(int, char**) {
115 test_signatures();
116 test_error_reporting();
117 basic_remove_test();
118 return 0;
119}
120

source code of libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.remove/remove.pass.cpp