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"
25 namespace fs = std::filesystem;
26 using namespace fs;
27 
test_signatures()28 static void test_signatures()
29 {
30     const path p; ((void)p);
31     std::error_code ec; ((void)ec);
32     ASSERT_NOT_NOEXCEPT(fs::create_symlink(p, p));
33     ASSERT_NOEXCEPT(fs::create_symlink(p, p, ec));
34 }
35 
test_error_reporting()36 static 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(sym, file2, ec);
45         assert(ec);
46     }
47 }
48 
create_symlink_basic()49 static 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(file_sym, dest, 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(dir_sym, dest, ec);
68         assert(!ec);
69         assert(is_symlink(dest));
70         assert(equivalent(dest, dir));
71     }
72 }
73 
create_symlink_dest_cleanup()74 static 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(sym_target, sym, ec);
87     assert(!ec);
88     assert(equivalent(sym, file, ec));
89     const path ret = fs::read_symlink(sym, ec);
90     assert(!ec);
91     assert(ret.native() == sym_target_normalized.native());
92 }
93 
main(int,char **)94 int 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