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

gulrak / filesystem / 4327853147

pending completion
4327853147

push

github

Steffen Schuemann
Updated README.md

3428 of 3600 relevant lines covered (95.22%)

436.78 hits per line

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

97.96
/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
#include "catch.hpp"
86

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

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

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

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

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

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

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

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

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

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

198
    const fs::path& path() const { return _path; }
228✔
199

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

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

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

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

276
static bool has_host_root_name_support()
16✔
277
{
278
    return fs::path("//host").has_root_name();
16✔
279
}
280

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

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

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

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

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

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

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

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

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

375
#ifndef GHC_OS_WINDOWS
376
TEST_CASE("fs.path.generic - path(\"//host\").has_root_name()", "[filesystem][path][fs.path.generic]")
×
377
{
378
    if (!has_host_root_name_support()) {
×
379
        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.)");
×
380
    }
381
}
×
382
#endif
383

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

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

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

482
TEST_CASE("fs.path.append - path appends", "[filesystem][path][fs.path.append]")
2✔
483
{
484
#ifdef GHC_OS_WINDOWS
485
    CHECK(fs::path("foo") / "c:/bar" == "c:/bar");
486
    CHECK(fs::path("foo") / "c:" == "c:");
487
    CHECK(fs::path("c:") / "" == "c:");
488
    CHECK(fs::path("c:foo") / "/bar" == "c:/bar");
489
    CHECK(fs::path("c:foo") / "c:bar" == "c:foo/bar");
490
#else
491
    CHECK(fs::path("foo") / "" == "foo/");
2✔
492
    CHECK(fs::path("foo") / "/bar" == "/bar");
2✔
493
    CHECK(fs::path("/foo") / "/" == "/");
2✔
494
    if (has_host_root_name_support()) {
2✔
495
        CHECK(fs::path("//host/foo") / "/bar" == "/bar");
2✔
496
        CHECK(fs::path("//host") / "/" == "//host/");
2✔
497
        CHECK(fs::path("//host/foo") / "/" == "/");
2✔
498
    }
499
#endif
500
    CHECK(fs::path("/foo/bar") / "some///other" == "/foo/bar/some/other");
2✔
501
    fs::path p1{"/tmp/test"};
4✔
502
    fs::path p2{"foobar.txt"};
4✔
503
    fs::path p3 = p1 / p2;
4✔
504
    CHECK("/tmp/test/foobar.txt" == p3);
2✔
505
    // TODO: append(first, last)
506
}
2✔
507

508
TEST_CASE("fs.path.concat - path concatenation", "[filesystem][path][fs.path.concat]")
2✔
509
{
510
    CHECK((fs::path("foo") += fs::path("bar")) == "foobar");
2✔
511
    CHECK((fs::path("foo") += fs::path("/bar")) == "foo/bar");
2✔
512

513
    CHECK((fs::path("foo") += std::string("bar")) == "foobar");
2✔
514
    CHECK((fs::path("foo") += std::string("/bar")) == "foo/bar");
2✔
515

516
    CHECK((fs::path("foo") += "bar") == "foobar");
2✔
517
    CHECK((fs::path("foo") += "/bar") == "foo/bar");
2✔
518
    CHECK((fs::path("foo") += L"bar") == "foobar");
2✔
519
    CHECK((fs::path("foo") += L"/bar") == "foo/bar");
2✔
520

521
    CHECK((fs::path("foo") += 'b') == "foob");
2✔
522
    CHECK((fs::path("foo") += '/') == "foo/");
2✔
523
    CHECK((fs::path("foo") += L'b') == "foob");
2✔
524
    CHECK((fs::path("foo") += L'/') == "foo/");
2✔
525

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

529
    CHECK((fs::path("foo") += std::u16string(u"bar")) == "foobar");
2✔
530
    CHECK((fs::path("foo") += std::u16string(u"/bar")) == "foo/bar");
2✔
531

532
    CHECK((fs::path("foo") += std::u32string(U"bar")) == "foobar");
2✔
533
    CHECK((fs::path("foo") += std::u32string(U"/bar")) == "foo/bar");
2✔
534

535
    CHECK(fs::path("foo").concat("bar") == "foobar");
2✔
536
    CHECK(fs::path("foo").concat("/bar") == "foo/bar");
2✔
537
    CHECK(fs::path("foo").concat(L"bar") == "foobar");
2✔
538
    CHECK(fs::path("foo").concat(L"/bar") == "foo/bar");
2✔
539
    std::string bar = "bar";
4✔
540
    CHECK(fs::path("foo").concat(bar.begin(), bar.end()) == "foobar");
2✔
541
#ifndef USE_STD_FS
542
    CHECK((fs::path("/foo/bar") += "/some///other") == "/foo/bar/some/other");
2✔
543
#endif
544
    // TODO: contat(first, last)
545
}
2✔
546

547
TEST_CASE("fs.path.modifiers - path modifiers", "[filesystem][path][fs.path.modifiers]")
2✔
548
{
549
    fs::path p = fs::path("/foo/bar");
4✔
550
    p.clear();
2✔
551
    CHECK(p == "");
2✔
552

553
    // make_preferred() is a no-op
554
#ifdef GHC_OS_WINDOWS
555
    CHECK(fs::path("foo\\bar") == "foo/bar");
556
    CHECK(fs::path("foo\\bar").make_preferred() == "foo/bar");
557
#else
558
    CHECK(fs::path("foo\\bar") == "foo\\bar");
2✔
559
    CHECK(fs::path("foo\\bar").make_preferred() == "foo\\bar");
2✔
560
#endif
561
    CHECK(fs::path("foo/bar").make_preferred() == "foo/bar");
2✔
562

563
    CHECK(fs::path("foo/bar").remove_filename() == "foo/");
2✔
564
    CHECK(fs::path("foo/").remove_filename() == "foo/");
2✔
565
    CHECK(fs::path("/foo").remove_filename() == "/");
2✔
566
    CHECK(fs::path("/").remove_filename() == "/");
2✔
567

568
    CHECK(fs::path("/foo").replace_filename("bar") == "/bar");
2✔
569
    CHECK(fs::path("/").replace_filename("bar") == "/bar");
2✔
570
    CHECK(fs::path("/foo").replace_filename("b//ar") == "/b/ar");
2✔
571

572
    CHECK(fs::path("/foo/bar.txt").replace_extension("odf") == "/foo/bar.odf");
2✔
573
    CHECK(fs::path("/foo/bar.txt").replace_extension() == "/foo/bar");
2✔
574
    CHECK(fs::path("/foo/bar").replace_extension("odf") == "/foo/bar.odf");
2✔
575
    CHECK(fs::path("/foo/bar").replace_extension(".odf") == "/foo/bar.odf");
2✔
576
    CHECK(fs::path("/foo/bar.").replace_extension(".odf") == "/foo/bar.odf");
2✔
577
    CHECK(fs::path("/foo/bar/").replace_extension("odf") == "/foo/bar/.odf");
2✔
578

579
    fs::path p1 = "foo";
4✔
580
    fs::path p2 = "bar";
4✔
581
    p1.swap(p2);
2✔
582
    CHECK(p1 == "bar");
2✔
583
    CHECK(p2 == "foo");
2✔
584
}
2✔
585

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

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

659
TEST_CASE("fs.path.compare - path compare", "[filesystem][path][fs.path.compare]")
2✔
660
{
661
    CHECK(fs::path("/foo/b").compare("/foo/a") > 0);
2✔
662
    CHECK(fs::path("/foo/b").compare("/foo/b") == 0);
2✔
663
    CHECK(fs::path("/foo/b").compare("/foo/c") < 0);
2✔
664

665
    CHECK(fs::path("/foo/b").compare(std::string("/foo/a")) > 0);
2✔
666
    CHECK(fs::path("/foo/b").compare(std::string("/foo/b")) == 0);
2✔
667
    CHECK(fs::path("/foo/b").compare(std::string("/foo/c")) < 0);
2✔
668

669
    CHECK(fs::path("/foo/b").compare(fs::path("/foo/a")) > 0);
2✔
670
    CHECK(fs::path("/foo/b").compare(fs::path("/foo/b")) == 0);
2✔
671
    CHECK(fs::path("/foo/b").compare(fs::path("/foo/c")) < 0);
2✔
672

673
#ifdef GHC_OS_WINDOWS
674
    CHECK(fs::path("c:\\a\\b").compare("C:\\a\\b") == 0);
675
    CHECK(fs::path("c:\\a\\b").compare("d:\\a\\b") != 0);
676
    CHECK(fs::path("c:\\a\\b").compare("C:\\A\\b") != 0);
677
#endif
678

679
#ifdef LWG_2936_BEHAVIOUR
680
    CHECK(fs::path("/a/b/").compare("/a/b/c") < 0);
2✔
681
    CHECK(fs::path("/a/b/").compare("a/c") > 0);
2✔
682
#endif // LWG_2936_BEHAVIOUR
683
}
2✔
684

685
TEST_CASE("fs.path.decompose - path decomposition", "[filesystem][path][fs.path.decompose]")
2✔
686
{
687
    // root_name()
688
    CHECK(fs::path("").root_name() == "");
2✔
689
    CHECK(fs::path(".").root_name() == "");
2✔
690
    CHECK(fs::path("..").root_name() == "");
2✔
691
    CHECK(fs::path("foo").root_name() == "");
2✔
692
    CHECK(fs::path("/").root_name() == "");
2✔
693
    CHECK(fs::path("/foo").root_name() == "");
2✔
694
    CHECK(fs::path("foo/").root_name() == "");
2✔
695
    CHECK(fs::path("/foo/").root_name() == "");
2✔
696
    CHECK(fs::path("foo/bar").root_name() == "");
2✔
697
    CHECK(fs::path("/foo/bar").root_name() == "");
2✔
698
    CHECK(fs::path("///foo/bar").root_name() == "");
2✔
699
#ifdef GHC_OS_WINDOWS
700
    CHECK(fs::path("C:/foo").root_name() == "C:");
701
    CHECK(fs::path("C:\\foo").root_name() == "C:");
702
    CHECK(fs::path("C:foo").root_name() == "C:");
703
#endif
704

705
    // root_directory()
706
    CHECK(fs::path("").root_directory() == "");
2✔
707
    CHECK(fs::path(".").root_directory() == "");
2✔
708
    CHECK(fs::path("..").root_directory() == "");
2✔
709
    CHECK(fs::path("foo").root_directory() == "");
2✔
710
    CHECK(fs::path("/").root_directory() == "/");
2✔
711
    CHECK(fs::path("/foo").root_directory() == "/");
2✔
712
    CHECK(fs::path("foo/").root_directory() == "");
2✔
713
    CHECK(fs::path("/foo/").root_directory() == "/");
2✔
714
    CHECK(fs::path("foo/bar").root_directory() == "");
2✔
715
    CHECK(fs::path("/foo/bar").root_directory() == "/");
2✔
716
    CHECK(fs::path("///foo/bar").root_directory() == "/");
2✔
717
#ifdef GHC_OS_WINDOWS
718
    CHECK(fs::path("C:/foo").root_directory() == "/");
719
    CHECK(fs::path("C:\\foo").root_directory() == "/");
720
    CHECK(fs::path("C:foo").root_directory() == "");
721
#endif
722

723
    // root_path()
724
    CHECK(fs::path("").root_path() == "");
2✔
725
    CHECK(fs::path(".").root_path() == "");
2✔
726
    CHECK(fs::path("..").root_path() == "");
2✔
727
    CHECK(fs::path("foo").root_path() == "");
2✔
728
    CHECK(fs::path("/").root_path() == "/");
2✔
729
    CHECK(fs::path("/foo").root_path() == "/");
2✔
730
    CHECK(fs::path("foo/").root_path() == "");
2✔
731
    CHECK(fs::path("/foo/").root_path() == "/");
2✔
732
    CHECK(fs::path("foo/bar").root_path() == "");
2✔
733
    CHECK(fs::path("/foo/bar").root_path() == "/");
2✔
734
    CHECK(fs::path("///foo/bar").root_path() == "/");
2✔
735
#ifdef GHC_OS_WINDOWS
736
    CHECK(fs::path("C:/foo").root_path() == "C:/");
737
    CHECK(fs::path("C:\\foo").root_path() == "C:/");
738
    CHECK(fs::path("C:foo").root_path() == "C:");
739
#endif
740

741
    // relative_path()
742
    CHECK(fs::path("").relative_path() == "");
2✔
743
    CHECK(fs::path(".").relative_path() == ".");
2✔
744
    CHECK(fs::path("..").relative_path() == "..");
2✔
745
    CHECK(fs::path("foo").relative_path() == "foo");
2✔
746
    CHECK(fs::path("/").relative_path() == "");
2✔
747
    CHECK(fs::path("/foo").relative_path() == "foo");
2✔
748
    CHECK(fs::path("foo/").relative_path() == "foo/");
2✔
749
    CHECK(fs::path("/foo/").relative_path() == "foo/");
2✔
750
    CHECK(fs::path("foo/bar").relative_path() == "foo/bar");
2✔
751
    CHECK(fs::path("/foo/bar").relative_path() == "foo/bar");
2✔
752
    CHECK(fs::path("///foo/bar").relative_path() == "foo/bar");
2✔
753
#ifdef GHC_OS_WINDOWS
754
    CHECK(fs::path("C:/foo").relative_path() == "foo");
755
    CHECK(fs::path("C:\\foo").relative_path() == "foo");
756
    CHECK(fs::path("C:foo").relative_path() == "foo");
757
#endif
758

759
    // parent_path()
760
    CHECK(fs::path("").parent_path() == "");
2✔
761
    CHECK(fs::path(".").parent_path() == "");
2✔
762
    CHECK(fs::path("..").parent_path() == "");  // unintuitive but as defined in the standard
2✔
763
    CHECK(fs::path("foo").parent_path() == "");
2✔
764
    CHECK(fs::path("/").parent_path() == "/");
2✔
765
    CHECK(fs::path("/foo").parent_path() == "/");
2✔
766
    CHECK(fs::path("foo/").parent_path() == "foo");
2✔
767
    CHECK(fs::path("/foo/").parent_path() == "/foo");
2✔
768
    CHECK(fs::path("foo/bar").parent_path() == "foo");
2✔
769
    CHECK(fs::path("/foo/bar").parent_path() == "/foo");
2✔
770
    CHECK(fs::path("///foo/bar").parent_path() == "/foo");
2✔
771
#ifdef GHC_OS_WINDOWS
772
    CHECK(fs::path("C:/foo").parent_path() == "C:/");
773
    CHECK(fs::path("C:\\foo").parent_path() == "C:/");
774
    CHECK(fs::path("C:foo").parent_path() == "C:");
775
#endif
776

777
    // filename()
778
    CHECK(fs::path("").filename() == "");
2✔
779
    CHECK(fs::path(".").filename() == ".");
2✔
780
    CHECK(fs::path("..").filename() == "..");
2✔
781
    CHECK(fs::path("foo").filename() == "foo");
2✔
782
    CHECK(fs::path("/").filename() == "");
2✔
783
    CHECK(fs::path("/foo").filename() == "foo");
2✔
784
    CHECK(fs::path("foo/").filename() == "");
2✔
785
    CHECK(fs::path("/foo/").filename() == "");
2✔
786
    CHECK(fs::path("foo/bar").filename() == "bar");
2✔
787
    CHECK(fs::path("/foo/bar").filename() == "bar");
2✔
788
    CHECK(fs::path("///foo/bar").filename() == "bar");
2✔
789
#ifdef GHC_OS_WINDOWS
790
    CHECK(fs::path("C:/foo").filename() == "foo");
791
    CHECK(fs::path("C:\\foo").filename() == "foo");
792
    CHECK(fs::path("C:foo").filename() == "foo");
793
    CHECK(fs::path("t:est.txt").filename() == "est.txt");
794
#else
795
    CHECK(fs::path("t:est.txt").filename() == "t:est.txt");
2✔
796
#endif
797

798
    // stem()
799
    CHECK(fs::path("/foo/bar.txt").stem() == "bar");
2✔
800
    {
801
        fs::path p = "foo.bar.baz.tar";
4✔
802
        CHECK(p.extension() == ".tar");
2✔
803
        p = p.stem();
2✔
804
        CHECK(p.extension() == ".baz");
2✔
805
        p = p.stem();
2✔
806
        CHECK(p.extension() == ".bar");
2✔
807
        p = p.stem();
2✔
808
        CHECK(p == "foo");
2✔
809
    }
810
    CHECK(fs::path("/foo/.profile").stem() == ".profile");
2✔
811
    CHECK(fs::path(".bar").stem() == ".bar");
2✔
812
    CHECK(fs::path("..bar").stem() == ".");
2✔
813
#ifdef GHC_OS_WINDOWS
814
    CHECK(fs::path("t:est.txt").stem() == "est");
815
#else
816
    CHECK(fs::path("t:est.txt").stem() == "t:est");
2✔
817
#endif
818

819
    // extension()
820
    CHECK(fs::path("/foo/bar.txt").extension() == ".txt");
2✔
821
    CHECK(fs::path("/foo/bar").extension() == "");
2✔
822
    CHECK(fs::path("/foo/.profile").extension() == "");
2✔
823
    CHECK(fs::path(".bar").extension() == "");
2✔
824
    CHECK(fs::path("..bar").extension() == ".bar");
2✔
825
    CHECK(fs::path("t:est.txt").extension() == ".txt");
2✔
826

827
    if (has_host_root_name_support()) {
2✔
828
        // //host-based root-names
829
        CHECK(fs::path("//host").root_name() == "//host");
2✔
830
        CHECK(fs::path("//host/foo").root_name() == "//host");
2✔
831
        CHECK(fs::path("//host").root_directory() == "");
2✔
832
        CHECK(fs::path("//host/foo").root_directory() == "/");
2✔
833
        CHECK(fs::path("//host").root_path() == "//host");
2✔
834
        CHECK(fs::path("//host/foo").root_path() == "//host/");
2✔
835
        CHECK(fs::path("//host").relative_path() == "");
2✔
836
        CHECK(fs::path("//host/foo").relative_path() == "foo");
2✔
837
        CHECK(fs::path("//host").parent_path() == "//host");
2✔
838
        CHECK(fs::path("//host/foo").parent_path() == "//host/");
2✔
839
        CHECK(fs::path("//host").filename() == "");
2✔
840
        CHECK(fs::path("//host/foo").filename() == "foo");
2✔
841
    }
842
}
2✔
843

