1 //===----------------------------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // UNSUPPORTED: c++98, c++03
12 // <experimental/filesystem>
14 // bool copy_file(const path& from, const path& to);
15 // bool copy_file(const path& from, const path& to, error_code& ec) noexcept;
16 // bool copy_file(const path& from, const path& to, copy_options options);
17 // bool copy_file(const path& from, const path& to, copy_options options,
18 // error_code& ec) noexcept;
20 #include <experimental/filesystem>
21 #include <type_traits>
25 #include "test_macros.h"
26 #include "rapid-cxx-test.hpp"
27 #include "filesystem_test_helper.hpp"
29 using namespace std::experimental::filesystem;
30 namespace fs = std::experimental::filesystem;
32 using CO = fs::copy_options;
34 TEST_SUITE(filesystem_copy_file_test_suite)
36 TEST_CASE(test_signatures)
38 const path p; ((void)p);
39 const copy_options opts{}; ((void)opts);
40 std::error_code ec; ((void)ec);
41 ASSERT_SAME_TYPE(decltype(fs::copy_file(p, p)), bool);
42 ASSERT_SAME_TYPE(decltype(fs::copy_file(p, p, opts)), bool);
43 ASSERT_SAME_TYPE(decltype(fs::copy_file(p, p, ec)), bool);
44 ASSERT_SAME_TYPE(decltype(fs::copy_file(p, p, opts, ec)), bool);
45 ASSERT_NOT_NOEXCEPT(fs::copy_file(p, p));
46 ASSERT_NOT_NOEXCEPT(fs::copy_file(p, p, opts));
47 ASSERT_NOEXCEPT(fs::copy_file(p, p, ec));
48 ASSERT_NOEXCEPT(fs::copy_file(p, p, opts, ec));
51 TEST_CASE(test_error_reporting)
53 auto checkThrow = [](path const& f, path const& t, const std::error_code& ec)
55 #ifndef TEST_HAS_NO_EXCEPTIONS
59 } catch (filesystem_error const& err) {
60 return err.path1() == f
65 ((void)f); ((void)t); ((void)ec);
71 const path file = env.create_file("file1", 42);
72 const path file2 = env.create_file("file2", 55);
73 const path dne = env.make_env_path("dne");
74 { // exists(to) && equivalent(to, from)
76 TEST_CHECK(fs::copy_file(file, file, ec) == false);
78 TEST_CHECK(checkThrow(file, file, ec));
80 { // exists(to) && !(skip_existing | overwrite_existing | update_existing)
82 TEST_CHECK(fs::copy_file(file, file2, ec) == false);
84 TEST_CHECK(checkThrow(file, file2, ec));
91 const path file = env.create_file("file1", 42);
94 const path dest = env.make_env_path("dest1");
96 TEST_REQUIRE(fs::copy_file(file, dest, ec) == true);
98 TEST_CHECK(file_size(dest) == 42);
100 { // exists(to) && overwrite_existing
101 const path dest = env.create_file("dest2", 55);
103 TEST_REQUIRE(fs::copy_file(file, dest,
104 copy_options::overwrite_existing, ec) == true);
106 TEST_CHECK(file_size(dest) == 42);
108 { // exists(to) && update_existing
109 using Sec = std::chrono::seconds;
110 const path older = env.create_file("older_file", 1);
113 const path from = env.create_file("update_from", 55);
116 const path newer = env.create_file("newer_file", 2);
119 TEST_REQUIRE(fs::copy_file(from, older, copy_options::update_existing, ec) == true);
121 TEST_CHECK(file_size(older) == 55);
123 TEST_REQUIRE(fs::copy_file(from, newer, copy_options::update_existing, ec) == false);
125 TEST_CHECK(file_size(newer) == 2);
128 const path file2 = env.create_file("file2", 55);
130 TEST_REQUIRE(fs::copy_file(file, file2, copy_options::skip_existing, ec) == false);
132 TEST_CHECK(file_size(file2) == 55);
136 TEST_CASE(test_attributes_get_copied)
139 const path file = env.create_file("file1", 42);
140 const path dest = env.make_env_path("file2");
141 auto st = status(file);
142 perms new_perms = perms::owner_read;
143 permissions(file, new_perms);
145 TEST_REQUIRE(fs::copy_file(file, dest, ec) == true);
147 auto new_st = status(dest);
148 TEST_CHECK(new_st.permissions() == new_perms);
151 TEST_CASE(copy_dir_test)
154 const path file = env.create_file("file1", 42);
155 const path dest = env.create_dir("dir1");
156 std::error_code ec = GetTestEC();
157 TEST_CHECK(fs::copy_file(file, dest, ec) == false);
159 TEST_CHECK(ec != GetTestEC());
161 TEST_CHECK(fs::copy_file(dest, file, ec) == false);
163 TEST_CHECK(ec != GetTestEC());
166 TEST_CASE(non_regular_file_test)
169 const path fifo = env.create_fifo("fifo");
170 const path dest = env.make_env_path("dest");
171 const path file = env.create_file("file", 42);
173 std::error_code ec = GetTestEC();
174 TEST_REQUIRE(fs::copy_file(fifo, dest, ec) == false);
176 TEST_CHECK(ec != GetTestEC());
177 TEST_CHECK(!exists(dest));
180 std::error_code ec = GetTestEC();
181 TEST_REQUIRE(fs::copy_file(file, fifo, copy_options::overwrite_existing, ec) == false);
183 TEST_CHECK(ec != GetTestEC());
184 TEST_CHECK(is_fifo(fifo));