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