summaryrefslogtreecommitdiffstats
path: root/libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.copy/copy.pass.cpp
blob: c791a74361df9f5e3f09562adff442c0d2f8274d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: c++98, c++03

// <filesystem>

// void copy(const path& from, const path& to);
// void copy(const path& from, const path& to, error_code& ec);
// void copy(const path& from, const path& to, copy_options options);
// void copy(const path& from, const path& to, copy_options options,
//           error_code& ec);

#include "filesystem_include.hpp"
#include <type_traits>
#include <cstddef>
#include <cassert>

#include "test_macros.h"
#include "rapid-cxx-test.hpp"
#include "filesystem_test_helper.hpp"

using namespace fs;

using CO = fs::copy_options;

TEST_SUITE(filesystem_copy_test_suite)

TEST_CASE(signature_test)
{
    const path p; ((void)p);
    std::error_code ec; ((void)ec);
    const copy_options opts{}; ((void)opts);
    ASSERT_NOT_NOEXCEPT(fs::copy(p, p));
    ASSERT_NOT_NOEXCEPT(fs::copy(p, p, ec));
    ASSERT_NOT_NOEXCEPT(copy(p, p, opts));
    ASSERT_NOT_NOEXCEPT(copy(p, p, opts, ec));
}

// There are 4 cases is the proposal for absolute path.
// Each scope tests one of the cases.
TEST_CASE(test_error_reporting)
{
    auto checkThrow = [](path const& f, path const& t, const std::error_code& ec)
    {
#ifndef TEST_HAS_NO_EXCEPTIONS
        try {
            fs::copy(f, t);
            return false;
        } catch (filesystem_error const& err) {
            return err.path1() == f
                && err.path2() == t
                && err.code() == ec;
        }
#else
        ((void)f); ((void)t); ((void)ec);
        return true;
#endif
    };

    scoped_test_env env;
    const path file = env.create_file("file1", 42);
    const path dir = env.create_dir("dir");
    const path fifo = env.create_fifo("fifo");
    TEST_REQUIRE(is_other(fifo));

    const auto test_ec = GetTestEC();

    // !exists(f)
    {
        std::error_code ec = test_ec;
        const path f = StaticEnv::DNE;
        const path t = env.test_root;
        fs::copy(f, t, ec);
        TEST_REQUIRE(ec);
        TEST_REQUIRE(ec != test_ec);
        TEST_CHECK(checkThrow(f, t, ec));
    }
    { // equivalent(f, t) == true
        std::error_code ec = test_ec;
        fs::copy(file, file, ec);
        TEST_REQUIRE(ec);
        TEST_REQUIRE(ec != test_ec);
        TEST_CHECK(checkThrow(file, file, ec));
    }
    { // is_directory(from) && is_file(to)
        std::error_code ec = test_ec;
        fs::copy(dir, file, ec);
        TEST_REQUIRE(ec);
        TEST_REQUIRE(ec != test_ec);
        TEST_CHECK(checkThrow(dir, file, ec));
    }
    { // is_other(from)
        std::error_code ec = test_ec;
        fs::copy(fifo, dir, ec);
        TEST_REQUIRE(ec);
        TEST_REQUIRE(ec != test_ec);
        TEST_CHECK(checkThrow(fifo, dir, ec));
    }
    { // is_other(to)
        std::error_code ec = test_ec;
        fs::copy(file, fifo, ec);
        TEST_REQUIRE(ec);
        TEST_REQUIRE(ec != test_ec);
        TEST_CHECK(checkThrow(file, fifo, ec));
    }
}

TEST_CASE(from_is_symlink)
{
    scoped_test_env env;
    const path file = env.create_file("file", 42);
    const path symlink = env.create_symlink(file, "sym");
    const path dne = env.make_env_path("dne");

    { // skip symlinks
        std::error_code ec = GetTestEC();
        fs::copy(symlink, dne, copy_options::skip_symlinks, ec);
        TEST_CHECK(!ec);
        TEST_CHECK(!exists(dne));
    }
    {
        const path dest = env.make_env_path("dest");
        std::error_code ec = GetTestEC();
        fs::copy(symlink, dest, copy_options::copy_symlinks, ec);
        TEST_CHECK(!ec);
        TEST_CHECK(exists(dest));
        TEST_CHECK(is_symlink(dest));
    }
    { // copy symlink but target exists
        std::error_code ec = GetTestEC();
        fs::copy(symlink, file, copy_options::copy_symlinks, ec);
        TEST_CHECK(ec);
        TEST_CHECK(ec != GetTestEC());
    }
    { // create symlinks but target exists
        std::error_code ec = GetTestEC();
        fs::copy(symlink, file, copy_options::create_symlinks, ec);
        TEST_CHECK(ec);
        TEST_CHECK(ec != GetTestEC());
    }
}

