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++03
10 // UNSUPPORTED: no-filesystem
11 // UNSUPPORTED: availability-filesystem-missing
12 
13 // <filesystem>
14 
15 // path current_path();
16 // path current_path(error_code& ec);
17 // void current_path(path const&);
18 // void current_path(path const&, std::error_code& ec) noexcept;
19 
20 #include "filesystem_include.h"
21 #include <type_traits>
22 #include <cassert>
23 
24 #include "test_macros.h"
25 #include "filesystem_test_helper.h"
26 
27 using namespace fs;
28 
29 static void current_path_signature_test()
30 {
31     const path p; ((void)p);
32     std::error_code ec; ((void)ec);
33     ASSERT_NOT_NOEXCEPT(current_path());
34     ASSERT_NOT_NOEXCEPT(current_path(ec));
35     ASSERT_NOT_NOEXCEPT(current_path(p));
36     ASSERT_NOEXCEPT(current_path(p, ec));
37 }
38 
39 static void current_path_test()
40 {
41     std::error_code ec;
42     const path p = current_path(ec);
43     assert(!ec);
44     assert(p.is_absolute());
45     assert(is_directory(p));
46 
47     const path p2 = current_path();
48     assert(p2 == p);
49 }
50 
51 static void current_path_after_change_test()
52 {
53     static_test_env static_env;
54     CWDGuard guard;
55     const path new_path = static_env.Dir;
56     current_path(new_path);
57     assert(current_path() == new_path);
58 }
59 
60 static void current_path_is_file_test()
61 {
62     static_test_env static_env;
63     CWDGuard guard;
64     const path p = static_env.File;
65     std::error_code ec;
66     const path old_p = current_path();
67     current_path(p, ec);
68     assert(ec);
69     assert(old_p == current_path());
70 }
71 
72 static void set_to_non_absolute_path()
73 {
74     static_test_env static_env;
75     CWDGuard guard;
76     const path base = static_env.Dir;
77     current_path(base);
78     const path p = static_env.Dir2.filename();
79     std::error_code ec;
80     current_path(p, ec);
81     assert(!ec);
82     const path new_cwd = current_path();
83     assert(new_cwd == static_env.Dir2);
84     assert(new_cwd.is_absolute());
85 }
86 
87 static void set_to_empty()
88 {
89     const path p = "";
90     std::error_code ec;
91     const path old_p = current_path();
92     current_path(p, ec);
93     assert(ec);
94     assert(old_p == current_path());
95 }
96 
97 int main(int, char**) {
98     current_path_signature_test();
99     current_path_test();
100     current_path_after_change_test();
101     current_path_is_file_test();
102     set_to_non_absolute_path();
103     set_to_empty();
104 
105     return 0;
106 }
107