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// UNSUPPORTED: availability-filesystem-missing
13
14// <filesystem>
15
16// void create_symlink(const path& existing_symlink, const path& new_symlink);
17// void create_symlink(const path& existing_symlink, const path& new_symlink,
18// error_code& ec) noexcept;
19
20#include <filesystem>
21#include <cassert>
22
23#include "test_macros.h"
24#include "filesystem_test_helper.h"
25namespace fs = std::filesystem;
26using namespace fs;
27
28static void test_signatures()
29{
30 const path p; ((void)p);
31 std::error_code ec; ((void)ec);
32 ASSERT_NOT_NOEXCEPT(fs::create_symlink(to: p, new_symlink: p));
33 ASSERT_NOEXCEPT(fs::create_symlink(to: p, new_symlink: p, ec&: ec));
34}
35
36static void test_error_reporting()
37{
38 scoped_test_env env;
39 const path file = env.create_file("file1", 42);
40 const path file2 = env.create_file("file2", 55);
41 const path sym = env.create_symlink(file, "sym");
42 { // destination exists
43 std::error_code ec;
44 fs::create_symlink(to: sym, new_symlink: file2, ec&: ec);
45 assert(ec);
46 }
47}
48
49static void create_symlink_basic()
50{
51 scoped_test_env env;
52 const path file = env.create_file("file", 42);
53 const path file_sym = env.create_symlink(file, "file_sym");
54 const path dir = env.create_dir("dir");
55 const path dir_sym = env.create_directory_symlink(dir, "dir_sym");
56 {
57 const path dest = env.make_env_path("dest1");
58 std::error_code ec;
59 fs::create_symlink(to: file_sym, new_symlink: dest, ec&: ec);
60 assert(!ec);
61 assert(is_symlink(dest));
62 assert(equivalent(dest, file));
63 }
64 {
65 const path dest = env.make_env_path("dest2");
66 std::error_code ec;
67 fs::create_directory_symlink(to: dir_sym, new_symlink: dest, ec&: ec);
68 assert(!ec);
69 assert(is_symlink(dest));
70 assert(equivalent(dest, dir));
71 }
72}
73
74static void create_symlink_dest_cleanup()
75{
76 scoped_test_env env;
77 const path dir = env.create_dir("dir");
78 const path file = env.create_file("file", 42);
79 const path sym = dir / "link";
80 // The target path has to be normalized to backslashes before creating
81 // the link on windows, otherwise the link isn't dereferencable.
82 const path sym_target = "../file";
83 path sym_target_normalized = sym_target;
84 sym_target_normalized.make_preferred();
85 std::error_code ec;
86 fs::create_symlink(to: sym_target, new_symlink: sym, ec&: ec);
87 assert(!ec);
88 assert(equivalent(sym, file, ec));
89 const path ret = fs::read_symlink(p: sym, ec&: ec);
90 assert(!ec);
91 assert(ret.native() == sym_target_normalized.native());
92}
93
94int main(int, char**) {
95 test_signatures();
96 test_error_reporting();
97 create_symlink_basic();
98 create_symlink_dest_cleanup();
99
100 return 0;
101}
102

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