TEST_CASE(from_is_regular_file)
{
    scoped_test_env env;
    const path file = env.create_file("file", 42);
    const path dir = env.create_dir("dir");
    { // skip copy because of directory
        const path dest = env.make_env_path("dest1");
        std::error_code ec = GetTestEC();
        fs::copy(file, dest, CO::directories_only, ec);
        TEST_CHECK(!ec);
        TEST_CHECK(!exists(dest));
    }
    { // create symlink to file
        const path dest = env.make_env_path("sym");
        std::error_code ec = GetTestEC();
        fs::copy(file, dest, CO::create_symlinks, ec);
        TEST_CHECK(!ec);
        TEST_CHECK(is_symlink(dest));
        TEST_CHECK(equivalent(file, canonical(dest)));
    }
    { // create hard link to file
        const path dest = env.make_env_path("hardlink");
        TEST_CHECK(hard_link_count(file) == 1);
        std::error_code ec = GetTestEC();
        fs::copy(file, dest, CO::create_hard_links, ec);
        TEST_CHECK(!ec);
        TEST_CHECK(exists(dest));
        TEST_CHECK(hard_link_count(file) == 2);
    }
    { // is_directory(t)
        const path dest_dir = env.create_dir("dest_dir");
        const path expect_dest = dest_dir / file.filename();
        std::error_code ec = GetTestEC();
        fs::copy(file, dest_dir, ec);
        TEST_CHECK(!ec);
        TEST_CHECK(is_regular_file(expect_dest));
    }
    { // otherwise copy_file(from, to, ...)
        const path dest = env.make_env_path("file_copy");
        std::error_code ec = GetTestEC();
        fs::copy(file, dest, ec);
        TEST_CHECK(!ec);
        TEST_CHECK(is_regular_file(dest));
    }
}

TEST_CASE(from_is_directory)
{
    struct FileInfo {
        path filename;
        std::size_t size;
    };
    const FileInfo files[] = {
        {"file1", 0},
        {"file2", 42},
        {"file3", 300}
    };
    scoped_test_env env;
    const path dir = env.create_dir("dir");
    const path nested_dir_name = "dir2";
    const path nested_dir = env.create_dir("dir/dir2");

    for (auto& FI : files) {
        env.create_file(dir / FI.filename, FI.size);
        env.create_file(nested_dir / FI.filename, FI.size);
    }
    { // test for non-existent directory
        const path dest = env.make_env_path("dest_dir1");
        std::error_code ec = GetTestEC();
        fs::copy(dir, dest, ec);
        TEST_REQUIRE(!ec);
        TEST_CHECK(is_directory(dest));
        for (auto& FI : files) {
            path created = dest / FI.filename;
            TEST_CHECK(is_regular_file(created));
            TEST_CHECK(file_size(created) == FI.size);
        }
        TEST_CHECK(!is_directory(dest / nested_dir_name));
    }
    { // test for existing directory
        const path dest = env.create_dir("dest_dir2");
        std::error_code ec = GetTestEC();
        fs::copy(dir, dest, ec);
        TEST_REQUIRE(!ec);
        TEST_CHECK(is_directory(dest));
        for (auto& FI : files) {
            path created = dest / FI.filename;
            TEST_CHECK(is_regular_file(created));
            TEST_CHECK(file_size(created) == FI.size);
        }
        TEST_CHECK(!is_directory(dest / nested_dir_name));
    }
    { // test recursive copy
        const path dest = env.make_env_path("dest_dir3");
        std::error_code ec = GetTestEC();
        fs::copy(dir, dest, CO::recursive, ec);
        TEST_REQUIRE(!ec);
        TEST_CHECK(is_directory(dest));
        const path nested_dest = dest / nested_dir_name;
        TEST_REQUIRE(is_directory(nested_dest));
        for (auto& FI : files) {
            path created = dest / FI.filename;
            path nested_created = nested_dest / FI.filename;
            TEST_CHECK(is_regular_file(created));
            TEST_CHECK(file_size(created) == FI.size);
            TEST_CHECK(is_regular_file(nested_created));
            TEST_CHECK(file_size(nested_created) == FI.size);
        }
    }
}

TEST_CASE(test_copy_symlinks_to_symlink_dir)
{
    scoped_test_env env;
    const path file1 = env.create_file("file1", 42);
    const path file2 = env.create_file("file2", 101);
    const path file2_sym = env.create_symlink(file2, "file2_sym");
    const path dir = env.create_dir("dir");
    const path dir_sym = env.create_symlink(dir, "dir_sym");
    {
        std::error_code ec = GetTestEC();
        fs::copy(file1, dir_sym, copy_options::copy_symlinks, ec);
        TEST_CHECK(!ec);
        const path dest = env.make_env_path("dir/file1");
        TEST_CHECK(exists(dest));
        TEST_CHECK(!is_symlink(dest));
        TEST_CHECK(file_size(dest) == 42);
    }
}


TEST_CASE(test_dir_create_symlink)
{
    scoped_test_env env;
    const path dir = env.create_dir("dir1");
    const path dest = env.make_env_path("dne");
    {
        std::error_code ec = GetTestEC();
        fs::copy(dir, dest, copy_options::create_symlinks, ec);
        TEST_CHECK(ec == std::make_error_code(std::errc::is_a_directory));
        TEST_CHECK(!exists(dest));
        TEST_CHECK(!is_symlink(dest));
    }
    {
        std::error_code ec = GetTestEC();
        fs::copy(dir, dest, copy_options::create_symlinks|copy_options::recursive, ec);
        TEST_CHECK(ec == std::make_error_code(std::errc::is_a_directory));
        TEST_CHECK(!exists(dest));
        TEST_CHECK(!is_symlink(dest));
    }
}

TEST_CASE(test_otherwise_no_effects_clause)
{
    scoped_test_env env;
    const path dir = env.create_dir("dir1");
    { // skip copy because of directory
        const path dest = env.make_env_path("dest1");
        std::error_code ec;
        fs::copy(dir, dest, CO::directories_only, ec);
        TEST_CHECK(!ec);
        TEST_CHECK(!exists(dest));
    }
}

TEST_SUITE_END()
OpenPOWER on IntegriCloud