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