844
TEST_CASE("fs.path.query - path query", "[fielsystem][path][fs.path.query]")
2✔
845
{
846
    // empty
847
    CHECK(fs::path("").empty());
2✔
848
    CHECK(!fs::path("foo").empty());
2✔
849

850
    // has_root_path()
851
    CHECK(!fs::path("foo").has_root_path());
2✔
852
    CHECK(!fs::path("foo/bar").has_root_path());
2✔
853
    CHECK(fs::path("/foo").has_root_path());
2✔
854
#ifdef GHC_OS_WINDOWS
855
    CHECK(fs::path("C:foo").has_root_path());
856
    CHECK(fs::path("C:/foo").has_root_path());
857
#endif
858

859
    // has_root_name()
860
    CHECK(!fs::path("foo").has_root_name());
2✔
861
    CHECK(!fs::path("foo/bar").has_root_name());
2✔
862
    CHECK(!fs::path("/foo").has_root_name());
2✔
863
#ifdef GHC_OS_WINDOWS
864
    CHECK(fs::path("C:foo").has_root_name());
865
    CHECK(fs::path("C:/foo").has_root_name());
866
#endif
867

868
    // has_root_directory()
869
    CHECK(!fs::path("foo").has_root_directory());
2✔
870
    CHECK(!fs::path("foo/bar").has_root_directory());
2✔
871
    CHECK(fs::path("/foo").has_root_directory());
2✔
872
#ifdef GHC_OS_WINDOWS
873
    CHECK(!fs::path("C:foo").has_root_directory());
874
    CHECK(fs::path("C:/foo").has_root_directory());
875
#endif
876

877
    // has_relative_path()
878
    CHECK(!fs::path("").has_relative_path());
2✔
879
    CHECK(!fs::path("/").has_relative_path());
2✔
880
    CHECK(fs::path("/foo").has_relative_path());
2✔
881

882
    // has_parent_path()
883
    CHECK(!fs::path("").has_parent_path());
2✔
884
    CHECK(!fs::path(".").has_parent_path());
2✔
885
    CHECK(!fs::path("..").has_parent_path());  // unintuitive but as defined in the standard
2✔
886
    CHECK(!fs::path("foo").has_parent_path());
2✔
887
    CHECK(fs::path("/").has_parent_path());
2✔
888
    CHECK(fs::path("/foo").has_parent_path());
2✔
889
    CHECK(fs::path("foo/").has_parent_path());
2✔
890
    CHECK(fs::path("/foo/").has_parent_path());
2✔
891

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

897
    // has_stem()
898
    CHECK(fs::path("foo").has_stem());
2✔
899
    CHECK(fs::path("foo.bar").has_stem());
2✔
900
    CHECK(fs::path(".profile").has_stem());
2✔
901
    CHECK(!fs::path("/foo/").has_stem());
2✔
902

903
    // has_extension()
904
    CHECK(!fs::path("foo").has_extension());
2✔
905
    CHECK(fs::path("foo.bar").has_extension());
2✔
906
    CHECK(!fs::path(".profile").has_extension());
2✔
907

908
    // is_absolute()
909
    CHECK(!fs::path("foo/bar").is_absolute());
2✔
910
#ifdef GHC_OS_WINDOWS
911
    CHECK(!fs::path("/foo").is_absolute());
912
    CHECK(!fs::path("c:foo").is_absolute());
913
    CHECK(fs::path("c:/foo").is_absolute());
914
#else
915
    CHECK(fs::path("/foo").is_absolute());
2✔
916
#endif
917

918
    // is_relative()
919
    CHECK(fs::path("foo/bar").is_relative());
2✔
920
#ifdef GHC_OS_WINDOWS
921
    CHECK(fs::path("/foo").is_relative());
922
    CHECK(fs::path("c:foo").is_relative());
923
    CHECK(!fs::path("c:/foo").is_relative());
924
#else
925
    CHECK(!fs::path("/foo").is_relative());
2✔
926
#endif
927

928
    if (has_host_root_name_support()) {
2✔
929
        CHECK(fs::path("//host").has_root_name());
2✔
930
        CHECK(fs::path("//host/foo").has_root_name());
2✔
931
        CHECK(fs::path("//host").has_root_path());
2✔
932
        CHECK(fs::path("//host/foo").has_root_path());
2✔
933
        CHECK(!fs::path("//host").has_root_directory());
2✔
934
        CHECK(fs::path("//host/foo").has_root_directory());
2✔
935
        CHECK(!fs::path("//host").has_relative_path());
2✔
936
        CHECK(fs::path("//host/foo").has_relative_path());
2✔
937
        CHECK(fs::path("//host/foo").is_absolute());
2✔
938
        CHECK(!fs::path("//host/foo").is_relative());
2✔
939
    }
940
}
2✔
941

942
TEST_CASE("fs.path.gen - path generation", "[filesystem][path][fs.path.gen]")
2✔
943
{
944
    // lexically_normal()
945
    CHECK(fs::path("foo/./bar/..").lexically_normal() == "foo/");
2✔
946
    CHECK(fs::path("foo/.///bar/../").lexically_normal() == "foo/");
2✔
947
    CHECK(fs::path("/foo/../..").lexically_normal() == "/");
2✔
948
    CHECK(fs::path("foo/..").lexically_normal() == ".");
2✔
949
    CHECK(fs::path("ab/cd/ef/../../qw").lexically_normal() == "ab/qw");
2✔
950
    CHECK(fs::path("a/b/../../../c").lexically_normal() == "../c");
2✔
951
    CHECK(fs::path("../").lexically_normal() == "..");
2✔
952
#ifdef GHC_OS_WINDOWS
953
    CHECK(fs::path("\\/\\///\\/").lexically_normal() == "/");
954
    CHECK(fs::path("a/b/..\\//..///\\/../c\\\\/").lexically_normal() == "../c/");
955
    CHECK(fs::path("..a/b/..\\//..///\\/../c\\\\/").lexically_normal() == "../c/");
956
    CHECK(fs::path("..\\").lexically_normal() == "..");
957
#endif
958

959
    // lexically_relative()
960
    CHECK(fs::path("/a/d").lexically_relative("/a/b/c") == "../../d");
2✔
961
    CHECK(fs::path("/a/b/c").lexically_relative("/a/d") == "../b/c");
2✔
962
    CHECK(fs::path("a/b/c").lexically_relative("a") == "b/c");
2✔
963
    CHECK(fs::path("a/b/c").lexically_relative("a/b/c/x/y") == "../..");
2✔
964
    CHECK(fs::path("a/b/c").lexically_relative("a/b/c") == ".");
2✔
965
    CHECK(fs::path("a/b").lexically_relative("c/d") == "../../a/b");
2✔
966
    CHECK(fs::path("a/b").lexically_relative("a/") == "b");
2✔
967
    if (has_host_root_name_support()) {
2✔
968
        CHECK(fs::path("//host1/foo").lexically_relative("//host2.bar") == "");
2✔
969
    }
970
#ifdef GHC_OS_WINDOWS
971
    CHECK(fs::path("c:/foo").lexically_relative("/bar") == "");
972
    CHECK(fs::path("c:foo").lexically_relative("c:/bar") == "");
973
    CHECK(fs::path("foo").lexically_relative("/bar") == "");
974
    CHECK(fs::path("c:/foo/bar.txt").lexically_relative("c:/foo/") == "bar.txt");
975
    CHECK(fs::path("c:/foo/bar.txt").lexically_relative("C:/foo/") == "bar.txt");
976
#else
977
    CHECK(fs::path("/foo").lexically_relative("bar") == "");
2✔
978
    CHECK(fs::path("foo").lexically_relative("/bar") == "");
2✔
979
#endif
980

981
    // lexically_proximate()
982
    CHECK(fs::path("/a/d").lexically_proximate("/a/b/c") == "../../d");
2✔
983
    if (has_host_root_name_support()) {
2✔
984
        CHECK(fs::path("//host1/a/d").lexically_proximate("//host2/a/b/c") == "//host1/a/d");
2✔
985
    }
986
    CHECK(fs::path("a/d").lexically_proximate("/a/b/c") == "a/d");
2✔
987
#ifdef GHC_OS_WINDOWS
988
    CHECK(fs::path("c:/a/d").lexically_proximate("c:/a/b/c") == "../../d");
989
    CHECK(fs::path("c:/a/d").lexically_proximate("d:/a/b/c") == "c:/a/d");
990
    CHECK(fs::path("c:/foo").lexically_proximate("/bar") == "c:/foo");
991
    CHECK(fs::path("c:foo").lexically_proximate("c:/bar") == "c:foo");
992
    CHECK(fs::path("foo").lexically_proximate("/bar") == "foo");
993
#else
994
    CHECK(fs::path("/foo").lexically_proximate("bar") == "/foo");
2✔
995
    CHECK(fs::path("foo").lexically_proximate("/bar") == "foo");
2✔
996
#endif
997
}
2✔
998

999
static std::string iterateResult(const fs::path& path)
30✔
1000
{
1001
    std::ostringstream result;
60✔
1002
    for (fs::path::const_iterator i = path.begin(); i != path.end(); ++i) {
94✔
1003
        if (i != path.begin()) {
64✔
1004
            result << ",";
36✔
1005
        }
1006
        result << i->generic_string();
64✔
1007
    }
1008
    return result.str();
60✔
1009
}
1010

1011
static std::string reverseIterateResult(const fs::path& path)
30✔
1012
{
1013
    std::ostringstream result;
60✔
1014
    fs::path::const_iterator iter = path.end();
60✔
1015
    bool first = true;
30✔
1016
    if (iter != path.begin()) {
30✔
1017
        do {
36✔
1018
            --iter;
64✔
1019
            if (!first) {
64✔
1020
                result << ",";
36✔
1021
            }
1022
            first = false;
64✔
1023
            result << iter->generic_string();
64✔
1024
        } while (iter != path.begin());
64✔
1025
    }
1026
    return result.str();
60✔
1027
}
1028

1029
TEST_CASE("fs.path.itr - path iterators", "[filesystem][path][fs.path.itr]")
2✔
1030
{
1031
    CHECK(iterateResult(fs::path()).empty());
2✔
1032
    CHECK("." == iterateResult(fs::path(".")));
2✔
1033
    CHECK(".." == iterateResult(fs::path("..")));
2✔
1034
    CHECK("foo" == iterateResult(fs::path("foo")));
2✔
1035
    CHECK("/" == iterateResult(fs::path("/")));
2✔
1036
    CHECK("/,foo" == iterateResult(fs::path("/foo")));
2✔
1037
    CHECK("foo," == iterateResult(fs::path("foo/")));
2✔
1038
    CHECK("/,foo," == iterateResult(fs::path("/foo/")));
2✔
1039
    CHECK("foo,bar" == iterateResult(fs::path("foo/bar")));
2✔
1040
    CHECK("/,foo,bar" == iterateResult(fs::path("/foo/bar")));
2✔
1041
#ifndef USE_STD_FS
1042
    // ghc::filesystem enforces redundant slashes to be reduced to one
1043
    CHECK("/,foo,bar" == iterateResult(fs::path("///foo/bar")));
2✔
1044
#else
1045
    // typically std::filesystem keeps them
1046
    CHECK("///,foo,bar" == iterateResult(fs::path("///foo/bar")));
1047
#endif
1048
    CHECK("/,foo,bar," == iterateResult(fs::path("/foo/bar///")));
2✔
1049
    CHECK("foo,.,bar,..," == iterateResult(fs::path("foo/.///bar/../")));
2✔
1050
#ifdef GHC_OS_WINDOWS
1051
    CHECK("C:,/,foo" == iterateResult(fs::path("C:/foo")));
1052
#endif
1053

1054
    CHECK(reverseIterateResult(fs::path()).empty());
2✔
1055
    CHECK("." == reverseIterateResult(fs::path(".")));
2✔
1056
    CHECK(".." == reverseIterateResult(fs::path("..")));
2✔
1057
    CHECK("foo" == reverseIterateResult(fs::path("foo")));
2✔
1058
    CHECK("/" == reverseIterateResult(fs::path("/")));
2✔
1059
    CHECK("foo,/" == reverseIterateResult(fs::path("/foo")));
2✔
1060
    CHECK(",foo" == reverseIterateResult(fs::path("foo/")));
2✔
1061
    CHECK(",foo,/" == reverseIterateResult(fs::path("/foo/")));
2✔
1062
    CHECK("bar,foo" == reverseIterateResult(fs::path("foo/bar")));
2✔
1063
    CHECK("bar,foo,/" == reverseIterateResult(fs::path("/foo/bar")));
2✔
1064
#ifndef USE_STD_FS
1065
    // ghc::filesystem enforces redundant slashes to be reduced to one
1066
    CHECK("bar,foo,/" == reverseIterateResult(fs::path("///foo/bar")));
2✔
1067
#else
1068
    // typically std::filesystem keeps them
1069
    CHECK("bar,foo,///" == reverseIterateResult(fs::path("///foo/bar")));
1070
#endif
1071
    CHECK(",bar,foo,/" == reverseIterateResult(fs::path("/foo/bar///")));
2✔
1072
    CHECK(",..,bar,.,foo" == reverseIterateResult(fs::path("foo/.///bar/../")));
2✔
1073
#ifdef GHC_OS_WINDOWS
1074
    CHECK("foo,/,C:" == reverseIterateResult(fs::path("C:/foo")));
1075
    CHECK("foo,C:" == reverseIterateResult(fs::path("C:foo")));
1076
#endif
1077
    {
1078
        fs::path p1 = "/foo/bar/test.txt";
4✔
1079
        fs::path p2;
4✔
1080
        for (auto pe : p1) {
10✔
1081
            p2 /= pe;
8✔
1082
        }
1083
        CHECK(p1 == p2);
2✔
1084
        CHECK("bar" == *(--fs::path("/foo/bar").end()));
2✔
1085
        auto p = fs::path("/foo/bar");
4✔
1086
        auto pi = p.end();
4✔
1087
        pi--;
2✔
1088
        CHECK("bar" == *pi);
2✔
1089
    }
1090

1091
    if (has_host_root_name_support()) {
2✔
1092
        CHECK("foo" == *(--fs::path("//host/foo").end()));
2✔
1093
        auto p = fs::path("//host/foo");
4✔
1094
        auto pi = p.end();
4✔
1095
        pi--;
2✔
1096
        CHECK("foo" == *pi);
2✔
1097
        CHECK("//host" == iterateResult(fs::path("//host")));
2✔
1098
        CHECK("//host,/,foo" == iterateResult(fs::path("//host/foo")));
2✔
1099
        CHECK("//host" == reverseIterateResult(fs::path("//host")));
2✔
1100
        CHECK("foo,/,//host" == reverseIterateResult(fs::path("//host/foo")));
2✔
1101
        {
1102
            fs::path p1 = "//host/foo/bar/test.txt";
4✔
1103
            fs::path p2;
4✔
1104
            for (auto pe : p1) {
12✔
1105
                p2 /= pe;
10✔
1106
            }
1107
            CHECK(p1 == p2);
2✔
1108
        }
1109
    }
1110
}
2✔
1111

1112
TEST_CASE("fs.path.nonmember - path non-member functions", "[filesystem][path][fs.path.nonmember]")
2✔
1113
{
1114
    fs::path p1("foo/bar");
4✔
1115
    fs::path p2("some/other");
4✔
1116
    fs::swap(p1, p2);
2✔
1117
    CHECK(p1 == "some/other");
2✔
1118
    CHECK(p2 == "foo/bar");
2✔
1119
    CHECK(hash_value(p1));
2✔
1120
    CHECK(p2 < p1);
2✔
1121
    CHECK(p2 <= p1);
2✔
1122
    CHECK(p1 <= p1);
2✔
1123
    CHECK(!(p1 < p2));
2✔
1124
    CHECK(!(p1 <= p2));
2✔
1125
    CHECK(p1 > p2);
2✔
1126
    CHECK(p1 >= p2);
2✔
1127
    CHECK(p1 >= p1);
2✔
1128
    CHECK(!(p2 > p1));
2✔
1129
    CHECK(!(p2 >= p1));
2✔
1130
    CHECK(p1 != p2);
2✔
1131
    CHECK(p1 / p2 == "some/other/foo/bar");
2✔
1132
}
2✔
1133

1134
TEST_CASE("fs.path.io - path inserter and extractor", "[filesystem][path][fs.path.io]")
2✔
1135
{
1136
    {
1137
        std::ostringstream os;
4✔
1138
        os << fs::path("/root/foo bar");
2✔
1139
#ifdef GHC_OS_WINDOWS
1140
        CHECK(os.str() == "\"\\\\root\\\\foo bar\"");
1141
#else
1142
        CHECK(os.str() == "\"/root/foo bar\"");
2✔
1143
#endif
1144
    }
1145
    {
1146
        std::ostringstream os;
4✔
1147
        os << fs::path("/root/foo\"bar");
2✔
1148
#ifdef GHC_OS_WINDOWS
1149
        CHECK(os.str() == "\"\\\\root\\\\foo\\\"bar\"");
1150
#else
1151
        CHECK(os.str() == "\"/root/foo\\\"bar\"");
2✔
1152
#endif
1153
    }
1154

1155
    {
1156
        std::istringstream is("\"/root/foo bar\"");
6✔
1157
        fs::path p;
4✔
1158
        is >> p;
2✔
1159
        CHECK(p == fs::path("/root/foo bar"));
2✔
1160
        CHECK((is.flags() & std::ios_base::skipws) == std::ios_base::skipws);
2✔
1161
    }
1162
    {
1163
        std::istringstream is("\"/root/foo bar\"");
6✔
1164
        is >> std::noskipws;
2✔
1165
        fs::path p;
4✔
1166
        is >> p;
2✔
1167
        CHECK(p == fs::path("/root/foo bar"));
2✔
1168
        CHECK((is.flags() & std::ios_base::skipws) != std::ios_base::skipws);
2✔
1169
    }
1170
    {
1171
        std::istringstream is("\"/root/foo\\\"bar\"");
6✔
1172
        fs::path p;
4✔
1173
        is >> p;
2✔
1174
        CHECK(p == fs::path("/root/foo\"bar"));
2✔
1175
    }
1176
    {
1177
        std::istringstream is("/root/foo");
6✔
1178
        fs::path p;
4✔
1179
        is >> p;
2✔
1180
        CHECK(p == fs::path("/root/foo"));
2✔
1181
    }
1182
}
2✔
1183

