• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

gulrak / filesystem / 29163892666

11 Jul 2026 06:40PM UTC coverage: 95.099%. Remained the same
29163892666

push

github

gulrak
Add support for device UNC paths in `canonical()` (#171)

3570 of 3754 relevant lines covered (95.1%)

409.94 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.03
/test/filesystem_test.cpp
1
//---------------------------------------------------------------------------------------
2
//
3
// Copyright (c) 2018, Steffen Schümann <s.schuemann@pobox.com>
4
//
5
// Permission is hereby granted, free of charge, to any person obtaining a copy
6
// of this software and associated documentation files (the "Software"), to deal
7
// in the Software without restriction, including without limitation the rights
8
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
// copies of the Software, and to permit persons to whom the Software is
10
// furnished to do so, subject to the following conditions:
11
//
12
// The above copyright notice and this permission notice shall be included in all
13
// copies or substantial portions of the Software.
14
//
15
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
// SOFTWARE.
22
//
23
//---------------------------------------------------------------------------------------
24
#include <algorithm>
25
#include <cstdio>
26
#include <cstring>
27
#include <fstream>
28
#include <functional>
29
#include <iomanip>
30
#include <iostream>
31
#include <map>
32
#include <random>
33
#include <set>
34
#include <sstream>
35
#include <thread>
36

37
#if (defined(WIN32) || defined(_WIN32)) && !defined(__GNUC__)
38
#define NOMINMAX 1
39
#endif
40

41
#ifdef USE_STD_FS
42
#include <filesystem>
43
namespace fs {
44
using namespace std::filesystem;
45
using ifstream = std::ifstream;
46
using ofstream = std::ofstream;
47
using fstream = std::fstream;
48
}  // namespace fs
49
#ifdef __GNUC__
50
#define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
51
#endif
52
#ifdef _MSC_VER
53
#define IS_WCHAR_PATH
54
#endif
55
#ifdef WIN32
56
#define GHC_OS_WINDOWS
57
#endif
58
#else
59
#ifdef GHC_FILESYSTEM_FWD_TEST
60
#include <ghc/fs_fwd.hpp>
61
#else
62
#include <ghc/filesystem.hpp>
63
#endif
64
namespace fs {
65
using namespace ghc::filesystem;
66
using ifstream = ghc::filesystem::ifstream;
67
using ofstream = ghc::filesystem::ofstream;
68
using fstream = ghc::filesystem::fstream;
69
}  // namespace fs
70
#endif
71

72
#if defined(WIN32) || defined(_WIN32)
73
#include <windows.h>
74
#else
75
#include <sys/socket.h>
76
#include <sys/stat.h>
77
#include <sys/types.h>
78
#include <sys/un.h>
79
#include <unistd.h>
80
#endif
81

82
#ifndef GHC_FILESYSTEM_FWD_TEST
83
#define CATCH_CONFIG_MAIN
84
#endif
85
#define CATCH_CONFIG_NO_COUNTER
86
#include "catch.hpp"
87

88
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
89
// Behaviour Switches (should match the config in ghc/filesystem.hpp):
90
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
91
// LWG #2682 disables the since then invalid use of the copy option create_symlinks on directories
92
#define TEST_LWG_2682_BEHAVIOUR
93
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
94
// LWG #2395 makes crate_directory/create_directories not emit an error if there is a regular
95
// file with that name, it is superceded by P1164R1, so only activate if really needed
96
// #define TEST_LWG_2935_BEHAVIOUR
97
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
98
// LWG #2937 enforces that fs::equivalent emits an error, if !fs::exists(p1)||!exists(p2)
99
#define TEST_LWG_2937_BEHAVIOUR
100
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
101

102
template <typename TP>
103
static std::time_t to_time_t(TP tp)
×
104
{
105
    using namespace std::chrono;
106
    auto sctp = time_point_cast<system_clock::duration>(tp - TP::clock::now() + system_clock::now());
×
107
    return system_clock::to_time_t(sctp);
×
108
}
109

110
template <typename TP>
111
static TP from_time_t(std::time_t t)
4✔
112
{
113
    using namespace std::chrono;
114
    auto sctp = system_clock::from_time_t(t);
4✔
115
    auto tp = time_point_cast<typename TP::duration>(sctp - system_clock::now() + TP::clock::now());
4✔
116
    return tp;
4✔
117
}
118

119
namespace Catch {
120
template <>
121
struct StringMaker<fs::path>
122
{
123
    static std::string convert(fs::path const& value) { return '"' + value.string() + '"'; }
×
124
};
125

126
template <>
127
struct StringMaker<fs::perms>
128
{
129
    static std::string convert(fs::perms const& value) { return std::to_string(static_cast<unsigned int>(value)); }
×
130
};
131

132
template <>
133
struct StringMaker<fs::file_status>
134
{
135
    static std::string convert(fs::file_status const& value) {
×
136
        return std::string("[") + std::to_string(static_cast<unsigned int>(value.type())) + "," + std::to_string(static_cast<unsigned int>(value.permissions())) + "]";
×
137
    }
138
};
139

140
#ifdef __cpp_lib_char8_t
141
template <>
142
struct StringMaker<char8_t>
143
{
144
    static std::string convert(char8_t const& value) { return std::to_string(static_cast<unsigned int>(value)); }
×
145
};
146
#endif
147

148
template <>
149
struct StringMaker<fs::file_time_type>
150
{
151
    static std::string convert(fs::file_time_type const& value)
×
152
    {
153
        std::time_t t = to_time_t(value);
×
154
        std::tm* ptm = std::localtime(&t);
×
155
        std::ostringstream os;
×
156
        if (ptm) {
×
157
            std::tm ttm = *ptm;
×
158
            os << std::put_time(&ttm, "%Y-%m-%d %H:%M:%S");
×
159
        }
160
        else {
161
            os << "(invalid-time)";
×
162
        }
163
        return os.str();
×
164
    }
×
165
};
166
}  // namespace Catch
167

168
enum class TempOpt { none, change_path };
169
class TemporaryDirectory
170
{
171
public:
172
    TemporaryDirectory(TempOpt opt = TempOpt::none)
98✔
173
    {
98✔
174
        static auto seed = std::chrono::high_resolution_clock::now().time_since_epoch().count();
98✔
175
        static auto rng = std::bind(std::uniform_int_distribution<int>(0, 35), std::mt19937(static_cast<unsigned int>(seed) ^ static_cast<unsigned int>(reinterpret_cast<ptrdiff_t>(&opt))));
98✔
176
        std::string filename;
98✔
177
        do {
178
            filename = "test_";
98✔
179
            for (int i = 0; i < 8; ++i) {
882✔
180
                filename += "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"[rng()];
784✔
181
            }
182
            _path = fs::canonical(fs::temp_directory_path()) / filename;
98✔
183
        } while (fs::exists(_path));
98✔
184
        fs::create_directories(_path);
98✔
185
        if (opt == TempOpt::change_path) {
98✔
186
            _orig_dir = fs::current_path();
74✔
187
            fs::current_path(_path);
74✔
188
        }
189
    }
98✔
190

191
    ~TemporaryDirectory()
98✔
192
    {
193
        if (!_orig_dir.empty()) {
98✔
194
            fs::current_path(_orig_dir);
74✔
195
        }
196
        fs::remove_all(_path);
98✔
197
    }
98✔
198

199
    const fs::path& path() const { return _path; }
236✔
200

201
private:
202
    fs::path _path;
203
    fs::path _orig_dir;
204
};
205

206
static void generateFile(const fs::path& pathname, int withSize = -1)
154✔
207
{
208
    fs::ofstream outfile(pathname);
154✔
209
    if (withSize < 0) {
154✔
210
        outfile << "Hello world!" << std::endl;
116✔
211
    }
212
    else {
213
        outfile << std::string(size_t(withSize), '*');
38✔
214
    }
215
}
154✔
216

217
#ifdef GHC_OS_WINDOWS
218
#if !defined(_WIN64) && defined(KEY_WOW64_64KEY)
219
static bool isWow64Proc()
220
{
221
    typedef BOOL(WINAPI * IsWow64Process_t)(HANDLE, PBOOL);
222
    BOOL bIsWow64 = FALSE;
223
    auto fnIsWow64Process = (IsWow64Process_t)GetProcAddress(GetModuleHandle(TEXT("kernel32")), "IsWow64Process");
224
    if (NULL != fnIsWow64Process) {
225
        if (!fnIsWow64Process(GetCurrentProcess(), &bIsWow64)) {
226
            bIsWow64 = FALSE;
227
        }
228
    }
229
    return bIsWow64 == TRUE;
230
}
231
#endif
232

233
static bool is_symlink_creation_supported()
234
{
235
    bool result = true;
236
    HKEY key;
237
    REGSAM flags = KEY_READ;
238
#ifdef _WIN64
239
    flags |= KEY_WOW64_64KEY;
240
#elif defined(KEY_WOW64_64KEY)
241
    if (isWow64Proc()) {
242
        flags |= KEY_WOW64_64KEY;
243
    }
244
    else {
245
        flags |= KEY_WOW64_32KEY;
246
    }
247
#else
248
    result = false;
249
#endif
250
    if (result) {
251
        auto err = RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\AppModelUnlock", 0, flags, &key);
252
        if (err == ERROR_SUCCESS) {
253
            DWORD val = 0, size = sizeof(DWORD);
254
            err = RegQueryValueExW(key, L"AllowDevelopmentWithoutDevLicense", 0, NULL, reinterpret_cast<LPBYTE>(&val), &size);
255
            RegCloseKey(key);
256
            if (err != ERROR_SUCCESS) {
257
                result = false;
258
            }
259
            else {
260
                result = (val != 0);
261
            }
262
        }
263
        else {
264
            result = false;
265
        }
266
    }
267
    if (!result) {
268
        std::clog << "Warning: Symlink creation not supported." << std::endl;
269
    }
270
    return result;
271
}
272
#else
273
static bool is_symlink_creation_supported()
62✔
274
{
275
    return true;
62✔
276
}
277
#endif
278

279
static bool has_host_root_name_support()
16✔
280
{
281
    return fs::path("//host").has_root_name();
16✔
282
}
283

284
template <class T>
285
class TestAllocator
286
{
287
public:
288
    using value_type = T;
289
    using pointer = T*;
290
    using const_pointer = const T*;
291
    using reference = T&;
292
    using const_reference = const T&;
293
    using difference_type = ptrdiff_t;
294
    using size_type = size_t;
295
    TestAllocator() noexcept {}
2✔
296
    template <class U>
297
    TestAllocator(TestAllocator<U> const&) noexcept
298
    {
299
    }
300
    value_type* allocate(std::size_t n) { return static_cast<value_type*>(::operator new(n * sizeof(value_type))); }
×
301
    void deallocate(value_type* p, std::size_t) noexcept { ::operator delete(p); }
×
302
    template<class U>
303
    struct rebind {
304
        typedef TestAllocator<U> other;
305
    };
306
};
307

308
template <class T, class U>
309
static bool operator==(TestAllocator<T> const&, TestAllocator<U> const&) noexcept
310
{
311
    return true;
312
}
313

314
template <class T, class U>
315
static bool operator!=(TestAllocator<T> const& x, TestAllocator<U> const& y) noexcept
316
{
317
    return !(x == y);
318
}
319

320
TEST_CASE("Temporary Directory", "[fs.test.tempdir]")
2✔
321
{
322
    fs::path tempPath;
2✔
323
    {
324
        TemporaryDirectory t;
2✔
325
        tempPath = t.path();
2✔
326
        REQUIRE(fs::exists(fs::path(t.path())));
2✔
327
        REQUIRE(fs::is_directory(t.path()));
2✔
328
    }
2✔
329
    REQUIRE(!fs::exists(tempPath));
2✔
330
}
2✔
331

332
#ifdef GHC_FILESYSTEM_VERSION
333
TEST_CASE("fs::detail::fromUtf8", "[filesystem][fs.detail.utf8]")
2✔
334
{
335
    CHECK(fs::detail::fromUtf8<std::wstring>("foobar").length() == 6);
2✔
336
    CHECK(fs::detail::fromUtf8<std::wstring>("foobar") == L"foobar");
2✔
337
    CHECK(fs::detail::fromUtf8<std::wstring>(u8"föobar").length() == 6);
2✔
338
    CHECK(fs::detail::fromUtf8<std::wstring>(u8"föobar") == L"föobar");
2✔
339

340
    CHECK(fs::detail::toUtf8(std::wstring(L"foobar")).length() == 6);
2✔
341
    CHECK(fs::detail::toUtf8(std::wstring(L"foobar")) == "foobar");
2✔
342
    CHECK(fs::detail::toUtf8(std::wstring(L"föobar")).length() == 7);
2✔
343
    //CHECK(fs::detail::toUtf8(std::wstring(L"föobar")) == u8"föobar");
344

345
#ifdef GHC_RAISE_UNICODE_ERRORS
346
    CHECK_THROWS_AS(fs::detail::fromUtf8<std::u16string>(std::string("\xed\xa0\x80")), fs::filesystem_error);
347
    CHECK_THROWS_AS(fs::detail::fromUtf8<std::u16string>(std::string("\xc3")), fs::filesystem_error);
348
#else
349
    CHECK(std::u16string(2,0xfffd) == fs::detail::fromUtf8<std::u16string>(std::string("\xed\xa0\x80")));
2✔
350
    CHECK(std::u16string(1,0xfffd) == fs::detail::fromUtf8<std::u16string>(std::string("\xc3")));
2✔
351
#endif
352
}
2✔
353

354
TEST_CASE("fs::detail::toUtf8", "[filesystem][fs.detail.utf8]")
2✔
355
{
356
    std::string t;
2✔
357
    CHECK(std::string("\xc3\xa4/\xe2\x82\xac\xf0\x9d\x84\x9e") == fs::detail::toUtf8(std::u16string(u"\u00E4/\u20AC\U0001D11E")));
2✔
358
#ifdef GHC_RAISE_UNICODE_ERRORS
359
    CHECK_THROWS_AS(fs::detail::toUtf8(std::u16string(1, 0xd800)), fs::filesystem_error);
360
    CHECK_THROWS_AS(fs::detail::appendUTF8(t, 0x200000), fs::filesystem_error);
361
#else
362
    CHECK(std::string("\xEF\xBF\xBD") == fs::detail::toUtf8(std::u16string(1, 0xd800)));
2✔
363
    fs::detail::appendUTF8(t, 0x200000);
2✔
364
    CHECK(std::string("\xEF\xBF\xBD") == t);
2✔
365
#endif
366
}
2✔
367
#endif
368

369
TEST_CASE("fs.path.generic - path::preferred_separator", "[filesystem][path][fs.path.generic]")
2✔
370
{
371
#ifdef GHC_OS_WINDOWS
372
    CHECK(fs::path::preferred_separator == '\\');
373
#else
374
    CHECK(fs::path::preferred_separator == '/');
2✔
375
#endif
376
}
2✔
377

378
#ifndef GHC_OS_WINDOWS
379
TEST_CASE("fs.path.generic - path(\"//host\").has_root_name()", "[filesystem][path][fs.path.generic]")
×
380
{
381
    if (!has_host_root_name_support()) {
×
382
        WARN("This implementation doesn't support path(\"//host\").has_root_name() == true [C++17 30.12.8.1 par. 4] on this platform, tests based on this are skipped. (Should be okay.)");
×
383
    }
384
}
×
385
#endif
386

387
TEST_CASE("fs.path.construct - path constructors and destructor", "[filesystem][path][fs.path.construct]")
2✔
388
{
389
    CHECK("/usr/local/bin" == fs::path("/usr/local/bin").generic_string());
2✔
390
    std::string str = "/usr/local/bin";
2✔
391
#if defined(__cpp_lib_char8_t) && !defined(GHC_FILESYSTEM_ENFORCE_CPP17_API)
392
    std::u8string u8str = u8"/usr/local/bin";
1✔
393
#endif
394
    std::u16string u16str = u"/usr/local/bin";
2✔
395
    std::u32string u32str = U"/usr/local/bin";
2✔
396
#if defined(__cpp_lib_char8_t) && !defined(GHC_FILESYSTEM_ENFORCE_CPP17_API)
397
    CHECK(u8str == fs::path(u8str).generic_u8string());
1✔
398
#endif
399
    CHECK(u16str == fs::path(u16str).generic_u16string());
2✔
400
    CHECK(u32str == fs::path(u32str).generic_u32string());
2✔
401
    CHECK(str == fs::path(str, fs::path::format::generic_format));
2✔
402
    CHECK(str == fs::path(str.begin(), str.end()));
2✔
403
    CHECK(fs::path(std::wstring(3, 67)) == "CCC");
2✔
404
#if defined(__cpp_lib_char8_t) && !defined(GHC_FILESYSTEM_ENFORCE_CPP17_API)
405
    CHECK(str == fs::path(u8str.begin(), u8str.end()));
1✔
406
#endif
407
    CHECK(str == fs::path(u16str.begin(), u16str.end()));
2✔
408
    CHECK(str == fs::path(u32str.begin(), u32str.end()));
2✔
409
#ifdef GHC_FILESYSTEM_VERSION
410
    CHECK(fs::path("///foo/bar") == "/foo/bar");
2✔
411
    CHECK(fs::path("//foo//bar") == "//foo/bar");
2✔
412
#endif
413
#ifdef GHC_OS_WINDOWS
414
    CHECK("\\usr\\local\\bin" == fs::path("/usr/local/bin"));
415
    CHECK("C:\\usr\\local\\bin" == fs::path("C:\\usr\\local\\bin"));
416
#else
417
    CHECK("/usr/local/bin" == fs::path("/usr/local/bin"));
2✔
418
#endif
419
    if (has_host_root_name_support()) {
2✔
420
        CHECK("//host/foo/bar" == fs::path("//host/foo/bar"));
2✔
421
    }
422

423
#if !defined(GHC_OS_WINDOWS) && !(defined(__GLIBCXX__) && !(defined(_GLIBCXX_RELEASE) && (_GLIBCXX_RELEASE >= 8))) && !defined(USE_STD_FS)
424
    std::locale loc;
2✔
425
    bool testUTF8Locale = false;
2✔
426
    try {
427
        if (const char* lang = std::getenv("LANG")) {
2✔
428
            loc = std::locale(lang);
2✔
429
        }
430
        else {
431
            loc = std::locale("en_US.UTF-8");
×
432
        }
433
        std::string name = loc.name();
2✔
434
        if (name.length() > 5 && (name.substr(name.length() - 5) == "UTF-8" || name.substr(name.length() - 5) == "utf-8")) {
2✔
435
            testUTF8Locale = true;
2✔
436
        }
437
    }
2✔
438
    catch (std::runtime_error&) {
×
439
        WARN("Couldn't create an UTF-8 locale!");
×
440
    }
×
441
    if (testUTF8Locale) {
2✔
442
        CHECK("/usr/local/bin" == fs::path("/usr/local/bin", loc));
2✔
443
        CHECK(str == fs::path(str.begin(), str.end(), loc));
2✔
444
        CHECK(str == fs::path(u16str.begin(), u16str.end(), loc));
2✔
445
        CHECK(str == fs::path(u32str.begin(), u32str.end(), loc));
2✔
446
    }
447
#endif
448
}
2✔
449

450
TEST_CASE("fs.path.assign - path assignments", "[filesystem][path][fs.path.assign]")
2✔
451
{
452
    fs::path p1{"/foo/bar"};
2✔
453
    fs::path p2{"/usr/local"};
2✔
454
    fs::path p3;
2✔
455
    p3 = p1;
2✔
456
    REQUIRE(p1 == p3);
2✔
457
    p3 = fs::path{"/usr/local"};
2✔
458
    REQUIRE(p2 == p3);
2✔
459
    p3 = fs::path{L"/usr/local"};
2✔
460
    REQUIRE(p2 == p3);
2✔
461
    p3.assign(L"/usr/local");
2✔
462
    REQUIRE(p2 == p3);
2✔
463
#if defined(IS_WCHAR_PATH) || defined(GHC_USE_WCHAR_T)
464
    p3 = fs::path::string_type{L"/foo/bar"};
465
    REQUIRE(p1 == p3);
466
    p3.assign(fs::path::string_type{L"/usr/local"});
467
    REQUIRE(p2 == p3);
468
#else
469
    p3 = fs::path::string_type{"/foo/bar"};
2✔
470
    REQUIRE(p1 == p3);
2✔
471
    p3.assign(fs::path::string_type{"/usr/local"});
2✔
472
    REQUIRE(p2 == p3);
2✔
473
#endif
474
    p3 = std::u16string(u"/foo/bar");
2✔
475
    REQUIRE(p1 == p3);
2✔
476
    p3 = U"/usr/local";
2✔
477
    REQUIRE(p2 == p3);
2✔
478
    p3.assign(std::u16string(u"/foo/bar"));
2✔
479
    REQUIRE(p1 == p3);
2✔
480
    std::string s{"/usr/local"};
2✔
481
    p3.assign(s.begin(), s.end());
2✔
482
    REQUIRE(p2 == p3);
2✔
483
}
2✔
484

485
TEST_CASE("fs.path.append - path appends", "[filesystem][path][fs.path.append]")
2✔
486
{
487
#ifdef GHC_OS_WINDOWS
488
    CHECK(fs::path("foo") / "c:/bar" == "c:/bar");
489
    CHECK(fs::path("foo") / "c:" == "c:");
490
    CHECK(fs::path("c:") / "" == "c:");
491
    CHECK(fs::path("c:foo") / "/bar" == "c:/bar");
492
    CHECK(fs::path("c:foo") / "c:bar" == "c:foo/bar");
493
    CHECK(fs::path(R"(\\server)") / "abc" == R"(\\server\abc)");
494
    CHECK(fs::path(R"(\\server)") / "" == R"(\\server\)");
495
    CHECK(fs::path(R"(\\server\)") / "abc" == R"(\\server\abc)");
496
    CHECK(fs::path(R"(\\server\share)") / "abc" == R"(\\server\share\abc)");
497
    CHECK(fs::path(R"(\\server)") / R"(\abc)" == R"(\\server\abc)");
498
    CHECK(fs::path("c:") / "abc" == "c:abc");
499
    CHECK(fs::path(R"(c:\)") / "abc" == R"(c:\abc)");
500
    CHECK(fs::path(R"(\rooted)") / "abc" == R"(\rooted\abc)");
501
#else
502
    CHECK(fs::path("foo") / "" == "foo/");
2✔
503
    CHECK(fs::path("foo") / "/bar" == "/bar");
2✔
504
    CHECK(fs::path("/foo") / "/" == "/");
2✔
505
    if (has_host_root_name_support()) {
2✔
506
        CHECK(fs::path("//host/foo") / "/bar" == "/bar");
2✔
507
        CHECK(fs::path("//host") / "/" == "//host/");
2✔
508
        CHECK(fs::path("//host/foo") / "/" == "/");
2✔
509
    }
510
#endif
511
    CHECK(fs::path("/foo/bar") / "some///other" == "/foo/bar/some/other");
2✔
512
    fs::path p1{"/tmp/test"};
2✔
513
    fs::path p2{"foobar.txt"};
2✔
514
    fs::path p3 = p1 / p2;
2✔
515
    CHECK("/tmp/test/foobar.txt" == p3);
2✔
516
    // TODO: append(first, last)
517
}
2✔
518

519
TEST_CASE("fs.path.concat - path concatenation", "[filesystem][path][fs.path.concat]")
2✔
520
{
521
    CHECK((fs::path("foo") += fs::path("bar")) == "foobar");
2✔
522
    CHECK((fs::path("foo") += fs::path("/bar")) == "foo/bar");
2✔
523

524
    CHECK((fs::path("foo") += std::string("bar")) == "foobar");
2✔
525
    CHECK((fs::path("foo") += std::string("/bar")) == "foo/bar");
2✔
526

527
    CHECK((fs::path("foo") += "bar") == "foobar");
2✔
528
    CHECK((fs::path("foo") += "/bar") == "foo/bar");
2✔
529
    CHECK((fs::path("foo") += L"bar") == "foobar");
2✔
530
    CHECK((fs::path("foo") += L"/bar") == "foo/bar");
2✔
531

532
    CHECK((fs::path("foo") += 'b') == "foob");
2✔
533
    CHECK((fs::path("foo") += '/') == "foo/");
2✔
534
    CHECK((fs::path("foo") += L'b') == "foob");
2✔
535
    CHECK((fs::path("foo") += L'/') == "foo/");
2✔
536

537
    CHECK((fs::path("foo") += std::string("bar")) == "foobar");
2✔
538
    CHECK((fs::path("foo") += std::string("/bar")) == "foo/bar");
2✔
539

540
    CHECK((fs::path("foo") += std::u16string(u"bar")) == "foobar");
2✔
541
    CHECK((fs::path("foo") += std::u16string(u"/bar")) == "foo/bar");
2✔
542

543
    CHECK((fs::path("foo") += std::u32string(U"bar")) == "foobar");
2✔
544
    CHECK((fs::path("foo") += std::u32string(U"/bar")) == "foo/bar");
2✔
545

546
    CHECK(fs::path("foo").concat("bar") == "foobar");
2✔
547
    CHECK(fs::path("foo").concat("/bar") == "foo/bar");
2✔
548
    CHECK(fs::path("foo").concat(L"bar") == "foobar");
2✔
549
    CHECK(fs::path("foo").concat(L"/bar") == "foo/bar");
2✔
550
    std::string bar = "bar";
2✔
551
    CHECK(fs::path("foo").concat(bar.begin(), bar.end()) == "foobar");
2✔
552
#ifndef USE_STD_FS
553
    CHECK((fs::path("/foo/bar") += "/some///other") == "/foo/bar/some/other");
2✔
554
#endif
555
    // TODO: contat(first, last)
556
}
2✔
557

558
TEST_CASE("fs.path.modifiers - path modifiers", "[filesystem][path][fs.path.modifiers]")
2✔
559
{
560
    fs::path p = fs::path("/foo/bar");
2✔
561
    p.clear();
2✔
562
    CHECK(p == "");
2✔
563

564
    // make_preferred() is a no-op
565
#ifdef GHC_OS_WINDOWS
566
    CHECK(fs::path("foo\\bar") == "foo/bar");
567
    CHECK(fs::path("foo\\bar").make_preferred() == "foo/bar");
568
#else
569
    CHECK(fs::path("foo\\bar") == "foo\\bar");
2✔
570
    CHECK(fs::path("foo\\bar").make_preferred() == "foo\\bar");
2✔
571
#endif
572
    CHECK(fs::path("foo/bar").make_preferred() == "foo/bar");
2✔
573

574
    CHECK(fs::path("foo/bar").remove_filename() == "foo/");
2✔
575
    CHECK(fs::path("foo/").remove_filename() == "foo/");
2✔
576
    CHECK(fs::path("/foo").remove_filename() == "/");
2✔
577
    CHECK(fs::path("/").remove_filename() == "/");
2✔
578

579
    CHECK(fs::path("/foo").replace_filename("bar") == "/bar");
2✔
580
    CHECK(fs::path("/").replace_filename("bar") == "/bar");
2✔
581
    CHECK(fs::path("/foo").replace_filename("b//ar") == "/b/ar");
2✔
582

583
    CHECK(fs::path("/foo/bar.txt").replace_extension("odf") == "/foo/bar.odf");
2✔
584
    CHECK(fs::path("/foo/bar.txt").replace_extension() == "/foo/bar");
2✔
585
    CHECK(fs::path("/foo/bar").replace_extension("odf") == "/foo/bar.odf");
2✔
586
    CHECK(fs::path("/foo/bar").replace_extension(".odf") == "/foo/bar.odf");
2✔
587
    CHECK(fs::path("/foo/bar.").replace_extension(".odf") == "/foo/bar.odf");
2✔
588
    CHECK(fs::path("/foo/bar/").replace_extension("odf") == "/foo/bar/.odf");
2✔
589

590
    fs::path p1 = "foo";
2✔
591
    fs::path p2 = "bar";
2✔
592
    p1.swap(p2);
2✔
593
    CHECK(p1 == "bar");
2✔
594
    CHECK(p2 == "foo");
2✔
595
}
2✔
596

597
TEST_CASE("fs.path.native.obs - path native format observers", "[filesystem][path][fs.path.native.obs]")
2✔
598
{
599
#ifdef GHC_OS_WINDOWS
600
#if defined(IS_WCHAR_PATH) || defined(GHC_USE_WCHAR_T)
601
    CHECK(fs::u8path("\xc3\xa4\\\xe2\x82\xac").native() == fs::path::string_type(L"\u00E4\\\u20AC"));
602
    // CHECK(fs::u8path("\xc3\xa4\\\xe2\x82\xac").string() == std::string("ä\\€")); // MSVCs returns local DBCS encoding
603
#else
604
    CHECK(fs::u8path("\xc3\xa4\\\xe2\x82\xac").native() == fs::path::string_type("\xc3\xa4\\\xe2\x82\xac"));
605
    CHECK(fs::u8path("\xc3\xa4\\\xe2\x82\xac").string() == std::string("\xc3\xa4\\\xe2\x82\xac"));
606
    CHECK(!::strcmp(fs::u8path("\xc3\xa4\\\xe2\x82\xac").c_str(), "\xc3\xa4\\\xe2\x82\xac"));
607
    CHECK((std::string)fs::u8path("\xc3\xa4\\\xe2\x82\xac") == std::string("\xc3\xa4\\\xe2\x82\xac"));
608
#endif
609
    CHECK(fs::u8path("\xc3\xa4\\\xe2\x82\xac").wstring() == std::wstring(L"\u00E4\\\u20AC"));
610
#if defined(__cpp_lib_char8_t) && !defined(GHC_FILESYSTEM_ENFORCE_CPP17_API)
611
    CHECK(fs::u8path("\xc3\xa4\\\xe2\x82\xac").u8string() == std::u8string(u8"\u00E4\\\u20AC"));
612
#else
613
    CHECK(fs::u8path("\xc3\xa4\\\xe2\x82\xac").u8string() == std::string("\xc3\xa4\\\xe2\x82\xac"));
614
#endif
615
    CHECK(fs::u8path("\xc3\xa4\\\xe2\x82\xac").u16string() == std::u16string(u"\u00E4\\\u20AC"));
616
    CHECK(fs::u8path("\xc3\xa4\\\xe2\x82\xac").u32string() == std::u32string(U"\U000000E4\\\U000020AC"));
617
#else
618
    CHECK(fs::u8path("\xc3\xa4/\xe2\x82\xac").native() == fs::path::string_type("\xc3\xa4/\xe2\x82\xac"));
2✔
619
    CHECK(!::strcmp(fs::u8path("\xc3\xa4/\xe2\x82\xac").c_str(), "\xc3\xa4/\xe2\x82\xac"));
2✔
620
    CHECK((std::string)fs::u8path("\xc3\xa4/\xe2\x82\xac") == std::string("\xc3\xa4/\xe2\x82\xac"));
2✔
621
    CHECK(fs::u8path("\xc3\xa4/\xe2\x82\xac").string() == std::string("\xc3\xa4/\xe2\x82\xac"));
2✔
622
    CHECK(fs::u8path("\xc3\xa4/\xe2\x82\xac").wstring() == std::wstring(L"ä/€"));
2✔
623
#if defined(__cpp_lib_char8_t) && !defined(GHC_FILESYSTEM_ENFORCE_CPP17_API)
624
    CHECK(fs::u8path("\xc3\xa4/\xe2\x82\xac").u8string() == std::u8string(u8"\xc3\xa4/\xe2\x82\xac"));
1✔
625
#else
626
    CHECK(fs::u8path("\xc3\xa4/\xe2\x82\xac").u8string() == std::string("\xc3\xa4/\xe2\x82\xac"));
1✔
627
#endif
628
    CHECK(fs::u8path("\xc3\xa4/\xe2\x82\xac").u16string() == std::u16string(u"\u00E4/\u20AC"));
2✔
629
    INFO("This check might fail on GCC8 (with \"Illegal byte sequence\") due to not detecting the valid unicode codepoint U+1D11E.");
2✔
630
    CHECK(fs::u8path("\xc3\xa4/\xe2\x82\xac\xf0\x9d\x84\x9e").u16string() == std::u16string(u"\u00E4/\u20AC\U0001D11E"));
2✔
631
    CHECK(fs::u8path("\xc3\xa4/\xe2\x82\xac").u32string() == std::u32string(U"\U000000E4/\U000020AC"));
2✔
632
#endif
633
}
2✔
634

635
TEST_CASE("fs.path.generic.obs - path generic format observers", "[filesystem][path][fs.path.generic.obs]")
2✔
636
{
637
#ifdef GHC_OS_WINDOWS
638
#ifndef IS_WCHAR_PATH
639
    CHECK(fs::u8path("\xc3\xa4\\\xe2\x82\xac").generic_string() == std::string("\xc3\xa4/\xe2\x82\xac"));
640
#endif
641
#ifndef USE_STD_FS
642
    auto t = fs::u8path("\xc3\xa4\\\xe2\x82\xac").generic_string<char, std::char_traits<char>, TestAllocator<char>>();
643
    CHECK(t.c_str() == std::string("\xc3\xa4/\xe2\x82\xac"));
644
#endif
645
    CHECK(fs::u8path("\xc3\xa4\\\xe2\x82\xac").generic_wstring() == std::wstring(L"\U000000E4/\U000020AC"));
646
#if defined(__cpp_lib_char8_t) && !defined(GHC_FILESYSTEM_ENFORCE_CPP17_API)
647
    CHECK(fs::u8path("\xc3\xa4\\\xe2\x82\xac").generic_u8string() == std::u8string(u8"\u00E4/\u20AC"));
648
#else
649
    CHECK(fs::u8path("\xc3\xa4\\\xe2\x82\xac").generic_u8string() == std::string("\xc3\xa4/\xe2\x82\xac"));
650
#endif
651
    CHECK(fs::u8path("\xc3\xa4\\\xe2\x82\xac").generic_u16string() == std::u16string(u"\u00E4/\u20AC"));
652
    CHECK(fs::u8path("\xc3\xa4\\\xe2\x82\xac").generic_u32string() == std::u32string(U"\U000000E4/\U000020AC"));
653
#else
654
    CHECK(fs::u8path("\xc3\xa4/\xe2\x82\xac").generic_string() == std::string("\xc3\xa4/\xe2\x82\xac"));
2✔
655
#ifndef USE_STD_FS
656
    auto t = fs::u8path("\xc3\xa4/\xe2\x82\xac").generic_string<char, std::char_traits<char>, TestAllocator<char>>();
2✔
657
    CHECK(t.c_str() == std::string("\xc3\xa4/\xe2\x82\xac"));
2✔
658
#endif
659
    CHECK(fs::u8path("\xc3\xa4/\xe2\x82\xac").generic_wstring() == std::wstring(L"ä/€"));
2✔
660
#if defined(__cpp_lib_char8_t) && !defined(GHC_FILESYSTEM_ENFORCE_CPP17_API)
661
    CHECK(fs::u8path("\xc3\xa4/\xe2\x82\xac").generic_u8string() == std::u8string(u8"\xc3\xa4/\xe2\x82\xac"));
1✔
662
#else
663
    CHECK(fs::u8path("\xc3\xa4/\xe2\x82\xac").generic_u8string() == std::string("\xc3\xa4/\xe2\x82\xac"));
1✔
664
#endif
665
    CHECK(fs::u8path("\xc3\xa4/\xe2\x82\xac").generic_u16string() == std::u16string(u"\u00E4/\u20AC"));
2✔
666
    CHECK(fs::u8path("\xc3\xa4/\xe2\x82\xac").generic_u32string() == std::u32string(U"\U000000E4/\U000020AC"));
2✔
667
#endif
668
}
2✔
669

670
TEST_CASE("fs.path.compare - path compare", "[filesystem][path][fs.path.compare]")
2✔
671
{
672
    CHECK(fs::path("/foo/b").compare("/foo/a") > 0);
2✔
673
    CHECK(fs::path("/foo/b").compare("/foo/b") == 0);
2✔
674
    CHECK(fs::path("/foo/b").compare("/foo/c") < 0);
2✔
675

676
    CHECK(fs::path("/foo/b").compare(std::string("/foo/a")) > 0);
2✔
677
    CHECK(fs::path("/foo/b").compare(std::string("/foo/b")) == 0);
2✔
678
    CHECK(fs::path("/foo/b").compare(std::string("/foo/c")) < 0);
2✔
679

680
    CHECK(fs::path("/foo/b").compare(fs::path("/foo/a")) > 0);
2✔
681
    CHECK(fs::path("/foo/b").compare(fs::path("/foo/b")) == 0);
2✔
682
    CHECK(fs::path("/foo/b").compare(fs::path("/foo/c")) < 0);
2✔
683

684
#ifdef GHC_OS_WINDOWS
685
    CHECK(fs::path("c:\\a\\b").compare("C:\\a\\b") == 0);
686
    CHECK(fs::path("c:\\a\\b").compare("d:\\a\\b") != 0);
687
    CHECK(fs::path("c:\\a\\b").compare("C:\\A\\b") != 0);
688
#endif
689

690
#ifdef LWG_2936_BEHAVIOUR
691
    CHECK(fs::path("/a/b/").compare("/a/b/c") < 0);
2✔
692
    CHECK(fs::path("/a/b/").compare("a/c") > 0);
2✔
693
#endif // LWG_2936_BEHAVIOUR
694
}
2✔
695

696
TEST_CASE("fs.path.decompose - path decomposition", "[filesystem][path][fs.path.decompose]")
2✔
697
{
698
    // root_name()
699
    CHECK(fs::path("").root_name() == "");
2✔
700
    CHECK(fs::path(".").root_name() == "");
2✔
701
    CHECK(fs::path("..").root_name() == "");
2✔
702
    CHECK(fs::path("foo").root_name() == "");
2✔
703
    CHECK(fs::path("/").root_name() == "");
2✔
704
    CHECK(fs::path("/foo").root_name() == "");
2✔
705
    CHECK(fs::path("foo/").root_name() == "");
2✔
706
    CHECK(fs::path("/foo/").root_name() == "");
2✔
707
    CHECK(fs::path("foo/bar").root_name() == "");
2✔
708
    CHECK(fs::path("/foo/bar").root_name() == "");
2✔
709
    CHECK(fs::path("///foo/bar").root_name() == "");
2✔
710
#ifdef GHC_OS_WINDOWS
711
    CHECK(fs::path("C:/foo").root_name() == "C:");
712
    CHECK(fs::path("C:\\foo").root_name() == "C:");
713
    CHECK(fs::path("C:foo").root_name() == "C:");
714
#endif
715

716
    // root_directory()
717
    CHECK(fs::path("").root_directory() == "");
2✔
718
    CHECK(fs::path(".").root_directory() == "");
2✔
719
    CHECK(fs::path("..").root_directory() == "");
2✔
720
    CHECK(fs::path("foo").root_directory() == "");
2✔
721
    CHECK(fs::path("/").root_directory() == "/");
2✔
722
    CHECK(fs::path("/foo").root_directory() == "/");
2✔
723
    CHECK(fs::path("foo/").root_directory() == "");
2✔
724
    CHECK(fs::path("/foo/").root_directory() == "/");
2✔
725
    CHECK(fs::path("foo/bar").root_directory() == "");
2✔
726
    CHECK(fs::path("/foo/bar").root_directory() == "/");
2✔
727
    CHECK(fs::path("///foo/bar").root_directory() == "/");
2✔
728
#ifdef GHC_OS_WINDOWS
729
    CHECK(fs::path("C:/foo").root_directory() == "/");
730
    CHECK(fs::path("C:\\foo").root_directory() == "/");
731
    CHECK(fs::path("C:foo").root_directory() == "");
732
#endif
733

734
    // root_path()
735
    CHECK(fs::path("").root_path() == "");
2✔
736
    CHECK(fs::path(".").root_path() == "");
2✔
737
    CHECK(fs::path("..").root_path() == "");
2✔
738
    CHECK(fs::path("foo").root_path() == "");
2✔
739
    CHECK(fs::path("/").root_path() == "/");
2✔
740
    CHECK(fs::path("/foo").root_path() == "/");
2✔
741
    CHECK(fs::path("foo/").root_path() == "");
2✔
742
    CHECK(fs::path("/foo/").root_path() == "/");
2✔
743
    CHECK(fs::path("foo/bar").root_path() == "");
2✔
744
    CHECK(fs::path("/foo/bar").root_path() == "/");
2✔
745
    CHECK(fs::path("///foo/bar").root_path() == "/");
2✔
746
#ifdef GHC_OS_WINDOWS
747
    CHECK(fs::path("C:/foo").root_path() == "C:/");
748
    CHECK(fs::path("C:\\foo").root_path() == "C:/");
749
    CHECK(fs::path("C:foo").root_path() == "C:");
750
#endif
751

752
    // relative_path()
753
    CHECK(fs::path("").relative_path() == "");
2✔
754
    CHECK(fs::path(".").relative_path() == ".");
2✔
755
    CHECK(fs::path("..").relative_path() == "..");
2✔
756
    CHECK(fs::path("foo").relative_path() == "foo");
2✔
757
    CHECK(fs::path("/").relative_path() == "");
2✔
758
    CHECK(fs::path("/foo").relative_path() == "foo");
2✔
759
    CHECK(fs::path("foo/").relative_path() == "foo/");
2✔
760
    CHECK(fs::path("/foo/").relative_path() == "foo/");
2✔
761
    CHECK(fs::path("foo/bar").relative_path() == "foo/bar");
2✔
762
    CHECK(fs::path("/foo/bar").relative_path() == "foo/bar");
2✔
763
    CHECK(fs::path("///foo/bar").relative_path() == "foo/bar");
2✔
764
#ifdef GHC_OS_WINDOWS
765
    CHECK(fs::path("C:/foo").relative_path() == "foo");
766
    CHECK(fs::path("C:\\foo").relative_path() == "foo");
767
    CHECK(fs::path("C:foo").relative_path() == "foo");
768
#endif
769

770
    // parent_path()
771
    CHECK(fs::path("").parent_path() == "");
2✔
772
    CHECK(fs::path(".").parent_path() == "");
2✔
773
    CHECK(fs::path("..").parent_path() == "");  // unintuitive but as defined in the standard
2✔
774
    CHECK(fs::path("foo").parent_path() == "");
2✔
775
    CHECK(fs::path("/").parent_path() == "/");
2✔
776
    CHECK(fs::path("/foo").parent_path() == "/");
2✔
777
    CHECK(fs::path("foo/").parent_path() == "foo");
2✔
778
    CHECK(fs::path("/foo/").parent_path() == "/foo");
2✔
779
    CHECK(fs::path("foo/bar").parent_path() == "foo");
2✔
780
    CHECK(fs::path("/foo/bar").parent_path() == "/foo");
2✔
781
    CHECK(fs::path("///foo/bar").parent_path() == "/foo");
2✔
782
#ifdef GHC_OS_WINDOWS
783
    CHECK(fs::path("C:/foo").parent_path() == "C:/");
784
    CHECK(fs::path("C:\\foo").parent_path() == "C:/");
785
    CHECK(fs::path("C:foo").parent_path() == "C:");
786
#endif
787

788
    // filename()
789
    CHECK(fs::path("").filename() == "");
2✔
790
    CHECK(fs::path(".").filename() == ".");
2✔
791
    CHECK(fs::path("..").filename() == "..");
2✔
792
    CHECK(fs::path("foo").filename() == "foo");
2✔
793
    CHECK(fs::path("/").filename() == "");
2✔
794
    CHECK(fs::path("/foo").filename() == "foo");
2✔
795
    CHECK(fs::path("foo/").filename() == "");
2✔
796
    CHECK(fs::path("/foo/").filename() == "");
2✔
797
    CHECK(fs::path("foo/bar").filename() == "bar");
2✔
798
    CHECK(fs::path("/foo/bar").filename() == "bar");
2✔
799
    CHECK(fs::path("///foo/bar").filename() == "bar");
2✔
800
#ifdef GHC_OS_WINDOWS
801
    CHECK(fs::path("C:/foo").filename() == "foo");
802
    CHECK(fs::path("C:\\foo").filename() == "foo");
803
    CHECK(fs::path("C:foo").filename() == "foo");
804
    CHECK(fs::path("t:est.txt").filename() == "est.txt");
805
#else
806
    CHECK(fs::path("t:est.txt").filename() == "t:est.txt");
2✔
807
#endif
808

809
    // stem()
810
    CHECK(fs::path("/foo/bar.txt").stem() == "bar");
2✔
811
    {
812
        fs::path p = "foo.bar.baz.tar";
2✔
813
        CHECK(p.extension() == ".tar");
2✔
814
        p = p.stem();
2✔
815
        CHECK(p.extension() == ".baz");
2✔
816
        p = p.stem();
2✔
817
        CHECK(p.extension() == ".bar");
2✔
818
        p = p.stem();
2✔
819
        CHECK(p == "foo");
2✔
820
    }
2✔
821
    CHECK(fs::path("/foo/.profile").stem() == ".profile");
2✔
822
    CHECK(fs::path(".bar").stem() == ".bar");
2✔
823
    CHECK(fs::path("..bar").stem() == ".");
2✔
824
#ifdef GHC_OS_WINDOWS
825
    CHECK(fs::path("t:est.txt").stem() == "est");
826
#else
827
    CHECK(fs::path("t:est.txt").stem() == "t:est");
2✔
828
#endif
829
    CHECK(fs::path("/foo/.").stem() == ".");
2✔
830
    CHECK(fs::path("/foo/..").stem() == "..");
2✔
831

832
    // extension()
833
    CHECK(fs::path("/foo/bar.txt").extension() == ".txt");
2✔
834
    CHECK(fs::path("/foo/bar").extension() == "");
2✔
835
    CHECK(fs::path("/foo/.profile").extension() == "");
2✔
836
    CHECK(fs::path(".bar").extension() == "");
2✔
837
    CHECK(fs::path("..bar").extension() == ".bar");
2✔
838
    CHECK(fs::path("t:est.txt").extension() == ".txt");
2✔
839
    CHECK(fs::path("/foo/.").extension() == "");
2✔
840
    CHECK(fs::path("/foo/..").extension() == "");
2✔
841

842
    if (has_host_root_name_support()) {
2✔
843
        // //host-based root-names
844
        CHECK(fs::path("//host").root_name() == "//host");
2✔
845
        CHECK(fs::path("//host/foo").root_name() == "//host");
2✔
846
        CHECK(fs::path("//host").root_directory() == "");
2✔
847
        CHECK(fs::path("//host/foo").root_directory() == "/");
2✔
848
        CHECK(fs::path("//host").root_path() == "//host");
2✔
849
        CHECK(fs::path("//host/foo").root_path() == "//host/");
2✔
850
        CHECK(fs::path("//host").relative_path() == "");
2✔
851
        CHECK(fs::path("//host/foo").relative_path() == "foo");
2✔
852
        CHECK(fs::path("//host").parent_path() == "//host");
2✔
853
        CHECK(fs::path("//host/foo").parent_path() == "//host/");
2✔
854
        CHECK(fs::path("//host").filename() == "");
2✔
855
        CHECK(fs::path("//host/foo").filename() == "foo");
2✔
856
    }
857
}
2✔
858

859
TEST_CASE("fs.path.query - path query", "[fielsystem][path][fs.path.query]")
2✔
860
{
861
    // empty
862
    CHECK(fs::path("").empty());
2✔
863
    CHECK(!fs::path("foo").empty());
2✔
864

865
    // has_root_path()
866
    CHECK(!fs::path("foo").has_root_path());
2✔
867
    CHECK(!fs::path("foo/bar").has_root_path());
2✔
868
    CHECK(fs::path("/foo").has_root_path());
2✔
869
#ifdef GHC_OS_WINDOWS
870
    CHECK(fs::path("C:foo").has_root_path());
871
    CHECK(fs::path("C:/foo").has_root_path());
872
#endif
873

874
    // has_root_name()
875
    CHECK(!fs::path("foo").has_root_name());
2✔
876
    CHECK(!fs::path("foo/bar").has_root_name());
2✔
877
    CHECK(!fs::path("/foo").has_root_name());
2✔
878
#ifdef GHC_OS_WINDOWS
879
    CHECK(fs::path("C:foo").has_root_name());
880
    CHECK(fs::path("C:/foo").has_root_name());
881
#endif
882

883
    // has_root_directory()
884
    CHECK(!fs::path("foo").has_root_directory());
2✔
885
    CHECK(!fs::path("foo/bar").has_root_directory());
2✔
886
    CHECK(fs::path("/foo").has_root_directory());
2✔
887
#ifdef GHC_OS_WINDOWS
888
    CHECK(!fs::path("C:foo").has_root_directory());
889
    CHECK(fs::path("C:/foo").has_root_directory());
890
#endif
891

892
    // has_relative_path()
893
    CHECK(!fs::path("").has_relative_path());
2✔
894
    CHECK(!fs::path("/").has_relative_path());
2✔
895
    CHECK(fs::path("/foo").has_relative_path());
2✔
896

897
    // has_parent_path()
898
    CHECK(!fs::path("").has_parent_path());
2✔
899
    CHECK(!fs::path(".").has_parent_path());
2✔
900
    CHECK(!fs::path("..").has_parent_path());  // unintuitive but as defined in the standard
2✔
901
    CHECK(!fs::path("foo").has_parent_path());
2✔
902
    CHECK(fs::path("/").has_parent_path());
2✔
903
    CHECK(fs::path("/foo").has_parent_path());
2✔
904
    CHECK(fs::path("foo/").has_parent_path());
2✔
905
    CHECK(fs::path("/foo/").has_parent_path());
2✔
906

907
    // has_filename()
908
    CHECK(fs::path("foo").has_filename());
2✔
909
    CHECK(fs::path("foo/bar").has_filename());
2✔
910
    CHECK(!fs::path("/foo/bar/").has_filename());
2✔
911

912
    // has_stem()
913
    CHECK(fs::path("foo").has_stem());
2✔
914
    CHECK(fs::path("foo.bar").has_stem());
2✔
915
    CHECK(fs::path(".profile").has_stem());
2✔
916
    CHECK(!fs::path("/foo/").has_stem());
2✔
917

918
    // has_extension()
919
    CHECK(!fs::path("foo").has_extension());
2✔
920
    CHECK(fs::path("foo.bar").has_extension());
2✔
921
    CHECK(!fs::path(".profile").has_extension());
2✔
922

923
    // is_absolute()
924
    CHECK(!fs::path("foo/bar").is_absolute());
2✔
925
#ifdef GHC_OS_WINDOWS
926
    CHECK(!fs::path("/foo").is_absolute());
927
    CHECK(!fs::path("c:foo").is_absolute());
928
    CHECK(fs::path("c:/foo").is_absolute());
929
#else
930
    CHECK(fs::path("/foo").is_absolute());
2✔
931
#endif
932

933
    // is_relative()
934
    CHECK(fs::path("foo/bar").is_relative());
2✔
935
#ifdef GHC_OS_WINDOWS
936
    CHECK(fs::path("/foo").is_relative());
937
    CHECK(fs::path("c:foo").is_relative());
938
    CHECK(!fs::path("c:/foo").is_relative());
939
#else
940
    CHECK(!fs::path("/foo").is_relative());
2✔
941
#endif
942

943
    if (has_host_root_name_support()) {
2✔
944
        CHECK(fs::path("//host").has_root_name());
2✔
945
        CHECK(fs::path("//host/foo").has_root_name());
2✔
946
        CHECK(fs::path("//host").has_root_path());
2✔
947
        CHECK(fs::path("//host/foo").has_root_path());
2✔
948
        CHECK(!fs::path("//host").has_root_directory());
2✔
949
        CHECK(fs::path("//host/foo").has_root_directory());
2✔
950
        CHECK(!fs::path("//host").has_relative_path());
2✔
951
        CHECK(fs::path("//host/foo").has_relative_path());
2✔
952
        CHECK(fs::path("//host/foo").is_absolute());
2✔
953
        CHECK(!fs::path("//host/foo").is_relative());
2✔
954
    }
955
}
2✔
956

957
TEST_CASE("fs.path.gen - path generation", "[filesystem][path][fs.path.gen]")
2✔
958
{
959
    // lexically_normal()
960
    CHECK(fs::path("foo/./bar/..").lexically_normal() == "foo/");
2✔
961
    CHECK(fs::path("foo/.///bar/../").lexically_normal() == "foo/");
2✔
962
    CHECK(fs::path("/foo/../..").lexically_normal() == "/");
2✔
963
    CHECK(fs::path("foo/..").lexically_normal() == ".");
2✔
964
    CHECK(fs::path("ab/cd/ef/../../qw").lexically_normal() == "ab/qw");
2✔
965
    CHECK(fs::path("a/b/../../../c").lexically_normal() == "../c");
2✔
966
    CHECK(fs::path("../").lexically_normal() == "..");
2✔
967
    CHECK(fs::path("./../foo/../bar").lexically_normal() == "../bar");
2✔
968
    CHECK(fs::path("./../foo/../../bar").lexically_normal() == "../../bar");
2✔
969
    CHECK(fs::path("../../foo/../bar").lexically_normal() == "../../bar");
2✔
970
#ifdef GHC_OS_WINDOWS
971
    CHECK(fs::path("\\/\\///\\/").lexically_normal() == "/");
972
    CHECK(fs::path("a/b/..\\//..///\\/../c\\\\/").lexically_normal() == "../c/");
973
    CHECK(fs::path("..a/b/..\\//..///\\/../c\\\\/").lexically_normal() == "../c/");
974
    CHECK(fs::path("..\\").lexically_normal() == "..");
975
    CHECK(fs::path("//?/UNC/::1/c$/foo").lexically_normal() == R"(\\?\UNC\::1\c$\foo)");
976
    CHECK(fs::path(R"(\\?\UNC\a::1\c$\foo)").lexically_normal() == R"(\\?\UNC\a::1\c$\foo)");
977
    CHECK(fs::path(R"(\\?\UNC\fe80::1\c$\foo)").lexically_normal() == R"(\\?\UNC\fe80::1\c$\foo)");
978
    CHECK(fs::path(R"(\\?\UNC\server\share\foo)").lexically_normal() == R"(\\?\UNC\server\share\foo)");
979
    CHECK(fs::path(R"(\\server\share\foo)").lexically_normal() == R"(\\server\share\foo)");
980
    CHECK(fs::path(R"(C:foo\..\bar)").lexically_normal() == R"(C:bar)");
981
#endif
982

983
    // lexically_relative()
984
    CHECK(fs::path("/a/d").lexically_relative("/a/b/c") == "../../d");
2✔
985
    CHECK(fs::path("/a/b/c").lexically_relative("/a/d") == "../b/c");
2✔
986
    CHECK(fs::path("/a/b/c").lexically_relative("/a/b/c/d/..") == ".");
2✔
987
    CHECK(fs::path("/a/b/c/").lexically_relative("/a/b/c/d/..") == ".");
2✔
988
    CHECK(fs::path("").lexically_relative("/a/..") == "");
2✔
989
    CHECK(fs::path("").lexically_relative("a/..") == ".");
2✔
990
    CHECK(fs::path("a/b/c").lexically_relative("a") == "b/c");
2✔
991
    CHECK(fs::path("a/b/c").lexically_relative("a/b/c/x/y") == "../..");
2✔
992
    CHECK(fs::path("a/b/c").lexically_relative("a/b/c") == ".");
2✔
993
    CHECK(fs::path("a/b").lexically_relative("c/d") == "../../a/b");
2✔
994
    CHECK(fs::path("a/b").lexically_relative("a/") == "b");
2✔
995
    if (has_host_root_name_support()) {
2✔
996
        CHECK(fs::path("//host1/foo").lexically_relative("//host2.bar") == "");
2✔
997
    }
998
#ifdef GHC_OS_WINDOWS
999
    CHECK(fs::path("c:/foo").lexically_relative("/bar") == "");
1000
    CHECK(fs::path("c:foo").lexically_relative("c:/bar") == "");
1001
    CHECK(fs::path("foo").lexically_relative("/bar") == "");
1002
    CHECK(fs::path("c:/foo/bar.txt").lexically_relative("c:/foo/") == "bar.txt");
1003
    CHECK(fs::path("c:/foo/bar.txt").lexically_relative("C:/foo/") == "bar.txt");
1004
#else
1005
    CHECK(fs::path("/foo").lexically_relative("bar") == "");
2✔
1006
    CHECK(fs::path("foo").lexically_relative("/bar") == "");
2✔
1007
#endif
1008

1009
    // lexically_proximate()
1010
    CHECK(fs::path("/a/d").lexically_proximate("/a/b/c") == "../../d");
2✔
1011
    if (has_host_root_name_support()) {
2✔
1012
        CHECK(fs::path("//host1/a/d").lexically_proximate("//host2/a/b/c") == "//host1/a/d");
2✔
1013
    }
1014
    CHECK(fs::path("a/d").lexically_proximate("/a/b/c") == "a/d");
2✔
1015
#ifdef GHC_OS_WINDOWS
1016
    CHECK(fs::path("c:/a/d").lexically_proximate("c:/a/b/c") == "../../d");
1017
    CHECK(fs::path("c:/a/d").lexically_proximate("d:/a/b/c") == "c:/a/d");
1018
    CHECK(fs::path("c:/foo").lexically_proximate("/bar") == "c:/foo");
1019
    CHECK(fs::path("c:foo").lexically_proximate("c:/bar") == "c:foo");
1020
    CHECK(fs::path("foo").lexically_proximate("/bar") == "foo");
1021
#else
1022
    CHECK(fs::path("/foo").lexically_proximate("bar") == "/foo");
2✔
1023
    CHECK(fs::path("foo").lexically_proximate("/bar") == "foo");
2✔
1024
#endif
1025
}
2✔
1026

1027
static std::string iterateResult(const fs::path& path)
30✔
1028
{
1029
    std::ostringstream result;
30✔
1030
    for (fs::path::const_iterator i = path.begin(); i != path.end(); ++i) {
94✔
1031
        if (i != path.begin()) {
64✔
1032
            result << ",";
36✔
1033
        }
1034
        result << i->generic_string();
64✔
1035
    }
30✔
1036
    return result.str();
60✔
1037
}
30✔
1038

1039
static std::string reverseIterateResult(const fs::path& path)
30✔
1040
{
1041
    std::ostringstream result;
30✔
1042
    fs::path::const_iterator iter = path.end();
30✔
1043
    bool first = true;
30✔
1044
    if (iter != path.begin()) {
30✔
1045
        do {
1046
            --iter;
64✔
1047
            if (!first) {
64✔
1048
                result << ",";
36✔
1049
            }
1050
            first = false;
64✔
1051
            result << iter->generic_string();
64✔
1052
        } while (iter != path.begin());
64✔
1053
    }
1054
    return result.str();
60✔
1055
}
30✔
1056

1057
TEST_CASE("fs.path.itr - path iterators", "[filesystem][path][fs.path.itr]")
2✔
1058
{
1059
    CHECK(iterateResult(fs::path()).empty());
2✔
1060
    CHECK("." == iterateResult(fs::path(".")));
2✔
1061
    CHECK(".." == iterateResult(fs::path("..")));
2✔
1062
    CHECK("foo" == iterateResult(fs::path("foo")));
2✔
1063
    CHECK("/" == iterateResult(fs::path("/")));
2✔
1064
    CHECK("/,foo" == iterateResult(fs::path("/foo")));
2✔
1065
    CHECK("foo," == iterateResult(fs::path("foo/")));
2✔
1066
    CHECK("/,foo," == iterateResult(fs::path("/foo/")));
2✔
1067
    CHECK("foo,bar" == iterateResult(fs::path("foo/bar")));
2✔
1068
    CHECK("/,foo,bar" == iterateResult(fs::path("/foo/bar")));
2✔
1069
#ifndef USE_STD_FS
1070
    // ghc::filesystem enforces redundant slashes to be reduced to one
1071
    CHECK("/,foo,bar" == iterateResult(fs::path("///foo/bar")));
2✔
1072
#else
1073
    // typically std::filesystem keeps them
1074
    CHECK("///,foo,bar" == iterateResult(fs::path("///foo/bar")));
1075
#endif
1076
    CHECK("/,foo,bar," == iterateResult(fs::path("/foo/bar///")));
2✔
1077
    CHECK("foo,.,bar,..," == iterateResult(fs::path("foo/.///bar/../")));
2✔
1078
#ifdef GHC_OS_WINDOWS
1079
    CHECK("C:,/,foo" == iterateResult(fs::path("C:/foo")));
1080
#endif
1081

1082
    CHECK(reverseIterateResult(fs::path()).empty());
2✔
1083
    CHECK("." == reverseIterateResult(fs::path(".")));
2✔
1084
    CHECK(".." == reverseIterateResult(fs::path("..")));
2✔
1085
    CHECK("foo" == reverseIterateResult(fs::path("foo")));
2✔
1086
    CHECK("/" == reverseIterateResult(fs::path("/")));
2✔
1087
    CHECK("foo,/" == reverseIterateResult(fs::path("/foo")));
2✔
1088
    CHECK(",foo" == reverseIterateResult(fs::path("foo/")));
2✔
1089
    CHECK(",foo,/" == reverseIterateResult(fs::path("/foo/")));
2✔
1090
    CHECK("bar,foo" == reverseIterateResult(fs::path("foo/bar")));
2✔
1091
    CHECK("bar,foo,/" == reverseIterateResult(fs::path("/foo/bar")));
2✔
1092
#ifndef USE_STD_FS
1093
    // ghc::filesystem enforces redundant slashes to be reduced to one
1094
    CHECK("bar,foo,/" == reverseIterateResult(fs::path("///foo/bar")));
2✔
1095
#else
1096
    // typically std::filesystem keeps them
1097
    CHECK("bar,foo,///" == reverseIterateResult(fs::path("///foo/bar")));
1098
#endif
1099
    CHECK(",bar,foo,/" == reverseIterateResult(fs::path("/foo/bar///")));
2✔
1100
    CHECK(",..,bar,.,foo" == reverseIterateResult(fs::path("foo/.///bar/../")));
2✔
1101
#ifdef GHC_OS_WINDOWS
1102
    CHECK("foo,/,C:" == reverseIterateResult(fs::path("C:/foo")));
1103
    CHECK("foo,C:" == reverseIterateResult(fs::path("C:foo")));
1104
#endif
1105
    {
1106
        fs::path p1 = "/foo/bar/test.txt";
2✔
1107
        fs::path p2;
2✔
1108
        for (auto pe : p1) {
10✔
1109
            p2 /= pe;
8✔
1110
        }
10✔
1111
        CHECK(p1 == p2);
2✔
1112
        CHECK("bar" == *(--fs::path("/foo/bar").end()));
2✔
1113
        auto p = fs::path("/foo/bar");
2✔
1114
        auto pi = p.end();
2✔
1115
        pi--;
2✔
1116
        CHECK("bar" == *pi);
2✔
1117
    }
2✔
1118

1119
    if (has_host_root_name_support()) {
2✔
1120
        CHECK("foo" == *(--fs::path("//host/foo").end()));
2✔
1121
        auto p = fs::path("//host/foo");
2✔
1122
        auto pi = p.end();
2✔
1123
        pi--;
2✔
1124
        CHECK("foo" == *pi);
2✔
1125
        CHECK("//host" == iterateResult(fs::path("//host")));
2✔
1126
        CHECK("//host,/,foo" == iterateResult(fs::path("//host/foo")));
2✔
1127
        CHECK("//host" == reverseIterateResult(fs::path("//host")));
2✔
1128
        CHECK("foo,/,//host" == reverseIterateResult(fs::path("//host/foo")));
2✔
1129
        {
1130
            fs::path p1 = "//host/foo/bar/test.txt";
2✔
1131
            fs::path p2;
2✔
1132
            for (auto pe : p1) {
12✔
1133
                p2 /= pe;
10✔
1134
            }
12✔
1135
            CHECK(p1 == p2);
2✔
1136
        }
2✔
1137
    }
2✔
1138
}
2✔
1139

1140
TEST_CASE("fs.path.nonmember - path non-member functions", "[filesystem][path][fs.path.nonmember]")
2✔
1141
{
1142
    fs::path p1("foo/bar");
2✔
1143
    fs::path p2("some/other");
2✔
1144
    fs::swap(p1, p2);
2✔
1145
    CHECK(p1 == "some/other");
2✔
1146
    CHECK(p2 == "foo/bar");
2✔
1147
    CHECK(hash_value(p1));
2✔
1148
    CHECK(p2 < p1);
2✔
1149
    CHECK(p2 <= p1);
2✔
1150
    CHECK(p1 <= p1);
2✔
1151
    CHECK(!(p1 < p2));
2✔
1152
    CHECK(!(p1 <= p2));
2✔
1153
    CHECK(p1 > p2);
2✔
1154
    CHECK(p1 >= p2);
2✔
1155
    CHECK(p1 >= p1);
2✔
1156
    CHECK(!(p2 > p1));
2✔
1157
    CHECK(!(p2 >= p1));
2✔
1158
    CHECK(p1 != p2);
2✔
1159
    CHECK(p1 / p2 == "some/other/foo/bar");
2✔
1160
}
2✔
1161

1162
TEST_CASE("fs.path.io - path inserter and extractor", "[filesystem][path][fs.path.io]")
2✔
1163
{
1164
    {
1165
        std::ostringstream os;
2✔
1166
        os << fs::path("/root/foo bar");
2✔
1167
#ifdef GHC_OS_WINDOWS
1168
        CHECK(os.str() == "\"\\\\root\\\\foo bar\"");
1169
#else
1170
        CHECK(os.str() == "\"/root/foo bar\"");
2✔
1171
#endif
1172
    }
2✔
1173
    {
1174
        std::ostringstream os;
2✔
1175
        os << fs::path("/root/foo\"bar");
2✔
1176
#ifdef GHC_OS_WINDOWS
1177
        CHECK(os.str() == "\"\\\\root\\\\foo\\\"bar\"");
1178
#else
1179
        CHECK(os.str() == "\"/root/foo\\\"bar\"");
2✔
1180
#endif
1181
    }
2✔
1182

1183
    {
1184
        std::istringstream is("\"/root/foo bar\"");
4✔
1185
        fs::path p;
2✔
1186
        is >> p;
2✔
1187
        CHECK(p == fs::path("/root/foo bar"));
2✔
1188
        CHECK((is.flags() & std::ios_base::skipws) == std::ios_base::skipws);
2✔
1189
    }
2✔
1190
    {
1191
        std::istringstream is("\"/root/foo bar\"");
4✔
1192
        is >> std::noskipws;
2✔
1193
        fs::path p;
2✔
1194
        is >> p;
2✔
1195
        CHECK(p == fs::path("/root/foo bar"));
2✔
1196
        CHECK((is.flags() & std::ios_base::skipws) != std::ios_base::skipws);
2✔
1197
    }
2✔
1198
    {
1199
        std::istringstream is("\"/root/foo\\\"bar\"");
4✔
1200
        fs::path p;
2✔
1201
        is >> p;
2✔
1202
        CHECK(p == fs::path("/root/foo\"bar"));
2✔
1203
    }
2✔
1204
    {
1205
        std::istringstream is("/root/foo");
4✔
1206
        fs::path p;
2✔
1207
        is >> p;
2✔
1208
        CHECK(p == fs::path("/root/foo"));
2✔
1209
    }
2✔
1210
}
2✔
1211

1212
TEST_CASE("fs.path.factory - path factory functions", "[filesystem][path][fs.path.factory]")
2✔
1213
{
1214
    CHECK(fs::u8path("foo/bar") == fs::path("foo/bar"));
2✔
1215
    CHECK(fs::u8path("foo/bar") == fs::path("foo/bar"));
2✔
1216
    std::string str("/foo/bar/test.txt");
2✔
1217
    CHECK(fs::u8path(str.begin(), str.end()) == str);
2✔
1218
}
2✔
1219

1220
TEST_CASE("fs.class.filesystem_error - class filesystem_error", "[filesystem][filesystem_error][fs.class.filesystem_error]")
2✔
1221
{
1222
    std::error_code ec(1, std::system_category());
2✔
1223
    fs::filesystem_error fse("None", std::error_code());
4✔
1224
    fse = fs::filesystem_error("Some error", ec);
2✔
1225
    CHECK(fse.code().value() == 1);
2✔
1226
    CHECK(!std::string(fse.what()).empty());
2✔
1227
    CHECK(fse.path1().empty());
2✔
1228
    CHECK(fse.path2().empty());
2✔
1229
    fse = fs::filesystem_error("Some error", fs::path("foo/bar"), ec);
2✔
1230
    CHECK(!std::string(fse.what()).empty());
2✔
1231
    CHECK(fse.path1() == "foo/bar");
2✔
1232
    CHECK(fse.path2().empty());
2✔
1233
    fse = fs::filesystem_error("Some error", fs::path("foo/bar"), fs::path("some/other"), ec);
2✔
1234
    CHECK(!std::string(fse.what()).empty());
2✔
1235
    CHECK(fse.path1() == "foo/bar");
2✔
1236
    CHECK(fse.path2() == "some/other");
2✔
1237
}
2✔
1238

1239
static constexpr fs::perms constExprOwnerAll()
1240
{
1241
    return fs::perms::owner_read | fs::perms::owner_write | fs::perms::owner_exec;
1242
}
1243

1244
TEST_CASE("fs.enum - enum class perms", "[filesystem][enum][fs.enum]")
2✔
1245
{
1246
    static_assert(constExprOwnerAll() == fs::perms::owner_all, "constexpr didn't result in owner_all");
1247
    CHECK((fs::perms::owner_read | fs::perms::owner_write | fs::perms::owner_exec) == fs::perms::owner_all);
2✔
1248
    CHECK((fs::perms::group_read | fs::perms::group_write | fs::perms::group_exec) == fs::perms::group_all);
2✔
1249
    CHECK((fs::perms::others_read | fs::perms::others_write | fs::perms::others_exec) == fs::perms::others_all);
2✔
1250
    CHECK((fs::perms::owner_all | fs::perms::group_all | fs::perms::others_all) == fs::perms::all);
2✔
1251
    CHECK((fs::perms::all | fs::perms::set_uid | fs::perms::set_gid | fs::perms::sticky_bit) == fs::perms::mask);
2✔
1252
}
2✔
1253

1254
TEST_CASE("fs.class.file_status - class file_status", "[filesystem][file_status][fs.class.file_status]")
2✔
1255
{
1256
    {
1257
        fs::file_status fs;
2✔
1258
        CHECK(fs.type() == fs::file_type::none);
2✔
1259
        CHECK(fs.permissions() == fs::perms::unknown);
2✔
1260
    }
2✔
1261
    {
1262
        fs::file_status fs{fs::file_type::regular};
2✔
1263
        CHECK(fs.type() == fs::file_type::regular);
2✔
1264
        CHECK(fs.permissions() == fs::perms::unknown);
2✔
1265
    }
2✔
1266
    {
1267
        fs::file_status fs{fs::file_type::directory, fs::perms::owner_read | fs::perms::owner_write | fs::perms::owner_exec};
2✔
1268
        CHECK(fs.type() == fs::file_type::directory);
2✔
1269
        CHECK(fs.permissions() == fs::perms::owner_all);
2✔
1270
        fs.type(fs::file_type::block);
2✔
1271
        CHECK(fs.type() == fs::file_type::block);
2✔
1272
        fs.type(fs::file_type::character);
2✔
1273
        CHECK(fs.type() == fs::file_type::character);
2✔
1274
        fs.type(fs::file_type::fifo);
2✔
1275
        CHECK(fs.type() == fs::file_type::fifo);
2✔
1276
        fs.type(fs::file_type::symlink);
2✔
1277
        CHECK(fs.type() == fs::file_type::symlink);
2✔
1278
        fs.type(fs::file_type::socket);
2✔
1279
        CHECK(fs.type() == fs::file_type::socket);
2✔
1280
        fs.permissions(fs.permissions() | fs::perms::group_all | fs::perms::others_all);
2✔
1281
        CHECK(fs.permissions() == fs::perms::all);
2✔
1282
    }
2✔
1283
    {
1284
        fs::file_status fst(fs::file_type::regular);
2✔
1285
        fs::file_status fs(std::move(fst));
2✔
1286
        CHECK(fs.type() == fs::file_type::regular);
2✔
1287
        CHECK(fs.permissions() == fs::perms::unknown);
2✔
1288
    }
2✔
1289
#if !defined(USE_STD_FS) || defined(GHC_FILESYSTEM_RUNNING_CPP20)
1290
    {
1291
        fs::file_status fs1{fs::file_type::regular, fs::perms::owner_read | fs::perms::owner_write | fs::perms::owner_exec};
2✔
1292
        fs::file_status fs2{fs::file_type::regular, fs::perms::owner_read | fs::perms::owner_write | fs::perms::owner_exec};
2✔
1293
        fs::file_status fs3{fs::file_type::directory, fs::perms::owner_read | fs::perms::owner_write | fs::perms::owner_exec};
2✔
1294
        fs::file_status fs4{fs::file_type::regular, fs::perms::owner_read | fs::perms::owner_write};
2✔
1295
        CHECK(fs1 == fs2);
2✔
1296
        CHECK_FALSE(fs1 == fs3);
2✔
1297
        CHECK_FALSE(fs1 == fs4);
2✔
1298
    }
2✔
1299
#endif
1300
}
2✔
1301

1302
TEST_CASE("fs.dir.entry - class directory_entry", "[filesystem][directory_entry][fs.dir.entry]")
2✔
1303
{
1304
    TemporaryDirectory t;
2✔
1305
    std::error_code ec;
2✔
1306
    auto de = fs::directory_entry(t.path());
2✔
1307
    CHECK(de.path() == t.path());
2✔
1308
    CHECK((fs::path)de == t.path());
2✔
1309
    CHECK(de.exists());
2✔
1310
    CHECK(!de.is_block_file());
2✔
1311
    CHECK(!de.is_character_file());
2✔
1312
    CHECK(de.is_directory());
2✔
1313
    CHECK(!de.is_fifo());
2✔
1314
    CHECK(!de.is_other());
2✔
1315
    CHECK(!de.is_regular_file());
2✔
1316
    CHECK(!de.is_socket());
2✔
1317
    CHECK(!de.is_symlink());
2✔
1318
    CHECK(de.status().type() == fs::file_type::directory);
2✔
1319
    ec.clear();
2✔
1320
    CHECK(de.status(ec).type() == fs::file_type::directory);
2✔
1321
    CHECK(!ec);
2✔
1322
    CHECK_NOTHROW(de.refresh());
2✔
1323
    fs::directory_entry none;
2✔
1324
    CHECK_THROWS_AS(none.refresh(), fs::filesystem_error);
2✔
1325
    ec.clear();
2✔
1326
    CHECK_NOTHROW(none.refresh(ec));
2✔
1327
    CHECK(ec);
2✔
1328
    CHECK_THROWS_AS(de.assign(""), fs::filesystem_error);
4✔
1329
    ec.clear();
2✔
1330
    CHECK_NOTHROW(de.assign("", ec));
2✔
1331
    CHECK(ec);
2✔
1332
    generateFile(t.path() / "foo", 1234);
2✔
1333
    auto now = fs::file_time_type::clock::now();
2✔
1334
    CHECK_NOTHROW(de.assign(t.path() / "foo"));
2✔
1335
    CHECK_NOTHROW(de.assign(t.path() / "foo", ec));
2✔
1336
    CHECK(!ec);
2✔
1337
    de = fs::directory_entry(t.path() / "foo");
2✔
1338
    CHECK(de.path() == t.path() / "foo");
2✔
1339
    CHECK(de.exists());
2✔
1340
    CHECK(de.exists(ec));
2✔
1341
    CHECK(!ec);
2✔
1342
    CHECK(!de.is_block_file());
2✔
1343
    CHECK(!de.is_block_file(ec));
2✔
1344
    CHECK(!ec);
2✔
1345
    CHECK(!de.is_character_file());
2✔
1346
    CHECK(!de.is_character_file(ec));
2✔
1347
    CHECK(!ec);
2✔
1348
    CHECK(!de.is_directory());
2✔
1349
    CHECK(!de.is_directory(ec));
2✔
1350
    CHECK(!ec);
2✔
1351
    CHECK(!de.is_fifo());
2✔
1352
    CHECK(!de.is_fifo(ec));
2✔
1353
    CHECK(!ec);
2✔
1354
    CHECK(!de.is_other());
2✔
1355
    CHECK(!de.is_other(ec));
2✔
1356
    CHECK(!ec);
2✔
1357
    CHECK(de.is_regular_file());
2✔
1358
    CHECK(de.is_regular_file(ec));
2✔
1359
    CHECK(!ec);
2✔
1360
    CHECK(!de.is_socket());
2✔
1361
    CHECK(!de.is_socket(ec));
2✔
1362
    CHECK(!ec);
2✔
1363
    CHECK(!de.is_symlink());
2✔
1364
    CHECK(!de.is_symlink(ec));
2✔
1365
    CHECK(!ec);
2✔
1366
    CHECK(de.file_size() == 1234);
2✔
1367
    CHECK(de.file_size(ec) == 1234);
2✔
1368
    CHECK(std::abs(std::chrono::duration_cast<std::chrono::seconds>(de.last_write_time() - now).count()) < 3);
2✔
1369
    ec.clear();
2✔
1370
    CHECK(std::abs(std::chrono::duration_cast<std::chrono::seconds>(de.last_write_time(ec) - now).count()) < 3);
2✔
1371
    CHECK(!ec);
2✔
1372
#ifndef GHC_OS_WEB
1373
    CHECK(de.hard_link_count() == 1);
2✔
1374
    CHECK(de.hard_link_count(ec) == 1);
2✔
1375
    CHECK(!ec);
2✔
1376
#endif
1377
    CHECK_THROWS_AS(de.replace_filename("bar"), fs::filesystem_error);
4✔
1378
    CHECK_NOTHROW(de.replace_filename("foo"));
2✔
1379
    ec.clear();
2✔
1380
    CHECK_NOTHROW(de.replace_filename("bar", ec));
2✔
1381
    CHECK(ec);
2✔
1382
    auto de2none = fs::directory_entry();
2✔
1383
    ec.clear();
2✔
1384
#ifndef GHC_OS_WEB
1385
    CHECK(de2none.hard_link_count(ec) == static_cast<uintmax_t>(-1));
2✔
1386
    CHECK_THROWS_AS(de2none.hard_link_count(), fs::filesystem_error);
2✔
1387
    CHECK(ec);
2✔
1388
#endif
1389
    ec.clear();
2✔
1390
    CHECK_NOTHROW(de2none.last_write_time(ec));
2✔
1391
    CHECK_THROWS_AS(de2none.last_write_time(), fs::filesystem_error);
2✔
1392
    CHECK(ec);
2✔
1393
    ec.clear();
2✔
1394
    CHECK_THROWS_AS(de2none.file_size(), fs::filesystem_error);
2✔
1395
    CHECK(de2none.file_size(ec) == static_cast<uintmax_t>(-1));
2✔
1396
    CHECK(ec);
2✔
1397
    ec.clear();
2✔
1398
    CHECK(de2none.status().type() == fs::file_type::not_found);
2✔
1399
    CHECK(de2none.status(ec).type() == fs::file_type::not_found);
2✔
1400
    CHECK(ec);
2✔
1401
    generateFile(t.path() / "a");
2✔
1402
    generateFile(t.path() / "b");
2✔
1403
    auto d1 = fs::directory_entry(t.path() / "a");
4✔
1404
    auto d2 = fs::directory_entry(t.path() / "b");
4✔
1405
    CHECK(d1 < d2);
2✔
1406
    CHECK(!(d2 < d1));
2✔
1407
    CHECK(d1 <= d2);
2✔
1408
    CHECK(!(d2 <= d1));
2✔
1409
    CHECK(d2 > d1);
2✔
1410
    CHECK(!(d1 > d2));
2✔
1411
    CHECK(d2 >= d1);
2✔
1412
    CHECK(!(d1 >= d2));
2✔
1413
    CHECK(d1 != d2);
2✔
1414
    CHECK(!(d2 != d2));
2✔
1415
    CHECK(d1 == d1);
2✔
1416
    CHECK(!(d1 == d2));
2✔
1417
    if(is_symlink_creation_supported()) {
2✔
1418
        fs::create_symlink(t.path() / "nonexistent", t.path() / "broken");
2✔
1419
        for (auto d3 : fs::directory_iterator(t.path())) {
12✔
1420
            CHECK_NOTHROW(d3.symlink_status());
8✔
1421
            CHECK_NOTHROW(d3.status());
8✔
1422
            CHECK_NOTHROW(d3.refresh());
8✔
1423
        }
10✔
1424
        fs::directory_entry entry(t.path() / "broken");
4✔
1425
        CHECK_NOTHROW(entry.refresh());
2✔
1426
    }
2✔
1427
}
2✔
1428

1429
TEST_CASE("fs.class.directory_iterator - class directory_iterator", "[filesystem][directory_iterator][fs.class.directory_iterator]")
2✔
1430
{
1431
    {
1432
        TemporaryDirectory t;
2✔
1433
        CHECK(fs::directory_iterator(t.path()) == fs::directory_iterator());
2✔
1434
        generateFile(t.path() / "test", 1234);
2✔
1435
        REQUIRE(fs::directory_iterator(t.path()) != fs::directory_iterator());
2✔
1436
        auto iter = fs::directory_iterator(t.path());
2✔
1437
        fs::directory_iterator iter2(iter);
2✔
1438
        fs::directory_iterator iter3, iter4;
2✔
1439
        iter3 = iter;
2✔
1440
        CHECK(iter->path().filename() == "test");
2✔
1441
        CHECK(iter2->path().filename() == "test");
2✔
1442
        CHECK(iter3->path().filename() == "test");
2✔
1443
        iter4 = std::move(iter3);
2✔
1444
        CHECK(iter4->path().filename() == "test");
2✔
1445
        CHECK(iter->path() == t.path() / "test");
2✔
1446
        CHECK(!iter->is_symlink());
2✔
1447
        CHECK(iter->is_regular_file());
2✔
1448
        CHECK(!iter->is_directory());
2✔
1449
        CHECK(iter->file_size() == 1234);
2✔
1450
        CHECK(++iter == fs::directory_iterator());
2✔
1451
        CHECK_THROWS_AS(fs::directory_iterator(t.path() / "non-existing"), fs::filesystem_error);
6✔
1452
        int cnt = 0;
2✔
1453
        for(auto de : fs::directory_iterator(t.path())) {
6✔
1454
            ++cnt;
2✔
1455
        }
4✔
1456
        CHECK(cnt == 1);
2✔
1457
    }
2✔
1458
    if (is_symlink_creation_supported()) {
2✔
1459
        TemporaryDirectory t;
2✔
1460
        fs::path td = t.path() / "testdir";
2✔
1461
        CHECK(fs::directory_iterator(t.path()) == fs::directory_iterator());
2✔
1462
        generateFile(t.path() / "test", 1234);
2✔
1463
        fs::create_directory(td);
2✔
1464
        REQUIRE_NOTHROW(fs::create_symlink(t.path() / "test", td / "testlink"));
2✔
1465
        std::error_code ec;
2✔
1466
        REQUIRE(fs::directory_iterator(td) != fs::directory_iterator());
2✔
1467
        auto iter = fs::directory_iterator(td);
2✔
1468
        CHECK(iter->path().filename() == "testlink");
2✔
1469
        CHECK(iter->path() == td / "testlink");
2✔
1470
        CHECK(iter->is_symlink());
2✔
1471
        CHECK(iter->is_regular_file());
2✔
1472
        CHECK(!iter->is_directory());
2✔
1473
        CHECK(iter->file_size() == 1234);
2✔
1474
        CHECK(++iter == fs::directory_iterator());
2✔
1475
    }
2✔
1476
    {
1477
        // Issue #8: check if resources are freed when iterator reaches end()
1478
        TemporaryDirectory t(TempOpt::change_path);
2✔
1479
        auto p = fs::path("test/");
2✔
1480
        fs::create_directory(p);
2✔
1481
        auto iter = fs::directory_iterator(p);
2✔
1482
        while (iter != fs::directory_iterator()) {
2✔
1483
            ++iter;
×
1484
        }
1485
        CHECK(fs::remove_all(p) == 1);
2✔
1486
        CHECK_NOTHROW(fs::create_directory(p));
2✔
1487
    }
2✔
1488
}
2✔
1489

1490
TEST_CASE("fs.class.rec.dir.itr - class recursive_directory_iterator", "[filesystem][recursive_directory_iterator][fs.class.rec.dir.itr]")
2✔
1491
{
1492
    {
1493
        auto iter = fs::recursive_directory_iterator(".");
2✔
1494
        iter.pop();
2✔
1495
        CHECK(iter == fs::recursive_directory_iterator());
2✔
1496
    }
2✔
1497
    {
1498
        TemporaryDirectory t;
2✔
1499
        CHECK(fs::recursive_directory_iterator(t.path()) == fs::recursive_directory_iterator());
2✔
1500
        generateFile(t.path() / "test", 1234);
2✔
1501
        REQUIRE(fs::recursive_directory_iterator(t.path()) != fs::recursive_directory_iterator());
2✔
1502
        auto iter = fs::recursive_directory_iterator(t.path());
2✔
1503
        CHECK(iter->path().filename() == "test");
2✔
1504
        CHECK(iter->path() == t.path() / "test");
2✔
1505
        CHECK(!iter->is_symlink());
2✔
1506
        CHECK(iter->is_regular_file());
2✔
1507
        CHECK(!iter->is_directory());
2✔
1508
        CHECK(iter->file_size() == 1234);
2✔
1509
        CHECK(++iter == fs::recursive_directory_iterator());
2✔
1510
    }
2✔
1511

1512
    {
1513
        TemporaryDirectory t;
2✔
1514
        fs::path td = t.path() / "testdir";
2✔
1515
        fs::create_directories(td);
2✔
1516
        generateFile(td / "test", 1234);
2✔
1517
        REQUIRE(fs::recursive_directory_iterator(t.path()) != fs::recursive_directory_iterator());
2✔
1518
        auto iter = fs::recursive_directory_iterator(t.path());
2✔
1519

1520
        CHECK(iter->path().filename() == "testdir");
2✔
1521
        CHECK(iter->path() == td);
2✔
1522
        CHECK(!iter->is_symlink());
2✔
1523
        CHECK(!iter->is_regular_file());
2✔
1524
        CHECK(iter->is_directory());
2✔
1525

1526
        CHECK(++iter != fs::recursive_directory_iterator());
2✔
1527

1528
        CHECK(iter->path().filename() == "test");
2✔
1529
        CHECK(iter->path() == td / "test");
2✔
1530
        CHECK(!iter->is_symlink());
2✔
1531
        CHECK(iter->is_regular_file());
2✔
1532
        CHECK(!iter->is_directory());
2✔
1533
        CHECK(iter->file_size() == 1234);
2✔
1534

1535
        CHECK(++iter == fs::recursive_directory_iterator());
2✔
1536
    }
2✔
1537
    {
1538
        TemporaryDirectory t;
2✔
1539
        std::error_code ec;
2✔
1540
        CHECK(fs::recursive_directory_iterator(t.path(), fs::directory_options::none) == fs::recursive_directory_iterator());
2✔
1541
        CHECK(fs::recursive_directory_iterator(t.path(), fs::directory_options::none, ec) == fs::recursive_directory_iterator());
2✔
1542
        CHECK(!ec);
2✔
1543
        CHECK(fs::recursive_directory_iterator(t.path(), ec) == fs::recursive_directory_iterator());
2✔
1544
        CHECK(!ec);
2✔
1545
        generateFile(t.path() / "test");
2✔
1546
        fs::recursive_directory_iterator rd1(t.path());
2✔
1547
        CHECK(fs::recursive_directory_iterator(rd1) != fs::recursive_directory_iterator());
2✔
1548
        fs::recursive_directory_iterator rd2(t.path());
2✔
1549
        CHECK(fs::recursive_directory_iterator(std::move(rd2)) != fs::recursive_directory_iterator());
2✔
1550
        fs::recursive_directory_iterator rd3(t.path(), fs::directory_options::skip_permission_denied);
2✔
1551
        CHECK(rd3.options() == fs::directory_options::skip_permission_denied);
2✔
1552
        fs::recursive_directory_iterator rd4;
2✔
1553
        rd4 = std::move(rd3);
2✔
1554
        CHECK(rd4 != fs::recursive_directory_iterator());
2✔
1555
        CHECK_NOTHROW(++rd4);
2✔
1556
        CHECK(rd4 == fs::recursive_directory_iterator());
2✔
1557
        fs::recursive_directory_iterator rd5;
2✔
1558
        rd5 = rd4;
2✔
1559
    }
2✔
1560
    {
1561
        TemporaryDirectory t(TempOpt::change_path);
2✔
1562
        generateFile("a");
2✔
1563
        fs::create_directory("d1");
2✔
1564
        fs::create_directory("d1/d2");
2✔
1565
        generateFile("d1/b");
2✔
1566
        generateFile("d1/c");
2✔
1567
        generateFile("d1/d2/d");
2✔
1568
        generateFile("e");
2✔
1569
        auto iter = fs::recursive_directory_iterator(".");
2✔
1570
        std::multimap<std::string, int> result;
2✔
1571
        while(iter != fs::recursive_directory_iterator()) {
16✔
1572
            result.insert(std::make_pair(iter->path().generic_string(), iter.depth()));
14✔
1573
            ++iter;
14✔
1574
        }
1575
        std::stringstream os;
2✔
1576
        for(auto p : result) {
16✔
1577
            os << "[" << p.first << "," << p.second << "],";
14✔
1578
        }
14✔
1579
        CHECK(os.str() == "[./a,0],[./d1,0],[./d1/b,1],[./d1/c,1],[./d1/d2,1],[./d1/d2/d,2],[./e,0],");
2✔
1580
    }
2✔
1581
    {
1582
        TemporaryDirectory t(TempOpt::change_path);
2✔
1583
        generateFile("a");
2✔
1584
        fs::create_directory("d1");
2✔
1585
        fs::create_directory("d1/d2");
2✔
1586
        generateFile("d1/b");
2✔
1587
        generateFile("d1/c");
2✔
1588
        generateFile("d1/d2/d");
2✔
1589
        generateFile("e");
2✔
1590
        std::multiset<std::string> result;
2✔
1591
        for(auto de : fs::recursive_directory_iterator(".")) {
18✔
1592
            result.insert(de.path().generic_string());
14✔
1593
        }
16✔
1594
        std::stringstream os;
2✔
1595
        for(auto p : result) {
16✔
1596
            os << p << ",";
14✔
1597
        }
14✔
1598
        CHECK(os.str() == "./a,./d1,./d1/b,./d1/c,./d1/d2,./d1/d2/d,./e,");
2✔
1599
    }
2✔
1600
    {
1601
        TemporaryDirectory t(TempOpt::change_path);
2✔
1602
        generateFile("a");
2✔
1603
        fs::create_directory("d1");
2✔
1604
        fs::create_directory("d1/d2");
2✔
1605
        generateFile("d1/d2/b");
2✔
1606
        generateFile("e");
2✔
1607
        auto iter = fs::recursive_directory_iterator(".");
2✔
1608
        std::multimap<std::string, int> result;
2✔
1609
        while(iter != fs::recursive_directory_iterator()) {
10✔
1610
            result.insert(std::make_pair(iter->path().generic_string(), iter.depth()));
8✔
1611
            if(iter->path() == "./d1/d2") {
8✔
1612
                iter.disable_recursion_pending();
2✔
1613
            }
1614
            ++iter;
8✔
1615
        }
1616
        std::stringstream os;
2✔
1617
        for(auto p : result) {
10✔
1618
            os << "[" << p.first << "," << p.second << "],";
8✔
1619
        }
8✔
1620
        CHECK(os.str() == "[./a,0],[./d1,0],[./d1/d2,1],[./e,0],");
2✔
1621
    }
2✔
1622
    {
1623
        TemporaryDirectory t(TempOpt::change_path);
2✔
1624
        generateFile("a");
2✔
1625
        fs::create_directory("d1");
2✔
1626
        fs::create_directory("d1/d2");
2✔
1627
        generateFile("d1/d2/b");
2✔
1628
        generateFile("e");
2✔
1629
        auto iter = fs::recursive_directory_iterator(".");
2✔
1630
        std::multimap<std::string, int> result;
2✔
1631
        while(iter != fs::recursive_directory_iterator()) {
10✔
1632
            result.insert(std::make_pair(iter->path().generic_string(), iter.depth()));
8✔
1633
            if(iter->path() == "./d1/d2") {
8✔
1634
                iter.pop();
2✔
1635
            }
1636
            else {
1637
                ++iter;
6✔
1638
            }
1639
        }
1640
        std::stringstream os;
2✔
1641
        for(auto p : result) {
10✔
1642
            os << "[" << p.first << "," << p.second << "],";
8✔
1643
        }
8✔
1644
        CHECK(os.str() == "[./a,0],[./d1,0],[./d1/d2,1],[./e,0],");
2✔
1645
    }
2✔
1646
    if (is_symlink_creation_supported()) {
2✔
1647
        TemporaryDirectory t(TempOpt::change_path);
2✔
1648
        fs::create_directory("d1");
2✔
1649
        generateFile("d1/a");
2✔
1650
        fs::create_directory("d2");
2✔
1651
        generateFile("d2/b");
2✔
1652
        fs::create_directory_symlink("../d1", "d2/ds1");
2✔
1653
        fs::create_directory_symlink("d3", "d2/ds2");
2✔
1654
        std::multiset<std::string> result;
2✔
1655
        REQUIRE_NOTHROW([&](){
14✔
1656
            for (const auto& de : fs::recursive_directory_iterator("d2", fs::directory_options::follow_directory_symlink)) {
1657
                result.insert(de.path().generic_string());
1658
            }
1659
        }());
1660
        std::stringstream os;
2✔
1661
        for(const auto& p : result) {
10✔
1662
            os << p << ",";
8✔
1663
        }
1664
        CHECK(os.str() == "d2/b,d2/ds1,d2/ds1/a,d2/ds2,");
2✔
1665
        os.str("");
2✔
1666
        result.clear();
2✔
1667
        REQUIRE_NOTHROW([&](){
12✔
1668
          for (const auto& de : fs::recursive_directory_iterator("d2")) {
1669
              result.insert(de.path().generic_string());
1670
          }
1671
        }());
1672
         for(const auto& p : result) {
8✔
1673
            os << p << ",";
6✔
1674
        }
1675
        CHECK(os.str() == "d2/b,d2/ds1,d2/ds2,");
2✔
1676
    }
2✔
1677
}
2✔
1678

1679
TEST_CASE("fs.op.absolute - absolute", "[filesystem][operations][fs.op.absolute]")
2✔
1680
{
1681
    CHECK(fs::absolute("") == fs::current_path() / "");
2✔
1682
    CHECK(fs::absolute(fs::current_path()) == fs::current_path());
2✔
1683
    CHECK(fs::absolute(".") == fs::current_path() / ".");
2✔
1684
    CHECK((fs::absolute("..") == fs::current_path().parent_path() || fs::absolute("..") == fs::current_path() / ".."));
2✔
1685
    CHECK(fs::absolute("foo") == fs::current_path() / "foo");
2✔
1686
    std::error_code ec;
2✔
1687
    CHECK(fs::absolute("", ec) == fs::current_path() / "");
2✔
1688
    CHECK(!ec);
2✔
1689
    CHECK(fs::absolute("foo", ec) == fs::current_path() / "foo");
2✔
1690
    CHECK(!ec);
2✔
1691
}
2✔
1692

1693
TEST_CASE("fs.op.canonical - canonical", "[filesystem][operations][fs.op.canonical]")
2✔
1694
{
1695
    CHECK_THROWS_AS(fs::canonical(""), fs::filesystem_error);
4✔
1696
    {
1697
        std::error_code ec;
2✔
1698
        CHECK(fs::canonical("", ec) == "");
2✔
1699
        CHECK(ec);
2✔
1700
    }
1701
    CHECK(fs::canonical(fs::current_path()) == fs::current_path());
2✔
1702

1703
    CHECK(fs::canonical(".") == fs::current_path());
2✔
1704
    CHECK(fs::canonical("..") == fs::current_path().parent_path());
2✔
1705
    CHECK(fs::canonical("/") == fs::current_path().root_path());
2✔
1706
    CHECK_THROWS_AS(fs::canonical("foo"), fs::filesystem_error);
4✔
1707
    {
1708
        std::error_code ec;
2✔
1709
        CHECK_NOTHROW(fs::canonical("foo", ec));
2✔
1710
        CHECK(ec);
2✔
1711
    }
1712
    {
1713
        TemporaryDirectory t(TempOpt::change_path);
2✔
1714
        auto dir = t.path() / "d0";
2✔
1715
        fs::create_directories(dir / "d1");
2✔
1716
        generateFile(dir / "f0");
2✔
1717
        fs::path rel(dir.filename());
2✔
1718
        CHECK(fs::canonical(dir) == dir);
2✔
1719
        CHECK(fs::canonical(rel) == dir);
2✔
1720
        CHECK(fs::canonical(dir / "f0") == dir / "f0");
2✔
1721
        CHECK(fs::canonical(rel / "f0") == dir / "f0");
2✔
1722
        CHECK(fs::canonical(rel / "./f0") == dir / "f0");
2✔
1723
        CHECK(fs::canonical(rel / "d1/../f0") == dir / "f0");
2✔
1724
    }
2✔
1725

1726
    if (is_symlink_creation_supported()) {
2✔
1727
        TemporaryDirectory t(TempOpt::change_path);
2✔
1728
        fs::create_directory(t.path() / "dir1");
2✔
1729
        generateFile(t.path() / "dir1/test1");
2✔
1730
        fs::create_directory(t.path() / "dir2");
2✔
1731
        fs::create_directory_symlink(t.path() / "dir1", t.path() / "dir2/dirSym");
2✔
1732
        CHECK(fs::canonical(t.path() / "dir2/dirSym/test1") == t.path() / "dir1/test1");
2✔
1733
    }
2✔
1734
}
2✔
1735

1736
TEST_CASE("fs.op.copy - copy", "[filesystem][operations][fs.op.copy]")
2✔
1737
{
1738
    {
1739
        TemporaryDirectory t(TempOpt::change_path);
2✔
1740
        std::error_code ec;
2✔
1741
        fs::create_directory("dir1");
2✔
1742
        generateFile("dir1/file1");
2✔
1743
        generateFile("dir1/file2");
2✔
1744
        fs::create_directory("dir1/dir2");
2✔
1745
        generateFile("dir1/dir2/file3");
2✔
1746
        CHECK_NOTHROW(fs::copy("dir1", "dir3"));
2✔
1747
        CHECK(fs::exists("dir3/file1"));
2✔
1748
        CHECK(fs::exists("dir3/file2"));
2✔
1749
        CHECK(!fs::exists("dir3/dir2"));
2✔
1750
        CHECK_NOTHROW(fs::copy("dir1", "dir4", fs::copy_options::recursive, ec));
2✔
1751
        CHECK(!ec);
2✔
1752
        CHECK(fs::exists("dir4/file1"));
2✔
1753
        CHECK(fs::exists("dir4/file2"));
2✔
1754
        CHECK(fs::exists("dir4/dir2/file3"));
2✔
1755
        fs::create_directory("dir5");
2✔
1756
        generateFile("dir5/file1");
2✔
1757
        CHECK_THROWS_AS(fs::copy("dir1/file1", "dir5/file1"), fs::filesystem_error);
6✔
1758
        CHECK_NOTHROW(fs::copy("dir1/file1", "dir5/file1", fs::copy_options::skip_existing));
2✔
1759
    }
2✔
1760
    if (is_symlink_creation_supported()) {
2✔
1761
        TemporaryDirectory t(TempOpt::change_path);
2✔
1762
        std::error_code ec;
2✔
1763
        fs::create_directory("dir1");
2✔
1764
        generateFile("dir1/file1");
2✔
1765
        generateFile("dir1/file2");
2✔
1766
        fs::create_directory("dir1/dir2");
2✔
1767
        generateFile("dir1/dir2/file3");
2✔
1768
#ifdef TEST_LWG_2682_BEHAVIOUR
1769
        REQUIRE_THROWS_AS(fs::copy("dir1", "dir3", fs::copy_options::create_symlinks | fs::copy_options::recursive), fs::filesystem_error);
6✔
1770
#else
1771
        REQUIRE_NOTHROW(fs::copy("dir1", "dir3", fs::copy_options::create_symlinks | fs::copy_options::recursive));
1772
        CHECK(!ec);
1773
        CHECK(fs::exists("dir3/file1"));
1774
        CHECK(fs::is_symlink("dir3/file1"));
1775
        CHECK(fs::exists("dir3/file2"));
1776
        CHECK(fs::is_symlink("dir3/file2"));
1777
        CHECK(fs::exists("dir3/dir2/file3"));
1778
        CHECK(fs::is_symlink("dir3/dir2/file3"));
1779
#endif
1780
    }
2✔
1781
#ifndef GHC_OS_WEB
1782
    {
1783
        TemporaryDirectory t(TempOpt::change_path);
2✔
1784
        std::error_code ec;
2✔
1785
        fs::create_directory("dir1");
2✔
1786
        generateFile("dir1/file1");
2✔
1787
        generateFile("dir1/file2");
2✔
1788
        fs::create_directory("dir1/dir2");
2✔
1789
        generateFile("dir1/dir2/file3");
2✔
1790
        auto f1hl = fs::hard_link_count("dir1/file1");
2✔
1791
        auto f2hl = fs::hard_link_count("dir1/file2");
2✔
1792
        auto f3hl = fs::hard_link_count("dir1/dir2/file3");
2✔
1793
        CHECK_NOTHROW(fs::copy("dir1", "dir3", fs::copy_options::create_hard_links | fs::copy_options::recursive, ec));
2✔
1794
        REQUIRE(!ec);
2✔
1795
        CHECK(fs::exists("dir3/file1"));
2✔
1796
        CHECK(fs::hard_link_count("dir1/file1") == f1hl + 1);
2✔
1797
        CHECK(fs::exists("dir3/file2"));
2✔
1798
        CHECK(fs::hard_link_count("dir1/file2") == f2hl + 1);
2✔
1799
        CHECK(fs::exists("dir3/dir2/file3"));
2✔
1800
        CHECK(fs::hard_link_count("dir1/dir2/file3") == f3hl + 1);
2✔
1801
    }
2✔
1802
#endif
1803
}
2✔
1804

1805
TEST_CASE("fs.op.copy_file - copy_file", "[filesystem][operations][fs.op.copy_file]")
2✔
1806
{
1807
    TemporaryDirectory t(TempOpt::change_path);
2✔
1808
    std::error_code ec;
2✔
1809
    generateFile("foo", 100);
2✔
1810
    CHECK(!fs::exists("bar"));
2✔
1811
    CHECK(fs::copy_file("foo", "bar"));
2✔
1812
    CHECK(fs::exists("bar"));
2✔
1813
    CHECK(fs::file_size("foo") == fs::file_size("bar"));
2✔
1814
    CHECK(fs::copy_file("foo", "bar2", ec));
2✔
1815
    CHECK(!ec);
2✔
1816
    std::this_thread::sleep_for(std::chrono::seconds(1));
2✔
1817
    generateFile("foo2", 200);
2✔
1818
    CHECK(fs::copy_file("foo2", "bar", fs::copy_options::update_existing));
2✔
1819
    CHECK(fs::file_size("bar") == 200);
2✔
1820
    CHECK(!fs::copy_file("foo", "bar", fs::copy_options::update_existing));
2✔
1821
    CHECK(fs::file_size("bar") == 200);
2✔
1822
    CHECK(fs::copy_file("foo", "bar", fs::copy_options::overwrite_existing));
2✔
1823
    CHECK(fs::file_size("bar") == 100);
2✔
1824
    CHECK_THROWS_AS(fs::copy_file("foobar", "foobar2"), fs::filesystem_error);
6✔
1825
    CHECK_NOTHROW(fs::copy_file("foobar", "foobar2", ec));
2✔
1826
    CHECK(ec);
2✔
1827
    CHECK(!fs::exists("foobar"));
2✔
1828
    fs::path file1("temp1.txt");
2✔
1829
    fs::path file2("temp2.txt");
2✔
1830
    generateFile(file1, 200);
2✔
1831
    generateFile(file2, 200);
2✔
1832
    auto allWrite = fs::perms::owner_write | fs::perms::group_write | fs::perms::others_write;
2✔
1833
    CHECK_NOTHROW(fs::permissions(file1, allWrite, fs::perm_options::remove));
2✔
1834
    CHECK((fs::status(file1).permissions() & fs::perms::owner_write) != fs::perms::owner_write);
2✔
1835
    CHECK_NOTHROW(fs::permissions(file2, allWrite, fs::perm_options::add));
2✔
1836
    CHECK((fs::status(file2).permissions() & fs::perms::owner_write) == fs::perms::owner_write);
2✔
1837
    fs::copy_file(file1, file2, fs::copy_options::overwrite_existing);
2✔
1838
    CHECK((fs::status(file2).permissions() & fs::perms::owner_write) != fs::perms::owner_write);
2✔
1839
    CHECK_NOTHROW(fs::permissions(file1, allWrite, fs::perm_options::add));
2✔
1840
    CHECK_NOTHROW(fs::permissions(file2, allWrite, fs::perm_options::add));
2✔
1841
}
2✔
1842

1843
TEST_CASE("fs.op.copy_symlink - copy_symlink", "[filesystem][operations][fs.op.copy_symlink]")
2✔
1844
{
1845
    TemporaryDirectory t(TempOpt::change_path);
2✔
1846
    std::error_code ec;
2✔
1847
    generateFile("foo");
2✔
1848
    fs::create_directory("dir");
2✔
1849
    if (is_symlink_creation_supported()) {
2✔
1850
        fs::create_symlink("foo", "sfoo");
2✔
1851
        fs::create_directory_symlink("dir", "sdir");
2✔
1852
        CHECK_NOTHROW(fs::copy_symlink("sfoo", "sfooc"));
2✔
1853
        CHECK(fs::exists("sfooc"));
2✔
1854
        CHECK_NOTHROW(fs::copy_symlink("sfoo", "sfooc2", ec));
2✔
1855
        CHECK(fs::exists("sfooc2"));
2✔
1856
        CHECK(!ec);
2✔
1857
        CHECK_NOTHROW(fs::copy_symlink("sdir", "sdirc"));
2✔
1858
        CHECK(fs::exists("sdirc"));
2✔
1859
        CHECK_NOTHROW(fs::copy_symlink("sdir", "sdirc2", ec));
2✔
1860
        CHECK(fs::exists("sdirc2"));
2✔
1861
        CHECK(!ec);
2✔
1862
    }
1863
    CHECK_THROWS_AS(fs::copy_symlink("bar", "barc"), fs::filesystem_error);
6✔
1864
    CHECK_NOTHROW(fs::copy_symlink("bar", "barc", ec));
2✔
1865
    CHECK(ec);
2✔
1866
}
2✔
1867

1868
TEST_CASE("fs.op.create_directories - create_directories", "[filesystem][operations][fs.op.create_directories]")
2✔
1869
{
1870
    TemporaryDirectory t;
2✔
1871
    fs::path p = t.path() / "testdir";
2✔
1872
    fs::path p2 = p / "nested";
2✔
1873
    REQUIRE(!fs::exists(p));
2✔
1874
    REQUIRE(!fs::exists(p2));
2✔
1875
    CHECK(fs::create_directories(p2));
2✔
1876
    CHECK(fs::is_directory(p));
2✔
1877
    CHECK(fs::is_directory(p2));
2✔
1878
    CHECK(!fs::create_directories(p2));
2✔
1879
#ifdef TEST_LWG_2935_BEHAVIOUR
1880
    INFO("This test expects LWG #2935 result conformance.");
1881
    p = t.path() / "testfile";
1882
    generateFile(p);
1883
    CHECK(fs::is_regular_file(p));
1884
    CHECK(!fs::is_directory(p));
1885
    bool created = false;
1886
    CHECK_NOTHROW((created = fs::create_directories(p)));
1887
    CHECK(!created);
1888
    CHECK(fs::is_regular_file(p));
1889
    CHECK(!fs::is_directory(p));
1890
    std::error_code ec;
1891
    CHECK_NOTHROW((created = fs::create_directories(p, ec)));
1892
    CHECK(!created);
1893
    CHECK(!ec);
1894
    CHECK(fs::is_regular_file(p));
1895
    CHECK(!fs::is_directory(p));
1896
    CHECK(!fs::create_directories(p, ec));
1897
#else
1898
    INFO("This test expects conformance with P1164R1. (implemented by GCC with issue #86910.)");
2✔
1899
    p = t.path() / "testfile";
2✔
1900
    generateFile(p);
2✔
1901
    CHECK(fs::is_regular_file(p));
2✔
1902
    CHECK(!fs::is_directory(p));
2✔
1903
    CHECK_THROWS_AS(fs::create_directories(p), fs::filesystem_error);
2✔
1904
    CHECK(fs::is_regular_file(p));
2✔
1905
    CHECK(!fs::is_directory(p));
2✔
1906
    std::error_code ec;
2✔
1907
    CHECK_NOTHROW(fs::create_directories(p, ec));
2✔
1908
    CHECK(ec);
2✔
1909
    CHECK(fs::is_regular_file(p));
2✔
1910
    CHECK(!fs::is_directory(p));
2✔
1911
    CHECK(!fs::create_directories(p, ec));
2✔
1912
#endif
1913
}
2✔
1914

1915
TEST_CASE("fs.op.create_directory - create_directory", "[filesystem][operations][fs.op.create_directory]")
2✔
1916
{
1917
    TemporaryDirectory t;
2✔
1918
    fs::path p = t.path() / "testdir";
2✔
1919
    REQUIRE(!fs::exists(p));
2✔
1920
    CHECK(fs::create_directory(p));
2✔
1921
    CHECK(fs::is_directory(p));
2✔
1922
    CHECK(!fs::is_regular_file(p));
2✔
1923
    CHECK(fs::create_directory(p / "nested", p));
2✔
1924
    CHECK(fs::is_directory(p / "nested"));
2✔
1925
    CHECK(!fs::is_regular_file(p / "nested"));
2✔
1926
#ifdef TEST_LWG_2935_BEHAVIOUR
1927
    INFO("This test expects LWG #2935 result conformance.");
1928
    p = t.path() / "testfile";
1929
    generateFile(p);
1930
    CHECK(fs::is_regular_file(p));
1931
    CHECK(!fs::is_directory(p));
1932
    bool created = false;
1933
    CHECK_NOTHROW((created = fs::create_directory(p)));
1934
    CHECK(!created);
1935
    CHECK(fs::is_regular_file(p));
1936
    CHECK(!fs::is_directory(p));
1937
    std::error_code ec;
1938
    CHECK_NOTHROW((created = fs::create_directory(p, ec)));
1939
    CHECK(!created);
1940
    CHECK(!ec);
1941
    CHECK(fs::is_regular_file(p));
1942
    CHECK(!fs::is_directory(p));
1943
    CHECK(!fs::create_directories(p, ec));
1944
#else
1945
    INFO("This test expects conformance with P1164R1. (implemented by GCC with issue #86910.)");
2✔
1946
    p = t.path() / "testfile";
2✔
1947
    generateFile(p);
2✔
1948
    CHECK(fs::is_regular_file(p));
2✔
1949
    CHECK(!fs::is_directory(p));
2✔
1950
    REQUIRE_THROWS_AS(fs::create_directory(p), fs::filesystem_error);
2✔
1951
    CHECK(fs::is_regular_file(p));
2✔
1952
    CHECK(!fs::is_directory(p));
2✔
1953
    std::error_code ec;
2✔
1954
    REQUIRE_NOTHROW(fs::create_directory(p, ec));
2✔
1955
    CHECK(ec);
2✔
1956
    CHECK(fs::is_regular_file(p));
2✔
1957
    CHECK(!fs::is_directory(p));
2✔
1958
    CHECK(!fs::create_directory(p, ec));
2✔
1959
#endif
1960
}
2✔
1961

1962
TEST_CASE("fs.op.create_directory_symlink - create_directory_symlink", "[filesystem][operations][fs.op.create_directory_symlink]")
2✔
1963
{
1964
    if (is_symlink_creation_supported()) {
2✔
1965
        TemporaryDirectory t;
2✔
1966
        fs::create_directory(t.path() / "dir1");
2✔
1967
        generateFile(t.path() / "dir1/test1");
2✔
1968
        fs::create_directory(t.path() / "dir2");
2✔
1969
        fs::create_directory_symlink(t.path() / "dir1", t.path() / "dir2/dirSym");
2✔
1970
        CHECK(fs::exists(t.path() / "dir2/dirSym"));
2✔
1971
        CHECK(fs::is_symlink(t.path() / "dir2/dirSym"));
2✔
1972
        CHECK(fs::exists(t.path() / "dir2/dirSym/test1"));
2✔
1973
        CHECK(fs::is_regular_file(t.path() / "dir2/dirSym/test1"));
2✔
1974
        CHECK_THROWS_AS(fs::create_directory_symlink(t.path() / "dir1", t.path() / "dir2/dirSym"), fs::filesystem_error);
10✔
1975
        std::error_code ec;
2✔
1976
        CHECK_NOTHROW(fs::create_directory_symlink(t.path() / "dir1", t.path() / "dir2/dirSym", ec));
2✔
1977
        CHECK(ec);
2✔
1978
    }
2✔
1979
}
2✔
1980

1981
TEST_CASE("fs.op.create_hard_link - create_hard_link", "[filesystem][operations][fs.op.create_hard_link]")
2✔
1982
{
1983
#ifndef GHC_OS_WEB
1984
    TemporaryDirectory t(TempOpt::change_path);
2✔
1985
    std::error_code ec;
2✔
1986
    generateFile("foo", 1234);
2✔
1987
    CHECK_NOTHROW(fs::create_hard_link("foo", "bar"));
2✔
1988
    CHECK(fs::exists("bar"));
2✔
1989
    CHECK(!fs::is_symlink("bar"));
2✔
1990
    CHECK_NOTHROW(fs::create_hard_link("foo", "bar2", ec));
2✔
1991
    CHECK(fs::exists("bar2"));
2✔
1992
    CHECK(!fs::is_symlink("bar2"));
2✔
1993
    CHECK(!ec);
2✔
1994
    CHECK_THROWS_AS(fs::create_hard_link("nofoo", "bar"), fs::filesystem_error);
6✔
1995
    CHECK_NOTHROW(fs::create_hard_link("nofoo", "bar", ec));
2✔
1996
    CHECK(ec);
2✔
1997
#endif
1998
}
2✔
1999

2000
TEST_CASE("fs.op.create_symlink - create_symlink", "[filesystem][operations][fs.op.create_symlink]")
2✔
2001
{
2002
    if (is_symlink_creation_supported()) {
2✔
2003
        TemporaryDirectory t;
2✔
2004
        fs::create_directory(t.path() / "dir1");
2✔
2005
        generateFile(t.path() / "dir1/test1");
2✔
2006
        fs::create_directory(t.path() / "dir2");
2✔
2007
        fs::create_symlink(t.path() / "dir1/test1", t.path() / "dir2/fileSym");
2✔
2008
        CHECK(fs::exists(t.path() / "dir2/fileSym"));
2✔
2009
        CHECK(fs::is_symlink(t.path() / "dir2/fileSym"));
2✔
2010
        CHECK(fs::exists(t.path() / "dir2/fileSym"));
2✔
2011
        CHECK(fs::is_regular_file(t.path() / "dir2/fileSym"));
2✔
2012
        CHECK_THROWS_AS(fs::create_symlink(t.path() / "dir1", t.path() / "dir2/fileSym"), fs::filesystem_error);
10✔
2013
        std::error_code ec;
2✔
2014
        CHECK_NOTHROW(fs::create_symlink(t.path() / "dir1", t.path() / "dir2/fileSym", ec));
2✔
2015
        CHECK(ec);
2✔
2016
    }
2✔
2017
}
2✔
2018

2019
TEST_CASE("fs.op.current_path - current_path", "[filesystem][operations][fs.op.current_path]")
2✔
2020
{
2021
    TemporaryDirectory t;
2✔
2022
    std::error_code ec;
2✔
2023
    fs::path p1 = fs::current_path();
2✔
2024
    CHECK_NOTHROW(fs::current_path(t.path()));
2✔
2025
    CHECK(p1 != fs::current_path());
2✔
2026
    CHECK_NOTHROW(fs::current_path(p1, ec));
2✔
2027
    CHECK(!ec);
2✔
2028
    CHECK_THROWS_AS(fs::current_path(t.path() / "foo"), fs::filesystem_error);
6✔
2029
    CHECK(p1 == fs::current_path());
2✔
2030
    CHECK_NOTHROW(fs::current_path(t.path() / "foo", ec));
2✔
2031
    CHECK(ec);
2✔
2032
}
2✔
2033

2034
TEST_CASE("fs.op.equivalent - equivalent", "[filesystem][operations][fs.op.equivalent]")
2✔
2035
{
2036
    TemporaryDirectory t(TempOpt::change_path);
2✔
2037
    generateFile("foo", 1234);
2✔
2038
    CHECK(fs::equivalent(t.path() / "foo", "foo"));
2✔
2039
    if (is_symlink_creation_supported()) {
2✔
2040
        std::error_code ec(42, std::system_category());
2✔
2041
        fs::create_symlink("foo", "foo2");
2✔
2042
        CHECK(fs::equivalent("foo", "foo2"));
2✔
2043
        CHECK(fs::equivalent("foo", "foo2", ec));
2✔
2044
        CHECK(!ec);
2✔
2045
    }
2046
#ifdef TEST_LWG_2937_BEHAVIOUR
2047
    INFO("This test expects LWG #2937 result conformance.");
2✔
2048
    std::error_code ec;
2✔
2049
    bool result = false;
2✔
2050
    REQUIRE_THROWS_AS(fs::equivalent("foo", "foo3"), fs::filesystem_error);
6✔
2051
    CHECK_NOTHROW(result = fs::equivalent("foo", "foo3", ec));
2✔
2052
    CHECK(!result);
2✔
2053
    CHECK(ec);
2✔
2054
    ec.clear();
2✔
2055
    CHECK_THROWS_AS(fs::equivalent("foo3", "foo"), fs::filesystem_error);
6✔
2056
    CHECK_NOTHROW(result = fs::equivalent("foo3", "foo", ec));
2✔
2057
    CHECK(!result);
2✔
2058
    CHECK(ec);
2✔
2059
    ec.clear();
2✔
2060
    CHECK_THROWS_AS(fs::equivalent("foo3", "foo4"), fs::filesystem_error);
6✔
2061
    CHECK_NOTHROW(result = fs::equivalent("foo3", "foo4", ec));
2✔
2062
    CHECK(!result);
2✔
2063
    CHECK(ec);
2✔
2064
#else
2065
    INFO("This test expects conformance predating LWG #2937 result.");
2066
    std::error_code ec;
2067
    bool result = false;
2068
    REQUIRE_NOTHROW(result = fs::equivalent("foo", "foo3"));
2069
    CHECK(!result);
2070
    CHECK_NOTHROW(result = fs::equivalent("foo", "foo3", ec));
2071
    CHECK(!result);
2072
    CHECK(!ec);
2073
    ec.clear();
2074
    CHECK_NOTHROW(result = fs::equivalent("foo3", "foo"));
2075
    CHECK(!result);
2076
    CHECK_NOTHROW(result = fs::equivalent("foo3", "foo", ec));
2077
    CHECK(!result);
2078
    CHECK(!ec);
2079
    ec.clear();
2080
    CHECK_THROWS_AS(result = fs::equivalent("foo4", "foo3"), fs::filesystem_error);
2081
    CHECK(!result);
2082
    CHECK_NOTHROW(result = fs::equivalent("foo4", "foo3", ec));
2083
    CHECK(!result);
2084
    CHECK(ec);
2085
#endif
2086
}
2✔
2087

2088
TEST_CASE("fs.op.exists - exists", "[filesystem][operations][fs.op.exists]")
2✔
2089
{
2090
    TemporaryDirectory t(TempOpt::change_path);
2✔
2091
    std::error_code ec;
2✔
2092
    CHECK(!fs::exists(""));
2✔
2093
    CHECK(!fs::exists("foo"));
2✔
2094
    CHECK(!fs::exists("foo", ec));
2✔
2095
    CHECK(!ec);
2✔
2096
    ec = std::error_code(42, std::system_category());
2✔
2097
    CHECK(!fs::exists("foo", ec));
2✔
2098
#if defined(__cpp_lib_char8_t) && !defined(GHC_FILESYSTEM_ENFORCE_CPP17_API)
2099
    CHECK(!fs::exists(u8"foo"));
1✔
2100
#endif
2101
    CHECK(!ec);
2✔
2102
    ec.clear();
2✔
2103
    CHECK(fs::exists(t.path()));
2✔
2104
    CHECK(fs::exists(t.path(), ec));
2✔
2105
    CHECK(!ec);
2✔
2106
    ec = std::error_code(42, std::system_category());
2✔
2107
    CHECK(fs::exists(t.path(), ec));
2✔
2108
    CHECK(!ec);
2✔
2109
#if defined(GHC_OS_WINDOWS) && !defined(GHC_FILESYSTEM_FWD)
2110
    if (::GetFileAttributesW(L"C:\\fs-test") != INVALID_FILE_ATTRIBUTES) {
2111
        CHECK(fs::exists("C:\\fs-test"));    
2112
    }
2113
#endif
2114
}
2✔
2115

2116
TEST_CASE("fs.op.file_size - file_size", "[filesystem][operations][fs.op.file_size]")
2✔
2117
{
2118
    TemporaryDirectory t(TempOpt::change_path);
2✔
2119
    std::error_code ec;
2✔
2120
    generateFile("foo", 0);
2✔
2121
    generateFile("bar", 1234);
2✔
2122
    CHECK(fs::file_size("foo") == 0);
2✔
2123
    ec = std::error_code(42, std::system_category());
2✔
2124
    CHECK(fs::file_size("foo", ec) == 0);
2✔
2125
    CHECK(!ec);
2✔
2126
    ec.clear();
2✔
2127
    CHECK(fs::file_size("bar") == 1234);
2✔
2128
    ec = std::error_code(42, std::system_category());
2✔
2129
    CHECK(fs::file_size("bar", ec) == 1234);
2✔
2130
    CHECK(!ec);
2✔
2131
    ec.clear();
2✔
2132
    CHECK_THROWS_AS(fs::file_size("foobar"), fs::filesystem_error);
4✔
2133
    CHECK(fs::file_size("foobar", ec) == static_cast<uintmax_t>(-1));
2✔
2134
    CHECK(ec);
2✔
2135
    ec.clear();
2✔
2136
}
2✔
2137

2138
#ifndef GHC_OS_WINDOWS
2139
static uintmax_t getHardlinkCount(const fs::path& p)
4✔
2140
{
2141
    struct stat st = {};
4✔
2142
    auto rc = ::lstat(p.c_str(), &st);
4✔
2143
    return rc == 0 ? st.st_nlink : ~0u;
4✔
2144
}
2145
#endif
2146

2147
TEST_CASE("fs.op.hard_link_count - hard_link_count", "[filesystem][operations][fs.op.hard_link_count]")
2✔
2148
{
2149
#ifndef GHC_OS_WEB
2150
    TemporaryDirectory t(TempOpt::change_path);
2✔
2151
    std::error_code ec;
2✔
2152
#ifdef GHC_OS_WINDOWS
2153
    // windows doesn't implement "."/".." as hardlinks, so it
2154
    // starts with 1 and subdirectories don't change the count
2155
    CHECK(fs::hard_link_count(t.path()) == 1);
2156
    fs::create_directory("dir");
2157
    CHECK(fs::hard_link_count(t.path()) == 1);
2158
#else
2159
    // unix/bsd/linux typically implements "."/".." as hardlinks
2160
    // so an empty dir has 2 (from parent and the ".") and
2161
    // adding a subdirectory adds one due to its ".."
2162
    CHECK(fs::hard_link_count(t.path()) == getHardlinkCount(t.path()));
2✔
2163
    fs::create_directory("dir");
2✔
2164
    CHECK(fs::hard_link_count(t.path()) == getHardlinkCount(t.path()));
2✔
2165
#endif
2166
    generateFile("foo");
2✔
2167
    CHECK(fs::hard_link_count(t.path() / "foo") == 1);
2✔
2168
    ec = std::error_code(42, std::system_category());
2✔
2169
    CHECK(fs::hard_link_count(t.path() / "foo", ec) == 1);
2✔
2170
    CHECK(!ec);
2✔
2171
    CHECK_THROWS_AS(fs::hard_link_count(t.path() / "bar"), fs::filesystem_error);
6✔
2172
    CHECK_NOTHROW(fs::hard_link_count(t.path() / "bar", ec));
2✔
2173
    CHECK(ec);
2✔
2174
    ec.clear();
2✔
2175
#else
2176
    WARN("Test for unsupportet features are disabled on JS/Wasm target.");
2177
#endif
2178
}
2✔
2179

2180
class FileTypeMixFixture
2181
{
2182
public:
2183
    FileTypeMixFixture()
16✔
2184
        : _t(TempOpt::change_path)
16✔
2185
        , _hasFifo(false)
16✔
2186
        , _hasSocket(false)
16✔
2187
    {
2188
        generateFile("regular");
16✔
2189
        fs::create_directory("directory");
16✔
2190
        if (is_symlink_creation_supported()) {
16✔
2191
            fs::create_symlink("regular", "file_symlink");
16✔
2192
            fs::create_directory_symlink("directory", "dir_symlink");
16✔
2193
        }
2194
#if !defined(GHC_OS_WINDOWS) && !defined(GHC_OS_WEB)
2195
        REQUIRE(::mkfifo("fifo", 0644) == 0);
16✔
2196
        _hasFifo = true;
16✔
2197
        struct ::sockaddr_un addr;
2198
        addr.sun_family = AF_UNIX;
16✔
2199
        std::strncpy(addr.sun_path, "socket", sizeof(addr.sun_path));
16✔
2200
        int fd = socket(PF_UNIX, SOCK_STREAM, 0);
16✔
2201
        bind(fd, (struct sockaddr*)&addr, sizeof addr);
16✔
2202
        _hasSocket = true;
16✔
2203
#endif
2204
    }
16✔
2205

2206
    ~FileTypeMixFixture() {}
16✔
2207

2208
    bool has_fifo() const { return _hasFifo; }
16✔
2209

2210
    bool has_socket() const { return _hasSocket; }
16✔
2211

2212
    fs::path block_path() const
16✔
2213
    {
2214
        std::error_code ec;
16✔
2215
        if (fs::exists("/dev/sda", ec)) {
16✔
2216
            return "/dev/sda";
×
2217
        }
2218
        else if (fs::exists("/dev/disk0", ec)) {
16✔
2219
            return "/dev/disk0";
×
2220
        }
2221
        return fs::path();
16✔
2222
    }
2223

2224
    fs::path character_path() const
32✔
2225
    {
2226
#ifndef GHC_OS_SOLARIS
2227
        std::error_code ec;
32✔
2228
        if (fs::exists("/dev/null", ec)) {
32✔
2229
            return "/dev/null";
32✔
2230
        }
2231
        else if (fs::exists("NUL", ec)) {
×
2232
            return "NUL";
×
2233
        }
2234
#endif
2235
        return fs::path();
×
2236
    }
2237
    fs::path temp_path() const { return _t.path(); }
2238

2239
private:
2240
    TemporaryDirectory _t;
2241
    bool _hasFifo;
2242
    bool _hasSocket;
2243
};
2244

2245
TEST_CASE_METHOD(FileTypeMixFixture, "fs.op.is_block_file - is_block_file", "[filesystem][operations][fs.op.is_block_file]")
2✔
2246
{
2247
    std::error_code ec;
2✔
2248
    CHECK(!fs::is_block_file("directory"));
2✔
2249
    CHECK(!fs::is_block_file("regular"));
2✔
2250
    if (is_symlink_creation_supported()) {
2✔
2251
        CHECK(!fs::is_block_file("dir_symlink"));
2✔
2252
        CHECK(!fs::is_block_file("file_symlink"));
2✔
2253
    }
2254
    CHECK((has_fifo() ? !fs::is_block_file("fifo") : true));
2✔
2255
    CHECK((has_socket() ? !fs::is_block_file("socket") : true));
2✔
2256
    CHECK((block_path().empty() ? true : fs::is_block_file(block_path())));
2✔
2257
    CHECK((character_path().empty() ? true : !fs::is_block_file(character_path())));
2✔
2258
    CHECK_NOTHROW(fs::is_block_file("notfound"));
2✔
2259
    CHECK_NOTHROW(fs::is_block_file("notfound", ec));
2✔
2260
    CHECK(ec);
2✔
2261
    ec.clear();
2✔
2262
    CHECK(!fs::is_block_file(fs::file_status(fs::file_type::none)));
2✔
2263
    CHECK(!fs::is_block_file(fs::file_status(fs::file_type::not_found)));
2✔
2264
    CHECK(!fs::is_block_file(fs::file_status(fs::file_type::regular)));
2✔
2265
    CHECK(!fs::is_block_file(fs::file_status(fs::file_type::directory)));
2✔
2266
    CHECK(!fs::is_block_file(fs::file_status(fs::file_type::symlink)));
2✔
2267
    CHECK(fs::is_block_file(fs::file_status(fs::file_type::block)));
2✔
2268
    CHECK(!fs::is_block_file(fs::file_status(fs::file_type::character)));
2✔
2269
    CHECK(!fs::is_block_file(fs::file_status(fs::file_type::fifo)));
2✔
2270
    CHECK(!fs::is_block_file(fs::file_status(fs::file_type::socket)));
2✔
2271
    CHECK(!fs::is_block_file(fs::file_status(fs::file_type::unknown)));
2✔
2272
}
2✔
2273

2274
TEST_CASE_METHOD(FileTypeMixFixture, "fs.op.is_character_file - is_character_file", "[filesystem][operations][fs.op.is_character_file]")
2✔
2275
{
2276
    std::error_code ec;
2✔
2277
    CHECK(!fs::is_character_file("directory"));
2✔
2278
    CHECK(!fs::is_character_file("regular"));
2✔
2279
    if (is_symlink_creation_supported()) {
2✔
2280
        CHECK(!fs::is_character_file("dir_symlink"));
2✔
2281
        CHECK(!fs::is_character_file("file_symlink"));
2✔
2282
    }
2283
    CHECK((has_fifo() ? !fs::is_character_file("fifo") : true));
2✔
2284
    CHECK((has_socket() ? !fs::is_character_file("socket") : true));
2✔
2285
    CHECK((block_path().empty() ? true : !fs::is_character_file(block_path())));
2✔
2286
    CHECK((character_path().empty() ? true : fs::is_character_file(character_path())));
2✔
2287
    CHECK_NOTHROW(fs::is_character_file("notfound"));
2✔
2288
    CHECK_NOTHROW(fs::is_character_file("notfound", ec));
2✔
2289
    CHECK(ec);
2✔
2290
    ec.clear();
2✔
2291
    CHECK(!fs::is_character_file(fs::file_status(fs::file_type::none)));
2✔
2292
    CHECK(!fs::is_character_file(fs::file_status(fs::file_type::not_found)));
2✔
2293
    CHECK(!fs::is_character_file(fs::file_status(fs::file_type::regular)));
2✔
2294
    CHECK(!fs::is_character_file(fs::file_status(fs::file_type::directory)));
2✔
2295
    CHECK(!fs::is_character_file(fs::file_status(fs::file_type::symlink)));
2✔
2296
    CHECK(!fs::is_character_file(fs::file_status(fs::file_type::block)));
2✔
2297
    CHECK(fs::is_character_file(fs::file_status(fs::file_type::character)));
2✔
2298
    CHECK(!fs::is_character_file(fs::file_status(fs::file_type::fifo)));
2✔
2299
    CHECK(!fs::is_character_file(fs::file_status(fs::file_type::socket)));
2✔
2300
    CHECK(!fs::is_character_file(fs::file_status(fs::file_type::unknown)));
2✔
2301
}
2✔
2302

2303
TEST_CASE_METHOD(FileTypeMixFixture, "fs.op.is_directory - is_directory", "[filesystem][operations][fs.op.is_directory]")
2✔
2304
{
2305
    std::error_code ec;
2✔
2306
    CHECK(fs::is_directory("directory"));
2✔
2307
    CHECK(!fs::is_directory("regular"));
2✔
2308
    if (is_symlink_creation_supported()) {
2✔
2309
        CHECK(fs::is_directory("dir_symlink"));
2✔
2310
        CHECK(!fs::is_directory("file_symlink"));
2✔
2311
    }
2312
    CHECK((has_fifo() ? !fs::is_directory("fifo") : true));
2✔
2313
    CHECK((has_socket() ? !fs::is_directory("socket") : true));
2✔
2314
    CHECK((block_path().empty() ? true : !fs::is_directory(block_path())));
2✔
2315
    CHECK((character_path().empty() ? true : !fs::is_directory(character_path())));
2✔
2316
    CHECK_NOTHROW(fs::is_directory("notfound"));
2✔
2317
    CHECK_NOTHROW(fs::is_directory("notfound", ec));
2✔
2318
    CHECK(ec);
2✔
2319
    ec.clear();
2✔
2320
    CHECK(!fs::is_directory(fs::file_status(fs::file_type::none)));
2✔
2321
    CHECK(!fs::is_directory(fs::file_status(fs::file_type::not_found)));
2✔
2322
    CHECK(!fs::is_directory(fs::file_status(fs::file_type::regular)));
2✔
2323
    CHECK(fs::is_directory(fs::file_status(fs::file_type::directory)));
2✔
2324
    CHECK(!fs::is_directory(fs::file_status(fs::file_type::symlink)));
2✔
2325
    CHECK(!fs::is_directory(fs::file_status(fs::file_type::block)));
2✔
2326
    CHECK(!fs::is_directory(fs::file_status(fs::file_type::character)));
2✔
2327
    CHECK(!fs::is_directory(fs::file_status(fs::file_type::fifo)));
2✔
2328
    CHECK(!fs::is_directory(fs::file_status(fs::file_type::socket)));
2✔
2329
    CHECK(!fs::is_directory(fs::file_status(fs::file_type::unknown)));
2✔
2330
}
2✔
2331

2332
TEST_CASE("fs.op.is_empty - is_empty", "[filesystem][operations][fs.op.is_empty]")
2✔
2333
{
2334
    TemporaryDirectory t(TempOpt::change_path);
2✔
2335
    std::error_code ec;
2✔
2336
    CHECK(fs::is_empty(t.path()));
2✔
2337
    CHECK(fs::is_empty(t.path(), ec));
2✔
2338
    CHECK(!ec);
2✔
2339
    generateFile("foo", 0);
2✔
2340
    generateFile("bar", 1234);
2✔
2341
    CHECK(fs::is_empty("foo"));
2✔
2342
    CHECK(fs::is_empty("foo", ec));
2✔
2343
    CHECK(!ec);
2✔
2344
    CHECK(!fs::is_empty("bar"));
2✔
2345
    CHECK(!fs::is_empty("bar", ec));
2✔
2346
    CHECK(!ec);
2✔
2347
    CHECK_THROWS_AS(fs::is_empty("foobar"), fs::filesystem_error);
4✔
2348
    bool result = false;
2✔
2349
    CHECK_NOTHROW(result = fs::is_empty("foobar", ec));
2✔
2350
    CHECK(!result);
2✔
2351
    CHECK(ec);
2✔
2352
}
2✔
2353

2354
TEST_CASE_METHOD(FileTypeMixFixture, "fs.op.is_fifo - is_fifo", "[filesystem][operations][fs.op.is_fifo]")
2✔
2355
{
2356
    std::error_code ec;
2✔
2357
    CHECK(!fs::is_fifo("directory"));
2✔
2358
    CHECK(!fs::is_fifo("regular"));
2✔
2359
    if (is_symlink_creation_supported()) {
2✔
2360
        CHECK(!fs::is_fifo("dir_symlink"));
2✔
2361
        CHECK(!fs::is_fifo("file_symlink"));
2✔
2362
    }
2363
    CHECK((has_fifo() ? fs::is_fifo("fifo") : true));
2✔
2364
    CHECK((has_socket() ? !fs::is_fifo("socket") : true));
2✔
2365
    CHECK((block_path().empty() ? true : !fs::is_fifo(block_path())));
2✔
2366
    CHECK((character_path().empty() ? true : !fs::is_fifo(character_path())));
2✔
2367
    CHECK_NOTHROW(fs::is_fifo("notfound"));
2✔
2368
    CHECK_NOTHROW(fs::is_fifo("notfound", ec));
2✔
2369
    CHECK(ec);
2✔
2370
    ec.clear();
2✔
2371
    CHECK(!fs::is_fifo(fs::file_status(fs::file_type::none)));
2✔
2372
    CHECK(!fs::is_fifo(fs::file_status(fs::file_type::not_found)));
2✔
2373
    CHECK(!fs::is_fifo(fs::file_status(fs::file_type::regular)));
2✔
2374
    CHECK(!fs::is_fifo(fs::file_status(fs::file_type::directory)));
2✔
2375
    CHECK(!fs::is_fifo(fs::file_status(fs::file_type::symlink)));
2✔
2376
    CHECK(!fs::is_fifo(fs::file_status(fs::file_type::block)));
2✔
2377
    CHECK(!fs::is_fifo(fs::file_status(fs::file_type::character)));
2✔
2378
    CHECK(fs::is_fifo(fs::file_status(fs::file_type::fifo)));
2✔
2379
    CHECK(!fs::is_fifo(fs::file_status(fs::file_type::socket)));
2✔
2380
    CHECK(!fs::is_fifo(fs::file_status(fs::file_type::unknown)));
2✔
2381
}
2✔
2382

2383
TEST_CASE_METHOD(FileTypeMixFixture, "fs.op.is_other - is_other", "[filesystem][operations][fs.op.is_other]")
2✔
2384
{
2385
    std::error_code ec;
2✔
2386
    CHECK(!fs::is_other("directory"));
2✔
2387
    CHECK(!fs::is_other("regular"));
2✔
2388
    if (is_symlink_creation_supported()) {
2✔
2389
        CHECK(!fs::is_other("dir_symlink"));
2✔
2390
        CHECK(!fs::is_other("file_symlink"));
2✔
2391
    }
2392
    CHECK((has_fifo() ? fs::is_other("fifo") : true));
2✔
2393
    CHECK((has_socket() ? fs::is_other("socket") : true));
2✔
2394
    CHECK((block_path().empty() ? true : fs::is_other(block_path())));
2✔
2395
    CHECK((character_path().empty() ? true : fs::is_other(character_path())));
2✔
2396
    CHECK_NOTHROW(fs::is_other("notfound"));
2✔
2397
    CHECK_NOTHROW(fs::is_other("notfound", ec));
2✔
2398
    CHECK(ec);
2✔
2399
    ec.clear();
2✔
2400
    CHECK(!fs::is_other(fs::file_status(fs::file_type::none)));
2✔
2401
    CHECK(!fs::is_other(fs::file_status(fs::file_type::not_found)));
2✔
2402
    CHECK(!fs::is_other(fs::file_status(fs::file_type::regular)));
2✔
2403
    CHECK(!fs::is_other(fs::file_status(fs::file_type::directory)));
2✔
2404
    CHECK(!fs::is_other(fs::file_status(fs::file_type::symlink)));
2✔
2405
    CHECK(fs::is_other(fs::file_status(fs::file_type::block)));
2✔
2406
    CHECK(fs::is_other(fs::file_status(fs::file_type::character)));
2✔
2407
    CHECK(fs::is_other(fs::file_status(fs::file_type::fifo)));
2✔
2408
    CHECK(fs::is_other(fs::file_status(fs::file_type::socket)));
2✔
2409
    CHECK(fs::is_other(fs::file_status(fs::file_type::unknown)));
2✔
2410
}
2✔
2411

2412
TEST_CASE_METHOD(FileTypeMixFixture, "fs.op.is_regular_file - is_regular_file", "[filesystem][operations][fs.op.is_regular_file]")
2✔
2413
{
2414
    std::error_code ec;
2✔
2415
    CHECK(!fs::is_regular_file("directory"));
2✔
2416
    CHECK(fs::is_regular_file("regular"));
2✔
2417
    if (is_symlink_creation_supported()) {
2✔
2418
        CHECK(!fs::is_regular_file("dir_symlink"));
2✔
2419
        CHECK(fs::is_regular_file("file_symlink"));
2✔
2420
    }
2421
    CHECK((has_fifo() ? !fs::is_regular_file("fifo") : true));
2✔
2422
    CHECK((has_socket() ? !fs::is_regular_file("socket") : true));
2✔
2423
    CHECK((block_path().empty() ? true : !fs::is_regular_file(block_path())));
2✔
2424
    CHECK((character_path().empty() ? true : !fs::is_regular_file(character_path())));
2✔
2425
    CHECK_NOTHROW(fs::is_regular_file("notfound"));
2✔
2426
    CHECK_NOTHROW(fs::is_regular_file("notfound", ec));
2✔
2427
    CHECK(ec);
2✔
2428
    ec.clear();
2✔
2429
    CHECK(!fs::is_regular_file(fs::file_status(fs::file_type::none)));
2✔
2430
    CHECK(!fs::is_regular_file(fs::file_status(fs::file_type::not_found)));
2✔
2431
    CHECK(fs::is_regular_file(fs::file_status(fs::file_type::regular)));
2✔
2432
    CHECK(!fs::is_regular_file(fs::file_status(fs::file_type::directory)));
2✔
2433
    CHECK(!fs::is_regular_file(fs::file_status(fs::file_type::symlink)));
2✔
2434
    CHECK(!fs::is_regular_file(fs::file_status(fs::file_type::block)));
2✔
2435
    CHECK(!fs::is_regular_file(fs::file_status(fs::file_type::character)));
2✔
2436
    CHECK(!fs::is_regular_file(fs::file_status(fs::file_type::fifo)));
2✔
2437
    CHECK(!fs::is_regular_file(fs::file_status(fs::file_type::socket)));
2✔
2438
    CHECK(!fs::is_regular_file(fs::file_status(fs::file_type::unknown)));
2✔
2439
}
2✔
2440

2441
TEST_CASE_METHOD(FileTypeMixFixture, "fs.op.is_socket - is_socket", "[filesystem][operations][fs.op.is_socket]")
2✔
2442
{
2443
    std::error_code ec;
2✔
2444
    CHECK(!fs::is_socket("directory"));
2✔
2445
    CHECK(!fs::is_socket("regular"));
2✔
2446
    if (is_symlink_creation_supported()) {
2✔
2447
        CHECK(!fs::is_socket("dir_symlink"));
2✔
2448
        CHECK(!fs::is_socket("file_symlink"));
2✔
2449
    }
2450
    CHECK((has_fifo() ? !fs::is_socket("fifo") : true));
2✔
2451
    CHECK((has_socket() ? fs::is_socket("socket") : true));
2✔
2452
    CHECK((block_path().empty() ? true : !fs::is_socket(block_path())));
2✔
2453
    CHECK((character_path().empty() ? true : !fs::is_socket(character_path())));
2✔
2454
    CHECK_NOTHROW(fs::is_socket("notfound"));
2✔
2455
    CHECK_NOTHROW(fs::is_socket("notfound", ec));
2✔
2456
    CHECK(ec);
2✔
2457
    ec.clear();
2✔
2458
    CHECK(!fs::is_socket(fs::file_status(fs::file_type::none)));
2✔
2459
    CHECK(!fs::is_socket(fs::file_status(fs::file_type::not_found)));
2✔
2460
    CHECK(!fs::is_socket(fs::file_status(fs::file_type::regular)));
2✔
2461
    CHECK(!fs::is_socket(fs::file_status(fs::file_type::directory)));
2✔
2462
    CHECK(!fs::is_socket(fs::file_status(fs::file_type::symlink)));
2✔
2463
    CHECK(!fs::is_socket(fs::file_status(fs::file_type::block)));
2✔
2464
    CHECK(!fs::is_socket(fs::file_status(fs::file_type::character)));
2✔
2465
    CHECK(!fs::is_socket(fs::file_status(fs::file_type::fifo)));
2✔
2466
    CHECK(fs::is_socket(fs::file_status(fs::file_type::socket)));
2✔
2467
    CHECK(!fs::is_socket(fs::file_status(fs::file_type::unknown)));
2✔
2468
}
2✔
2469

2470
TEST_CASE_METHOD(FileTypeMixFixture, "fs.op.is_symlink - is_symlink", "[filesystem][operations][fs.op.is_symlink]")
2✔
2471
{
2472
    std::error_code ec;
2✔
2473
    CHECK(!fs::is_symlink("directory"));
2✔
2474
    CHECK(!fs::is_symlink("regular"));
2✔
2475
    if (is_symlink_creation_supported()) {
2✔
2476
        CHECK(fs::is_symlink("dir_symlink"));
2✔
2477
        CHECK(fs::is_symlink("file_symlink"));
2✔
2478
    }
2479
    CHECK((has_fifo() ? !fs::is_symlink("fifo") : true));
2✔
2480
    CHECK((has_socket() ? !fs::is_symlink("socket") : true));
2✔
2481
    CHECK((block_path().empty() ? true : !fs::is_symlink(block_path())));
2✔
2482
    CHECK((character_path().empty() ? true : !fs::is_symlink(character_path())));
2✔
2483
    CHECK_NOTHROW(fs::is_symlink("notfound"));
2✔
2484
    CHECK_NOTHROW(fs::is_symlink("notfound", ec));
2✔
2485
    CHECK(ec);
2✔
2486
    ec.clear();
2✔
2487
    CHECK(!fs::is_symlink(fs::file_status(fs::file_type::none)));
2✔
2488
    CHECK(!fs::is_symlink(fs::file_status(fs::file_type::not_found)));
2✔
2489
    CHECK(!fs::is_symlink(fs::file_status(fs::file_type::regular)));
2✔
2490
    CHECK(!fs::is_symlink(fs::file_status(fs::file_type::directory)));
2✔
2491
    CHECK(fs::is_symlink(fs::file_status(fs::file_type::symlink)));
2✔
2492
    CHECK(!fs::is_symlink(fs::file_status(fs::file_type::block)));
2✔
2493
    CHECK(!fs::is_symlink(fs::file_status(fs::file_type::character)));
2✔
2494
    CHECK(!fs::is_symlink(fs::file_status(fs::file_type::fifo)));
2✔
2495
    CHECK(!fs::is_symlink(fs::file_status(fs::file_type::socket)));
2✔
2496
    CHECK(!fs::is_symlink(fs::file_status(fs::file_type::unknown)));
2✔
2497
}
2✔
2498

2499
#ifndef GHC_OS_WEB
2500
static fs::file_time_type timeFromString(const std::string& str)
4✔
2501
{
2502
    struct ::tm tm;
2503
    ::memset(&tm, 0, sizeof(::tm));
4✔
2504
    std::istringstream is(str);
4✔
2505
    is >> std::get_time(&tm, "%Y-%m-%dT%H:%M:%S");
4✔
2506
    if (is.fail()) {
4✔
2507
        throw std::exception();
×
2508
    }
2509
    return from_time_t<fs::file_time_type>(std::mktime(&tm));
8✔
2510
}
4✔
2511
#endif
2512

2513
TEST_CASE("fs.op.last_write_time - last_write_time", "[filesystem][operations][fs.op.last_write_time]")
2✔
2514
{
2515
    TemporaryDirectory t(TempOpt::change_path);
2✔
2516
    std::error_code ec;
2✔
2517
    fs::file_time_type ft;
2✔
2518
    generateFile("foo");
2✔
2519
    auto now = fs::file_time_type::clock::now();
2✔
2520
    CHECK(std::abs(std::chrono::duration_cast<std::chrono::seconds>(fs::last_write_time(t.path()) - now).count()) < 3);
2✔
2521
    CHECK(std::abs(std::chrono::duration_cast<std::chrono::seconds>(fs::last_write_time("foo") - now).count()) < 3);
2✔
2522
    CHECK_THROWS_AS(fs::last_write_time("bar"), fs::filesystem_error);
4✔
2523
    CHECK_NOTHROW(ft = fs::last_write_time("bar", ec));
2✔
2524
    CHECK(ft == fs::file_time_type::min());
2✔
2525
    CHECK(ec);
2✔
2526
    ec.clear();
2✔
2527
    if (is_symlink_creation_supported()) {
2✔
2528
        std::this_thread::sleep_for(std::chrono::seconds(1));
2✔
2529
        fs::create_symlink("foo", "foo2");
2✔
2530
        ft = fs::last_write_time("foo");
2✔
2531
        // checks that the time of the symlink is fetched
2532
        CHECK(ft == fs::last_write_time("foo2"));
2✔
2533
    }
2534
#ifndef GHC_OS_WEB
2535
    auto nt = timeFromString("2015-10-21T04:30:00");
2✔
2536
    CHECK_NOTHROW(fs::last_write_time(t.path() / "foo", nt));
2✔
2537
    CHECK(std::abs(std::chrono::duration_cast<std::chrono::seconds>(fs::last_write_time("foo") - nt).count()) < 1);
2✔
2538
    nt = timeFromString("2015-10-21T04:29:00");
2✔
2539
    CHECK_NOTHROW(fs::last_write_time("foo", nt, ec));
2✔
2540
    CHECK(std::abs(std::chrono::duration_cast<std::chrono::seconds>(fs::last_write_time("foo") - nt).count()) < 1);
2✔
2541
    CHECK(!ec);
2✔
2542
    CHECK_THROWS_AS(fs::last_write_time("bar", nt), fs::filesystem_error);
4✔
2543
    CHECK_NOTHROW(fs::last_write_time("bar", nt, ec));
2✔
2544
    CHECK(ec);
2✔
2545
#endif
2546
}
2✔
2547

2548
TEST_CASE("fs.op.permissions - permissions", "[filesystem][operations][fs.op.permissions]")
2✔
2549
{
2550
    TemporaryDirectory t(TempOpt::change_path);
2✔
2551
    std::error_code ec;
2✔
2552
    generateFile("foo", 512);
2✔
2553
    auto allWrite = fs::perms::owner_write | fs::perms::group_write | fs::perms::others_write;
2✔
2554
    CHECK_NOTHROW(fs::permissions("foo", allWrite, fs::perm_options::remove));
2✔
2555
    CHECK((fs::status("foo").permissions() & fs::perms::owner_write) != fs::perms::owner_write);
2✔
2556
#if !defined(GHC_OS_WINDOWS)
2557
    if (geteuid() != 0)
2✔
2558
#endif
2559
    {
2560
        CHECK_THROWS_AS(fs::resize_file("foo", 1024), fs::filesystem_error);
4✔
2561
        CHECK(fs::file_size("foo") == 512);
2✔
2562
    }
2563
    CHECK_NOTHROW(fs::permissions("foo", fs::perms::owner_write, fs::perm_options::add));
2✔
2564
    CHECK((fs::status("foo").permissions() & fs::perms::owner_write) == fs::perms::owner_write);
2✔
2565
    CHECK_NOTHROW(fs::resize_file("foo", 2048));
2✔
2566
    CHECK(fs::file_size("foo") == 2048);
2✔
2567
    CHECK_THROWS_AS(fs::permissions("bar", fs::perms::owner_write, fs::perm_options::add), fs::filesystem_error);
4✔
2568
    CHECK_NOTHROW(fs::permissions("bar", fs::perms::owner_write, fs::perm_options::add, ec));
2✔
2569
    CHECK(ec);
2✔
2570
    CHECK_THROWS_AS(fs::permissions("bar", fs::perms::owner_write, static_cast<fs::perm_options>(0)), fs::filesystem_error);
4✔
2571
}
2✔
2572

2573
TEST_CASE("fs.op.proximate - proximate", "[filesystem][operations][fs.op.proximate]")
2✔
2574
{
2575
    std::error_code ec;
2✔
2576
    CHECK(fs::proximate("/a/d", "/a/b/c") == "../../d");
2✔
2577
    CHECK(fs::proximate("/a/d", "/a/b/c", ec) == "../../d");
2✔
2578
    CHECK(!ec);
2✔
2579
    CHECK(fs::proximate("/a/b/c", "/a/d") == "../b/c");
2✔
2580
    CHECK(fs::proximate("/a/b/c", "/a/d", ec) == "../b/c");
2✔
2581
    CHECK(!ec);
2✔
2582
    CHECK(fs::proximate("a/b/c", "a") == "b/c");
2✔
2583
    CHECK(fs::proximate("a/b/c", "a", ec) == "b/c");
2✔
2584
    CHECK(!ec);
2✔
2585
    CHECK(fs::proximate("a/b/c", "a/b/c/x/y") == "../..");
2✔
2586
    CHECK(fs::proximate("a/b/c", "a/b/c/x/y", ec) == "../..");
2✔
2587
    CHECK(!ec);
2✔
2588
    CHECK(fs::proximate("a/b/c", "a/b/c") == ".");
2✔
2589
    CHECK(fs::proximate("a/b/c", "a/b/c", ec) == ".");
2✔
2590
    CHECK(!ec);
2✔
2591
    CHECK(fs::proximate("a/b", "c/d") == "../../a/b");
2✔
2592
    CHECK(fs::proximate("a/b", "c/d", ec) == "../../a/b");
2✔
2593
    CHECK(!ec);
2✔
2594
#ifndef GHC_OS_WINDOWS
2595
    if (has_host_root_name_support()) {
2✔
2596
        CHECK(fs::proximate("//host1/a/d", "//host2/a/b/c") == "//host1/a/d");
2✔
2597
        CHECK(fs::proximate("//host1/a/d", "//host2/a/b/c", ec) == "//host1/a/d");
2✔
2598
        CHECK(!ec);
2✔
2599
    }
2600
#endif
2601
}
2✔
2602

2603
TEST_CASE("fs.op.read_symlink - read_symlink", "[filesystem][operations][fs.op.read_symlink]")
2✔
2604
{
2605
    if (is_symlink_creation_supported()) {
2✔
2606
        TemporaryDirectory t(TempOpt::change_path);
2✔
2607
        std::error_code ec;
2✔
2608
        generateFile("foo");
2✔
2609
        fs::create_symlink(t.path() / "foo", "bar");
2✔
2610
        CHECK(fs::read_symlink("bar") == t.path() / "foo");
2✔
2611
        CHECK(fs::read_symlink("bar", ec) == t.path() / "foo");
2✔
2612
        CHECK(!ec);
2✔
2613
        CHECK_THROWS_AS(fs::read_symlink("foobar"), fs::filesystem_error);
4✔
2614
        CHECK(fs::read_symlink("foobar", ec) == fs::path());
2✔
2615
        CHECK(ec);
2✔
2616
    }
2✔
2617
}
2✔
2618

2619
TEST_CASE("fs.op.relative - relative", "[filesystem][operations][fs.op.relative]")
2✔
2620
{
2621
    CHECK(fs::relative("/a/d", "/a/b/c") == "../../d");
2✔
2622
    CHECK(fs::relative("/a/b/c", "/a/d") == "../b/c");
2✔
2623
    CHECK(fs::relative("a/b/c", "a") == "b/c");
2✔
2624
    CHECK(fs::relative("a/b/c", "a/b/c/x/y") == "../..");
2✔
2625
    CHECK(fs::relative("a/b/c", "a/b/c") == ".");
2✔
2626
    CHECK(fs::relative("a/b", "c/d") == "../../a/b");
2✔
2627
    std::error_code ec;
2✔
2628
    CHECK(fs::relative(fs::current_path() / "foo", ec) == "foo");
2✔
2629
    CHECK(!ec);
2✔
2630
}
2✔
2631

2632
TEST_CASE("fs.op.remove - remove", "[filesystem][operations][fs.op.remove]")
2✔
2633
{
2634
    TemporaryDirectory t(TempOpt::change_path);
2✔
2635
    std::error_code ec;
2✔
2636
    generateFile("foo");
2✔
2637
    CHECK(fs::remove("foo"));
2✔
2638
    CHECK(!fs::exists("foo"));
2✔
2639
    CHECK(!fs::remove("foo"));
2✔
2640
    generateFile("foo");
2✔
2641
    CHECK(fs::remove("foo", ec));
2✔
2642
    CHECK(!fs::exists("foo"));
2✔
2643
    if (is_symlink_creation_supported()) {
2✔
2644
        generateFile("foo");
2✔
2645
        fs::create_symlink("foo", "bar");
2✔
2646
        CHECK(fs::exists(fs::symlink_status("bar")));
2✔
2647
        CHECK(fs::remove("bar", ec));
2✔
2648
        CHECK(fs::exists("foo"));
2✔
2649
        CHECK(!fs::exists(fs::symlink_status("bar")));
2✔
2650
    }
2651
    CHECK(!fs::remove("bar"));
2✔
2652
    CHECK(!fs::remove("bar", ec));
2✔
2653
    CHECK(!ec);
2✔
2654
}
2✔
2655

2656
TEST_CASE("fs.op.remove_all - remove_all", "[filesystem][operations][fs.op.remove_all]")
2✔
2657
{
2658
    TemporaryDirectory t(TempOpt::change_path);
2✔
2659
    std::error_code ec;
2✔
2660
    generateFile("foo");
2✔
2661
    CHECK(fs::remove_all("foo", ec) == 1);
2✔
2662
    CHECK(!ec);
2✔
2663
    ec.clear();
2✔
2664
    CHECK(fs::directory_iterator(t.path()) == fs::directory_iterator());
2✔
2665
    fs::create_directories("dir1/dir1a");
2✔
2666
    fs::create_directories("dir1/dir1b");
2✔
2667
    generateFile("dir1/dir1a/f1");
2✔
2668
    generateFile("dir1/dir1b/f2");
2✔
2669
    CHECK_NOTHROW(fs::remove_all("dir1/non-existing", ec));
2✔
2670
    CHECK(!ec);
2✔
2671
    CHECK(fs::remove_all("dir1/non-existing", ec) == 0);
2✔
2672
    if (is_symlink_creation_supported()) {
2✔
2673
        fs::create_directory_symlink("dir1", "dir1link");
2✔
2674
        CHECK(fs::remove_all("dir1link") == 1);
2✔
2675
    }
2676
    CHECK(fs::remove_all("dir1") == 5);
2✔
2677
    CHECK(fs::directory_iterator(t.path()) == fs::directory_iterator());
2✔
2678
}
2✔
2679

2680
TEST_CASE("fs.op.rename - rename", "[filesystem][operations][fs.op.rename]")
2✔
2681
{
2682
    TemporaryDirectory t(TempOpt::change_path);
2✔
2683
    std::error_code ec;
2✔
2684
    generateFile("foo", 123);
2✔
2685
    fs::create_directory("dir1");
2✔
2686
    CHECK_NOTHROW(fs::rename("foo", "bar"));
2✔
2687
    CHECK(!fs::exists("foo"));
2✔
2688
    CHECK(fs::exists("bar"));
2✔
2689
    CHECK_NOTHROW(fs::rename("dir1", "dir2"));
2✔
2690
    CHECK(fs::exists("dir2"));
2✔
2691
    generateFile("foo2", 42);
2✔
2692
    CHECK_NOTHROW(fs::rename("bar", "foo2"));
2✔
2693
    CHECK(fs::exists("foo2"));
2✔
2694
    CHECK(fs::file_size("foo2") == 123u);
2✔
2695
    CHECK(!fs::exists("bar"));
2✔
2696
    CHECK_NOTHROW(fs::rename("foo2", "foo", ec));
2✔
2697
    CHECK(!ec);
2✔
2698
    CHECK_THROWS_AS(fs::rename("foobar", "barfoo"), fs::filesystem_error);
6✔
2699
    CHECK_NOTHROW(fs::rename("foobar", "barfoo", ec));
2✔
2700
    CHECK(ec);
2✔
2701
    CHECK(!fs::exists("barfoo"));
2✔
2702
}
2✔
2703

2704
TEST_CASE("fs.op.resize_file - resize_file", "[filesystem][operations][fs.op.resize_file]")
2✔
2705
{
2706
    TemporaryDirectory t(TempOpt::change_path);
2✔
2707
    std::error_code ec;
2✔
2708
    generateFile("foo", 1024);
2✔
2709
    CHECK(fs::file_size("foo") == 1024);
2✔
2710
    CHECK_NOTHROW(fs::resize_file("foo", 2048));
2✔
2711
    CHECK(fs::file_size("foo") == 2048);
2✔
2712
    CHECK_NOTHROW(fs::resize_file("foo", 1000, ec));
2✔
2713
    CHECK(!ec);
2✔
2714
    CHECK(fs::file_size("foo") == 1000);
2✔
2715
    CHECK_THROWS_AS(fs::resize_file("bar", 2048), fs::filesystem_error);
4✔
2716
    CHECK(!fs::exists("bar"));
2✔
2717
    CHECK_NOTHROW(fs::resize_file("bar", 4096, ec));
2✔
2718
    CHECK(ec);
2✔
2719
    CHECK(!fs::exists("bar"));
2✔
2720
}
2✔
2721

2722
TEST_CASE("fs.op.space - space", "[filesystem][operations][fs.op.space]")
2✔
2723
{
2724
    {
2725
        fs::space_info si;
2726
        CHECK_NOTHROW(si = fs::space(fs::current_path()));
2✔
2727
        CHECK(si.capacity > 1024 * 1024);
2✔
2728
        CHECK(si.capacity > si.free);
2✔
2729
        CHECK(si.free >= si.available);
2✔
2730
    }
2731
    {
2732
        std::error_code ec;
2✔
2733
        fs::space_info si;
2734
        CHECK_NOTHROW(si = fs::space(fs::current_path(), ec));
2✔
2735
        CHECK(si.capacity > 1024 * 1024);
2✔
2736
        CHECK(si.capacity > si.free);
2✔
2737
        CHECK(si.free >= si.available);
2✔
2738
        CHECK(!ec);
2✔
2739
    }
2740
#ifndef GHC_OS_WEB // statvfs under emscripten always returns a result, so this tests would fail
2741
    {
2742
        std::error_code ec;
2✔
2743
        fs::space_info si;
2744
        CHECK_NOTHROW(si = fs::space("foobar42", ec));
2✔
2745
        CHECK(si.capacity == static_cast<uintmax_t>(-1));
2✔
2746
        CHECK(si.free == static_cast<uintmax_t>(-1));
2✔
2747
        CHECK(si.available == static_cast<uintmax_t>(-1));
2✔
2748
        CHECK(ec);
2✔
2749
    }
2750
    CHECK_THROWS_AS(fs::space("foobar42"), fs::filesystem_error);
4✔
2751
#endif
2752
}
2✔
2753

2754
TEST_CASE("fs.op.status - status", "[filesystem][operations][fs.op.status]")
2✔
2755
{
2756
    TemporaryDirectory t(TempOpt::change_path);
2✔
2757
    std::error_code ec;
2✔
2758
    fs::file_status fs;
2✔
2759
    CHECK_NOTHROW(fs = fs::status("foo"));
2✔
2760
    CHECK(fs.type() == fs::file_type::not_found);
2✔
2761
    CHECK(fs.permissions() == fs::perms::unknown);
2✔
2762
    CHECK_NOTHROW(fs = fs::status("bar", ec));
2✔
2763
    CHECK(fs.type() == fs::file_type::not_found);
2✔
2764
    CHECK(fs.permissions() == fs::perms::unknown);
2✔
2765
    CHECK(ec);
2✔
2766
    ec.clear();
2✔
2767
    fs = fs::status(t.path());
2✔
2768
    CHECK(fs.type() == fs::file_type::directory);
2✔
2769
    CHECK((fs.permissions() & (fs::perms::owner_read | fs::perms::owner_write)) == (fs::perms::owner_read | fs::perms::owner_write));
2✔
2770
    generateFile("foobar");
2✔
2771
    fs = fs::status(t.path() / "foobar");
2✔
2772
    CHECK(fs.type() == fs::file_type::regular);
2✔
2773
    CHECK((fs.permissions() & (fs::perms::owner_read | fs::perms::owner_write)) == (fs::perms::owner_read | fs::perms::owner_write));
2✔
2774
    if (is_symlink_creation_supported()) {
2✔
2775
        fs::create_symlink(t.path() / "foobar", t.path() / "barfoo");
2✔
2776
        fs = fs::status(t.path() / "barfoo");
2✔
2777
        CHECK(fs.type() == fs::file_type::regular);
2✔
2778
        CHECK((fs.permissions() & (fs::perms::owner_read | fs::perms::owner_write)) == (fs::perms::owner_read | fs::perms::owner_write));
2✔
2779
    }
2780
}
2✔
2781

2782
TEST_CASE("fs.op.status_known - status_known", "[filesystem][operations][fs.op.status_known]")
2✔
2783
{
2784
    CHECK(!fs::status_known(fs::file_status()));
2✔
2785
    CHECK(fs::status_known(fs::file_status(fs::file_type::not_found)));
2✔
2786
    CHECK(fs::status_known(fs::file_status(fs::file_type::regular)));
2✔
2787
    CHECK(fs::status_known(fs::file_status(fs::file_type::directory)));
2✔
2788
    CHECK(fs::status_known(fs::file_status(fs::file_type::symlink)));
2✔
2789
    CHECK(fs::status_known(fs::file_status(fs::file_type::character)));
2✔
2790
    CHECK(fs::status_known(fs::file_status(fs::file_type::fifo)));
2✔
2791
    CHECK(fs::status_known(fs::file_status(fs::file_type::socket)));
2✔
2792
    CHECK(fs::status_known(fs::file_status(fs::file_type::unknown)));
2✔
2793
}
2✔
2794

2795
TEST_CASE("fs.op.symlink_status - symlink_status", "[filesystem][operations][fs.op.symlink_status]")
2✔
2796
{
2797
    TemporaryDirectory t(TempOpt::change_path);
2✔
2798
    std::error_code ec;
2✔
2799
    fs::file_status fs;
2✔
2800
    CHECK_NOTHROW(fs = fs::symlink_status("foo"));
2✔
2801
    CHECK(fs.type() == fs::file_type::not_found);
2✔
2802
    CHECK(fs.permissions() == fs::perms::unknown);
2✔
2803
    CHECK_NOTHROW(fs = fs::symlink_status("bar", ec));
2✔
2804
    CHECK(fs.type() == fs::file_type::not_found);
2✔
2805
    CHECK(fs.permissions() == fs::perms::unknown);
2✔
2806
    CHECK(ec);
2✔
2807
    ec.clear();
2✔
2808
    fs = fs::symlink_status(t.path());
2✔
2809
    CHECK(fs.type() == fs::file_type::directory);
2✔
2810
    CHECK((fs.permissions() & (fs::perms::owner_read | fs::perms::owner_write)) == (fs::perms::owner_read | fs::perms::owner_write));
2✔
2811
    generateFile("foobar");
2✔
2812
    fs = fs::symlink_status(t.path() / "foobar");
2✔
2813
    CHECK(fs.type() == fs::file_type::regular);
2✔
2814
    CHECK((fs.permissions() & (fs::perms::owner_read | fs::perms::owner_write)) == (fs::perms::owner_read | fs::perms::owner_write));
2✔
2815
    if (is_symlink_creation_supported()) {
2✔
2816
        fs::create_symlink(t.path() / "foobar", t.path() / "barfoo");
2✔
2817
        fs = fs::symlink_status(t.path() / "barfoo");
2✔
2818
        CHECK(fs.type() == fs::file_type::symlink);
2✔
2819
    }
2820
}
2✔
2821

2822
TEST_CASE("fs.op.temp_dir_path - temporary_directory_path", "[filesystem][operations][fs.op.temp_dir_path]")
2✔
2823
{
2824
    std::error_code ec;
2✔
2825
    CHECK_NOTHROW(fs::exists(fs::temp_directory_path()));
2✔
2826
    CHECK_NOTHROW(fs::exists(fs::temp_directory_path(ec)));
2✔
2827
    CHECK(!fs::temp_directory_path().empty());
2✔
2828
    CHECK(!ec);
2✔
2829
}
2✔
2830

2831
TEST_CASE("fs.op.weakly_canonical - weakly_canonical", "[filesystem][operations][fs.op.weakly_canonical]")
2✔
2832
{
2833
    INFO("This might fail on std::implementations that return fs::current_path() for fs::canonical(\"\")");
2✔
2834
    CHECK(fs::weakly_canonical("") == ".");
2✔
2835
    if(fs::weakly_canonical("") == ".") {
2✔
2836
        CHECK(fs::weakly_canonical("foo/bar") == "foo/bar");
2✔
2837
        CHECK(fs::weakly_canonical("foo/./bar") == "foo/bar");
2✔
2838
        CHECK(fs::weakly_canonical("foo/../bar") == "bar");
2✔
2839
    }
2840
    else {
2841
        CHECK(fs::weakly_canonical("foo/bar") == fs::current_path() / "foo/bar");
×
2842
        CHECK(fs::weakly_canonical("foo/./bar") == fs::current_path() / "foo/bar");
×
2843
        CHECK(fs::weakly_canonical("foo/../bar") == fs::current_path() / "bar");
×
2844
    }
2845

2846
    {
2847
        TemporaryDirectory t(TempOpt::change_path);
2✔
2848
        auto dir = t.path() / "d0";
2✔
2849
        fs::create_directories(dir / "d1");
2✔
2850
        generateFile(dir / "f0");
2✔
2851
        fs::path rel(dir.filename());
2✔
2852
        CHECK(fs::weakly_canonical(dir) == dir);
2✔
2853
        CHECK(fs::weakly_canonical(rel) == dir);
2✔
2854
        CHECK(fs::weakly_canonical(dir / "f0") == dir / "f0");
2✔
2855
        CHECK(fs::weakly_canonical(dir / "f0/") == dir / "f0/");
2✔
2856
        CHECK(fs::weakly_canonical(dir / "f1") == dir / "f1");
2✔
2857
        CHECK(fs::weakly_canonical(rel / "f0") == dir / "f0");
2✔
2858
        CHECK(fs::weakly_canonical(rel / "f0/") == dir / "f0/");
2✔
2859
        CHECK(fs::weakly_canonical(rel / "f1") == dir / "f1");
2✔
2860
        CHECK(fs::weakly_canonical(rel / "./f0") == dir / "f0");
2✔
2861
        CHECK(fs::weakly_canonical(rel / "./f1") == dir / "f1");
2✔
2862
        CHECK(fs::weakly_canonical(rel / "d1/../f0") == dir / "f0");
2✔
2863
        CHECK(fs::weakly_canonical(rel / "d1/../f1") == dir / "f1");
2✔
2864
        CHECK(fs::weakly_canonical(rel / "d1/../f1/../f2") == dir / "f2");
2✔
2865
    }
2✔
2866
}
2✔
2867

2868
TEST_CASE("std::string_view support", "[filesystem][fs.string_view]")
2✔
2869
{
2870
#if defined(GHC_HAS_STD_STRING_VIEW) || defined(GHC_HAS_STD_EXPERIMENTAL_STRING_VIEW)
2871

2872
#if defined(GHC_HAS_STD_STRING_VIEW)
2873
    using namespace std::literals;
2874
    using string_view = std::string_view;
2875
    using wstring_view = std::wstring_view;
2876
#elif defined(GHC_HAS_STD_EXPERIMENTAL_STRING_VIEW)
2877
    using string_view = std::experimental::string_view;
2878
    using wstring_view = std::experimental::wstring_view;
2879
#endif
2880

2881
    {
2882
        std::string p("foo/bar");
2✔
2883
        string_view sv(p);
2✔
2884
        CHECK(fs::path(sv, fs::path::format::generic_format).generic_string() == "foo/bar");
2✔
2885
        fs::path p2("fo");
2✔
2886
        p2 += string_view("o");
2✔
2887
        CHECK(p2 == "foo");
2✔
2888
        CHECK(p2.compare(string_view("foo")) == 0);
2✔
2889
    }
2✔
2890
    {
2891
        auto p = fs::path{"XYZ"};
2✔
2892
        p /= string_view("Appendix");
2✔
2893
        CHECK(p == "XYZ/Appendix");
2✔
2894
    }
2✔
2895
    {
2896
        std::wstring p(L"foo/bar");
2✔
2897
        wstring_view sv(p);
2✔
2898
        CHECK(fs::path(sv, fs::path::format::generic_format).generic_string() == "foo/bar");
2✔
2899
        fs::path p2(L"fo");
2✔
2900
        p2 += wstring_view(L"o");
2✔
2901
        CHECK(p2 == "foo");
2✔
2902
        CHECK(p2.compare(wstring_view(L"foo")) == 0);
2✔
2903
    }
2✔
2904

2905
#else
2906
    WARN("std::string_view specific tests are empty without std::string_view.");
2907
#endif
2908
}
2✔
2909

2910
TEST_CASE("Windows: Long filename support", "[filesystem][path][fs.path.win.long]")
2✔
2911
{
2912
#ifdef GHC_OS_WINDOWS
2913
    TemporaryDirectory t(TempOpt::change_path);
2914
    char c = 'A';
2915
    fs::path dir{"\\\\?\\"};
2916
    dir += fs::current_path().u8string();
2917
    for (; c <= 'Z'; ++c) {
2918
        std::string part = std::string(16, c);
2919
        dir /= part;
2920
        CHECK_NOTHROW(fs::create_directory(dir));
2921
        CHECK(fs::exists(dir));
2922
        generateFile(dir / "f0");
2923
        REQUIRE(fs::exists(dir / "f0"));
2924
    }
2925
    CHECK(c > 'Z');
2926
    fs::remove_all(fs::current_path() / std::string(16, 'A'));
2927
    CHECK(!fs::exists(fs::current_path() / std::string(16, 'A')));
2928
    CHECK_NOTHROW(fs::create_directories(dir));
2929
    CHECK(fs::exists(dir));
2930
    generateFile(dir / "f0");
2931
    CHECK(fs::exists(dir / "f0"));
2932
#else
2933
    WARN("Windows specific tests are empty on non-Windows systems.");
2✔
2934
#endif
2935
}
2✔
2936

2937
TEST_CASE("Windows: path namespace handling", "[filesystem][path][fs.path.win.namespaces]")
2✔
2938
{
2939
#ifdef GHC_OS_WINDOWS
2940
    {
2941
        std::error_code ec;
2942
        fs::path p(R"(\\localhost\c$\Windows)");
2943
        auto symstat = fs::symlink_status(p, ec);
2944
        CHECK(!ec);
2945
        auto p2 = fs::canonical(p, ec);
2946
        CHECK(!ec);
2947
        CHECK(p2 == p);
2948

2949
        auto p3 = fs::canonical(R"(\\.\UNC\localhost\c$\Windows)", ec);
2950
        CHECK(!ec);
2951
        CHECK(p3 == p);
2952

2953
        auto p4 = fs::canonical(R"(\\?\UNC\localhost\c$\Windows)", ec);
2954
        CHECK(!ec);
2955
        CHECK(p4 == p);
2956
    }
2957
    
2958
    struct TestInfo
2959
    {
2960
        std::string _path;
2961
        std::string _string;
2962
        std::string _rootName;
2963
        std::string _rootPath;
2964
        std::string _iterateResult;
2965
    };
2966
    std::vector<TestInfo> variants = {
2967
        {R"(C:\Windows\notepad.exe)", R"(C:\Windows\notepad.exe)", "C:", "C:\\", "C:,/,Windows,notepad.exe"},
2968
#ifdef USE_STD_FS
2969
        {R"(\\?\C:\Windows\notepad.exe)", R"(\\?\C:\Windows\notepad.exe)", "\\\\?", "\\\\?\\", "//?,/,C:,Windows,notepad.exe"},
2970
        {R"(\??\C:\Windows\notepad.exe)", R"(\??\C:\Windows\notepad.exe)", "\\??", "\\??\\", "/??,/,C:,Windows,notepad.exe"},
2971
#else
2972
        {R"(\\?\C:\Windows\notepad.exe)", R"(\\?\C:\Windows\notepad.exe)", "C:", "C:\\", "//?/,C:,/,Windows,notepad.exe"},
2973
        {R"(\??\C:\Windows\notepad.exe)", R"(\??\C:\Windows\notepad.exe)", "C:", "C:\\", "/?\?/,C:,/,Windows,notepad.exe"},
2974
#endif
2975
        {R"(\\.\C:\Windows\notepad.exe)", R"(\\.\C:\Windows\notepad.exe)", "\\\\.", "\\\\.\\", "//.,/,C:,Windows,notepad.exe"},
2976
        {R"(\\?\HarddiskVolume1\Windows\notepad.exe)", R"(\\?\HarddiskVolume1\Windows\notepad.exe)", "\\\\?", "\\\\?\\", "//?,/,HarddiskVolume1,Windows,notepad.exe"},
2977
        {R"(\\?\Harddisk0Partition1\Windows\notepad.exe)", R"(\\?\Harddisk0Partition1\Windows\notepad.exe)", "\\\\?", "\\\\?\\", "//?,/,Harddisk0Partition1,Windows,notepad.exe"},
2978
        {R"(\\.\GLOBALROOT\Device\HarddiskVolume1\Windows\notepad.exe)", R"(\\.\GLOBALROOT\Device\HarddiskVolume1\Windows\notepad.exe)", "\\\\.", "\\\\.\\", "//.,/,GLOBALROOT,Device,HarddiskVolume1,Windows,notepad.exe"},
2979
        {R"(\\?\GLOBALROOT\Device\Harddisk0\Partition1\Windows\notepad.exe)", R"(\\?\GLOBALROOT\Device\Harddisk0\Partition1\Windows\notepad.exe)", "\\\\?", "\\\\?\\", "//?,/,GLOBALROOT,Device,Harddisk0,Partition1,Windows,notepad.exe"},
2980
        {R"(\\?\Volume{e8a4a89d-0000-0000-0000-100000000000}\Windows\notepad.exe)", R"(\\?\Volume{e8a4a89d-0000-0000-0000-100000000000}\Windows\notepad.exe)", "\\\\?", "\\\\?\\", "//?,/,Volume{e8a4a89d-0000-0000-0000-100000000000},Windows,notepad.exe"},
2981
        {R"(\\LOCALHOST\C$\Windows\notepad.exe)", R"(\\LOCALHOST\C$\Windows\notepad.exe)", "\\\\LOCALHOST", "\\\\LOCALHOST\\", "//LOCALHOST,/,C$,Windows,notepad.exe"},
2982
        {R"(\\?\UNC\C$\Windows\notepad.exe)", R"(\\?\UNC\C$\Windows\notepad.exe)", "\\\\?", "\\\\?\\", "//?,/,UNC,C$,Windows,notepad.exe"},
2983
        {R"(\\?\GLOBALROOT\Device\Mup\C$\Windows\notepad.exe)", R"(\\?\GLOBALROOT\Device\Mup\C$\Windows\notepad.exe)", "\\\\?", "\\\\?\\", "//?,/,GLOBALROOT,Device,Mup,C$,Windows,notepad.exe"},
2984
    };
2985

2986
    for (auto ti : variants) {
2987
        INFO("Used path: " + ti._path);
2988
        auto p = fs::path(ti._path);
2989
        CHECK(p.string() == ti._string);
2990
        CHECK(p.is_absolute());
2991
        CHECK(p.root_name().string() == ti._rootName);
2992
        CHECK(p.root_path().string() == ti._rootPath);
2993
        CHECK(iterateResult(p) == ti._iterateResult);
2994
    }
2995
#else
2996
    WARN("Windows specific tests are empty on non-Windows systems.");
2✔
2997
#endif
2998
}
2✔
2999

3000
TEST_CASE("Windows: Mapped folders handling ", "[filesystem][fs.win][fs.win.mapped]")
2✔
3001
{
3002
#ifdef GHC_OS_WINDOWS
3003
    // this test expects a mapped volume on C:\\fs-test as is the case on the development test system
3004
    // does nothing on other systems
3005
    if (fs::exists("C:\\fs-test")) {
3006
        CHECK(fs::canonical("C:\\fs-test\\Test.txt").string() == "C:\\fs-test\\Test.txt");
3007
    }
3008
#else
3009
    WARN("Windows specific tests are empty on non-Windows systems.");
2✔
3010
#endif
3011
}
2✔
3012

3013
TEST_CASE("Windows: Deletion of Read-only Files", "[filesystem][fs.win][fs.win.remove]")
2✔
3014
{
3015
#ifdef GHC_OS_WINDOWS
3016
    TemporaryDirectory t(TempOpt::change_path);
3017
    std::error_code ec;
3018
    generateFile("foo", 512);
3019
    auto allWrite = fs::perms::owner_write | fs::perms::group_write | fs::perms::others_write;
3020
    CHECK_NOTHROW(fs::permissions("foo", allWrite, fs::perm_options::remove));
3021
    CHECK_NOTHROW(fs::remove("foo"));
3022
    CHECK(!fs::exists("foo"));
3023
#else
3024
    WARN("Windows specific tests are empty on non-Windows systems.");
2✔
3025
#endif
3026
}
2✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc