aboutsummaryrefslogtreecommitdiff
path: root/libcxx/test/std/input.output/filesystems/class.directory_entry/directory_entry.cons/path.pass.cpp
blob: a1a37012369d4cdbbf35a3b8cea11fb64fb8e2d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

// REQUIRES: can-create-symlinks
// UNSUPPORTED: c++03, c++11, c++14

// <filesystem>

// class directory_entry

// explicit directory_entry(const path);
// directory_entry(const path&, error_code& ec);

#include <filesystem>
#include <type_traits>
#include <cassert>

#include "assert_macros.h"
#include "test_macros.h"
#include "filesystem_test_helper.h"
#include "test_convertible.h"
namespace fs = std::filesystem;

static void path_ctor() {
  using namespace fs;
  {
    static_assert(std::is_constructible<directory_entry, const path&>::value,
                  "directory_entry must be constructible from path");
    static_assert(
        !std::is_nothrow_constructible<directory_entry, const path&>::value,
        "directory_entry constructor should not be noexcept");
    static_assert(!std::is_convertible<path const&, directory_entry>::value,
                  "directory_entry constructor should be explicit");
  }
  {
    const path p("foo/bar/baz");
    const directory_entry e(p);
    assert(e.path() == p);
  }
}

static void path_ec_ctor() {
  static_test_env static_env;
  using namespace fs;
  {
    static_assert(
        std::is_constructible<directory_entry, const path&,
                              std::error_code&>::value,
        "directory_entry must be constructible from path and error_code");
    static_assert(!std::is_nothrow_constructible<directory_entry, const path&,
                                                 std::error_code&>::value,
                  "directory_entry constructor should not be noexcept");
    static_assert(
        test_convertible<directory_entry, const path&, std::error_code&>(),
        "directory_entry constructor should not be explicit");
  }
  {
    std::error_code ec = GetTestEC();
    const directory_entry e(static_env.File, ec);
    assert(e.path() == static_env.File);
    assert(!ec);
  }
  {
    const path p("foo/bar/baz");
    std::error_code ec = GetTestEC();
    const directory_entry e(p, ec);
    assert(e.path() == p);
    assert(ErrorIs(ec, std::errc::no_such_file_or_directory));
  }
}

static void path_ctor_calls_refresh() {
  using namespace fs;
  scoped_test_env env;
  const path dir = env.create_dir("dir");
  const path file = env.create_file("dir/file", 42);
  const path sym = env.create_symlink("dir/file", "sym");

  {
    directory_entry ent(file);
    std::error_code ec = GetTestEC();
    directory_entry ent_ec(file, ec);
    assert(!ec);

    LIBCPP_ONLY(remove(file));

    assert(ent.exists());
    assert(ent_ec.exists());

    assert(ent.file_size() == 42);
    assert(ent_ec.file_size() == 42);
  }

  env.create_file("dir/file", 101);

  {
    directory_entry ent(sym);
    std::error_code ec = GetTestEC();
    directory_entry ent_ec(sym, ec);
    assert(!ec);

    LIBCPP_ONLY(remove(file));
    LIBCPP_ONLY(remove(sym));

    assert(ent.is_symlink());
    assert(ent_ec.is_symlink());

    assert(ent.is_regular_file());
    assert(ent_ec.is_regular_file());

    assert(ent.file_size() == 101);
    assert(ent_ec.file_size() == 101);
  }
}

static void path_ctor_dne() {
  using namespace fs;

  static_test_env static_env;

  {
    std::error_code ec = GetTestEC();
    directory_entry ent(static_env.DNE, ec);
    assert(ErrorIs(ec, std::errc::no_such_file_or_directory));
    assert(ent.path() == static_env.DNE);
  }
  // don't report dead symlinks as an error.
  {
    std::error_code ec = GetTestEC();
    directory_entry ent(static_env.BadSymlink, ec);
    assert(!ec);
    assert(ent.path() == static_env.BadSymlink);
  }
  // DNE does not cause the constructor to throw
  {
    directory_entry ent(static_env.DNE);
    assert(ent.path() == static_env.DNE);

    directory_entry ent_two(static_env.BadSymlink);
    assert(ent_two.path() == static_env.BadSymlink);
  }
}

static void path_ctor_cannot_resolve() {
  using namespace fs;
#ifdef _WIN32
  // Windows doesn't support setting perms::none to trigger failures
  // reading directories; test using a special inaccessible directory
  // instead.
  const path dir = GetWindowsInaccessibleDir();
  if (dir.empty())
    return;
  const path file = dir / "file";
  {
    std::error_code ec = GetTestEC();
    directory_entry ent(file, ec);
    assert(ErrorIs(ec, std::errc::no_such_file_or_directory));
    assert(ent.path() == file);
  }
  {
    TEST_DOES_NOT_THROW(directory_entry(file));
  }
#else
  scoped_test_env env;
  const path dir = env.create_dir("dir");
  const path file = env.create_file("dir/file", 42);
  const path file_out_of_dir = env.create_file("file1", 101);
  const path sym_out_of_dir = env.create_symlink("dir/file", "sym");
  const path sym_in_dir = env.create_symlink("dir/file1", "dir/sym2");
  permissions(dir, perms::none);

  {
    std::error_code ec = GetTestEC();
    directory_entry ent(file, ec);
    assert(ErrorIs(ec, std::errc::permission_denied));
    assert(ent.path() == file);
  }
  {
    std::error_code ec = GetTestEC();
    directory_entry ent(sym_in_dir, ec);
    assert(ErrorIs(ec, std::errc::permission_denied));
    assert(ent.path() == sym_in_dir);
  }
  {
    std::error_code ec = GetTestEC();
    directory_entry ent(sym_out_of_dir, ec);
    assert(!ec);
    assert(ent.path() == sym_out_of_dir);
  }
  {
    TEST_DOES_NOT_THROW(directory_entry(file));
    TEST_DOES_NOT_THROW(directory_entry(sym_in_dir));
    TEST_DOES_NOT_THROW(directory_entry(sym_out_of_dir));
  }
#endif
}

int main(int, char**) {
  path_ctor();
  path_ec_ctor();
  path_ctor_calls_refresh();
  path_ctor_dne();
  path_ctor_cannot_resolve();

  return 0;
}