• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 // UNSUPPORTED: c++98, c++03
11 
12 // <filesystem>
13 
14 // bool remove(const path& p);
15 // bool remove(const path& p, error_code& ec) noexcept;
16 
17 #include "filesystem_include.hpp"
18 
19 #include "test_macros.h"
20 #include "rapid-cxx-test.hpp"
21 #include "filesystem_test_helper.hpp"
22 
23 using namespace fs;
24 
25 TEST_SUITE(filesystem_remove_test_suite)
26 
TEST_CASE(test_signatures)27 TEST_CASE(test_signatures)
28 {
29     const path p; ((void)p);
30     std::error_code ec; ((void)ec);
31     ASSERT_SAME_TYPE(decltype(fs::remove(p)), bool);
32     ASSERT_SAME_TYPE(decltype(fs::remove(p, ec)), bool);
33 
34     ASSERT_NOT_NOEXCEPT(fs::remove(p));
35     ASSERT_NOEXCEPT(fs::remove(p, ec));
36 }
37 
TEST_CASE(test_error_reporting)38 TEST_CASE(test_error_reporting)
39 {
40     auto checkThrow = [](path const& f, const std::error_code& ec)
41     {
42 #ifndef TEST_HAS_NO_EXCEPTIONS
43         try {
44             fs::remove(f);
45             return false;
46         } catch (filesystem_error const& err) {
47             return err.path1() == f
48                 && err.path2() == ""
49                 && err.code() == ec;
50         }
51 #else
52         ((void)f); ((void)ec);
53         return true;
54 #endif
55     };
56     scoped_test_env env;
57     const path non_empty_dir = env.create_dir("dir");
58     env.create_file(non_empty_dir / "file1", 42);
59     const path bad_perms_dir = env.create_dir("bad_dir");
60     const path file_in_bad_dir = env.create_file(bad_perms_dir / "file", 42);
61     permissions(bad_perms_dir, perms::none);
62     const path testCases[] = {
63         non_empty_dir,
64         file_in_bad_dir,
65     };
66     for (auto& p : testCases) {
67         std::error_code ec;
68 
69         TEST_CHECK(!fs::remove(p, ec));
70         TEST_CHECK(ec);
71         TEST_CHECK(checkThrow(p, ec));
72     }
73 
74     // PR#35780
75     const path testCasesNonexistant[] = {
76         "",
77         env.make_env_path("dne")
78     };
79 
80     for (auto& p : testCasesNonexistant) {
81         std::error_code ec;
82 
83         TEST_CHECK(!fs::remove(p, ec));
84         TEST_CHECK(!ec);
85     }
86 }
87 
TEST_CASE(basic_remove_test)88 TEST_CASE(basic_remove_test)
89 {
90     scoped_test_env env;
91     const path dne = env.make_env_path("dne");
92     const path link = env.create_symlink(dne, "link");
93     const path nested_link = env.make_env_path("nested_link");
94     create_symlink(link, nested_link);
95     const path testCases[] = {
96         env.create_file("file", 42),
97         env.create_dir("empty_dir"),
98         nested_link,
99         link
100     };
101     for (auto& p : testCases) {
102         std::error_code ec = std::make_error_code(std::errc::address_in_use);
103         TEST_CHECK(remove(p, ec));
104         TEST_CHECK(!ec);
105         TEST_CHECK(!exists(symlink_status(p)));
106     }
107 }
108 
109 TEST_SUITE_END()
110