Home | History | Annotate | Download | only in fs.op.read_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 // <experimental/filesystem>
     13 
     14 // path read_symlink(const path& p);
     15 // path read_symlink(const path& p, error_code& ec);
     16 
     17 #include <experimental/filesystem>
     18 
     19 #include "test_macros.h"
     20 #include "rapid-cxx-test.hpp"
     21 #include "filesystem_test_helper.hpp"
     22 
     23 using namespace std::experimental::filesystem;
     24 namespace fs = std::experimental::filesystem;
     25 
     26 TEST_SUITE(filesystem_read_symlink_test_suite)
     27 
     28 TEST_CASE(test_signatures)
     29 {
     30     const path p; ((void)p);
     31     std::error_code ec; ((void)ec);
     32     ASSERT_SAME_TYPE(decltype(fs::read_symlink(p)), fs::path);
     33     ASSERT_SAME_TYPE(decltype(fs::read_symlink(p, ec)), fs::path);
     34 
     35     ASSERT_NOT_NOEXCEPT(fs::read_symlink(p));
     36     // Not noexcept because of narrow contract
     37     ASSERT_NOT_NOEXCEPT(fs::read_symlink(p, ec));
     38 }
     39 
     40 TEST_CASE(test_error_reporting)
     41 {
     42     auto checkThrow = [](path const& f, const std::error_code& ec)
     43     {
     44 #ifndef TEST_HAS_NO_EXCEPTIONS
     45         try {
     46             fs::read_symlink(f);
     47             return false;
     48         } catch (filesystem_error const& err) {
     49             return err.path1() == f
     50                 && err.path2() == ""
     51                 && err.code() == ec;
     52         }
     53 #else
     54         ((void)f); ((void)ec);
     55         return true;
     56 #endif
     57     };
     58 
     59     scoped_test_env env;
     60     const path cases[] = {
     61         env.make_env_path("dne"),
     62         env.create_file("file", 42),
     63         env.create_dir("dir")
     64     };
     65     for (path const& p : cases) {
     66         std::error_code ec;
     67         const path ret = fs::read_symlink(p, ec);
     68         TEST_REQUIRE(ec);
     69         TEST_CHECK(ret == path{});
     70         TEST_CHECK(checkThrow(p, ec));
     71     }
     72 
     73 }
     74 
     75 TEST_CASE(basic_symlink_test)
     76 {
     77     scoped_test_env env;
     78     const path dne = env.make_env_path("dne");
     79     const path file = env.create_file("file", 42);
     80     const path dir = env.create_dir("dir");
     81     const path link = env.create_symlink(dne, "link");
     82     const path nested_link = env.make_env_path("nested_link");
     83     create_symlink(link, nested_link);
     84     struct TestCase {
     85       path symlink;
     86       path expected;
     87     } testCases[] = {
     88         {env.create_symlink(dne, "dne_link"), dne},
     89         {env.create_symlink(file, "file_link"), file},
     90         {env.create_symlink(dir, "dir_link"), dir},
     91         {nested_link, link}
     92     };
     93     for (auto& TC : testCases) {
     94         std::error_code ec = std::make_error_code(std::errc::address_in_use);
     95         const path ret = read_symlink(TC.symlink, ec);
     96         TEST_CHECK(!ec);
     97         TEST_CHECK(ret == TC.expected);
     98     }
     99 }
    100 
    101 TEST_SUITE_END()
    102