| 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 |
|
| 13 |
// This test requires the dylib support introduced in e4ed349c7658. |
| 14 |
// XFAIL: using-built-library-before-llvm-12 |
| 15 |
|
| 16 |
// <filesystem> |
| 17 |
|
| 18 |
// bool create_directories(const path& p); |
| 19 |
// bool create_directories(const path& p, error_code& ec) noexcept; |
| 20 |
|
| 21 |
#include <filesystem> |
| 22 |
#include <type_traits> |
| 23 |
#include <cassert> |
| 24 |
|
| 25 |
#include "assert_macros.h" |
| 26 |
#include "test_macros.h" |
| 27 |
#include "filesystem_test_helper.h" |
| 28 |
namespace fs = std::filesystem; |
| 29 |
using namespace fs; |
| 30 |
|
| 31 |
static void test_signatures() |
| 32 |
{ |
| 33 |
const path p; ((void)p); |
| 34 |
std::error_code ec; ((void)ec); |
| 35 |
ASSERT_SAME_TYPE(decltype(fs::create_directories(p)), bool); |
| 36 |
ASSERT_SAME_TYPE(decltype(fs::create_directories(p, ec)), bool); |
| 37 |
ASSERT_NOT_NOEXCEPT(fs::create_directories(p)); |
| 38 |
ASSERT_NOT_NOEXCEPT(fs::create_directories(p, ec)); |
| 39 |
} |
| 40 |
|
| 41 |
static void create_existing_directory() |
| 42 |
{ |
| 43 |
scoped_test_env env; |
| 44 |
const path dir = env.create_dir("dir1"); |
| 45 |
std::error_code ec; |
| 46 |
assert(fs::create_directories(dir, ec) == false); |
| 47 |
assert(!ec); |
| 48 |
assert(is_directory(dir)); |
| 49 |
} |
| 50 |
|
| 51 |
static void create_directory_one_level() |
| 52 |
{ |
| 53 |
scoped_test_env env; |
| 54 |
const path dir = env.make_env_path("dir1"); |
| 55 |
std::error_code ec; |
| 56 |
assert(fs::create_directories(dir, ec) == true); |
| 57 |
assert(!ec); |
| 58 |
assert(is_directory(dir)); |
| 59 |
} |
| 60 |
|
| 61 |
static void create_directories_multi_level() |
| 62 |
{ |
| 63 |
scoped_test_env env; |
| 64 |
const path dir = env.make_env_path("dir1/dir2/dir3"); |
| 65 |
std::error_code ec; |
| 66 |
assert(fs::create_directories(dir, ec) == true); |
| 67 |
assert(!ec); |
| 68 |
assert(is_directory(dir)); |
| 69 |
} |
| 70 |
|
| 71 |
static void create_directory_symlinks() { |
| 72 |
scoped_test_env env; |
| 73 |
const path root = env.create_dir("dir"); |
| 74 |
const path sym_dest_dead = env.make_env_path("dead"); |
| 75 |
const path dead_sym = env.create_directory_symlink(sym_dest_dead, "dir/sym_dir"); |
| 76 |
const path target = env.make_env_path("dir/sym_dir/foo"); |
| 77 |
{ |
| 78 |
std::error_code ec = GetTestEC(); |
| 79 |
assert(create_directories(target, ec) == false); |
| 80 |
assert(ec); |
| 81 |
assert(ErrorIs(ec, std::errc::file_exists)); |
| 82 |
assert(!exists(sym_dest_dead)); |
| 83 |
assert(!exists(dead_sym)); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
static void create_directory_through_symlinks() { |
| 88 |
scoped_test_env env; |
| 89 |
const path root = env.create_dir("dir"); |
| 90 |
const path sym_dir = env.create_directory_symlink(root, "sym_dir"); |
| 91 |
const path target = env.make_env_path("sym_dir/foo"); |
| 92 |
const path resolved_target = env.make_env_path("dir/foo"); |
| 93 |
assert(is_directory(sym_dir)); |
| 94 |
{ |
| 95 |
std::error_code ec = GetTestEC(); |
| 96 |
assert(create_directories(target, ec) == true); |
| 97 |
assert(!ec); |
| 98 |
assert(is_directory(target)); |
| 99 |
assert(is_directory(resolved_target)); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
static void dest_is_file() |
| 104 |
{ |
| 105 |
scoped_test_env env; |
| 106 |
const path file = env.create_file("file", 42); |
| 107 |
std::error_code ec = GetTestEC(); |
| 108 |
assert(fs::create_directories(file, ec) == false); |
| 109 |
assert(ec); |
| 110 |
assert(ErrorIs(ec, std::errc::file_exists)); |
| 111 |
assert(is_regular_file(file)); |
| 112 |
} |
| 113 |
|
| 114 |
static void dest_part_is_file() |
| 115 |
{ |
| 116 |
scoped_test_env env; |
| 117 |
const path file = env.create_file("file"); |
| 118 |
const path dir = env.make_env_path("file/dir1"); |
| 119 |
std::error_code ec = GetTestEC(); |
| 120 |
assert(fs::create_directories(dir, ec) == false); |
| 121 |
assert(ec); |
| 122 |
assert(ErrorIs(ec, std::errc::not_a_directory)); |
| 123 |
assert(is_regular_file(file)); |
| 124 |
assert(!exists(dir)); |
| 125 |
} |
| 126 |
|
| 127 |
static void dest_final_part_is_file() |
| 128 |
{ |
| 129 |
scoped_test_env env; |
| 130 |
env.create_dir("dir"); |
| 131 |
const path file = env.create_file("dir/file"); |
| 132 |
const path dir = env.make_env_path("dir/file/dir1"); |
| 133 |
std::error_code ec = GetTestEC(); |
| 134 |
assert(fs::create_directories(dir, ec) == false); |
| 135 |
assert(ec); |
| 136 |
assert(ErrorIs(ec, std::errc::not_a_directory)); |
| 137 |
assert(is_regular_file(file)); |
| 138 |
assert(!exists(dir)); |
| 139 |
} |
| 140 |
|
| 141 |
static void dest_is_empty_path() |
| 142 |
{ |
| 143 |
std::error_code ec = GetTestEC(); |
| 144 |
assert(fs::create_directories(fs::path{}, ec) == false); |
| 145 |
assert(ec); |
| 146 |
assert(ErrorIs(ec, std::errc::no_such_file_or_directory)); |
| 147 |
ExceptionChecker Checker(path{}, std::errc::no_such_file_or_directory, |
| 148 |
"create_directories"); |
| 149 |
TEST_VALIDATE_EXCEPTION(filesystem_error, Checker, |
| 150 |
fs::create_directories(path{})); |
| 151 |
} |
| 152 |
|
| 153 |
#ifdef _WIN32 |
| 154 |
static void nonexistent_root() |
| 155 |
{ |
| 156 |
std::error_code ec = GetTestEC(); |
| 157 |
// If Q:\ doesn't exist, create_directories would try to recurse upwards |
| 158 |
// to parent_path() until it finds a directory that does exist. As the |
| 159 |
// whole path is the root name, parent_path() returns itself, and it |
| 160 |
// would recurse indefinitely, unless the recursion is broken. |
| 161 |
if (!exists("Q:\\")) |
| 162 |
assert(fs::create_directories("Q:\\", ec) == false); |
| 163 |
assert(fs::create_directories("\\\\nonexistentserver", ec) == false); |
| 164 |
} |
| 165 |
#endif // _WIN32 |
| 166 |
|
| 167 |
int main(int, char**) { |
| 168 |
test_signatures(); |
| 169 |
create_existing_directory(); |
| 170 |
create_directory_one_level(); |
| 171 |
create_directories_multi_level(); |
| 172 |
create_directory_symlinks(); |
| 173 |
create_directory_through_symlinks(); |
| 174 |
dest_is_file(); |
| 175 |
dest_part_is_file(); |
| 176 |
dest_final_part_is_file(); |
| 177 |
dest_is_empty_path(); |
| 178 |
#ifdef _WIN32 |
| 179 |
nonexistent_root(); |
| 180 |
#endif |
| 181 |
|
| 182 |
return 0; |
| 183 |
} |