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// bool remove(const path& p);
15// bool remove(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
23using namespace std::experimental::filesystem;
24namespace fs = std::experimental::filesystem;
25
26TEST_SUITE(filesystem_remove_test_suite)
27
28TEST_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
39TEST_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        "",
65        env.make_env_path("dne"),
66        non_empty_dir,
67        file_in_bad_dir,
68    };
69    for (auto& p : testCases) {
70        std::error_code ec;
71        TEST_CHECK(!fs::remove(p, ec));
72        TEST_CHECK(ec);
73        TEST_CHECK(checkThrow(p, ec));
74    }
75}
76
77TEST_CASE(basic_remove_test)
78{
79    scoped_test_env env;
80    const path dne = env.make_env_path("dne");
81    const path link = env.create_symlink(dne, "link");
82    const path nested_link = env.make_env_path("nested_link");
83    create_symlink(link, nested_link);
84    const path testCases[] = {
85        env.create_file("file", 42),
86        env.create_dir("empty_dir"),
87        nested_link,
88        link
89    };
90    for (auto& p : testCases) {
91        std::error_code ec = std::make_error_code(std::errc::address_in_use);
92        TEST_CHECK(remove(p, ec));
93        TEST_CHECK(!ec);
94        TEST_CHECK(!exists(symlink_status(p)));
95    }
96}
97
98TEST_SUITE_END()
99