1184
TEST_CASE("fs.path.factory - path factory functions", "[filesystem][path][fs.path.factory]")
2✔
1185
{
1186
    CHECK(fs::u8path("foo/bar") == fs::path("foo/bar"));
2✔
1187
    CHECK(fs::u8path("foo/bar") == fs::path("foo/bar"));
2✔
1188
    std::string str("/foo/bar/test.txt");
4✔
1189
    CHECK(fs::u8path(str.begin(), str.end()) == str);
2✔
1190
}
2✔
1191

1192
TEST_CASE("fs.class.filesystem_error - class filesystem_error", "[filesystem][filesystem_error][fs.class.filesystem_error]")
2✔
1193
{
1194
    std::error_code ec(1, std::system_category());
2✔
1195
    fs::filesystem_error fse("None", std::error_code());
6✔
1196
    fse = fs::filesystem_error("Some error", ec);
2✔
1197
    CHECK(fse.code().value() == 1);
2✔
1198
    CHECK(!std::string(fse.what()).empty());
2✔
1199
    CHECK(fse.path1().empty());
2✔
1200
    CHECK(fse.path2().empty());
2✔
1201
    fse = fs::filesystem_error("Some error", fs::path("foo/bar"), ec);
2✔
1202
    CHECK(!std::string(fse.what()).empty());
2✔
1203
    CHECK(fse.path1() == "foo/bar");
2✔
1204
    CHECK(fse.path2().empty());
2✔
1205
    fse = fs::filesystem_error("Some error", fs::path("foo/bar"), fs::path("some/other"), ec);
2✔
1206
    CHECK(!std::string(fse.what()).empty());
2✔
1207
    CHECK(fse.path1() == "foo/bar");
2✔
1208
    CHECK(fse.path2() == "some/other");
2✔
1209
}
2✔
1210

1211
static constexpr fs::perms constExprOwnerAll()
1212
{
1213
    return fs::perms::owner_read | fs::perms::owner_write | fs::perms::owner_exec;
1214
}
1215

1216
TEST_CASE("fs.enum - enum class perms", "[filesystem][enum][fs.enum]")
2✔
1217
{
1218
    static_assert(constExprOwnerAll() == fs::perms::owner_all, "constexpr didn't result in owner_all");
1219
    CHECK((fs::perms::owner_read | fs::perms::owner_write | fs::perms::owner_exec) == fs::perms::owner_all);
2✔
1220
    CHECK((fs::perms::group_read | fs::perms::group_write | fs::perms::group_exec) == fs::perms::group_all);
2✔
1221
    CHECK((fs::perms::others_read | fs::perms::others_write | fs::perms::others_exec) == fs::perms::others_all);
2✔
1222
    CHECK((fs::perms::owner_all | fs::perms::group_all | fs::perms::others_all) == fs::perms::all);
2✔
1223
    CHECK((fs::perms::all | fs::perms::set_uid | fs::perms::set_gid | fs::perms::sticky_bit) == fs::perms::mask);
2✔
1224
}
2✔
1225

1226
TEST_CASE("fs.class.file_status - class file_status", "[filesystem][file_status][fs.class.file_status]")
2✔
1227
{
1228
    {
1229
        fs::file_status fs;
4✔
1230
        CHECK(fs.type() == fs::file_type::none);
2✔
1231
        CHECK(fs.permissions() == fs::perms::unknown);
2✔
1232
    }
1233
    {
1234
        fs::file_status fs{fs::file_type::regular};
4✔
1235
        CHECK(fs.type() == fs::file_type::regular);
2✔
1236
        CHECK(fs.permissions() == fs::perms::unknown);
2✔
1237
    }
1238
    {
1239
        fs::file_status fs{fs::file_type::directory, fs::perms::owner_read | fs::perms::owner_write | fs::perms::owner_exec};
4✔
1240
        CHECK(fs.type() == fs::file_type::directory);
2✔
1241
        CHECK(fs.permissions() == fs::perms::owner_all);
2✔
1242
        fs.type(fs::file_type::block);
2✔
1243
        CHECK(fs.type() == fs::file_type::block);
2✔
1244
        fs.type(fs::file_type::character);
2✔
1245
        CHECK(fs.type() == fs::file_type::character);
2✔
1246
        fs.type(fs::file_type::fifo);
2✔
1247
        CHECK(fs.type() == fs::file_type::fifo);
2✔
1248
        fs.type(fs::file_type::symlink);
2✔
1249
        CHECK(fs.type() == fs::file_type::symlink);
2✔
1250
        fs.type(fs::file_type::socket);
2✔
1251
        CHECK(fs.type() == fs::file_type::socket);
2✔
1252
        fs.permissions(fs.permissions() | fs::perms::group_all | fs::perms::others_all);
2✔
1253
        CHECK(fs.permissions() == fs::perms::all);
2✔
1254
    }
1255
    {
1256
        fs::file_status fst(fs::file_type::regular);
4✔
1257
        fs::file_status fs(std::move(fst));
4✔
1258
        CHECK(fs.type() == fs::file_type::regular);
2✔
1259
        CHECK(fs.permissions() == fs::perms::unknown);
2✔
1260
    }
1261
#if !defined(USE_STD_FS) || defined(GHC_FILESYSTEM_RUNNING_CPP20)
1262
    {
1263
        fs::file_status fs1{fs::file_type::regular, fs::perms::owner_read | fs::perms::owner_write | fs::perms::owner_exec};
4✔
1264
        fs::file_status fs2{fs::file_type::regular, fs::perms::owner_read | fs::perms::owner_write | fs::perms::owner_exec};
4✔
1265
        fs::file_status fs3{fs::file_type::directory, fs::perms::owner_read | fs::perms::owner_write | fs::perms::owner_exec};
4✔
1266
        fs::file_status fs4{fs::file_type::regular, fs::perms::owner_read | fs::perms::owner_write};
4✔
1267
        CHECK(fs1 == fs2);
2✔
1268
        CHECK_FALSE(fs1 == fs3);
2✔
1269
        CHECK_FALSE(fs1 == fs4);
2✔
1270
    }
1271
#endif
1272
}
2✔
1273

1274
TEST_CASE("fs.dir.entry - class directory_entry", "[filesystem][directory_entry][fs.dir.entry]")
2✔
1275
{
1276
    TemporaryDirectory t;
4✔
1277
    std::error_code ec;
2✔
1278
    auto de = fs::directory_entry(t.path());
4✔
1279
    CHECK(de.path() == t.path());
2✔
1280
    CHECK((fs::path)de == t.path());
2✔
1281
    CHECK(de.exists());
2✔
1282
    CHECK(!de.is_block_file());
2✔
1283
    CHECK(!de.is_character_file());
2✔
1284
    CHECK(de.is_directory());
2✔
1285
    CHECK(!de.is_fifo());
2✔
1286
    CHECK(!de.is_other());
2✔
1287
    CHECK(!de.is_regular_file());
2✔
1288
    CHECK(!de.is_socket());
2✔
1289
    CHECK(!de.is_symlink());
2✔
1290
    CHECK(de.status().type() == fs::file_type::directory);
2✔
1291
    ec.clear();
2✔
1292
    CHECK(de.status(ec).type() == fs::file_type::directory);
2✔
1293
    CHECK(!ec);
2✔
1294
    CHECK_NOTHROW(de.refresh());
2✔
1295
    fs::directory_entry none;
4✔
1296
    CHECK_THROWS_AS(none.refresh(), fs::filesystem_error);
4✔
1297
    ec.clear();
2✔
1298
    CHECK_NOTHROW(none.refresh(ec));
2✔
1299
    CHECK(ec);
2✔
1300
    CHECK_THROWS_AS(de.assign(""), fs::filesystem_error);
6✔
1301
    ec.clear();
2✔
1302
    CHECK_NOTHROW(de.assign("", ec));
2✔
1303
    CHECK(ec);
2✔
1304
    generateFile(t.path() / "foo", 1234);
2✔
1305
    auto now = fs::file_time_type::clock::now();
2✔
1306
    CHECK_NOTHROW(de.assign(t.path() / "foo"));
2✔
1307
    CHECK_NOTHROW(de.assign(t.path() / "foo", ec));
2✔
1308
    CHECK(!ec);
2✔
1309
    de = fs::directory_entry(t.path() / "foo");
2✔
1310
    CHECK(de.path() == t.path() / "foo");
2✔
1311
    CHECK(de.exists());
2✔
1312
    CHECK(de.exists(ec));
2✔
1313
    CHECK(!ec);
2✔
1314
    CHECK(!de.is_block_file());
2✔
1315
    CHECK(!de.is_block_file(ec));
2✔
1316
    CHECK(!ec);
2✔
1317
    CHECK(!de.is_character_file());
2✔
1318
    CHECK(!de.is_character_file(ec));
2✔
1319
    CHECK(!ec);
2✔
1320
    CHECK(!de.is_directory());
2✔
1321
    CHECK(!de.is_directory(ec));
2✔
1322
    CHECK(!ec);
2✔
1323
    CHECK(!de.is_fifo());
2✔
1324
    CHECK(!de.is_fifo(ec));
2✔
1325
    CHECK(!ec);
2✔
1326
    CHECK(!de.is_other());
2✔
1327
    CHECK(!de.is_other(ec));
2✔
1328
    CHECK(!ec);
2✔
1329
    CHECK(de.is_regular_file());
2✔
1330
    CHECK(de.is_regular_file(ec));
2✔
1331
    CHECK(!ec);
2✔
1332
    CHECK(!de.is_socket());
2✔
1333
    CHECK(!de.is_socket(ec));
2✔
1334
    CHECK(!ec);
2✔
1335
    CHECK(!de.is_symlink());
2✔
1336
    CHECK(!de.is_symlink(ec));
2✔
1337
    CHECK(!ec);
2✔
1338
    CHECK(de.file_size() == 1234);
2✔
1339
    CHECK(de.file_size(ec) == 1234);
2✔
1340
    CHECK(std::abs(std::chrono::duration_cast<std::chrono::seconds>(de.last_write_time() - now).count()) < 3);
2✔
1341
    ec.clear();
2✔
1342
    CHECK(std::abs(std::chrono::duration_cast<std::chrono::seconds>(de.last_write_time(ec) - now).count()) < 3);
2✔
1343
    CHECK(!ec);
2✔
1344
#ifndef GHC_OS_WEB
1345
    CHECK(de.hard_link_count() == 1);
2✔
1346
    CHECK(de.hard_link_count(ec) == 1);
2✔
1347
    CHECK(!ec);
2✔
1348
#endif
1349
    CHECK_THROWS_AS(de.replace_filename("bar"), fs::filesystem_error);
6✔
1350
    CHECK_NOTHROW(de.replace_filename("foo"));
2✔
1351
    ec.clear();
2✔
1352
    CHECK_NOTHROW(de.replace_filename("bar", ec));
2✔
1353
    CHECK(ec);
2✔
1354
    auto de2none = fs::directory_entry();
4✔
1355
    ec.clear();
2✔
1356
#ifndef GHC_OS_WEB
1357
    CHECK(de2none.hard_link_count(ec) == static_cast<uintmax_t>(-1));
2✔
1358
    CHECK_THROWS_AS(de2none.hard_link_count(), fs::filesystem_error);
4✔
1359
    CHECK(ec);
2✔
1360
#endif
1361
    ec.clear();
2✔
1362
    CHECK_NOTHROW(de2none.last_write_time(ec));
2✔
1363
    CHECK_THROWS_AS(de2none.last_write_time(), fs::filesystem_error);
4✔
1364
    CHECK(ec);
2✔
1365
    ec.clear();
2✔
1366
    CHECK_THROWS_AS(de2none.file_size(), fs::filesystem_error);
4✔
1367
    CHECK(de2none.file_size(ec) == static_cast<uintmax_t>(-1));
2✔
1368
    CHECK(ec);
2✔
1369
    ec.clear();
2✔
1370
    CHECK(de2none.status().type() == fs::file_type::not_found);
2✔
1371
    CHECK(de2none.status(ec).type() == fs::file_type::not_found);
2✔
1372
    CHECK(ec);
2✔
1373
    generateFile(t.path() / "a");
2✔
1374
    generateFile(t.path() / "b");
2✔
1375
    auto d1 = fs::directory_entry(t.path() / "a");
6✔
1376
    auto d2 = fs::directory_entry(t.path() / "b");
6✔
1377
    CHECK(d1 < d2);
2✔
1378
    CHECK(!(d2 < d1));
2✔
1379
    CHECK(d1 <= d2);
2✔
1380
    CHECK(!(d2 <= d1));
2✔
1381
    CHECK(d2 > d1);
2✔
1382
    CHECK(!(d1 > d2));
2✔
1383
    CHECK(d2 >= d1);
2✔
1384
    CHECK(!(d1 >= d2));
2✔
1385
    CHECK(d1 != d2);
2✔
1386
    CHECK(!(d2 != d2));
2✔
1387
    CHECK(d1 == d1);
2✔
1388
    CHECK(!(d1 == d2));
2✔
1389
}
2✔
1390

1391
TEST_CASE("fs.class.directory_iterator - class directory_iterator", "[filesystem][directory_iterator][fs.class.directory_iterator]")
2✔
1392
{
1393
    {
1394
        TemporaryDirectory t;
4✔
1395
        CHECK(fs::directory_iterator(t.path()) == fs::directory_iterator());
2✔
1396
        generateFile(t.path() / "test", 1234);
2✔
1397
        REQUIRE(fs::directory_iterator(t.path()) != fs::directory_iterator());
2✔
1398
        auto iter = fs::directory_iterator(t.path());
4✔
1399
        fs::directory_iterator iter2(iter);
4✔
1400
        fs::directory_iterator iter3, iter4;
4✔
1401
        iter3 = iter;
2✔
1402
        CHECK(iter->path().filename() == "test");
2✔
1403
        CHECK(iter2->path().filename() == "test");
2✔
1404
        CHECK(iter3->path().filename() == "test");
2✔
1405
        iter4 = std::move(iter3);
2✔
1406
        CHECK(iter4->path().filename() == "test");
2✔
1407
        CHECK(iter->path() == t.path() / "test");
2✔
1408
        CHECK(!iter->is_symlink());
2✔
1409
        CHECK(iter->is_regular_file());
2✔
1410
        CHECK(!iter->is_directory());
2✔
1411
        CHECK(iter->file_size() == 1234);
2✔
1412
        CHECK(++iter == fs::directory_iterator());
2✔
1413
        CHECK_THROWS_AS(fs::directory_iterator(t.path() / "non-existing"), fs::filesystem_error);
8✔
1414
        int cnt = 0;
2✔
1415
        for(auto de : fs::directory_iterator(t.path())) {
6✔
1416
            ++cnt;
2✔
1417
        }
1418
        CHECK(cnt == 1);
2✔
1419
    }
1420
    if (is_symlink_creation_supported()) {
2✔
1421
        TemporaryDirectory t;
4✔
1422
        fs::path td = t.path() / "testdir";
4✔
1423
        CHECK(fs::directory_iterator(t.path()) == fs::directory_iterator());
2✔
1424
        generateFile(t.path() / "test", 1234);
2✔
1425
        fs::create_directory(td);
2✔
1426
        REQUIRE_NOTHROW(fs::create_symlink(t.path() / "test", td / "testlink"));
2✔
1427
        std::error_code ec;
2✔
1428
        REQUIRE(fs::directory_iterator(td) != fs::directory_iterator());
2✔
1429
        auto iter = fs::directory_iterator(td);
4✔
1430
        CHECK(iter->path().filename() == "testlink");
2✔
1431
        CHECK(iter->path() == td / "testlink");
2✔
1432
        CHECK(iter->is_symlink());
2✔
1433
        CHECK(iter->is_regular_file());
2✔
1434
        CHECK(!iter->is_directory());
2✔
1435
        CHECK(iter->file_size() == 1234);
2✔
1436
        CHECK(++iter == fs::directory_iterator());
2✔
1437
    }
1438
    {
1439
        // Issue #8: check if resources are freed when iterator reaches end()
1440
        TemporaryDirectory t(TempOpt::change_path);
4✔
1441
        auto p = fs::path("test/");
4✔
1442
        fs::create_directory(p);
2✔
1443
        auto iter = fs::directory_iterator(p);
4✔
1444
        while (iter != fs::directory_iterator()) {
2✔
1445
            ++iter;
×
1446
        }
1447
        CHECK(fs::remove_all(p) == 1);
2✔
1448
        CHECK_NOTHROW(fs::create_directory(p));
2✔
1449
    }
1450
}
2✔
1451

