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 canonical(const path& p, const path& base = current_path());
15 // path canonical(const path& p, error_code& ec);
16 // path canonical(const path& p, const path& base, error_code& ec);
17 
18 #include <experimental/filesystem>
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 std::experimental::filesystem;
27 
28 TEST_SUITE(filesystem_canonical_path_test_suite)
29 
TEST_CASE(signature_test)30 TEST_CASE(signature_test)
31 {
32     const path p; ((void)p);
33     std::error_code ec; ((void)ec);
34     ASSERT_NOT_NOEXCEPT(canonical(p));
35     ASSERT_NOT_NOEXCEPT(canonical(p, p));
36     ASSERT_NOT_NOEXCEPT(canonical(p, ec));
37     ASSERT_NOT_NOEXCEPT(canonical(p, p, ec));
38 }
39 
40 // There are 4 cases is the proposal for absolute path.
41 // Each scope tests one of the cases.
TEST_CASE(test_canonical)42 TEST_CASE(test_canonical)
43 {
44     // has_root_name() && has_root_directory()
45     const path Root = StaticEnv::Root;
46     const path RootName = Root.filename();
47     const path DirName = StaticEnv::Dir.filename();
48     const path SymlinkName = StaticEnv::SymlinkToFile.filename();
49     struct TestCase {
50         path p;
51         path expect;
52         path base;
53         TestCase(path p1, path e, path b = StaticEnv::Root)
54             : p(p1), expect(e), base(b) {}
55     };
56     const TestCase testCases[] = {
57         { ".", Root, Root},
58         { DirName / ".." / "." / DirName, StaticEnv::Dir, Root},
59         { StaticEnv::Dir2 / "..",    StaticEnv::Dir },
60         { StaticEnv::Dir3 / "../..", StaticEnv::Dir },
61         { StaticEnv::Dir / ".",      StaticEnv::Dir },
62         { Root / "." / DirName / ".." / DirName, StaticEnv::Dir},
63         { path("..") / "." / RootName / DirName / ".." / DirName, StaticEnv::Dir, Root},
64         { StaticEnv::SymlinkToFile,  StaticEnv::File },
65         { SymlinkName, StaticEnv::File, StaticEnv::Root}
66     };
67     for (auto& TC : testCases) {
68         std::error_code ec;
69         const path ret = canonical(TC.p, TC.base, ec);
70         TEST_REQUIRE(!ec);
71         const path ret2 = canonical(TC.p, TC.base);
72         TEST_CHECK(ret == TC.expect);
73         TEST_CHECK(ret == ret2);
74         TEST_CHECK(ret.is_absolute());
75     }
76 }
77 
TEST_CASE(test_dne_path)78 TEST_CASE(test_dne_path)
79 {
80     std::error_code ec;
81     {
82         const path ret = canonical(StaticEnv::DNE, ec);
83         TEST_REQUIRE(ec);
84         TEST_CHECK(ret == path{});
85     }
86     ec.clear();
87     {
88         const path ret = canonical(StaticEnv::DNE, StaticEnv::Root, ec);
89         TEST_REQUIRE(ec);
90         TEST_CHECK(ret == path{});
91     }
92     {
93         TEST_CHECK_THROW(filesystem_error, canonical(StaticEnv::DNE));
94         TEST_CHECK_THROW(filesystem_error, canonical(StaticEnv::DNE, StaticEnv::Root));
95     }
96 }
97 
TEST_CASE(test_exception_contains_paths)98 TEST_CASE(test_exception_contains_paths)
99 {
100 #ifndef TEST_HAS_NO_EXCEPTIONS
101     const path p = "blabla/dne";
102     const path base = StaticEnv::Root;
103     try {
104         canonical(p, base);
105         TEST_REQUIRE(false);
106     } catch (filesystem_error const& err) {
107         TEST_CHECK(err.path1() == p);
108         TEST_CHECK(err.path2() == base);
109     }
110     try {
111         canonical(p);
112         TEST_REQUIRE(false);
113     } catch (filesystem_error const& err) {
114         TEST_CHECK(err.path1() == p);
115         TEST_CHECK(err.path2() == current_path());
116     }
117 #endif
118 }
119 
120 TEST_SUITE_END()
121