| 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 |
// bool is_directory(file_status s) noexcept |
| 17 |
// bool is_directory(path const& p); |
| 18 |
// bool is_directory(path const& p, std::error_code& ec) noexcept; |
| 19 |
|
| 20 |
#include <filesystem> |
| 21 |
#include <type_traits> |
| 22 |
#include <cassert> |
| 23 |
|
| 24 |
#include "assert_macros.h" |
| 25 |
#include "test_macros.h" |
| 26 |
#include "filesystem_test_helper.h" |
| 27 |
namespace fs = std::filesystem; |
| 28 |
using namespace fs; |
| 29 |
|
| 30 |
static void signature_test() |
| 31 |
{ |
| 32 |
file_status s; ((void)s); |
| 33 |
const path p; ((void)p); |
| 34 |
std::error_code ec; ((void)ec); |
| 35 |
ASSERT_NOEXCEPT(is_directory(s)); |
| 36 |
ASSERT_NOEXCEPT(is_directory(p, ec)); |
| 37 |
ASSERT_NOT_NOEXCEPT(is_directory(p)); |
| 38 |
} |
| 39 |
|
| 40 |
static void is_directory_status_test() |
| 41 |
{ |
| 42 |
struct TestCase { |
| 43 |
file_type type; |
| 44 |
bool expect; |
| 45 |
}; |
| 46 |
const TestCase testCases[] = { |
| 47 |
{file_type::none, false}, |
| 48 |
{file_type::not_found, false}, |
| 49 |
{file_type::regular, false}, |
| 50 |
{file_type::directory, true}, |
| 51 |
{file_type::symlink, false}, |
| 52 |
{file_type::block, false}, |
| 53 |
{file_type::character, false}, |
| 54 |
{file_type::fifo, false}, |
| 55 |
{file_type::socket, false}, |
| 56 |
{file_type::unknown, false} |
| 57 |
}; |
| 58 |
for (auto& TC : testCases) { |
| 59 |
file_status s(TC.type); |
| 60 |
assert(is_directory(s) == TC.expect); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
static void test_exist_not_found() |
| 65 |
{ |
| 66 |
static_test_env static_env; |
| 67 |
const path p = static_env.DNE; |
| 68 |
assert(is_directory(p) == false); |
| 69 |
} |
| 70 |
|
| 71 |
static void static_env_test() |
| 72 |
{ |
| 73 |
static_test_env static_env; |
| 74 |
assert(is_directory(static_env.Dir)); |
| 75 |
assert(is_directory(static_env.SymlinkToDir)); |
| 76 |
assert(!is_directory(static_env.File)); |
| 77 |
} |
| 78 |
|
| 79 |
static void test_is_directory_fails() |
| 80 |
{ |
| 81 |
scoped_test_env env; |
| 82 |
#ifdef _WIN32 |
| 83 |
// Windows doesn't support setting perms::none to trigger failures |
| 84 |
// reading directories; test using a special inaccessible directory |
| 85 |
// instead. |
| 86 |
const path p = GetWindowsInaccessibleDir(); |
| 87 |
if (p.empty()) |
| 88 |
return; |
| 89 |
#else |
| 90 |
const path dir = env.create_dir("dir"); |
| 91 |
const path p = env.create_dir("dir/dir2"); |
| 92 |
permissions(dir, perms::none); |
| 93 |
#endif |
| 94 |
|
| 95 |
std::error_code ec; |
| 96 |
assert(is_directory(p, ec) == false); |
| 97 |
assert(ec); |
| 98 |
|
| 99 |
TEST_THROWS_TYPE(filesystem_error, is_directory(p)); |
| 100 |
} |
| 101 |
|
| 102 |
int main(int, char**) { |
| 103 |
signature_test(); |
| 104 |
is_directory_status_test(); |
| 105 |
test_exist_not_found(); |
| 106 |
static_env_test(); |
| 107 |
test_is_directory_fails(); |
| 108 |
|
| 109 |
return 0; |
| 110 |
} |