1452
TEST_CASE("fs.class.rec.dir.itr - class recursive_directory_iterator", "[filesystem][recursive_directory_iterator][fs.class.rec.dir.itr]")
2✔
1453
{
1454
    {
1455
        auto iter = fs::recursive_directory_iterator(".");
4✔
1456
        iter.pop();
2✔
1457
        CHECK(iter == fs::recursive_directory_iterator());
2✔
1458
    }
1459
    {
1460
        TemporaryDirectory t;
4✔
1461
        CHECK(fs::recursive_directory_iterator(t.path()) == fs::recursive_directory_iterator());
2✔
1462
        generateFile(t.path() / "test", 1234);
2✔
1463
        REQUIRE(fs::recursive_directory_iterator(t.path()) != fs::recursive_directory_iterator());
2✔
1464
        auto iter = fs::recursive_directory_iterator(t.path());
4✔
1465
        CHECK(iter->path().filename() == "test");
2✔
1466
        CHECK(iter->path() == t.path() / "test");
2✔
1467
        CHECK(!iter->is_symlink());
2✔
1468
        CHECK(iter->is_regular_file());
2✔
1469
        CHECK(!iter->is_directory());
2✔
1470
        CHECK(iter->file_size() == 1234);
2✔
1471
        CHECK(++iter == fs::recursive_directory_iterator());
2✔
1472
    }
1473

1474
    {
1475
        TemporaryDirectory t;
4✔
1476
        fs::path td = t.path() / "testdir";
4✔
1477
        fs::create_directories(td);
2✔
1478
        generateFile(td / "test", 1234);
2✔
1479
        REQUIRE(fs::recursive_directory_iterator(t.path()) != fs::recursive_directory_iterator());
2✔
1480
        auto iter = fs::recursive_directory_iterator(t.path());
4✔
1481

1482
        CHECK(iter->path().filename() == "testdir");
2✔
1483
        CHECK(iter->path() == td);
2✔
1484
        CHECK(!iter->is_symlink());
2✔
1485
        CHECK(!iter->is_regular_file());
2✔
1486
        CHECK(iter->is_directory());
2✔
1487

1488
        CHECK(++iter != fs::recursive_directory_iterator());
2✔
1489

1490
        CHECK(iter->path().filename() == "test");
2✔
1491
        CHECK(iter->path() == td / "test");
2✔
1492
        CHECK(!iter->is_symlink());
2✔
1493
        CHECK(iter->is_regular_file());
2✔
1494
        CHECK(!iter->is_directory());
2✔
1495
        CHECK(iter->file_size() == 1234);
2✔
1496

1497
        CHECK(++iter == fs::recursive_directory_iterator());
2✔
1498
    }
1499
    {
1500
        TemporaryDirectory t;
4✔
1501
        std::error_code ec;
2✔
1502
        CHECK(fs::recursive_directory_iterator(t.path(), fs::directory_options::none) == fs::recursive_directory_iterator());
2✔
1503
        CHECK(fs::recursive_directory_iterator(t.path(), fs::directory_options::none, ec) == fs::recursive_directory_iterator());
2✔
1504
        CHECK(!ec);
2✔
1505
        CHECK(fs::recursive_directory_iterator(t.path(), ec) == fs::recursive_directory_iterator());
2✔
1506
        CHECK(!ec);
2✔
1507
        generateFile(t.path() / "test");
2✔
1508
        fs::recursive_directory_iterator rd1(t.path());
4✔
1509
        CHECK(fs::recursive_directory_iterator(rd1) != fs::recursive_directory_iterator());
2✔
1510
        fs::recursive_directory_iterator rd2(t.path());
4✔
1511
        CHECK(fs::recursive_directory_iterator(std::move(rd2)) != fs::recursive_directory_iterator());
2✔
1512
        fs::recursive_directory_iterator rd3(t.path(), fs::directory_options::skip_permission_denied);
4✔
1513
        CHECK(rd3.options() == fs::directory_options::skip_permission_denied);
2✔
1514
        fs::recursive_directory_iterator rd4;
4✔
1515
        rd4 = std::move(rd3);
2✔
1516
        CHECK(rd4 != fs::recursive_directory_iterator());
2✔
1517
        CHECK_NOTHROW(++rd4);
2✔
1518
        CHECK(rd4 == fs::recursive_directory_iterator());
2✔
1519
        fs::recursive_directory_iterator rd5;
4✔
1520
        rd5 = rd4;
2✔
1521
    }
1522
    {
1523
        TemporaryDirectory t(TempOpt::change_path);
4✔
1524
        generateFile("a");
2✔
1525
        fs::create_directory("d1");
2✔
1526
        fs::create_directory("d1/d2");
2✔
1527
        generateFile("d1/b");
2✔
1528
        generateFile("d1/c");
2✔
1529
        generateFile("d1/d2/d");
2✔
1530
        generateFile("e");
2✔
1531
        auto iter = fs::recursive_directory_iterator(".");
4✔
1532
        std::multimap<std::string, int> result;
4✔
1533
        while(iter != fs::recursive_directory_iterator()) {
16✔
1534
            result.insert(std::make_pair(iter->path().generic_string(), iter.depth()));
14✔
1535
            ++iter;
14✔
1536
        }
1537
        std::stringstream os;
4✔
1538
        for(auto p : result) {
16✔
1539
            os << "[" << p.first << "," << p.second << "],";
14✔
1540
        }
1541
        CHECK(os.str() == "[./a,0],[./d1,0],[./d1/b,1],[./d1/c,1],[./d1/d2,1],[./d1/d2/d,2],[./e,0],");
2✔
1542
    }
1543
    {
1544
        TemporaryDirectory t(TempOpt::change_path);
4✔
1545
        generateFile("a");
2✔
1546
        fs::create_directory("d1");
2✔
1547
        fs::create_directory("d1/d2");
2✔
1548
        generateFile("d1/b");
2✔
1549
        generateFile("d1/c");
2✔
1550
        generateFile("d1/d2/d");
2✔
1551
        generateFile("e");
2✔
1552
        std::multiset<std::string> result;
4✔
1553
        for(auto de : fs::recursive_directory_iterator(".")) {
18✔
1554
            result.insert(de.path().generic_string());
14✔
1555
        }
1556
        std::stringstream os;
4✔
1557
        for(auto p : result) {
16✔
1558
            os << p << ",";
14✔
1559
        }
1560
        CHECK(os.str() == "./a,./d1,./d1/b,./d1/c,./d1/d2,./d1/d2/d,./e,");
2✔
1561
    }
1562
    {
1563
        TemporaryDirectory t(TempOpt::change_path);
4✔
1564
        generateFile("a");
2✔
1565
        fs::create_directory("d1");
2✔
1566
        fs::create_directory("d1/d2");
2✔
1567
        generateFile("d1/d2/b");
2✔
1568
        generateFile("e");
2✔
1569
        auto iter = fs::recursive_directory_iterator(".");
4✔
1570
        std::multimap<std::string, int> result;
4✔
1571
        while(iter != fs::recursive_directory_iterator()) {
10✔
1572
            result.insert(std::make_pair(iter->path().generic_string(), iter.depth()));
8✔
1573
            if(iter->path() == "./d1/d2") {
8✔
1574
                iter.disable_recursion_pending();
2✔
1575
            }
1576
            ++iter;
8✔
1577
        }
1578
        std::stringstream os;
4✔
1579
        for(auto p : result) {
10✔
1580
            os << "[" << p.first << "," << p.second << "],";
8✔
1581
        }
1582
        CHECK(os.str() == "[./a,0],[./d1,0],[./d1/d2,1],[./e,0],");
2✔
1583
    }
1584
    {
1585
        TemporaryDirectory t(TempOpt::change_path);
4✔
1586
        generateFile("a");
2✔
1587
        fs::create_directory("d1");
2✔
1588
        fs::create_directory("d1/d2");
2✔
1589
        generateFile("d1/d2/b");
2✔
1590
        generateFile("e");
2✔
1591
        auto iter = fs::recursive_directory_iterator(".");
4✔
1592
        std::multimap<std::string, int> result;
4✔
1593
        while(iter != fs::recursive_directory_iterator()) {
10✔
1594
            result.insert(std::make_pair(iter->path().generic_string(), iter.depth()));
8✔
1595
            if(iter->path() == "./d1/d2") {
8✔
1596
                iter.pop();
2✔
1597
            }
1598
            else {
1599
                ++iter;
6✔
1600
            }
1601
        }
1602
        std::stringstream os;
4✔
1603
        for(auto p : result) {
10✔
1604
            os << "[" << p.first << "," << p.second << "],";
8✔
1605
        }
1606
        CHECK(os.str() == "[./a,0],[./d1,0],[./d1/d2,1],[./e,0],");
2✔
1607
    }
1608
    if (is_symlink_creation_supported()) {
2✔
1609
        TemporaryDirectory t(TempOpt::change_path);
4✔
1610
        fs::create_directory("d1");
2✔
1611
        generateFile("d1/a");
2✔
1612
        fs::create_directory("d2");
2✔
1613
        generateFile("d2/b");
2✔
1614
        fs::create_directory_symlink("../d1", "d2/ds1");
2✔
1615
        fs::create_directory_symlink("d3", "d2/ds2");
2✔
1616
        std::multiset<std::string> result;
4✔
1617
        REQUIRE_NOTHROW([&](){
14✔
1618
            for (const auto& de : fs::recursive_directory_iterator("d2", fs::directory_options::follow_directory_symlink)) {
1619
                result.insert(de.path().generic_string());
1620
            }
1621
        }());
1622
        std::stringstream os;
4✔
1623
        for(const auto& p : result) {
10✔
1624
            os << p << ",";
8✔
1625
        }
1626
        CHECK(os.str() == "d2/b,d2/ds1,d2/ds1/a,d2/ds2,");
2✔
1627
        os.str("");
2✔
1628
        result.clear();
2✔
1629
        REQUIRE_NOTHROW([&](){
12✔
1630
          for (const auto& de : fs::recursive_directory_iterator("d2")) {
1631
              result.insert(de.path().generic_string());
1632
          }
1633
        }());
1634
         for(const auto& p : result) {
8✔
1635
            os << p << ",";
6✔
1636
        }
1637
        CHECK(os.str() == "d2/b,d2/ds1,d2/ds2,");
2✔
1638
    }
1639
}
2✔
1640

1641
TEST_CASE("fs.op.absolute - absolute", "[filesystem][operations][fs.op.absolute]")
2✔
1642
{
1643
    CHECK(fs::absolute("") == fs::current_path() / "");
2✔
1644
    CHECK(fs::absolute(fs::current_path()) == fs::current_path());
2✔
1645
    CHECK(fs::absolute(".") == fs::current_path() / ".");
2✔
1646
    CHECK((fs::absolute("..") == fs::current_path().parent_path() || fs::absolute("..") == fs::current_path() / ".."));
2✔
1647
    CHECK(fs::absolute("foo") == fs::current_path() / "foo");
2✔
1648
    std::error_code ec;
2✔
1649
    CHECK(fs::absolute("", ec) == fs::current_path() / "");
2✔
1650
    CHECK(!ec);
2✔
1651
    CHECK(fs::absolute("foo", ec) == fs::current_path() / "foo");
2✔
1652
    CHECK(!ec);
2✔
1653
}
2✔
1654

1655
TEST_CASE("fs.op.canonical - canonical", "[filesystem][operations][fs.op.canonical]")
2✔
1656
{
1657
    CHECK_THROWS_AS(fs::canonical(""), fs::filesystem_error);
6✔
1658
    {
1659
        std::error_code ec;
2✔
1660
        CHECK(fs::canonical("", ec) == "");
2✔
1661
        CHECK(ec);
2✔
1662
    }
1663
    CHECK(fs::canonical(fs::current_path()) == fs::current_path());
2✔
1664

1665
    CHECK(fs::canonical(".") == fs::current_path());
2✔
1666
    CHECK(fs::canonical("..") == fs::current_path().parent_path());
2✔
1667
    CHECK(fs::canonical("/") == fs::current_path().root_path());
2✔
1668
    CHECK_THROWS_AS(fs::canonical("foo"), fs::filesystem_error);
6✔
1669
    {
1670
        std::error_code ec;
2✔
1671
        CHECK_NOTHROW(fs::canonical("foo", ec));
2✔
1672
        CHECK(ec);
2✔
1673
    }
1674
    {
1675
        TemporaryDirectory t(TempOpt::change_path);
4✔
1676
        auto dir = t.path() / "d0";
4✔
1677
        fs::create_directories(dir / "d1");
2✔
1678
        generateFile(dir / "f0");
2✔
1679
        fs::path rel(dir.filename());
4✔
1680
        CHECK(fs::canonical(dir) == dir);
2✔
1681
        CHECK(fs::canonical(rel) == dir);
2✔
1682
        CHECK(fs::canonical(dir / "f0") == dir / "f0");
2✔
1683
        CHECK(fs::canonical(rel / "f0") == dir / "f0");
2✔
1684
        CHECK(fs::canonical(rel / "./f0") == dir / "f0");
2✔
1685
        CHECK(fs::canonical(rel / "d1/../f0") == dir / "f0");
2✔
1686
    }
1687

1688
    if (is_symlink_creation_supported()) {
2✔
1689
        TemporaryDirectory t(TempOpt::change_path);
4✔
1690
        fs::create_directory(t.path() / "dir1");
2✔
1691
        generateFile(t.path() / "dir1/test1");
2✔
1692
        fs::create_directory(t.path() / "dir2");
2✔
1693
        fs::create_directory_symlink(t.path() / "dir1", t.path() / "dir2/dirSym");
2✔
1694
        CHECK(fs::canonical(t.path() / "dir2/dirSym/test1") == t.path() / "dir1/test1");
2✔
1695
    }
1696
}
2✔
1697

1698
TEST_CASE("fs.op.copy - copy", "[filesystem][operations][fs.op.copy]")
2✔
1699
{
1700
    {
1701
        TemporaryDirectory t(TempOpt::change_path);
4✔
1702
        std::error_code ec;
2✔
1703
        fs::create_directory("dir1");
2✔
1704
        generateFile("dir1/file1");
2✔
1705
        generateFile("dir1/file2");
2✔
1706
        fs::create_directory("dir1/dir2");
2✔
1707
        generateFile("dir1/dir2/file3");
2✔
1708
        CHECK_NOTHROW(fs::copy("dir1", "dir3"));
2✔
1709
        CHECK(fs::exists("dir3/file1"));
2✔
1710
        CHECK(fs::exists("dir3/file2"));
2✔
1711
        CHECK(!fs::exists("dir3/dir2"));
2✔
1712
        CHECK_NOTHROW(fs::copy("dir1", "dir4", fs::copy_options::recursive, ec));
2✔
1713
        CHECK(!ec);
2✔
1714
        CHECK(fs::exists("dir4/file1"));
2✔
1715
        CHECK(fs::exists("dir4/file2"));
2✔
1716
        CHECK(fs::exists("dir4/dir2/file3"));
2✔
1717
        fs::create_directory("dir5");
2✔
1718
        generateFile("dir5/file1");
2✔
1719
        CHECK_THROWS_AS(fs::copy("dir1/file1", "dir5/file1"), fs::filesystem_error);
8✔
1720
        CHECK_NOTHROW(fs::copy("dir1/file1", "dir5/file1", fs::copy_options::skip_existing));
2✔
1721
    }
1722
    if (is_symlink_creation_supported()) {
2✔
1723
        TemporaryDirectory t(TempOpt::change_path);
4✔
1724
        std::error_code ec;
2✔
1725
        fs::create_directory("dir1");
2✔
1726
        generateFile("dir1/file1");
2✔
1727
        generateFile("dir1/file2");
2✔
1728
        fs::create_directory("dir1/dir2");
2✔
1729
        generateFile("dir1/dir2/file3");
2✔
1730
#ifdef TEST_LWG_2682_BEHAVIOUR
1731
        REQUIRE_THROWS_AS(fs::copy("dir1", "dir3", fs::copy_options::create_symlinks | fs::copy_options::recursive), fs::filesystem_error);
8✔
1732
#else
1733
        REQUIRE_NOTHROW(fs::copy("dir1", "dir3", fs::copy_options::create_symlinks | fs::copy_options::recursive));
1734
        CHECK(!ec);
1735
        CHECK(fs::exists("dir3/file1"));
1736
        CHECK(fs::is_symlink("dir3/file1"));
1737
        CHECK(fs::exists("dir3/file2"));
1738
        CHECK(fs::is_symlink("dir3/file2"));
1739
        CHECK(fs::exists("dir3/dir2/file3"));
1740
        CHECK(fs::is_symlink("dir3/dir2/file3"));
1741
#endif
1742
    }
1743
#ifndef GHC_OS_WEB
1744
    {
1745
        TemporaryDirectory t(TempOpt::change_path);
4✔
1746
        std::error_code ec;
2✔
1747
        fs::create_directory("dir1");
2✔
1748
        generateFile("dir1/file1");
2✔
1749
        generateFile("dir1/file2");
2✔
1750
        fs::create_directory("dir1/dir2");
2✔
1751
        generateFile("dir1/dir2/file3");
2✔
1752
        auto f1hl = fs::hard_link_count("dir1/file1");
2✔
1753
        auto f2hl = fs::hard_link_count("dir1/file2");
2✔
1754
        auto f3hl = fs::hard_link_count("dir1/dir2/file3");
2✔
1755
        CHECK_NOTHROW(fs::copy("dir1", "dir3", fs::copy_options::create_hard_links | fs::copy_options::recursive, ec));
2✔
1756
        REQUIRE(!ec);
2✔
1757
        CHECK(fs::exists("dir3/file1"));
2✔
1758
        CHECK(fs::hard_link_count("dir1/file1") == f1hl + 1);
2✔
1759
        CHECK(fs::exists("dir3/file2"));
2✔
1760
        CHECK(fs::hard_link_count("dir1/file2") == f2hl + 1);
2✔
1761
        CHECK(fs::exists("dir3/dir2/file3"));
2✔
1762
        CHECK(fs::hard_link_count("dir1/dir2/file3") == f3hl + 1);
2✔
1763
    }
1764
#endif
1765
}
2✔
1766

