]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - test/std/experimental/filesystem/fs.op.funcs/fs.op.copy_file/copy_file.pass.cpp
Vendor import of libc++ trunk r290819:
[FreeBSD/FreeBSD.git] / test / std / experimental / filesystem / fs.op.funcs / fs.op.copy_file / copy_file.pass.cpp
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 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;
19
20 #include <experimental/filesystem>
21 #include <type_traits>
22 #include <chrono>
23 #include <cassert>
24
25 #include "test_macros.h"
26 #include "rapid-cxx-test.hpp"
27 #include "filesystem_test_helper.hpp"
28
29 using namespace std::experimental::filesystem;
30 namespace fs = std::experimental::filesystem;
31
32 using CO = fs::copy_options;
33
34 TEST_SUITE(filesystem_copy_file_test_suite)
35
36 TEST_CASE(test_signatures)
37 {
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));
49 }
50
51 TEST_CASE(test_error_reporting)
52 {
53     auto checkThrow = [](path const& f, path const& t, const std::error_code& ec)
54     {
55 #ifndef TEST_HAS_NO_EXCEPTIONS
56         try {
57             fs::copy_file(f, t);
58             return false;
59         } catch (filesystem_error const& err) {
60             return err.path1() == f
61                 && err.path2() == t
62                 && err.code() == ec;
63         }
64 #else
65         ((void)f); ((void)t); ((void)ec);
66         return true;
67 #endif
68     };
69
70     scoped_test_env env;
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)
75         std::error_code ec;
76         TEST_CHECK(fs::copy_file(file, file, ec) == false);
77         TEST_REQUIRE(ec);
78         TEST_CHECK(checkThrow(file, file, ec));
79     }
80     { // exists(to) && !(skip_existing | overwrite_existing | update_existing)
81         std::error_code ec;
82         TEST_CHECK(fs::copy_file(file, file2, ec) == false);
83         TEST_REQUIRE(ec);
84         TEST_CHECK(checkThrow(file, file2, ec));
85     }
86 }
87
88 TEST_CASE(copy_file)
89 {
90     scoped_test_env env;
91     const path file = env.create_file("file1", 42);
92
93     { // !exists(to)
94         const path dest = env.make_env_path("dest1");
95         std::error_code ec;
96         TEST_REQUIRE(fs::copy_file(file, dest, ec) == true);
97         TEST_CHECK(!ec);
98         TEST_CHECK(file_size(dest) == 42);
99     }
100     { // exists(to) && overwrite_existing
101         const path dest = env.create_file("dest2", 55);
102         std::error_code ec;
103         TEST_REQUIRE(fs::copy_file(file, dest,
104                                    copy_options::overwrite_existing, ec) == true);
105         TEST_CHECK(!ec);
106         TEST_CHECK(file_size(dest) == 42);
107     }
108     { // exists(to) && update_existing
109         using Sec = std::chrono::seconds;
110         const path older = env.create_file("older_file", 1);
111
112         SleepFor(Sec(2));
113         const path from = env.create_file("update_from", 55);
114
115         SleepFor(Sec(2));
116         const path newer = env.create_file("newer_file", 2);
117
118         std::error_code ec;
119         TEST_REQUIRE(fs::copy_file(from, older, copy_options::update_existing, ec) == true);
120         TEST_CHECK(!ec);
121         TEST_CHECK(file_size(older) == 55);
122
123         TEST_REQUIRE(fs::copy_file(from, newer, copy_options::update_existing, ec) == false);
124         TEST_CHECK(!ec);
125         TEST_CHECK(file_size(newer) == 2);
126     }
127     { // skip_existing
128         const path file2 = env.create_file("file2", 55);
129         std::error_code ec;
130         TEST_REQUIRE(fs::copy_file(file, file2, copy_options::skip_existing, ec) == false);
131         TEST_CHECK(!ec);
132         TEST_CHECK(file_size(file2) == 55);
133     }
134 }
135
136 TEST_CASE(test_attributes_get_copied)
137 {
138     scoped_test_env env;
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);
144     std::error_code ec;
145     TEST_REQUIRE(fs::copy_file(file, dest, ec) == true);
146     TEST_CHECK(!ec);
147     auto new_st = status(dest);
148     TEST_CHECK(new_st.permissions() == new_perms);
149 }
150
151 TEST_CASE(copy_dir_test)
152 {
153     scoped_test_env env;
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);
158     TEST_CHECK(ec);
159     TEST_CHECK(ec != GetTestEC());
160     ec = GetTestEC();
161     TEST_CHECK(fs::copy_file(dest, file, ec) == false);
162     TEST_CHECK(ec);
163     TEST_CHECK(ec != GetTestEC());
164 }
165
166 TEST_CASE(non_regular_file_test)
167 {
168     scoped_test_env env;
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);
172     {
173         std::error_code ec = GetTestEC();
174         TEST_REQUIRE(fs::copy_file(fifo, dest, ec) == false);
175         TEST_CHECK(ec);
176         TEST_CHECK(ec != GetTestEC());
177         TEST_CHECK(!exists(dest));
178     }
179     {
180         std::error_code ec = GetTestEC();
181         TEST_REQUIRE(fs::copy_file(file, fifo, copy_options::overwrite_existing, ec) == false);
182         TEST_CHECK(ec);
183         TEST_CHECK(ec != GetTestEC());
184         TEST_CHECK(is_fifo(fifo));
185     }
186 }
187
188 TEST_SUITE_END()