Home | History | Annotate | Download | only in fs.op.copy_symlink
      1 //===----------------------------------------------------------------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is dual licensed under the MIT and the University of Illinois Open
      6 // Source Licenses. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 
     10 // UNSUPPORTED: c++98, c++03
     11 
     12 // <filesystem>
     13 
     14 // void copy_symlink(const path& existing_symlink, const path& new_symlink);
     15 // void copy_symlink(const path& existing_symlink, const path& new_symlink,
     16 //                   error_code& ec) noexcept;
     17 
     18 #include "filesystem_include.hpp"
     19 #include <type_traits>
     20 #include <cassert>
     21 
     22 #include "test_macros.h"
     23 #include "rapid-cxx-test.hpp"
     24 #include "filesystem_test_helper.hpp"
     25 
     26 using namespace fs;
     27 
     28 TEST_SUITE(filesystem_copy_symlink_test_suite)
     29 
     30 TEST_CASE(test_signatures)
     31 {
     32     const path p; ((void)p);
     33     std::error_code ec; ((void)ec);
     34     ASSERT_NOT_NOEXCEPT(fs::copy_symlink(p, p));
     35     ASSERT_NOEXCEPT(fs::copy_symlink(p, p, ec));
     36 }
     37 
     38 
     39 TEST_CASE(test_error_reporting)
     40 {
     41     auto checkThrow = [](path const& f, path const& t, const std::error_code& ec)
     42     {
     43 #ifndef TEST_HAS_NO_EXCEPTIONS
     44         try {
     45             fs::copy_symlink(f, t);
     46             return true;
     47         } catch (filesystem_error const& err) {
     48             return err.path1() == f
     49                 && err.code() == ec;
     50         }
     51 #else
     52         ((void)f); ((void)t); ((void)ec);
     53         return true;
     54 #endif
     55     };
     56 
     57     scoped_test_env env;
     58     const path file = env.create_file("file1", 42);
     59     const path file2 = env.create_file("file2", 55);
     60     const path sym = env.create_symlink(file, "sym");
     61     const path dir = env.create_dir("dir");
     62     const path dne = env.make_env_path("dne");
     63     { // from is a file, not a symlink
     64         std::error_code ec;
     65         fs::copy_symlink(file, dne, ec);
     66         TEST_REQUIRE(ec);
     67         TEST_CHECK(checkThrow(file, dne, ec));
     68     }
     69     { // from is a file, not a symlink
     70         std::error_code ec;
     71         fs::copy_symlink(dir, dne, ec);
     72         TEST_REQUIRE(ec);
     73         TEST_CHECK(checkThrow(dir, dne, ec));
     74     }
     75     { // destination exists
     76         std::error_code ec;
     77         fs::copy_symlink(sym, file2, ec);
     78         TEST_REQUIRE(ec);
     79     }
     80 }
     81 
     82 TEST_CASE(copy_symlink_basic)
     83 {
     84     scoped_test_env env;
     85     const path dir = env.create_dir("dir");
     86     const path dir_sym = env.create_symlink(dir, "dir_sym");
     87     const path file = env.create_file("file", 42);
     88     const path file_sym = env.create_symlink(file, "file_sym");
     89     { // test for directory symlinks
     90         const path dest = env.make_env_path("dest1");
     91         std::error_code ec;
     92         fs::copy_symlink(dir_sym, dest, ec);
     93         TEST_REQUIRE(!ec);
     94         TEST_CHECK(is_symlink(dest));
     95         TEST_CHECK(equivalent(dest, dir));
     96     }
     97     { // test for file symlinks
     98         const path dest = env.make_env_path("dest2");
     99         std::error_code ec;
    100         fs::copy_symlink(file_sym, dest, ec);
    101         TEST_REQUIRE(!ec);
    102         TEST_CHECK(is_symlink(dest));
    103         TEST_CHECK(equivalent(dest, file));
    104     }
    105 }
    106 
    107 
    108 TEST_SUITE_END()
    109