1767
TEST_CASE("fs.op.copy_file - copy_file", "[filesystem][operations][fs.op.copy_file]")
2✔
1768
{
1769
    TemporaryDirectory t(TempOpt::change_path);
4✔
1770
    std::error_code ec;
2✔
1771
    generateFile("foo", 100);
2✔
1772
    CHECK(!fs::exists("bar"));
2✔
1773
    CHECK(fs::copy_file("foo", "bar"));
2✔
1774
    CHECK(fs::exists("bar"));
2✔
1775
    CHECK(fs::file_size("foo") == fs::file_size("bar"));
2✔
1776
    CHECK(fs::copy_file("foo", "bar2", ec));
2✔
1777
    CHECK(!ec);
2✔
1778
    std::this_thread::sleep_for(std::chrono::seconds(1));
2✔
1779
    generateFile("foo2", 200);
2✔
1780
    CHECK(fs::copy_file("foo2", "bar", fs::copy_options::update_existing));
2✔
1781
    CHECK(fs::file_size("bar") == 200);
2✔
1782
    CHECK(!fs::copy_file("foo", "bar", fs::copy_options::update_existing));
2✔
1783
    CHECK(fs::file_size("bar") == 200);
2✔
1784
    CHECK(fs::copy_file("foo", "bar", fs::copy_options::overwrite_existing));
2✔
1785
    CHECK(fs::file_size("bar") == 100);
2✔
1786
    CHECK_THROWS_AS(fs::copy_file("foobar", "foobar2"), fs::filesystem_error);
8✔
1787
    CHECK_NOTHROW(fs::copy_file("foobar", "foobar2", ec));
2✔
1788
    CHECK(ec);
2✔
1789
    CHECK(!fs::exists("foobar"));
2✔
1790
    fs::path file1("temp1.txt");
4✔
1791
    fs::path file2("temp2.txt");
4✔
1792
    generateFile(file1, 200);
2✔
1793
    generateFile(file2, 200);
2✔
1794
    auto allWrite = fs::perms::owner_write | fs::perms::group_write | fs::perms::others_write;
2✔
1795
    CHECK_NOTHROW(fs::permissions(file1, allWrite, fs::perm_options::remove));
2✔
1796
    CHECK((fs::status(file1).permissions() & fs::perms::owner_write) != fs::perms::owner_write);
2✔
1797
    CHECK_NOTHROW(fs::permissions(file2, allWrite, fs::perm_options::add));
2✔
1798
    CHECK((fs::status(file2).permissions() & fs::perms::owner_write) == fs::perms::owner_write);
2✔
1799
    fs::copy_file(file1, file2, fs::copy_options::overwrite_existing);
2✔
1800
    CHECK((fs::status(file2).permissions() & fs::perms::owner_write) != fs::perms::owner_write);
2✔
1801
    CHECK_NOTHROW(fs::permissions(file1, allWrite, fs::perm_options::add));
2✔
1802
    CHECK_NOTHROW(fs::permissions(file2, allWrite, fs::perm_options::add));
2✔
1803
}
2✔
1804

1805
TEST_CASE("fs.op.copy_symlink - copy_symlink", "[filesystem][operations][fs.op.copy_symlink]")
2✔
1806
{
1807
    TemporaryDirectory t(TempOpt::change_path);
4✔
1808
    std::error_code ec;
2✔
1809
    generateFile("foo");
2✔
1810
    fs::create_directory("dir");
2✔
1811
    if (is_symlink_creation_supported()) {
2✔
1812
        fs::create_symlink("foo", "sfoo");
2✔
1813
        fs::create_directory_symlink("dir", "sdir");
2✔
1814
        CHECK_NOTHROW(fs::copy_symlink("sfoo", "sfooc"));
2✔
1815
        CHECK(fs::exists("sfooc"));
2✔
1816
        CHECK_NOTHROW(fs::copy_symlink("sfoo", "sfooc2", ec));
2✔
1817
        CHECK(fs::exists("sfooc2"));
2✔
1818
        CHECK(!ec);
2✔
1819
        CHECK_NOTHROW(fs::copy_symlink("sdir", "sdirc"));
2✔
1820
        CHECK(fs::exists("sdirc"));
2✔
1821
        CHECK_NOTHROW(fs::copy_symlink("sdir", "sdirc2", ec));
2✔
1822
        CHECK(fs::exists("sdirc2"));
2✔
1823
        CHECK(!ec);
2✔
1824
    }
1825
    CHECK_THROWS_AS(fs::copy_symlink("bar", "barc"), fs::filesystem_error);
8✔
1826
    CHECK_NOTHROW(fs::copy_symlink("bar", "barc", ec));
2✔
1827
    CHECK(ec);
2✔
1828
}
2✔
1829

1830
TEST_CASE("fs.op.create_directories - create_directories", "[filesystem][operations][fs.op.create_directories]")
2✔
1831
{
1832
    TemporaryDirectory t;
4✔
1833
    fs::path p = t.path() / "testdir";
4✔
1834
    fs::path p2 = p / "nested";
4✔
1835
    REQUIRE(!fs::exists(p));
2✔
1836
    REQUIRE(!fs::exists(p2));
2✔
1837
    CHECK(fs::create_directories(p2));
2✔
1838
    CHECK(fs::is_directory(p));
2✔
1839
    CHECK(fs::is_directory(p2));
2✔
1840
    CHECK(!fs::create_directories(p2));
2✔
1841
#ifdef TEST_LWG_2935_BEHAVIOUR
1842
    INFO("This test expects LWG #2935 result conformance.");
1843
    p = t.path() / "testfile";
1844
    generateFile(p);
1845
    CHECK(fs::is_regular_file(p));
1846
    CHECK(!fs::is_directory(p));
1847
    bool created = false;
1848
    CHECK_NOTHROW((created = fs::create_directories(p)));
1849
    CHECK(!created);
1850
    CHECK(fs::is_regular_file(p));
1851
    CHECK(!fs::is_directory(p));
1852
    std::error_code ec;
1853
    CHECK_NOTHROW((created = fs::create_directories(p, ec)));
1854
    CHECK(!created);
1855
    CHECK(!ec);
1856
    CHECK(fs::is_regular_file(p));
1857
    CHECK(!fs::is_directory(p));
1858
    CHECK(!fs::create_directories(p, ec));
1859
#else
1860
    INFO("This test expects conformance with P1164R1. (implemented by GCC with issue #86910.)");
4✔
1861
    p = t.path() / "testfile";
2✔
1862
    generateFile(p);
2✔
1863
    CHECK(fs::is_regular_file(p));
2✔
1864
    CHECK(!fs::is_directory(p));
2✔
1865
    CHECK_THROWS_AS(fs::create_directories(p), fs::filesystem_error);
4✔
1866
    CHECK(fs::is_regular_file(p));
2✔
1867
    CHECK(!fs::is_directory(p));
2✔
1868
    std::error_code ec;
2✔
1869
    CHECK_NOTHROW(fs::create_directories(p, ec));
2✔
1870
    CHECK(ec);
2✔
1871
    CHECK(fs::is_regular_file(p));
2✔
1872
    CHECK(!fs::is_directory(p));
2✔
1873
    CHECK(!fs::create_directories(p, ec));
2✔
1874
#endif
1875
}
2✔
1876

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

1924
TEST_CASE("fs.op.create_directory_symlink - create_directory_symlink", "[filesystem][operations][fs.op.create_directory_symlink]")
2✔
1925
{
1926
    if (is_symlink_creation_supported()) {
2✔
1927
        TemporaryDirectory t;
4✔
1928
        fs::create_directory(t.path() / "dir1");
2✔
1929
        generateFile(t.path() / "dir1/test1");
2✔
1930
        fs::create_directory(t.path() / "dir2");
2✔
1931
        fs::create_directory_symlink(t.path() / "dir1", t.path() / "dir2/dirSym");
2✔
1932
        CHECK(fs::exists(t.path() / "dir2/dirSym"));
2✔
1933
        CHECK(fs::is_symlink(t.path() / "dir2/dirSym"));
2✔
1934
        CHECK(fs::exists(t.path() / "dir2/dirSym/test1"));
2✔
1935
        CHECK(fs::is_regular_file(t.path() / "dir2/dirSym/test1"));
2✔
1936
        CHECK_THROWS_AS(fs::create_directory_symlink(t.path() / "dir1", t.path() / "dir2/dirSym"), fs::filesystem_error);
12✔
1937
        std::error_code ec;
2✔
1938
        CHECK_NOTHROW(fs::create_directory_symlink(t.path() / "dir1", t.path() / "dir2/dirSym", ec));
2✔
1939
        CHECK(ec);
2✔
1940
    }
1941
}
2✔
1942

1943
TEST_CASE("fs.op.create_hard_link - create_hard_link", "[filesystem][operations][fs.op.create_hard_link]")
2✔
1944
{
1945
#ifndef GHC_OS_WEB
1946
    TemporaryDirectory t(TempOpt::change_path);
4✔
1947
    std::error_code ec;
2✔
1948
    generateFile("foo", 1234);
2✔
1949
    CHECK_NOTHROW(fs::create_hard_link("foo", "bar"));
2✔
1950
    CHECK(fs::exists("bar"));
2✔
1951
    CHECK(!fs::is_symlink("bar"));
2✔
1952
    CHECK_NOTHROW(fs::create_hard_link("foo", "bar2", ec));
2✔
1953
    CHECK(fs::exists("bar2"));
2✔
1954
    CHECK(!fs::is_symlink("bar2"));
2✔
1955
    CHECK(!ec);
2✔
1956
    CHECK_THROWS_AS(fs::create_hard_link("nofoo", "bar"), fs::filesystem_error);
8✔
1957
    CHECK_NOTHROW(fs::create_hard_link("nofoo", "bar", ec));
2✔
1958
    CHECK(ec);
2✔
1959
#endif
1960
}
2✔
1961

1962
TEST_CASE("fs.op.create_symlink - create_symlink", "[filesystem][operations][fs.op.create_symlink]")
2✔
1963
{
1964
    if (is_symlink_creation_supported()) {
2✔
1965
        TemporaryDirectory t;
4✔
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_symlink(t.path() / "dir1/test1", t.path() / "dir2/fileSym");
2✔
1970
        CHECK(fs::exists(t.path() / "dir2/fileSym"));
2✔
1971
        CHECK(fs::is_symlink(t.path() / "dir2/fileSym"));
2✔
1972
        CHECK(fs::exists(t.path() / "dir2/fileSym"));
2✔
1973
        CHECK(fs::is_regular_file(t.path() / "dir2/fileSym"));
2✔
1974
        CHECK_THROWS_AS(fs::create_symlink(t.path() / "dir1", t.path() / "dir2/fileSym"), fs::filesystem_error);
12✔
1975
        std::error_code ec;
2✔
1976
        CHECK_NOTHROW(fs::create_symlink(t.path() / "dir1", t.path() / "dir2/fileSym", ec));
2✔
1977
        CHECK(ec);
2✔
1978
    }
1979
}
2✔
1980

1981
TEST_CASE("fs.op.current_path - current_path", "[filesystem][operations][fs.op.current_path]")
2✔
1982
{
1983
    TemporaryDirectory t;
4✔
1984
    std::error_code ec;
2✔
1985
    fs::path p1 = fs::current_path();
4✔
1986
    CHECK_NOTHROW(fs::current_path(t.path()));
2✔
1987
    CHECK(p1 != fs::current_path());
2✔
1988
    CHECK_NOTHROW(fs::current_path(p1, ec));
2✔
1989
    CHECK(!ec);
2✔
1990
    CHECK_THROWS_AS(fs::current_path(t.path() / "foo"), fs::filesystem_error);
8✔
1991
    CHECK(p1 == fs::current_path());
2✔
1992
    CHECK_NOTHROW(fs::current_path(t.path() / "foo", ec));
2✔
1993
    CHECK(ec);
2✔
1994
}
2✔
1995

1996
TEST_CASE("fs.op.equivalent - equivalent", "[filesystem][operations][fs.op.equivalent]")
2✔
1997
{
1998
    TemporaryDirectory t(TempOpt::change_path);
4✔
1999
    generateFile("foo", 1234);
2✔
2000
    CHECK(fs::equivalent(t.path() / "foo", "foo"));
2✔
2001
    if (is_symlink_creation_supported()) {
2✔
2002
        std::error_code ec(42, std::system_category());
2✔
2003
        fs::create_symlink("foo", "foo2");
2✔
2004
        CHECK(fs::equivalent("foo", "foo2"));
2✔
2005
        CHECK(fs::equivalent("foo", "foo2", ec));
2✔
2006
        CHECK(!ec);
2✔
2007
    }
2008
#ifdef TEST_LWG_2937_BEHAVIOUR
2009
    INFO("This test expects LWG #2937 result conformance.");
4✔
2010
    std::error_code ec;
2✔
2011
    bool result = false;
2✔
2012
    REQUIRE_THROWS_AS(fs::equivalent("foo", "foo3"), fs::filesystem_error);
8✔
2013
    CHECK_NOTHROW(result = fs::equivalent("foo", "foo3", ec));
2✔
2014
    CHECK(!result);
2✔
2015
    CHECK(ec);
2✔
2016
    ec.clear();
2✔
2017
    CHECK_THROWS_AS(fs::equivalent("foo3", "foo"), fs::filesystem_error);
8✔
2018
    CHECK_NOTHROW(result = fs::equivalent("foo3", "foo", ec));
2✔
2019
    CHECK(!result);
2✔
2020
    CHECK(ec);
2✔
2021
    ec.clear();
2✔
2022
    CHECK_THROWS_AS(fs::equivalent("foo3", "foo4"), fs::filesystem_error);
8✔
2023
    CHECK_NOTHROW(result = fs::equivalent("foo3", "foo4", ec));
2✔
2024
    CHECK(!result);
2✔
2025
    CHECK(ec);
2✔
2026
#else
2027
    INFO("This test expects conformance predating LWG #2937 result.");
2028
    std::error_code ec;
2029
    bool result = false;
2030
    REQUIRE_NOTHROW(result = fs::equivalent("foo", "foo3"));
2031
    CHECK(!result);
2032
    CHECK_NOTHROW(result = fs::equivalent("foo", "foo3", ec));
2033
    CHECK(!result);
2034
    CHECK(!ec);
2035
    ec.clear();
2036
    CHECK_NOTHROW(result = fs::equivalent("foo3", "foo"));
2037
    CHECK(!result);
2038
    CHECK_NOTHROW(result = fs::equivalent("foo3", "foo", ec));
2039
    CHECK(!result);
2040
    CHECK(!ec);
2041
    ec.clear();
2042
    CHECK_THROWS_AS(result = fs::equivalent("foo4", "foo3"), fs::filesystem_error);
2043
    CHECK(!result);
2044
    CHECK_NOTHROW(result = fs::equivalent("foo4", "foo3", ec));
2045
    CHECK(!result);
2046
    CHECK(ec);
2047
#endif
2048
}
2✔
2049

2050
TEST_CASE("fs.op.exists - exists", "[filesystem][operations][fs.op.exists]")
2✔
2051
{
2052
    TemporaryDirectory t(TempOpt::change_path);
4✔
2053
    std::error_code ec;
2✔
2054
    CHECK(!fs::exists(""));
2✔
2055
    CHECK(!fs::exists("foo"));
2✔
2056
    CHECK(!fs::exists("foo", ec));
2✔
2057
    CHECK(!ec);
2✔
2058
    ec = std::error_code(42, std::system_category());
2✔
2059
    CHECK(!fs::exists("foo", ec));
2✔
2060
#if defined(__cpp_lib_char8_t) && !defined(GHC_FILESYSTEM_ENFORCE_CPP17_API)
2061
    CHECK(!fs::exists(u8"foo"));
1✔
2062
#endif
2063
    CHECK(!ec);
2✔
2064
    ec.clear();
2✔
2065
    CHECK(fs::exists(t.path()));
2✔
2066
    CHECK(fs::exists(t.path(), ec));
2✔
2067
    CHECK(!ec);
2✔
2068
    ec = std::error_code(42, std::system_category());
2✔
2069
    CHECK(fs::exists(t.path(), ec));
2✔
2070
    CHECK(!ec);
2✔
2071
#if defined(GHC_OS_WINDOWS) && !defined(GHC_FILESYSTEM_FWD)
2072
    if (::GetFileAttributesW(L"C:\\fs-test") != INVALID_FILE_ATTRIBUTES) {
2073
        CHECK(fs::exists("C:\\fs-test"));    
2074
    }
2075
#endif
2076
}
2✔
2077

2078
TEST_CASE("fs.op.file_size - file_size", "[filesystem][operations][fs.op.file_size]")
2✔
2079
{
2080
    TemporaryDirectory t(TempOpt::change_path);
4✔
2081
    std::error_code ec;
2✔
2082
    generateFile("foo", 0);
2✔
2083
    generateFile("bar", 1234);
2✔
2084
    CHECK(fs::file_size("foo") == 0);
2✔
2085
    ec = std::error_code(42, std::system_category());
2✔
2086
    CHECK(fs::file_size("foo", ec) == 0);
2✔
2087
    CHECK(!ec);
2✔
2088
    ec.clear();
2✔
2089
    CHECK(fs::file_size("bar") == 1234);
2✔
2090
    ec = std::error_code(42, std::system_category());
2✔
2091
    CHECK(fs::file_size("bar", ec) == 1234);
2✔
2092
    CHECK(!ec);
2✔
2093
    ec.clear();
2✔
2094
    CHECK_THROWS_AS(fs::file_size("foobar"), fs::filesystem_error);
6✔
2095
    CHECK(fs::file_size("foobar", ec) == static_cast<uintmax_t>(-1));
2✔
2096
    CHECK(ec);
2✔
2097
    ec.clear();
2✔
2098
}
2✔
2099

2100
#ifndef GHC_OS_WINDOWS
2101
static uintmax_t getHardlinkCount(const fs::path& p)
4✔
2102
{
2103
    struct stat st = {};
4✔
2104
    auto rc = ::lstat(p.c_str(), &st);
4✔
2105
    return rc == 0 ? st.st_nlink : ~0u;
4✔
2106
}
2107
#endif
2108

2109
TEST_CASE("fs.op.hard_link_count - hard_link_count", "[filesystem][operations][fs.op.hard_link_count]")
2✔
2110
{
2111
#ifndef GHC_OS_WEB
2112
    TemporaryDirectory t(TempOpt::change_path);
4✔
2113
    std::error_code ec;
2✔
2114
#ifdef GHC_OS_WINDOWS
2115
    // windows doesn't implement "."/".." as hardlinks, so it
2116
    // starts with 1 and subdirectories don't change the count
2117
    CHECK(fs::hard_link_count(t.path()) == 1);
2118
    fs::create_directory("dir");
2119
    CHECK(fs::hard_link_count(t.path()) == 1);
2120
#else
2121
    // unix/bsd/linux typically implements "."/".." as hardlinks
2122
    // so an empty dir has 2 (from parent and the ".") and
2123
    // adding a subdirectory adds one due to its ".."
2124
    CHECK(fs::hard_link_count(t.path()) == getHardlinkCount(t.path()));
2✔
2125
    fs::create_directory("dir");
2✔
2126
    CHECK(fs::hard_link_count(t.path()) == getHardlinkCount(t.path()));
2✔
2127
#endif
2128
    generateFile("foo");
2✔
2129
    CHECK(fs::hard_link_count(t.path() / "foo") == 1);
2✔
2130
    ec = std::error_code(42, std::system_category());
2✔
2131
    CHECK(fs::hard_link_count(t.path() / "foo", ec) == 1);
2✔
2132
    CHECK(!ec);
2✔
2133
    CHECK_THROWS_AS(fs::hard_link_count(t.path() / "bar"), fs::filesystem_error);
8✔
2134
    CHECK_NOTHROW(fs::hard_link_count(t.path() / "bar", ec));
2✔
2135
    CHECK(ec);
2✔
2136
    ec.clear();
2✔
2137
#else
2138
    WARN("Test for unsupportet features are disabled on JS/Wasm target.");
2139
#endif
2140
}
2✔
2141

