Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / test / std / input.output / filesystems / fs.op.funcs / fs.op.remove / remove.pass.cpp
blob7abe973eb3bdb3e9aac3601da93b2c03b993f010
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 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++03
10 // UNSUPPORTED: no-filesystem
11 // UNSUPPORTED: availability-filesystem-missing
13 // <filesystem>
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"
23 using namespace fs;
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
41 try {
42 fs::remove(f);
43 return false;
44 } catch (filesystem_error const& err) {
45 return err.path1() == f
46 && err.path2() == ""
47 && err.code() == ec;
49 #else
50 ((void)f); ((void)ec);
51 return true;
52 #endif
54 scoped_test_env env;
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[] = {
61 non_empty_dir,
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.
67 file_in_bad_dir,
68 #endif
70 for (auto& p : testCases) {
71 std::error_code ec;
73 assert(!fs::remove(p, ec));
74 assert(ec);
75 assert(checkThrow(p, ec));
78 // PR#35780
79 const path testCasesNonexistant[] = {
80 "",
81 env.make_env_path("dne")
84 for (auto& p : testCasesNonexistant) {
85 std::error_code ec;
87 assert(!fs::remove(p, ec));
88 assert(!ec);
92 static void basic_remove_test()
94 scoped_test_env env;
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"),
102 nested_link,
103 link
105 for (auto& p : testCases) {
106 std::error_code ec = std::make_error_code(std::errc::address_in_use);
107 assert(remove(p, ec));
108 assert(!ec);
109 assert(!exists(symlink_status(p)));
113 int main(int, char**) {
114 test_signatures();
115 test_error_reporting();
116 basic_remove_test();
117 return 0;