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