Debian Codegraph @

libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.create_symlink/create_symlink.pass.cpp

raw
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 // REQUIRES: can-create-symlinks
10 // UNSUPPORTED: c++03, c++11, c++14
11 // UNSUPPORTED: no-filesystem
12
13 // <filesystem>
14
15 // void create_symlink(const path& existing_symlink, const path& new_symlink);
16 // void create_symlink(const path& existing_symlink, const path& new_symlink,
17 // error_code& ec) noexcept;
18
19 #include <filesystem>
20 #include <cassert>
21
22 #include "test_macros.h"
23 #include "filesystem_test_helper.h"
24 namespace fs = std::filesystem;
25 using namespace fs;
26
27 static void test_signatures()
28 {
29 const path p; ((void)p);
30 std::error_code ec; ((void)ec);
31 ASSERT_NOT_NOEXCEPT(fs::create_symlink(p, p));
32 ASSERT_NOEXCEPT(fs::create_symlink(p, p, ec));
33 }
34
35 static void test_error_reporting()
36 {
37 scoped_test_env env;
38 const path file = env.create_file("file1", 42);
39 const path file2 = env.create_file("file2", 55);
40 const path sym = env.create_symlink(file, "sym");
41 { // destination exists
42 std::error_code ec;
43 fs::create_symlink(sym, file2, ec);
44 assert(ec);
45 }
46 }
47
48 static void create_symlink_basic()
49 {
50 scoped_test_env env;
51 const path file = env.create_file("file", 42);
52 const path file_sym = env.create_symlink(file, "file_sym");
53 const path dir = env.create_dir("dir");
54 const path dir_sym = env.create_directory_symlink(dir, "dir_sym");
55 {
56 const path dest = env.make_env_path("dest1");
57 std::error_code ec;
58 fs::create_symlink(file_sym, dest, ec);
59 assert(!ec);
60 assert(is_symlink(dest));
61 assert(equivalent(dest, file));
62 }
63 {
64 const path dest = env.make_env_path("dest2");
65 std::error_code ec;
66 fs::create_directory_symlink(dir_sym, dest, ec);
67 assert(!ec);
68 assert(is_symlink(dest));
69 assert(equivalent(dest, dir));
70 }
71 }
72
73 static void create_symlink_dest_cleanup()
74 {
75 scoped_test_env env;
76 const path dir = env.create_dir("dir");
77 const path file = env.create_file("file", 42);
78 const path sym = dir / "link";
79 // The target path has to be normalized to backslashes before creating
80 // the link on windows, otherwise the link isn't dereferencable.
81 const path sym_target = "../file";
82 path sym_target_normalized = sym_target;
83 sym_target_normalized.make_preferred();
84 std::error_code ec;
85 fs::create_symlink(sym_target, sym, ec);
86 assert(!ec);
87 assert(equivalent(sym, file, ec));
88 const path ret = fs::read_symlink(sym, ec);
89 assert(!ec);
90 assert(ret.native() == sym_target_normalized.native());
91 }
92
93 int main(int, char**) {
94 test_signatures();
95 test_error_reporting();
96 create_symlink_basic();
97 create_symlink_dest_cleanup();
98
99 return 0;
100 }