Home | History | Annotate | Download | only in fs.op.remove_all
      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 // <experimental/filesystem>
     13 
     14 // uintmax_t remove_all(const path& p);
     15 // uintmax_t remove_all(const path& p, error_code& ec) noexcept;
     16 
     17 #include <experimental/filesystem>
     18 
     19 #include "test_macros.h"
     20 #include "rapid-cxx-test.hpp"
     21 #include "filesystem_test_helper.hpp"
     22 
     23 using namespace std::experimental::filesystem;
     24 namespace fs = std::experimental::filesystem;
     25 
     26 TEST_SUITE(filesystem_remove_all_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_all(p)), std::uintmax_t);
     33     ASSERT_SAME_TYPE(decltype(fs::remove_all(p, ec)), std::uintmax_t);
     34 
     35     ASSERT_NOT_NOEXCEPT(fs::remove_all(p));
     36     ASSERT_NOEXCEPT(fs::remove_all(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_all(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 bad_perms_file = env.create_file("file2", 42);
     64     permissions(bad_perms_file, perms::none);
     65 
     66     const path testCases[] = {
     67         env.make_env_path("dne"),
     68         file_in_bad_dir
     69     };
     70     const auto BadRet = static_cast<std::uintmax_t>(-1);
     71     for (auto& p : testCases) {
     72         std::error_code ec;
     73         TEST_CHECK(fs::remove_all(p, ec) == BadRet);
     74         TEST_CHECK(ec);
     75         TEST_CHECK(checkThrow(p, ec));
     76     }
     77 }
     78 
     79 TEST_CASE(basic_remove_all_test)
     80 {
     81     scoped_test_env env;
     82     const path dne = env.make_env_path("dne");
     83     const path link = env.create_symlink(dne, "link");
     84     const path nested_link = env.make_env_path("nested_link");
     85     create_symlink(link, nested_link);
     86     const path testCases[] = {
     87         env.create_file("file", 42),
     88         env.create_dir("empty_dir"),
     89         nested_link,
     90         link
     91     };
     92     for (auto& p : testCases) {
     93         std::error_code ec = std::make_error_code(std::errc::address_in_use);
     94         TEST_CHECK(remove(p, ec));
     95         TEST_CHECK(!ec);
     96         TEST_CHECK(!exists(symlink_status(p)));
     97     }
     98 }
     99 
    100 TEST_CASE(symlink_to_dir)
    101 {
    102     scoped_test_env env;
    103     const path dir = env.create_dir("dir");
    104     const path file = env.create_file(dir / "file", 42);
    105     const path link = env.create_symlink(dir, "sym");
    106 
    107     {
    108         std::error_code ec = std::make_error_code(std::errc::address_in_use);
    109         TEST_CHECK(remove_all(link, ec) == 1);
    110         TEST_CHECK(!ec);
    111         TEST_CHECK(!exists(symlink_status(link)));
    112         TEST_CHECK(exists(dir));
    113         TEST_CHECK(exists(file));
    114     }
    115 }
    116 
    117 
    118 TEST_CASE(nested_dir)
    119 {
    120     scoped_test_env env;
    121     const path dir = env.create_dir("dir");
    122     const path dir1 = env.create_dir(dir / "dir1");
    123     const path out_of_dir_file = env.create_file("file1", 42);
    124     const path all_files[] = {
    125         dir, dir1,
    126         env.create_file(dir / "file1", 42),
    127         env.create_symlink(out_of_dir_file, dir / "sym1"),
    128         env.create_file(dir1 / "file2", 42),
    129         env.create_symlink(dir, dir1 / "sym2")
    130     };
    131     const std::size_t expected_count = sizeof(all_files) / sizeof(all_files[0]);
    132 
    133     std::error_code ec = std::make_error_code(std::errc::address_in_use);
    134     TEST_CHECK(remove_all(dir, ec) == expected_count);
    135     TEST_CHECK(!ec);
    136     for (auto const& p : all_files) {
    137         TEST_CHECK(!exists(symlink_status(p)));
    138     }
    139     TEST_CHECK(exists(out_of_dir_file));
    140 }
    141 
    142 TEST_SUITE_END()
    143