112 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9// REQUIRES: can-create-symlinks10// UNSUPPORTED: c++03, c++11, c++1411// UNSUPPORTED: no-filesystem12 13// <filesystem>14 15// void copy_symlink(const path& existing_symlink, const path& new_symlink);16// void copy_symlink(const path& existing_symlink, const path& new_symlink,17// error_code& ec) noexcept;18 19#include <filesystem>20#include <type_traits>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::copy_symlink(p, p));33 ASSERT_NOEXCEPT(fs::copy_symlink(p, p, ec));34}35 36 37static void test_error_reporting()38{39 auto checkThrow = [](path const& f, path const& t, const std::error_code& ec)40 {41#ifndef TEST_HAS_NO_EXCEPTIONS42 try {43 fs::copy_symlink(f, t);44 return true;45 } catch (filesystem_error const& err) {46 return err.path1() == f47 && err.code() == ec;48 }49#else50 ((void)f); ((void)t); ((void)ec);51 return true;52#endif53 };54 55 scoped_test_env env;56 const path file = env.create_file("file1", 42);57 const path file2 = env.create_file("file2", 55);58 const path sym = env.create_symlink(file, "sym");59 const path dir = env.create_dir("dir");60 const path dne = env.make_env_path("dne");61 { // from is a file, not a symlink62 std::error_code ec;63 fs::copy_symlink(file, dne, ec);64 assert(ec);65 assert(checkThrow(file, dne, ec));66 }67 { // from is a file, not a symlink68 std::error_code ec;69 fs::copy_symlink(dir, dne, ec);70 assert(ec);71 assert(checkThrow(dir, dne, ec));72 }73 { // destination exists74 std::error_code ec;75 fs::copy_symlink(sym, file2, ec);76 assert(ec);77 }78}79 80static void copy_symlink_basic()81{82 scoped_test_env env;83 const path dir = env.create_dir("dir");84 const path dir_sym = env.create_directory_symlink(dir, "dir_sym");85 const path file = env.create_file("file", 42);86 const path file_sym = env.create_symlink(file, "file_sym");87 { // test for directory symlinks88 const path dest = env.make_env_path("dest1");89 std::error_code ec;90 fs::copy_symlink(dir_sym, dest, ec);91 assert(!ec);92 assert(is_symlink(dest));93 assert(equivalent(dest, dir));94 }95 { // test for file symlinks96 const path dest = env.make_env_path("dest2");97 std::error_code ec;98 fs::copy_symlink(file_sym, dest, ec);99 assert(!ec);100 assert(is_symlink(dest));101 assert(equivalent(dest, file));102 }103}104 105int main(int, char**) {106 test_signatures();107 test_error_reporting();108 copy_symlink_basic();109 110 return 0;111}112