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-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // UNSUPPORTED: c++98, c++03
10 
11 // <filesystem>
12 
13 // path current_path();
14 // path current_path(error_code& ec);
15 // void current_path(path const&);
16 // void current_path(path const&, std::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_current_path_path_test_suite)
29 
30 TEST_CASE(current_path_signature_test)
31 {
32     const path p; ((void)p);
33     std::error_code ec; ((void)ec);
34     ASSERT_NOT_NOEXCEPT(current_path());
35     ASSERT_NOT_NOEXCEPT(current_path(ec));
36     ASSERT_NOT_NOEXCEPT(current_path(p));
37     ASSERT_NOEXCEPT(current_path(p, ec));
38 }
39 
40 TEST_CASE(current_path_test)
41 {
42     std::error_code ec;
43     const path p = current_path(ec);
44     TEST_REQUIRE(!ec);
45     TEST_CHECK(p.is_absolute());
46     TEST_CHECK(is_directory(p));
47 
48     const path p2 = current_path();
49     TEST_CHECK(p2 == p);
50 }
51 
52 TEST_CASE(current_path_after_change_test)
53 {
54     const path new_path = StaticEnv::Dir;
55     current_path(new_path);
56     TEST_CHECK(current_path() == new_path);
57 }
58 
59 TEST_CASE(current_path_is_file_test)
60 {
61     const path p = StaticEnv::File;
62     std::error_code ec;
63     const path old_p = current_path();
64     current_path(p, ec);
65     TEST_CHECK(ec);
66     TEST_CHECK(old_p == current_path());
67 }
68 
69 TEST_CASE(set_to_non_absolute_path)
70 {
71     const path base = StaticEnv::Dir;
72     current_path(base);
73     const path p = StaticEnv::Dir2.filename();
74     std::error_code ec;
75     current_path(p, ec);
76     TEST_CHECK(!ec);
77     const path new_cwd = current_path();
78     TEST_CHECK(new_cwd == StaticEnv::Dir2);
79     TEST_CHECK(new_cwd.is_absolute());
80 }
81 
82 TEST_CASE(set_to_empty)
83 {
84     const path p = "";
85     std::error_code ec;
86     const path old_p = current_path();
87     current_path(p, ec);
88     TEST_CHECK(ec);
89     TEST_CHECK(old_p == current_path());
90 }
91 
92 TEST_SUITE_END()
93