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 
11 // XFAIL: LIBCXX-AIX-FIXME
12 
13 // <filesystem>
14 
15 // bool is_empty(path const& p);
16 // bool is_empty(path const& p, std::error_code& ec);
17 
18 #include "filesystem_include.h"
19 #include <type_traits>
20 #include <cassert>
21 
22 #include "test_macros.h"
23 #include "rapid-cxx-test.h"
24 #include "filesystem_test_helper.h"
25 
26 using namespace fs;
27 
28 TEST_SUITE(is_empty_test_suite)
29 
30 TEST_CASE(signature_test)
31 {
32     const path p; ((void)p);
33     std::error_code ec; ((void)ec);
34     ASSERT_NOT_NOEXCEPT(is_empty(p, ec));
35     ASSERT_NOT_NOEXCEPT(is_empty(p));
36 }
37 
38 TEST_CASE(test_exist_not_found)
39 {
40     static_test_env static_env;
41     const path p = static_env.DNE;
42     std::error_code ec;
43     TEST_CHECK(is_empty(p, ec) == false);
44     TEST_CHECK(ec);
45     TEST_CHECK_THROW(filesystem_error, is_empty(p));
46 }
47 
48 TEST_CASE(test_is_empty_directory)
49 {
50     static_test_env static_env;
51     TEST_CHECK(!is_empty(static_env.Dir));
52     TEST_CHECK(!is_empty(static_env.SymlinkToDir));
53 }
54 
55 TEST_CASE(test_is_empty_directory_dynamic)
56 {
57     scoped_test_env env;
58     TEST_CHECK(is_empty(env.test_root));
59     env.create_file("foo", 42);
60     TEST_CHECK(!is_empty(env.test_root));
61 }
62 
63 TEST_CASE(test_is_empty_file)
64 {
65     static_test_env static_env;
66     TEST_CHECK(is_empty(static_env.EmptyFile));
67     TEST_CHECK(!is_empty(static_env.NonEmptyFile));
68 }
69 
70 TEST_CASE(test_is_empty_fails)
71 {
72     scoped_test_env env;
73 #ifdef _WIN32
74     // Windows doesn't support setting perms::none to trigger failures
75     // reading directories; test using a special inaccessible directory
76     // instead.
77     const path p = GetWindowsInaccessibleDir();
78     if (p.empty())
79         TEST_UNSUPPORTED();
80 #else
81     const path dir = env.create_dir("dir");
82     const path p = env.create_dir("dir/dir2");
83     permissions(dir, perms::none);
84 #endif
85 
86     std::error_code ec;
87     TEST_CHECK(is_empty(p, ec) == false);
88     TEST_CHECK(ec);
89 
90     TEST_CHECK_THROW(filesystem_error, is_empty(p));
91 }
92 
93 TEST_CASE(test_directory_access_denied)
94 {
95     scoped_test_env env;
96 #ifdef _WIN32
97     // Windows doesn't support setting perms::none to trigger failures
98     // reading directories; test using a special inaccessible directory
99     // instead.
100     const path dir = GetWindowsInaccessibleDir();
101     if (dir.empty())
102         TEST_UNSUPPORTED();
103 #else
104     const path dir = env.create_dir("dir");
105     const path file1 = env.create_file("dir/file", 42);
106     permissions(dir, perms::none);
107 #endif
108 
109     std::error_code ec = GetTestEC();
110     TEST_CHECK(is_empty(dir, ec) == false);
111     TEST_CHECK(ec);
112     TEST_CHECK(ec != GetTestEC());
113 
114     TEST_CHECK_THROW(filesystem_error, is_empty(dir));
115 }
116 
117 
118 #ifndef _WIN32
119 TEST_CASE(test_fifo_fails)
120 {
121     scoped_test_env env;
122     const path fifo = env.create_fifo("fifo");
123 
124     std::error_code ec = GetTestEC();
125     TEST_CHECK(is_empty(fifo, ec) == false);
126     TEST_CHECK(ec);
127     TEST_CHECK(ec != GetTestEC());
128 
129     TEST_CHECK_THROW(filesystem_error, is_empty(fifo));
130 }
131 #endif
132 
133 TEST_SUITE_END()
134