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