2142
class FileTypeMixFixture
2143
{
2144
public:
2145
    FileTypeMixFixture()
16✔
2146
        : _t(TempOpt::change_path)
16✔
2147
        , _hasFifo(false)
2148
        , _hasSocket(false)
16✔
2149
    {
2150
        generateFile("regular");
16✔
2151
        fs::create_directory("directory");
16✔
2152
        if (is_symlink_creation_supported()) {
16✔
2153
            fs::create_symlink("regular", "file_symlink");
16✔
2154
            fs::create_directory_symlink("directory", "dir_symlink");
16✔
2155
        }
2156
#if !defined(GHC_OS_WINDOWS) && !defined(GHC_OS_WEB)
2157
        REQUIRE(::mkfifo("fifo", 0644) == 0);
16✔
2158
        _hasFifo = true;
16✔
2159
        struct ::sockaddr_un addr;
2160
        addr.sun_family = AF_UNIX;
16✔
2161
        std::strncpy(addr.sun_path, "socket", sizeof(addr.sun_path));
16✔
2162
        int fd = socket(PF_UNIX, SOCK_STREAM, 0);
16✔
2163
        bind(fd, (struct sockaddr*)&addr, sizeof addr);
16✔
2164
        _hasSocket = true;
16✔
2165
#endif
2166
    }
16✔
2167

2168
    ~FileTypeMixFixture() {}
16✔
2169

2170
    bool has_fifo() const { return _hasFifo; }
16✔
2171

2172
    bool has_socket() const { return _hasSocket; }
16✔
2173

2174
    fs::path block_path() const
32✔
2175
    {
2176
        std::error_code ec;
32✔
2177
        if (fs::exists("/dev/sda", ec)) {
32✔
2178
            return "/dev/sda";
32✔
2179
        }
2180
        else if (fs::exists("/dev/disk0", ec)) {
×
2181
            return "/dev/disk0";
×
2182
        }
2183
        return fs::path();
×
2184
    }
2185

2186
    fs::path character_path() const
32✔
2187
    {
2188
#ifndef GHC_OS_SOLARIS
2189
        std::error_code ec;
32✔
2190
        if (fs::exists("/dev/null", ec)) {
32✔
2191
            return "/dev/null";
32✔
2192
        }
2193
        else if (fs::exists("NUL", ec)) {
×
2194
            return "NUL";
×
2195
        }
2196
#endif
2197
        return fs::path();
×
2198
    }
2199
    fs::path temp_path() const { return _t.path(); }
2200

2201
private:
2202
    TemporaryDirectory _t;
2203
    bool _hasFifo;
2204
    bool _hasSocket;
2205
};
2206

2207
TEST_CASE_METHOD(FileTypeMixFixture, "fs.op.is_block_file - is_block_file", "[filesystem][operations][fs.op.is_block_file]")
2✔
2208
{
2209
    std::error_code ec;
2✔
2210
    CHECK(!fs::is_block_file("directory"));
2✔
2211
    CHECK(!fs::is_block_file("regular"));
2✔
2212
    if (is_symlink_creation_supported()) {
2✔
2213
        CHECK(!fs::is_block_file("dir_symlink"));
2✔
2214
        CHECK(!fs::is_block_file("file_symlink"));
2✔
2215
    }
2216
    CHECK((has_fifo() ? !fs::is_block_file("fifo") : true));
2✔
2217
    CHECK((has_socket() ? !fs::is_block_file("socket") : true));
2✔
2218
    CHECK((block_path().empty() ? true : fs::is_block_file(block_path())));
2✔
2219
    CHECK((character_path().empty() ? true : !fs::is_block_file(character_path())));
2✔
2220
    CHECK_NOTHROW(fs::is_block_file("notfound"));
2✔
2221
    CHECK_NOTHROW(fs::is_block_file("notfound", ec));
2✔
2222
    CHECK(ec);
2✔
2223
    ec.clear();
2✔
2224
    CHECK(!fs::is_block_file(fs::file_status(fs::file_type::none)));
2✔
2225
    CHECK(!fs::is_block_file(fs::file_status(fs::file_type::not_found)));
2✔
2226
    CHECK(!fs::is_block_file(fs::file_status(fs::file_type::regular)));
2✔
2227
    CHECK(!fs::is_block_file(fs::file_status(fs::file_type::directory)));
2✔
2228
    CHECK(!fs::is_block_file(fs::file_status(fs::file_type::symlink)));
2✔
2229
    CHECK(fs::is_block_file(fs::file_status(fs::file_type::block)));
2✔
2230
    CHECK(!fs::is_block_file(fs::file_status(fs::file_type::character)));
2✔
2231
    CHECK(!fs::is_block_file(fs::file_status(fs::file_type::fifo)));
2✔
2232
    CHECK(!fs::is_block_file(fs::file_status(fs::file_type::socket)));
2✔
2233
    CHECK(!fs::is_block_file(fs::file_status(fs::file_type::unknown)));
2✔
2234
}
2✔
2235

2236
TEST_CASE_METHOD(FileTypeMixFixture, "fs.op.is_character_file - is_character_file", "[filesystem][operations][fs.op.is_character_file]")
2✔
2237
{
2238
    std::error_code ec;
2✔
2239
    CHECK(!fs::is_character_file("directory"));
2✔
2240
    CHECK(!fs::is_character_file("regular"));
2✔
2241
    if (is_symlink_creation_supported()) {
2✔
2242
        CHECK(!fs::is_character_file("dir_symlink"));
2✔
2243
        CHECK(!fs::is_character_file("file_symlink"));
2✔
2244
    }
2245
    CHECK((has_fifo() ? !fs::is_character_file("fifo") : true));
2✔
2246
    CHECK((has_socket() ? !fs::is_character_file("socket") : true));
2✔
2247
    CHECK((block_path().empty() ? true : !fs::is_character_file(block_path())));
2✔
2248
    CHECK((character_path().empty() ? true : fs::is_character_file(character_path())));
2✔
2249
    CHECK_NOTHROW(fs::is_character_file("notfound"));
2✔
2250
    CHECK_NOTHROW(fs::is_character_file("notfound", ec));
2✔
2251
    CHECK(ec);
2✔
2252
    ec.clear();
2✔
2253
    CHECK(!fs::is_character_file(fs::file_status(fs::file_type::none)));
2✔
2254
    CHECK(!fs::is_character_file(fs::file_status(fs::file_type::not_found)));
2✔
2255
    CHECK(!fs::is_character_file(fs::file_status(fs::file_type::regular)));
2✔
2256
    CHECK(!fs::is_character_file(fs::file_status(fs::file_type::directory)));
2✔
2257
    CHECK(!fs::is_character_file(fs::file_status(fs::file_type::symlink)));
2✔
2258
    CHECK(!fs::is_character_file(fs::file_status(fs::file_type::block)));
2✔
2259
    CHECK(fs::is_character_file(fs::file_status(fs::file_type::character)));
2✔
2260
    CHECK(!fs::is_character_file(fs::file_status(fs::file_type::fifo)));
2✔
2261
    CHECK(!fs::is_character_file(fs::file_status(fs::file_type::socket)));
2✔
2262
    CHECK(!fs::is_character_file(fs::file_status(fs::file_type::unknown)));
2✔
2263
}
2✔
2264

2265
TEST_CASE_METHOD(FileTypeMixFixture, "fs.op.is_directory - is_directory", "[filesystem][operations][fs.op.is_directory]")
2✔
2266
{
2267
    std::error_code ec;
2✔
2268
    CHECK(fs::is_directory("directory"));
2✔
2269
    CHECK(!fs::is_directory("regular"));
2✔
2270
    if (is_symlink_creation_supported()) {
2✔
2271
        CHECK(fs::is_directory("dir_symlink"));
2✔
2272
        CHECK(!fs::is_directory("file_symlink"));
2✔
2273
    }
2274
    CHECK((has_fifo() ? !fs::is_directory("fifo") : true));
2✔
2275
    CHECK((has_socket() ? !fs::is_directory("socket") : true));
2✔
2276
    CHECK((block_path().empty() ? true : !fs::is_directory(block_path())));
2✔
2277
    CHECK((character_path().empty() ? true : !fs::is_directory(character_path())));
2✔
2278
    CHECK_NOTHROW(fs::is_directory("notfound"));
2✔
2279
    CHECK_NOTHROW(fs::is_directory("notfound", ec));
2✔
2280
    CHECK(ec);
2✔
2281
    ec.clear();
2✔
2282
    CHECK(!fs::is_directory(fs::file_status(fs::file_type::none)));
2✔
2283
    CHECK(!fs::is_directory(fs::file_status(fs::file_type::not_found)));
2✔
2284
    CHECK(!fs::is_directory(fs::file_status(fs::file_type::regular)));
2✔
2285
    CHECK(fs::is_directory(fs::file_status(fs::file_type::directory)));
2✔
2286
    CHECK(!fs::is_directory(fs::file_status(fs::file_type::symlink)));
2✔
2287
    CHECK(!fs::is_directory(fs::file_status(fs::file_type::block)));
2✔
2288
    CHECK(!fs::is_directory(fs::file_status(fs::file_type::character)));
2✔
2289
    CHECK(!fs::is_directory(fs::file_status(fs::file_type::fifo)));
2✔
2290
    CHECK(!fs::is_directory(fs::file_status(fs::file_type::socket)));
2✔
2291
    CHECK(!fs::is_directory(fs::file_status(fs::file_type::unknown)));
2✔
2292
}
2✔
2293

2294
TEST_CASE("fs.op.is_empty - is_empty", "[filesystem][operations][fs.op.is_empty]")
2✔
2295
{
2296
    TemporaryDirectory t(TempOpt::change_path);
4✔
2297
    std::error_code ec;
2✔
2298
    CHECK(fs::is_empty(t.path()));
2✔
2299
    CHECK(fs::is_empty(t.path(), ec));
2✔
2300
    CHECK(!ec);
2✔
2301
    generateFile("foo", 0);
2✔
2302
    generateFile("bar", 1234);
2✔
2303
    CHECK(fs::is_empty("foo"));
2✔
2304
    CHECK(fs::is_empty("foo", ec));
2✔
2305
    CHECK(!ec);
2✔
2306
    CHECK(!fs::is_empty("bar"));
2✔
2307
    CHECK(!fs::is_empty("bar", ec));
2✔
2308
    CHECK(!ec);
2✔
2309
    CHECK_THROWS_AS(fs::is_empty("foobar"), fs::filesystem_error);
6✔
2310
    bool result = false;
2✔
2311
    CHECK_NOTHROW(result = fs::is_empty("foobar", ec));
2✔
2312
    CHECK(!result);
2✔
2313
    CHECK(ec);
2✔
2314
}
2✔
2315

2316
TEST_CASE_METHOD(FileTypeMixFixture, "fs.op.is_fifo - is_fifo", "[filesystem][operations][fs.op.is_fifo]")
2✔
2317
{
2318
    std::error_code ec;
2✔
2319
    CHECK(!fs::is_fifo("directory"));
2✔
2320
    CHECK(!fs::is_fifo("regular"));
2✔
2321
    if (is_symlink_creation_supported()) {
2✔
2322
        CHECK(!fs::is_fifo("dir_symlink"));
2✔
2323
        CHECK(!fs::is_fifo("file_symlink"));
2✔
2324
    }
2325
    CHECK((has_fifo() ? fs::is_fifo("fifo") : true));
2✔
2326
    CHECK((has_socket() ? !fs::is_fifo("socket") : true));
2✔
2327
    CHECK((block_path().empty() ? true : !fs::is_fifo(block_path())));
2✔
2328
    CHECK((character_path().empty() ? true : !fs::is_fifo(character_path())));
2✔
2329
    CHECK_NOTHROW(fs::is_fifo("notfound"));
2✔
2330
    CHECK_NOTHROW(fs::is_fifo("notfound", ec));
2✔
2331
    CHECK(ec);
2✔
2332
    ec.clear();
2✔
2333
    CHECK(!fs::is_fifo(fs::file_status(fs::file_type::none)));
2✔
2334
    CHECK(!fs::is_fifo(fs::file_status(fs::file_type::not_found)));
2✔
2335
    CHECK(!fs::is_fifo(fs::file_status(fs::file_type::regular)));
2✔
2336
    CHECK(!fs::is_fifo(fs::file_status(fs::file_type::directory)));
2✔
2337
    CHECK(!fs::is_fifo(fs::file_status(fs::file_type::symlink)));
2✔
2338
    CHECK(!fs::is_fifo(fs::file_status(fs::file_type::block)));
2✔
2339
    CHECK(!fs::is_fifo(fs::file_status(fs::file_type::character)));
2✔
2340
    CHECK(fs::is_fifo(fs::file_status(fs::file_type::fifo)));
2✔
2341
    CHECK(!fs::is_fifo(fs::file_status(fs::file_type::socket)));
2✔
2342
    CHECK(!fs::is_fifo(fs::file_status(fs::file_type::unknown)));
2✔
2343
}
2✔
2344

2345
TEST_CASE_METHOD(FileTypeMixFixture, "fs.op.is_other - is_other", "[filesystem][operations][fs.op.is_other]")
2✔
2346
{
2347
    std::error_code ec;
2✔
2348
    CHECK(!fs::is_other("directory"));
2✔
2349
    CHECK(!fs::is_other("regular"));
2✔
2350
    if (is_symlink_creation_supported()) {
2✔
2351
        CHECK(!fs::is_other("dir_symlink"));
2✔
2352
        CHECK(!fs::is_other("file_symlink"));
2✔
2353
    }
2354
    CHECK((has_fifo() ? fs::is_other("fifo") : true));
2✔
2355
    CHECK((has_socket() ? fs::is_other("socket") : true));
2✔
2356
    CHECK((block_path().empty() ? true : fs::is_other(block_path())));
2✔
2357
    CHECK((character_path().empty() ? true : fs::is_other(character_path())));
2✔
2358
    CHECK_NOTHROW(fs::is_other("notfound"));
2✔
2359
    CHECK_NOTHROW(fs::is_other("notfound", ec));
2✔
2360
    CHECK(ec);
2✔
2361
    ec.clear();
2✔
2362
    CHECK(!fs::is_other(fs::file_status(fs::file_type::none)));
2✔
2363
    CHECK(!fs::is_other(fs::file_status(fs::file_type::not_found)));
2✔
2364
    CHECK(!fs::is_other(fs::file_status(fs::file_type::regular)));
2✔
2365
    CHECK(!fs::is_other(fs::file_status(fs::file_type::directory)));
2✔
2366
    CHECK(!fs::is_other(fs::file_status(fs::file_type::symlink)));
2✔
2367
    CHECK(fs::is_other(fs::file_status(fs::file_type::block)));
2✔
2368
    CHECK(fs::is_other(fs::file_status(fs::file_type::character)));
2✔
2369
    CHECK(fs::is_other(fs::file_status(fs::file_type::fifo)));
2✔
2370
    CHECK(fs::is_other(fs::file_status(fs::file_type::socket)));
2✔
2371
    CHECK(fs::is_other(fs::file_status(fs::file_type::unknown)));
2✔
2372
}
2✔
2373

2374
TEST_CASE_METHOD(FileTypeMixFixture, "fs.op.is_regular_file - is_regular_file", "[filesystem][operations][fs.op.is_regular_file]")
2✔
2375
{
2376
    std::error_code ec;
2✔
2377
    CHECK(!fs::is_regular_file("directory"));
2✔
2378
    CHECK(fs::is_regular_file("regular"));
2✔
2379
    if (is_symlink_creation_supported()) {
2✔
2380
        CHECK(!fs::is_regular_file("dir_symlink"));
2✔
2381
        CHECK(fs::is_regular_file("file_symlink"));
2✔
2382
    }
2383
    CHECK((has_fifo() ? !fs::is_regular_file("fifo") : true));
2✔
2384
    CHECK((has_socket() ? !fs::is_regular_file("socket") : true));
2✔
2385
    CHECK((block_path().empty() ? true : !fs::is_regular_file(block_path())));
2✔
2386
    CHECK((character_path().empty() ? true : !fs::is_regular_file(character_path())));
2✔
2387
    CHECK_NOTHROW(fs::is_regular_file("notfound"));
2✔
2388
    CHECK_NOTHROW(fs::is_regular_file("notfound", ec));
2✔
2389
    CHECK(ec);
2✔
2390
    ec.clear();
2✔
2391
    CHECK(!fs::is_regular_file(fs::file_status(fs::file_type::none)));
2✔
2392
    CHECK(!fs::is_regular_file(fs::file_status(fs::file_type::not_found)));
2✔
2393
    CHECK(fs::is_regular_file(fs::file_status(fs::file_type::regular)));
2✔
2394
    CHECK(!fs::is_regular_file(fs::file_status(fs::file_type::directory)));
2✔
2395
    CHECK(!fs::is_regular_file(fs::file_status(fs::file_type::symlink)));
2✔
2396
    CHECK(!fs::is_regular_file(fs::file_status(fs::file_type::block)));
2✔
2397
    CHECK(!fs::is_regular_file(fs::file_status(fs::file_type::character)));
2✔
2398
    CHECK(!fs::is_regular_file(fs::file_status(fs::file_type::fifo)));
2✔
2399
    CHECK(!fs::is_regular_file(fs::file_status(fs::file_type::socket)));
2✔
2400
    CHECK(!fs::is_regular_file(fs::file_status(fs::file_type::unknown)));
2✔
2401
}
2✔
2402

