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 // XFAIL: LIBCXX-WINDOWS-FIXME 12 13 // <filesystem> 14 15 // bool remove(const path& p); 16 // bool remove(const path& p, error_code& ec) noexcept; 17 18 #include "filesystem_include.h" 19 20 #include "test_macros.h" 21 #include "rapid-cxx-test.h" 22 #include "filesystem_test_helper.h" 23 24 using namespace fs; 25 26 TEST_SUITE(filesystem_remove_test_suite) 27 28 TEST_CASE(test_signatures) 29 { 30 const path p; ((void)p); 31 std::error_code ec; ((void)ec); 32 ASSERT_SAME_TYPE(decltype(fs::remove(p)), bool); 33 ASSERT_SAME_TYPE(decltype(fs::remove(p, ec)), bool); 34 35 ASSERT_NOT_NOEXCEPT(fs::remove(p)); 36 ASSERT_NOEXCEPT(fs::remove(p, ec)); 37 } 38 39 TEST_CASE(test_error_reporting) 40 { 41 auto checkThrow = [](path const& f, const std::error_code& ec) 42 { 43 #ifndef TEST_HAS_NO_EXCEPTIONS 44 try { 45 fs::remove(f); 46 return false; 47 } catch (filesystem_error const& err) { 48 return err.path1() == f 49 && err.path2() == "" 50 && err.code() == ec; 51 } 52 #else 53 ((void)f); ((void)ec); 54 return true; 55 #endif 56 }; 57 scoped_test_env env; 58 const path non_empty_dir = env.create_dir("dir"); 59 env.create_file(non_empty_dir / "file1", 42); 60 const path bad_perms_dir = env.create_dir("bad_dir"); 61 const path file_in_bad_dir = env.create_file(bad_perms_dir / "file", 42); 62 permissions(bad_perms_dir, perms::none); 63 const path testCases[] = { 64 non_empty_dir, 65 file_in_bad_dir, 66 }; 67 for (auto& p : testCases) { 68 std::error_code ec; 69 70 TEST_CHECK(!fs::remove(p, ec)); 71 TEST_CHECK(ec); 72 TEST_CHECK(checkThrow(p, ec)); 73 } 74 75 // PR#35780 76 const path testCasesNonexistant[] = { 77 "", 78 env.make_env_path("dne") 79 }; 80 81 for (auto& p : testCasesNonexistant) { 82 std::error_code ec; 83 84 TEST_CHECK(!fs::remove(p, ec)); 85 TEST_CHECK(!ec); 86 } 87 } 88 89 TEST_CASE(basic_remove_test) 90 { 91 scoped_test_env env; 92 const path dne = env.make_env_path("dne"); 93 const path link = env.create_symlink(dne, "link"); 94 const path nested_link = env.make_env_path("nested_link"); 95 create_symlink(link, nested_link); 96 const path testCases[] = { 97 env.create_file("file", 42), 98 env.create_dir("empty_dir"), 99 nested_link, 100 link 101 }; 102 for (auto& p : testCases) { 103 std::error_code ec = std::make_error_code(std::errc::address_in_use); 104 TEST_CHECK(remove(p, ec)); 105 TEST_CHECK(!ec); 106 TEST_CHECK(!exists(symlink_status(p))); 107 } 108 } 109 110 TEST_SUITE_END() 111