2403
TEST_CASE_METHOD(FileTypeMixFixture, "fs.op.is_socket - is_socket", "[filesystem][operations][fs.op.is_socket]")
2✔
2404
{
2405
    std::error_code ec;
2✔
2406
    CHECK(!fs::is_socket("directory"));
2✔
2407
    CHECK(!fs::is_socket("regular"));
2✔
2408
    if (is_symlink_creation_supported()) {
2✔
2409
        CHECK(!fs::is_socket("dir_symlink"));
2✔
2410
        CHECK(!fs::is_socket("file_symlink"));
2✔
2411
    }
2412
    CHECK((has_fifo() ? !fs::is_socket("fifo") : true));
2✔
2413
    CHECK((has_socket() ? fs::is_socket("socket") : true));
2✔
2414
    CHECK((block_path().empty() ? true : !fs::is_socket(block_path())));
2✔
2415
    CHECK((character_path().empty() ? true : !fs::is_socket(character_path())));
2✔
2416
    CHECK_NOTHROW(fs::is_socket("notfound"));
2✔
2417
    CHECK_NOTHROW(fs::is_socket("notfound", ec));
2✔
2418
    CHECK(ec);
2✔
2419
    ec.clear();
2✔
2420
    CHECK(!fs::is_socket(fs::file_status(fs::file_type::none)));
2✔
2421
    CHECK(!fs::is_socket(fs::file_status(fs::file_type::not_found)));
2✔
2422
    CHECK(!fs::is_socket(fs::file_status(fs::file_type::regular)));
2✔
2423
    CHECK(!fs::is_socket(fs::file_status(fs::file_type::directory)));
2✔
2424
    CHECK(!fs::is_socket(fs::file_status(fs::file_type::symlink)));
2✔
2425
    CHECK(!fs::is_socket(fs::file_status(fs::file_type::block)));
2✔
2426
    CHECK(!fs::is_socket(fs::file_status(fs::file_type::character)));
2✔
2427
    CHECK(!fs::is_socket(fs::file_status(fs::file_type::fifo)));
2✔
2428
    CHECK(fs::is_socket(fs::file_status(fs::file_type::socket)));
2✔
2429
    CHECK(!fs::is_socket(fs::file_status(fs::file_type::unknown)));
2✔
2430
}
2✔
2431

2432
TEST_CASE_METHOD(FileTypeMixFixture, "fs.op.is_symlink - is_symlink", "[filesystem][operations][fs.op.is_symlink]")
2✔
2433
{
2434
    std::error_code ec;
2✔
2435
    CHECK(!fs::is_symlink("directory"));
2✔
2436
    CHECK(!fs::is_symlink("regular"));
2✔
2437
    if (is_symlink_creation_supported()) {
2✔
2438
        CHECK(fs::is_symlink("dir_symlink"));
2✔
2439
        CHECK(fs::is_symlink("file_symlink"));
2✔
2440
    }
2441
    CHECK((has_fifo() ? !fs::is_symlink("fifo") : true));
2✔
2442
    CHECK((has_socket() ? !fs::is_symlink("socket") : true));
2✔
2443
    CHECK((block_path().empty() ? true : !fs::is_symlink(block_path())));
2✔
2444
    CHECK((character_path().empty() ? true : !fs::is_symlink(character_path())));
2✔
2445
    CHECK_NOTHROW(fs::is_symlink("notfound"));
2✔
2446
    CHECK_NOTHROW(fs::is_symlink("notfound", ec));
2✔
2447
    CHECK(ec);
2✔
2448
    ec.clear();
2✔
2449
    CHECK(!fs::is_symlink(fs::file_status(fs::file_type::none)));
2✔
2450
    CHECK(!fs::is_symlink(fs::file_status(fs::file_type::not_found)));
2✔
2451
    CHECK(!fs::is_symlink(fs::file_status(fs::file_type::regular)));
2✔
2452
    CHECK(!fs::is_symlink(fs::file_status(fs::file_type::directory)));
2✔
2453
    CHECK(fs::is_symlink(fs::file_status(fs::file_type::symlink)));
2✔
2454
    CHECK(!fs::is_symlink(fs::file_status(fs::file_type::block)));
2✔
2455
    CHECK(!fs::is_symlink(fs::file_status(fs::file_type::character)));
2✔
2456
    CHECK(!fs::is_symlink(fs::file_status(fs::file_type::fifo)));
2✔
2457
    CHECK(!fs::is_symlink(fs::file_status(fs::file_type::socket)));
2✔
2458
    CHECK(!fs::is_symlink(fs::file_status(fs::file_type::unknown)));
2✔
2459
}
2✔
2460

2461
#ifndef GHC_OS_WEB
2462
static fs::file_time_type timeFromString(const std::string& str)
4✔
2463
{
2464
    struct ::tm tm;
2465
    ::memset(&tm, 0, sizeof(::tm));
4✔
2466
    std::istringstream is(str);
8✔
2467
    is >> std::get_time(&tm, "%Y-%m-%dT%H:%M:%S");
4✔
2468
    if (is.fail()) {
4✔
2469
        throw std::exception();
×
2470
    }
2471
    return from_time_t<fs::file_time_type>(std::mktime(&tm));
8✔
2472
}
2473
#endif
2474

2475
TEST_CASE("fs.op.last_write_time - last_write_time", "[filesystem][operations][fs.op.last_write_time]")
2✔
2476
{
2477
    TemporaryDirectory t(TempOpt::change_path);
4✔
2478
    std::error_code ec;
2✔
2479
    fs::file_time_type ft;
2✔
2480
    generateFile("foo");
2✔
2481
    auto now = fs::file_time_type::clock::now();
2✔
2482
    CHECK(std::abs(std::chrono::duration_cast<std::chrono::seconds>(fs::last_write_time(t.path()) - now).count()) < 3);
2✔
2483
    CHECK(std::abs(std::chrono::duration_cast<std::chrono::seconds>(fs::last_write_time("foo") - now).count()) < 3);
2✔
2484
    CHECK_THROWS_AS(fs::last_write_time("bar"), fs::filesystem_error);
6✔
2485
    CHECK_NOTHROW(ft = fs::last_write_time("bar", ec));
2✔
2486
    CHECK(ft == fs::file_time_type::min());
2✔
2487
    CHECK(ec);
2✔
2488
    ec.clear();
2✔
2489
    if (is_symlink_creation_supported()) {
2✔
2490
        std::this_thread::sleep_for(std::chrono::seconds(1));
2✔
2491
        fs::create_symlink("foo", "foo2");
2✔
2492
        ft = fs::last_write_time("foo");
2✔
2493
        // checks that the time of the symlink is fetched
2494
        CHECK(ft == fs::last_write_time("foo2"));
2✔
2495
    }
2496
#ifndef GHC_OS_WEB
2497
    auto nt = timeFromString("2015-10-21T04:30:00");
2✔
2498
    CHECK_NOTHROW(fs::last_write_time(t.path() / "foo", nt));
2✔
2499
    CHECK(std::abs(std::chrono::duration_cast<std::chrono::seconds>(fs::last_write_time("foo") - nt).count()) < 1);
2✔
2500
    nt = timeFromString("2015-10-21T04:29:00");
2✔
2501
    CHECK_NOTHROW(fs::last_write_time("foo", nt, ec));
2✔
2502
    CHECK(std::abs(std::chrono::duration_cast<std::chrono::seconds>(fs::last_write_time("foo") - nt).count()) < 1);
2✔
2503
    CHECK(!ec);
2✔
2504
    CHECK_THROWS_AS(fs::last_write_time("bar", nt), fs::filesystem_error);
6✔
2505
    CHECK_NOTHROW(fs::last_write_time("bar", nt, ec));
2✔
2506
    CHECK(ec);
2✔
2507
#endif
2508
}
2✔
2509

2510
TEST_CASE("fs.op.permissions - permissions", "[filesystem][operations][fs.op.permissions]")
2✔
2511
{
2512
    TemporaryDirectory t(TempOpt::change_path);
4✔
2513
    std::error_code ec;
2✔
2514
    generateFile("foo", 512);
2✔
2515
    auto allWrite = fs::perms::owner_write | fs::perms::group_write | fs::perms::others_write;
2✔
2516
    CHECK_NOTHROW(fs::permissions("foo", allWrite, fs::perm_options::remove));
2✔
2517
    CHECK((fs::status("foo").permissions() & fs::perms::owner_write) != fs::perms::owner_write);
2✔
2518
#if !defined(GHC_OS_WINDOWS)
2519
    if (geteuid() != 0)
2✔
2520
#endif
2521
    {
2522
        CHECK_THROWS_AS(fs::resize_file("foo", 1024), fs::filesystem_error);
6✔
2523
        CHECK(fs::file_size("foo") == 512);
2✔
2524
    }
2525
    CHECK_NOTHROW(fs::permissions("foo", fs::perms::owner_write, fs::perm_options::add));
2✔
2526
    CHECK((fs::status("foo").permissions() & fs::perms::owner_write) == fs::perms::owner_write);
2✔
2527
    CHECK_NOTHROW(fs::resize_file("foo", 2048));
2✔
2528
    CHECK(fs::file_size("foo") == 2048);
2✔
2529
    CHECK_THROWS_AS(fs::permissions("bar", fs::perms::owner_write, fs::perm_options::add), fs::filesystem_error);
6✔
2530
    CHECK_NOTHROW(fs::permissions("bar", fs::perms::owner_write, fs::perm_options::add, ec));
2✔
2531
    CHECK(ec);
2✔
2532
    CHECK_THROWS_AS(fs::permissions("bar", fs::perms::owner_write, static_cast<fs::perm_options>(0)), fs::filesystem_error);
6✔
2533
}
2✔
2534

2535
TEST_CASE("fs.op.proximate - proximate", "[filesystem][operations][fs.op.proximate]")
2✔
2536
{
2537
    std::error_code ec;
2✔
2538
    CHECK(fs::proximate("/a/d", "/a/b/c") == "../../d");
2✔
2539
    CHECK(fs::proximate("/a/d", "/a/b/c", ec) == "../../d");
2✔
2540
    CHECK(!ec);
2✔
2541
    CHECK(fs::proximate("/a/b/c", "/a/d") == "../b/c");
2✔
2542
    CHECK(fs::proximate("/a/b/c", "/a/d", ec) == "../b/c");
2✔
2543
    CHECK(!ec);
2✔
2544
    CHECK(fs::proximate("a/b/c", "a") == "b/c");
2✔
2545
    CHECK(fs::proximate("a/b/c", "a", ec) == "b/c");
2✔
2546
    CHECK(!ec);
2✔
2547
    CHECK(fs::proximate("a/b/c", "a/b/c/x/y") == "../..");
2✔
2548
    CHECK(fs::proximate("a/b/c", "a/b/c/x/y", ec) == "../..");
2✔
2549
    CHECK(!ec);
2✔
2550
    CHECK(fs::proximate("a/b/c", "a/b/c") == ".");
2✔
2551
    CHECK(fs::proximate("a/b/c", "a/b/c", ec) == ".");
2✔
2552
    CHECK(!ec);
2✔
2553
    CHECK(fs::proximate("a/b", "c/d") == "../../a/b");
2✔
2554
    CHECK(fs::proximate("a/b", "c/d", ec) == "../../a/b");
2✔
2555
    CHECK(!ec);
2✔
2556
#ifndef GHC_OS_WINDOWS
2557
    if (has_host_root_name_support()) {
2✔
2558
        CHECK(fs::proximate("//host1/a/d", "//host2/a/b/c") == "//host1/a/d");
2✔
2559
        CHECK(fs::proximate("//host1/a/d", "//host2/a/b/c", ec) == "//host1/a/d");
2✔
2560
        CHECK(!ec);
2✔
2561
    }
2562
#endif
2563
}
2✔
2564

2565
TEST_CASE("fs.op.read_symlink - read_symlink", "[filesystem][operations][fs.op.read_symlink]")
2✔
2566
{
2567
    if (is_symlink_creation_supported()) {
2✔
2568
        TemporaryDirectory t(TempOpt::change_path);
4✔
2569
        std::error_code ec;
2✔
2570
        generateFile("foo");
2✔
2571
        fs::create_symlink(t.path() / "foo", "bar");
2✔
2572
        CHECK(fs::read_symlink("bar") == t.path() / "foo");
2✔
2573
        CHECK(fs::read_symlink("bar", ec) == t.path() / "foo");
2✔
2574
        CHECK(!ec);
2✔
2575
        CHECK_THROWS_AS(fs::read_symlink("foobar"), fs::filesystem_error);
6✔
2576
        CHECK(fs::read_symlink("foobar", ec) == fs::path());
2✔
2577
        CHECK(ec);
2✔
2578
    }
2579
}
2✔
2580

2581
TEST_CASE("fs.op.relative - relative", "[filesystem][operations][fs.op.relative]")
2✔
2582
{
2583
    CHECK(fs::relative("/a/d", "/a/b/c") == "../../d");
2✔
2584
    CHECK(fs::relative("/a/b/c", "/a/d") == "../b/c");
2✔
2585
    CHECK(fs::relative("a/b/c", "a") == "b/c");
2✔
2586
    CHECK(fs::relative("a/b/c", "a/b/c/x/y") == "../..");
2✔
2587
    CHECK(fs::relative("a/b/c", "a/b/c") == ".");
2✔
2588
    CHECK(fs::relative("a/b", "c/d") == "../../a/b");
2✔
2589
    std::error_code ec;
2✔
2590
    CHECK(fs::relative(fs::current_path() / "foo", ec) == "foo");
2✔
2591
    CHECK(!ec);
2✔
2592
}
2✔
2593

2594
TEST_CASE("fs.op.remove - remove", "[filesystem][operations][fs.op.remove]")
2✔
2595
{
2596
    TemporaryDirectory t(TempOpt::change_path);
4✔
2597
    std::error_code ec;
2✔
2598
    generateFile("foo");
2✔
2599
    CHECK(fs::remove("foo"));
2✔
2600
    CHECK(!fs::exists("foo"));
2✔
2601
    CHECK(!fs::remove("foo"));
2✔
2602
    generateFile("foo");
2✔
2603
    CHECK(fs::remove("foo", ec));
2✔
2604
    CHECK(!fs::exists("foo"));
2✔
2605
    if (is_symlink_creation_supported()) {
2✔
2606
        generateFile("foo");
2✔
2607
        fs::create_symlink("foo", "bar");
2✔
2608
        CHECK(fs::exists(fs::symlink_status("bar")));
2✔
2609
        CHECK(fs::remove("bar", ec));
2✔
2610
        CHECK(fs::exists("foo"));
2✔
2611
        CHECK(!fs::exists(fs::symlink_status("bar")));
2✔
2612
    }
2613
    CHECK(!fs::remove("bar"));
2✔
2614
    CHECK(!fs::remove("bar", ec));
2✔
2615
    CHECK(!ec);
2✔
2616
}
2✔
2617

2618
TEST_CASE("fs.op.remove_all - remove_all", "[filesystem][operations][fs.op.remove_all]")
2✔
2619
{
2620
    TemporaryDirectory t(TempOpt::change_path);
4✔
2621
    std::error_code ec;
2✔
2622
    generateFile("foo");
2✔
2623
    CHECK(fs::remove_all("foo", ec) == 1);
2✔
2624
    CHECK(!ec);
2✔
2625
    ec.clear();
2✔
2626
    CHECK(fs::directory_iterator(t.path()) == fs::directory_iterator());
2✔
2627
    fs::create_directories("dir1/dir1a");
2✔
2628
    fs::create_directories("dir1/dir1b");
2✔
2629
    generateFile("dir1/dir1a/f1");
2✔
2630
    generateFile("dir1/dir1b/f2");
2✔
2631
    CHECK_NOTHROW(fs::remove_all("dir1/non-existing", ec));
2✔
2632
    CHECK(!ec);
2✔
2633
    CHECK(fs::remove_all("dir1/non-existing", ec) == 0);
2✔
2634
    if (is_symlink_creation_supported()) {
2✔
2635
        fs::create_directory_symlink("dir1", "dir1link");
2✔
2636
        CHECK(fs::remove_all("dir1link") == 1);
2✔
2637
    }
2638
    CHECK(fs::remove_all("dir1") == 5);
2✔
2639
    CHECK(fs::directory_iterator(t.path()) == fs::directory_iterator());
2✔
2640
}
2✔
2641

2642
TEST_CASE("fs.op.rename - rename", "[filesystem][operations][fs.op.rename]")
2✔
2643
{
2644
    TemporaryDirectory t(TempOpt::change_path);
4✔
2645
    std::error_code ec;
2✔
2646
    generateFile("foo", 123);
2✔
2647
    fs::create_directory("dir1");
2✔
2648
    CHECK_NOTHROW(fs::rename("foo", "bar"));
2✔
2649
    CHECK(!fs::exists("foo"));
2✔
2650
    CHECK(fs::exists("bar"));
2✔
2651
    CHECK_NOTHROW(fs::rename("dir1", "dir2"));
2✔
2652
    CHECK(fs::exists("dir2"));
2✔
2653
    generateFile("foo2", 42);
2✔
2654
    CHECK_NOTHROW(fs::rename("bar", "foo2"));
2✔
2655
    CHECK(fs::exists("foo2"));
2✔
2656
    CHECK(fs::file_size("foo2") == 123u);
2✔
2657
    CHECK(!fs::exists("bar"));
2✔
2658
    CHECK_NOTHROW(fs::rename("foo2", "foo", ec));
2✔
2659
    CHECK(!ec);
2✔
2660
    CHECK_THROWS_AS(fs::rename("foobar", "barfoo"), fs::filesystem_error);
8✔
2661
    CHECK_NOTHROW(fs::rename("foobar", "barfoo", ec));
2✔
2662
    CHECK(ec);
2✔
2663
    CHECK(!fs::exists("barfoo"));
2✔
2664
}
2✔
2665

2666
TEST_CASE("fs.op.resize_file - resize_file", "[filesystem][operations][fs.op.resize_file]")
2✔
2667
{
2668
    TemporaryDirectory t(TempOpt::change_path);
4✔
2669
    std::error_code ec;
2✔
2670
    generateFile("foo", 1024);
2✔
2671
    CHECK(fs::file_size("foo") == 1024);
2✔
2672
    CHECK_NOTHROW(fs::resize_file("foo", 2048));
2✔
2673
    CHECK(fs::file_size("foo") == 2048);
2✔
2674
    CHECK_NOTHROW(fs::resize_file("foo", 1000, ec));
2✔
2675
    CHECK(!ec);
2✔
2676
    CHECK(fs::file_size("foo") == 1000);
2✔
2677
    CHECK_THROWS_AS(fs::resize_file("bar", 2048), fs::filesystem_error);
6✔
2678
    CHECK(!fs::exists("bar"));
2✔
2679
    CHECK_NOTHROW(fs::resize_file("bar", 4096, ec));
2✔
2680
    CHECK(ec);
2✔
2681
    CHECK(!fs::exists("bar"));
2✔
2682
}
2✔
2683

2684
TEST_CASE("fs.op.space - space", "[filesystem][operations][fs.op.space]")
2✔
2685
{
2686
    {
2687
        fs::space_info si;
2688
        CHECK_NOTHROW(si = fs::space(fs::current_path()));
2✔
2689
        CHECK(si.capacity > 1024 * 1024);
2✔
2690
        CHECK(si.capacity > si.free);
2✔
2691
        CHECK(si.free >= si.available);
2✔
2692
    }
2693
    {
2694
        std::error_code ec;
2✔
2695
        fs::space_info si;
2696
        CHECK_NOTHROW(si = fs::space(fs::current_path(), ec));
2✔
2697
        CHECK(si.capacity > 1024 * 1024);
2✔
2698
        CHECK(si.capacity > si.free);
2✔
2699
        CHECK(si.free >= si.available);
2✔
2700
        CHECK(!ec);
2✔
2701
    }
2702
#ifndef GHC_OS_WEB // statvfs under emscripten always returns a result, so this tests would fail
2703
    {
2704
        std::error_code ec;
2✔
2705
        fs::space_info si;
2706
        CHECK_NOTHROW(si = fs::space("foobar42", ec));
2✔
2707
        CHECK(si.capacity == static_cast<uintmax_t>(-1));
2✔
2708
        CHECK(si.free == static_cast<uintmax_t>(-1));
2✔
2709
        CHECK(si.available == static_cast<uintmax_t>(-1));
2✔
2710
        CHECK(ec);
2✔
2711
    }
2712
    CHECK_THROWS_AS(fs::space("foobar42"), fs::filesystem_error);
6✔
2713
#endif
2714
}
2✔
2715

2716
TEST_CASE("fs.op.status - status", "[filesystem][operations][fs.op.status]")
2✔
2717
{
2718
    TemporaryDirectory t(TempOpt::change_path);
4✔
2719
    std::error_code ec;
2✔
2720
    fs::file_status fs;
4✔
2721
    CHECK_NOTHROW(fs = fs::status("foo"));
2✔
2722
    CHECK(fs.type() == fs::file_type::not_found);
2✔
2723
    CHECK(fs.permissions() == fs::perms::unknown);
2✔
2724
    CHECK_NOTHROW(fs = fs::status("bar", ec));
2✔
2725
    CHECK(fs.type() == fs::file_type::not_found);
2✔
2726
    CHECK(fs.permissions() == fs::perms::unknown);
2✔
2727
    CHECK(ec);
2✔
2728
    ec.clear();
2✔
2729
    fs = fs::status(t.path());
2✔
2730
    CHECK(fs.type() == fs::file_type::directory);
2✔
2731
    CHECK((fs.permissions() & (fs::perms::owner_read | fs::perms::owner_write)) == (fs::perms::owner_read | fs::perms::owner_write));
2✔
2732
    generateFile("foobar");
2✔
2733
    fs = fs::status(t.path() / "foobar");
2✔
2734
    CHECK(fs.type() == fs::file_type::regular);
2✔
2735
    CHECK((fs.permissions() & (fs::perms::owner_read | fs::perms::owner_write)) == (fs::perms::owner_read | fs::perms::owner_write));
2✔
2736
    if (is_symlink_creation_supported()) {
2✔
2737
        fs::create_symlink(t.path() / "foobar", t.path() / "barfoo");
2✔
2738
        fs = fs::status(t.path() / "barfoo");
2✔
2739
        CHECK(fs.type() == fs::file_type::regular);
2✔
2740
        CHECK((fs.permissions() & (fs::perms::owner_read | fs::perms::owner_write)) == (fs::perms::owner_read | fs::perms::owner_write));
2✔
2741
    }
2742
}
2✔
2743

2744
TEST_CASE("fs.op.status_known - status_known", "[filesystem][operations][fs.op.status_known]")
2✔
2745
{
2746
    CHECK(!fs::status_known(fs::file_status()));
2✔
2747
    CHECK(fs::status_known(fs::file_status(fs::file_type::not_found)));
2✔
2748
    CHECK(fs::status_known(fs::file_status(fs::file_type::regular)));
2✔
2749
    CHECK(fs::status_known(fs::file_status(fs::file_type::directory)));
2✔
2750
    CHECK(fs::status_known(fs::file_status(fs::file_type::symlink)));
2✔
2751
    CHECK(fs::status_known(fs::file_status(fs::file_type::character)));
2✔
2752
    CHECK(fs::status_known(fs::file_status(fs::file_type::fifo)));
2✔
2753
    CHECK(fs::status_known(fs::file_status(fs::file_type::socket)));
2✔
2754
    CHECK(fs::status_known(fs::file_status(fs::file_type::unknown)));
2✔
2755
}
2✔
2756

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

2784
TEST_CASE("fs.op.temp_dir_path - temporary_directory_path", "[filesystem][operations][fs.op.temp_dir_path]")
2✔
2785
{
2786
    std::error_code ec;
2✔
2787
    CHECK_NOTHROW(fs::exists(fs::temp_directory_path()));
2✔
2788
    CHECK_NOTHROW(fs::exists(fs::temp_directory_path(ec)));
2✔
2789
    CHECK(!fs::temp_directory_path().empty());
2✔
2790
    CHECK(!ec);
2✔
2791
}
2✔
2792

2793
TEST_CASE("fs.op.weakly_canonical - weakly_canonical", "[filesystem][operations][fs.op.weakly_canonical]")
2✔
2794
{
2795
    INFO("This might fail on std::implementations that return fs::current_path() for fs::canonical(\"\")");
4✔
2796
    CHECK(fs::weakly_canonical("") == ".");
2✔
2797
    if(fs::weakly_canonical("") == ".") {
2✔
2798
        CHECK(fs::weakly_canonical("foo/bar") == "foo/bar");
2✔
2799
        CHECK(fs::weakly_canonical("foo/./bar") == "foo/bar");
2✔
2800
        CHECK(fs::weakly_canonical("foo/../bar") == "bar");
2✔
2801
    }
2802
    else {
2803
        CHECK(fs::weakly_canonical("foo/bar") == fs::current_path() / "foo/bar");
×
2804
        CHECK(fs::weakly_canonical("foo/./bar") == fs::current_path() / "foo/bar");
×
2805
        CHECK(fs::weakly_canonical("foo/../bar") == fs::current_path() / "bar");
×
2806
    }
2807

2808
    {
2809
        TemporaryDirectory t(TempOpt::change_path);
4✔
2810
        auto dir = t.path() / "d0";
4✔
2811
        fs::create_directories(dir / "d1");
2✔
2812
        generateFile(dir / "f0");
2✔
2813
        fs::path rel(dir.filename());
4✔
2814
        CHECK(fs::weakly_canonical(dir) == dir);
2✔
2815
        CHECK(fs::weakly_canonical(rel) == dir);
2✔
2816
        CHECK(fs::weakly_canonical(dir / "f0") == dir / "f0");
2✔
2817
        CHECK(fs::weakly_canonical(dir / "f0/") == dir / "f0/");
2✔
2818
        CHECK(fs::weakly_canonical(dir / "f1") == dir / "f1");
2✔
2819
        CHECK(fs::weakly_canonical(rel / "f0") == dir / "f0");
2✔
2820
        CHECK(fs::weakly_canonical(rel / "f0/") == dir / "f0/");
2✔
2821
        CHECK(fs::weakly_canonical(rel / "f1") == dir / "f1");
2✔
2822
        CHECK(fs::weakly_canonical(rel / "./f0") == dir / "f0");
2✔
2823
        CHECK(fs::weakly_canonical(rel / "./f1") == dir / "f1");
2✔
2824
        CHECK(fs::weakly_canonical(rel / "d1/../f0") == dir / "f0");
2✔
2825
        CHECK(fs::weakly_canonical(rel / "d1/../f1") == dir / "f1");
2✔
2826
        CHECK(fs::weakly_canonical(rel / "d1/../f1/../f2") == dir / "f2");
2✔
2827
    }
2828
}
2✔
2829

2830
TEST_CASE("std::string_view support", "[filesystem][fs.string_view]")
2✔
2831
{
2832
#if defined(GHC_HAS_STD_STRING_VIEW) || defined(GHC_HAS_STD_EXPERIMENTAL_STRING_VIEW)
2833

2834
#if defined(GHC_HAS_STD_STRING_VIEW)
2835
    using namespace std::literals;
2836
    using string_view = std::string_view;
2837
    using wstring_view = std::wstring_view;
2838
#elif defined(GHC_HAS_STD_EXPERIMENTAL_STRING_VIEW)
2839
    using string_view = std::experimental::string_view;
2840
    using wstring_view = std::experimental::wstring_view;
2841
#endif
2842

2843
    {
2844
        std::string p("foo/bar");
4✔
2845
        string_view sv(p);
2✔
2846
        CHECK(fs::path(sv, fs::path::format::generic_format).generic_string() == "foo/bar");
2✔
2847
        fs::path p2("fo");
4✔
2848
        p2 += string_view("o");
2✔
2849
        CHECK(p2 == "foo");
2✔
2850
        CHECK(p2.compare(string_view("foo")) == 0);
2✔
2851
    }
2852
    {
2853
        auto p = fs::path{"XYZ"};
4✔
2854
        p /= string_view("Appendix");
2✔
2855
        CHECK(p == "XYZ/Appendix");
2✔
2856
    }
2857
    {
2858
        std::wstring p(L"foo/bar");
4✔
2859
        wstring_view sv(p);
2✔
2860
        CHECK(fs::path(sv, fs::path::format::generic_format).generic_string() == "foo/bar");
2✔
2861
        fs::path p2(L"fo");
4✔
2862
        p2 += wstring_view(L"o");
2✔
2863
        CHECK(p2 == "foo");
2✔
2864
        CHECK(p2.compare(wstring_view(L"foo")) == 0);
2✔
2865
    }
2866

2867
#else
2868
    WARN("std::string_view specific tests are empty without std::string_view.");
2869
#endif
2870
}
2✔
2871

2872
TEST_CASE("Windows: Long filename support", "[filesystem][path][fs.path.win.long]")
2✔
2873
{
2874
#ifdef GHC_OS_WINDOWS
2875
    TemporaryDirectory t(TempOpt::change_path);
2876
    char c = 'A';
2877
    fs::path dir{"\\\\?\\"};
2878
    dir += fs::current_path().u8string();
2879
    for (; c <= 'Z'; ++c) {
2880
        std::string part = std::string(16, c);
2881
        dir /= part;
2882
        CHECK_NOTHROW(fs::create_directory(dir));
2883
        CHECK(fs::exists(dir));
2884
        generateFile(dir / "f0");
2885
        REQUIRE(fs::exists(dir / "f0"));
2886
    }
2887
    CHECK(c > 'Z');
2888
    fs::remove_all(fs::current_path() / std::string(16, 'A'));
2889
    CHECK(!fs::exists(fs::current_path() / std::string(16, 'A')));
2890
    CHECK_NOTHROW(fs::create_directories(dir));
2891
    CHECK(fs::exists(dir));
2892
    generateFile(dir / "f0");
2893
    CHECK(fs::exists(dir / "f0"));
2894
#else
2895
    WARN("Windows specific tests are empty on non-Windows systems.");
2✔
2896
#endif
2897
}
2✔
2898

2899
TEST_CASE("Windows: path namespace handling", "[filesystem][path][fs.path.win.namespaces]")
2✔
2900
{
2901
#ifdef GHC_OS_WINDOWS
2902
    {
2903
        std::error_code ec;
2904
        fs::path p(R"(\\localhost\c$\Windows)");
2905
        auto symstat = fs::symlink_status(p, ec);
2906
        CHECK(!ec);
2907
        auto p2 = fs::canonical(p, ec);
2908
        CHECK(!ec);
2909
        CHECK(p2 == p);
2910
    }
2911
    
2912
    struct TestInfo
2913
    {
2914
        std::string _path;
2915
        std::string _string;
2916
        std::string _rootName;
2917
        std::string _rootPath;
2918
        std::string _iterateResult;
2919
    };
2920
    std::vector<TestInfo> variants = {
2921
        {R"(C:\Windows\notepad.exe)", R"(C:\Windows\notepad.exe)", "C:", "C:\\", "C:,/,Windows,notepad.exe"},
2922
#ifdef USE_STD_FS
2923
        {R"(\\?\C:\Windows\notepad.exe)", R"(\\?\C:\Windows\notepad.exe)", "\\\\?", "\\\\?\\", "//?,/,C:,Windows,notepad.exe"},
2924
        {R"(\??\C:\Windows\notepad.exe)", R"(\??\C:\Windows\notepad.exe)", "\\??", "\\??\\", "/??,/,C:,Windows,notepad.exe"},
2925
#else
2926
        {R"(\\?\C:\Windows\notepad.exe)", R"(\\?\C:\Windows\notepad.exe)", "C:", "C:\\", "//?/,C:,/,Windows,notepad.exe"},
2927
        {R"(\??\C:\Windows\notepad.exe)", R"(\??\C:\Windows\notepad.exe)", "C:", "C:\\", "/?\?/,C:,/,Windows,notepad.exe"},
2928
#endif
2929
        {R"(\\.\C:\Windows\notepad.exe)", R"(\\.\C:\Windows\notepad.exe)", "\\\\.", "\\\\.\\", "//.,/,C:,Windows,notepad.exe"},
2930
        {R"(\\?\HarddiskVolume1\Windows\notepad.exe)", R"(\\?\HarddiskVolume1\Windows\notepad.exe)", "\\\\?", "\\\\?\\", "//?,/,HarddiskVolume1,Windows,notepad.exe"},
2931
        {R"(\\?\Harddisk0Partition1\Windows\notepad.exe)", R"(\\?\Harddisk0Partition1\Windows\notepad.exe)", "\\\\?", "\\\\?\\", "//?,/,Harddisk0Partition1,Windows,notepad.exe"},
2932
        {R"(\\.\GLOBALROOT\Device\HarddiskVolume1\Windows\notepad.exe)", R"(\\.\GLOBALROOT\Device\HarddiskVolume1\Windows\notepad.exe)", "\\\\.", "\\\\.\\", "//.,/,GLOBALROOT,Device,HarddiskVolume1,Windows,notepad.exe"},
2933
        {R"(\\?\GLOBALROOT\Device\Harddisk0\Partition1\Windows\notepad.exe)", R"(\\?\GLOBALROOT\Device\Harddisk0\Partition1\Windows\notepad.exe)", "\\\\?", "\\\\?\\", "//?,/,GLOBALROOT,Device,Harddisk0,Partition1,Windows,notepad.exe"},
2934
        {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"},
2935
        {R"(\\LOCALHOST\C$\Windows\notepad.exe)", R"(\\LOCALHOST\C$\Windows\notepad.exe)", "\\\\LOCALHOST", "\\\\LOCALHOST\\", "//LOCALHOST,/,C$,Windows,notepad.exe"},
2936
        {R"(\\?\UNC\C$\Windows\notepad.exe)", R"(\\?\UNC\C$\Windows\notepad.exe)", "\\\\?", "\\\\?\\", "//?,/,UNC,C$,Windows,notepad.exe"},
2937
        {R"(\\?\GLOBALROOT\Device\Mup\C$\Windows\notepad.exe)", R"(\\?\GLOBALROOT\Device\Mup\C$\Windows\notepad.exe)", "\\\\?", "\\\\?\\", "//?,/,GLOBALROOT,Device,Mup,C$,Windows,notepad.exe"},
2938
    };
2939

2940
    for (auto ti : variants) {
2941
        INFO("Used path: " + ti._path);
2942
        auto p = fs::path(ti._path);
2943
        CHECK(p.string() == ti._string);
2944
        CHECK(p.is_absolute());
2945
        CHECK(p.root_name().string() == ti._rootName);
2946
        CHECK(p.root_path().string() == ti._rootPath);
2947
        CHECK(iterateResult(p) == ti._iterateResult);
2948
    }
2949
#else
2950
    WARN("Windows specific tests are empty on non-Windows systems.");
2✔
2951
#endif
2952
}
2✔
2953

2954
TEST_CASE("Windows: Mapped folders handling ", "[filesystem][fs.win][fs.win.mapped]")
2✔
2955
{
2956
#ifdef GHC_OS_WINDOWS
2957
    // this test expects a mapped volume on C:\\fs-test as is the case on the development test system
2958
    // does nothing on other systems
2959
    if (fs::exists("C:\\fs-test")) {
2960
        CHECK(fs::canonical("C:\\fs-test\\Test.txt").string() == "C:\\fs-test\\Test.txt");
2961
    }
2962
#else
2963
    WARN("Windows specific tests are empty on non-Windows systems.");
2✔
2964
#endif
2965
}
2✔
2966

2967
TEST_CASE("Windows: Deletion of Read-only Files", "[filesystem][fs.win][fs.win.remove]")
2✔
2968
{
2969
#ifdef GHC_OS_WINDOWS
2970
    TemporaryDirectory t(TempOpt::change_path);
2971
    std::error_code ec;
2972
    generateFile("foo", 512);
2973
    auto allWrite = fs::perms::owner_write | fs::perms::group_write | fs::perms::others_write;
2974
    CHECK_NOTHROW(fs::permissions("foo", allWrite, fs::perm_options::remove));
2975
    CHECK_NOTHROW(fs::remove("foo"));
2976
    CHECK(!fs::exists("foo"));
2977
#else
2978
    WARN("Windows specific tests are empty on non-Windows systems.");
2✔
2979
#endif
2980
}
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

© 2025 Coveralls, Inc