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

gulrak / filesystem / 4205649244

pending completion
4205649244

push

github

GitHub
Merge pull request #154 from sthibaul/master

3409 of 3578 relevant lines covered (95.28%)

441.88 hits per line

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

97.94
/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
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
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)
150✔
206
{
207
    fs::ofstream outfile(pathname);
300✔
208
    if (withSize < 0) {
150✔
209
        outfile << "Hello world!" << std::endl;
116✔
210
    }
211
    else {
212
        outfile << std::string(size_t(withSize), '*');
34✔
213
    }
214
}
150✔
215

216
#ifdef GHC_OS_WINDOWS
217
inline 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
bool operator==(TestAllocator<T> const&, TestAllocator<U> const&) noexcept
307
{
308
    return true;
309
}
310

311
template <class T, class U>
312
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
#endif
794

795
    // stem()
796
    CHECK(fs::path("/foo/bar.txt").stem() == "bar");
2✔
797
    {
798
        fs::path p = "foo.bar.baz.tar";
4✔
799
        CHECK(p.extension() == ".tar");
2✔
800
        p = p.stem();
2✔
801
        CHECK(p.extension() == ".baz");
2✔
802
        p = p.stem();
2✔
803
        CHECK(p.extension() == ".bar");
2✔
804
        p = p.stem();
2✔
805
        CHECK(p == "foo");
2✔
806
    }
807
    CHECK(fs::path("/foo/.profile").stem() == ".profile");
2✔
808
    CHECK(fs::path(".bar").stem() == ".bar");
2✔
809
    CHECK(fs::path("..bar").stem() == ".");
2✔
810

811
    // extension()
812
    CHECK(fs::path("/foo/bar.txt").extension() == ".txt");
2✔
813
    CHECK(fs::path("/foo/bar").extension() == "");
2✔
814
    CHECK(fs::path("/foo/.profile").extension() == "");
2✔
815
    CHECK(fs::path(".bar").extension() == "");
2✔
816
    CHECK(fs::path("..bar").extension() == ".bar");
2✔
817

818
    if (has_host_root_name_support()) {
2✔
819
        // //host-based root-names
820
        CHECK(fs::path("//host").root_name() == "//host");
2✔
821
        CHECK(fs::path("//host/foo").root_name() == "//host");
2✔
822
        CHECK(fs::path("//host").root_directory() == "");
2✔
823
        CHECK(fs::path("//host/foo").root_directory() == "/");
2✔
824
        CHECK(fs::path("//host").root_path() == "//host");
2✔
825
        CHECK(fs::path("//host/foo").root_path() == "//host/");
2✔
826
        CHECK(fs::path("//host").relative_path() == "");
2✔
827
        CHECK(fs::path("//host/foo").relative_path() == "foo");
2✔
828
        CHECK(fs::path("//host").parent_path() == "//host");
2✔
829
        CHECK(fs::path("//host/foo").parent_path() == "//host/");
2✔
830
        CHECK(fs::path("//host").filename() == "");
2✔
831
        CHECK(fs::path("//host/foo").filename() == "foo");
2✔
832
    }
833
}
2✔
834

835
TEST_CASE("fs.path.query - path query", "[fielsystem][path][fs.path.query]")
2✔
836
{
837
    // empty
838
    CHECK(fs::path("").empty());
2✔
839
    CHECK(!fs::path("foo").empty());
2✔
840

841
    // has_root_path()
842
    CHECK(!fs::path("foo").has_root_path());
2✔
843
    CHECK(!fs::path("foo/bar").has_root_path());
2✔
844
    CHECK(fs::path("/foo").has_root_path());
2✔
845
#ifdef GHC_OS_WINDOWS
846
    CHECK(fs::path("C:foo").has_root_path());
847
    CHECK(fs::path("C:/foo").has_root_path());
848
#endif
849

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

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

868
    // has_relative_path()
869
    CHECK(!fs::path("").has_relative_path());
2✔
870
    CHECK(!fs::path("/").has_relative_path());
2✔
871
    CHECK(fs::path("/foo").has_relative_path());
2✔
872

873
    // has_parent_path()
874
    CHECK(!fs::path("").has_parent_path());
2✔
875
    CHECK(!fs::path(".").has_parent_path());
2✔
876
    CHECK(!fs::path("..").has_parent_path());  // unintuitive but as defined in the standard
2✔
877
    CHECK(!fs::path("foo").has_parent_path());
2✔
878
    CHECK(fs::path("/").has_parent_path());
2✔
879
    CHECK(fs::path("/foo").has_parent_path());
2✔
880
    CHECK(fs::path("foo/").has_parent_path());
2✔
881
    CHECK(fs::path("/foo/").has_parent_path());
2✔
882

883
    // has_filename()
884
    CHECK(fs::path("foo").has_filename());
2✔
885
    CHECK(fs::path("foo/bar").has_filename());
2✔
886
    CHECK(!fs::path("/foo/bar/").has_filename());
2✔
887

888
    // has_stem()
889
    CHECK(fs::path("foo").has_stem());
2✔
890
    CHECK(fs::path("foo.bar").has_stem());
2✔
891
    CHECK(fs::path(".profile").has_stem());
2✔
892
    CHECK(!fs::path("/foo/").has_stem());
2✔
893

894
    // has_extension()
895
    CHECK(!fs::path("foo").has_extension());
2✔
896
    CHECK(fs::path("foo.bar").has_extension());
2✔
897
    CHECK(!fs::path(".profile").has_extension());
2✔
898

899
    // is_absolute()
900
    CHECK(!fs::path("foo/bar").is_absolute());
2✔
901
#ifdef GHC_OS_WINDOWS
902
    CHECK(!fs::path("/foo").is_absolute());
903
    CHECK(!fs::path("c:foo").is_absolute());
904
    CHECK(fs::path("c:/foo").is_absolute());
905
#else
906
    CHECK(fs::path("/foo").is_absolute());
2✔
907
#endif
908

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

919
    if (has_host_root_name_support()) {
2✔
920
        CHECK(fs::path("//host").has_root_name());
2✔
921
        CHECK(fs::path("//host/foo").has_root_name());
2✔
922
        CHECK(fs::path("//host").has_root_path());
2✔
923
        CHECK(fs::path("//host/foo").has_root_path());
2✔
924
        CHECK(!fs::path("//host").has_root_directory());
2✔
925
        CHECK(fs::path("//host/foo").has_root_directory());
2✔
926
        CHECK(!fs::path("//host").has_relative_path());
2✔
927
        CHECK(fs::path("//host/foo").has_relative_path());
2✔
928
        CHECK(fs::path("//host/foo").is_absolute());
2✔
929
        CHECK(!fs::path("//host/foo").is_relative());
2✔
930
    }
931
}
2✔
932

933
TEST_CASE("fs.path.gen - path generation", "[filesystem][path][fs.path.gen]")
2✔
934
{
935
    // lexically_normal()
936
    CHECK(fs::path("foo/./bar/..").lexically_normal() == "foo/");
2✔
937
    CHECK(fs::path("foo/.///bar/../").lexically_normal() == "foo/");
2✔
938
    CHECK(fs::path("/foo/../..").lexically_normal() == "/");
2✔
939
    CHECK(fs::path("foo/..").lexically_normal() == ".");
2✔
940
    CHECK(fs::path("ab/cd/ef/../../qw").lexically_normal() == "ab/qw");
2✔
941
    CHECK(fs::path("a/b/../../../c").lexically_normal() == "../c");
2✔
942
    CHECK(fs::path("../").lexically_normal() == "..");
2✔
943
#ifdef GHC_OS_WINDOWS
944
    CHECK(fs::path("\\/\\///\\/").lexically_normal() == "/");
945
    CHECK(fs::path("a/b/..\\//..///\\/../c\\\\/").lexically_normal() == "../c/");
946
    CHECK(fs::path("..a/b/..\\//..///\\/../c\\\\/").lexically_normal() == "../c/");
947
    CHECK(fs::path("..\\").lexically_normal() == "..");
948
#endif
949

950
    // lexically_relative()
951
    CHECK(fs::path("/a/d").lexically_relative("/a/b/c") == "../../d");
2✔
952
    CHECK(fs::path("/a/b/c").lexically_relative("/a/d") == "../b/c");
2✔
953
    CHECK(fs::path("a/b/c").lexically_relative("a") == "b/c");
2✔
954
    CHECK(fs::path("a/b/c").lexically_relative("a/b/c/x/y") == "../..");
2✔
955
    CHECK(fs::path("a/b/c").lexically_relative("a/b/c") == ".");
2✔
956
    CHECK(fs::path("a/b").lexically_relative("c/d") == "../../a/b");
2✔
957
    CHECK(fs::path("a/b").lexically_relative("a/") == "b");
2✔
958
    if (has_host_root_name_support()) {
2✔
959
        CHECK(fs::path("//host1/foo").lexically_relative("//host2.bar") == "");
2✔
960
    }
961
#ifdef GHC_OS_WINDOWS
962
    CHECK(fs::path("c:/foo").lexically_relative("/bar") == "");
963
    CHECK(fs::path("c:foo").lexically_relative("c:/bar") == "");
964
    CHECK(fs::path("foo").lexically_relative("/bar") == "");
965
    CHECK(fs::path("c:/foo/bar.txt").lexically_relative("c:/foo/") == "bar.txt");
966
    CHECK(fs::path("c:/foo/bar.txt").lexically_relative("C:/foo/") == "bar.txt");
967
#else
968
    CHECK(fs::path("/foo").lexically_relative("bar") == "");
2✔
969
    CHECK(fs::path("foo").lexically_relative("/bar") == "");
2✔
970
#endif
971

972
    // lexically_proximate()
973
    CHECK(fs::path("/a/d").lexically_proximate("/a/b/c") == "../../d");
2✔
974
    if (has_host_root_name_support()) {
2✔
975
        CHECK(fs::path("//host1/a/d").lexically_proximate("//host2/a/b/c") == "//host1/a/d");
2✔
976
    }
977
    CHECK(fs::path("a/d").lexically_proximate("/a/b/c") == "a/d");
2✔
978
#ifdef GHC_OS_WINDOWS
979
    CHECK(fs::path("c:/a/d").lexically_proximate("c:/a/b/c") == "../../d");
980
    CHECK(fs::path("c:/a/d").lexically_proximate("d:/a/b/c") == "c:/a/d");
981
    CHECK(fs::path("c:/foo").lexically_proximate("/bar") == "c:/foo");
982
    CHECK(fs::path("c:foo").lexically_proximate("c:/bar") == "c:foo");
983
    CHECK(fs::path("foo").lexically_proximate("/bar") == "foo");
984
#else
985
    CHECK(fs::path("/foo").lexically_proximate("bar") == "/foo");
2✔
986
    CHECK(fs::path("foo").lexically_proximate("/bar") == "foo");
2✔
987
#endif
988
}
2✔
989

990
static std::string iterateResult(const fs::path& path)
30✔
991
{
992
    std::ostringstream result;
60✔
993
    for (fs::path::const_iterator i = path.begin(); i != path.end(); ++i) {
94✔
994
        if (i != path.begin()) {
64✔
995
            result << ",";
36✔
996
        }
997
        result << i->generic_string();
64✔
998
    }
999
    return result.str();
60✔
1000
}
1001

1002
static std::string reverseIterateResult(const fs::path& path)
30✔
1003
{
1004
    std::ostringstream result;
60✔
1005
    fs::path::const_iterator iter = path.end();
60✔
1006
    bool first = true;
30✔
1007
    if (iter != path.begin()) {
30✔
1008
        do {
36✔
1009
            --iter;
64✔
1010
            if (!first) {
64✔
1011
                result << ",";
36✔
1012
            }
1013
            first = false;
64✔
1014
            result << iter->generic_string();
64✔
1015
        } while (iter != path.begin());
64✔
1016
    }
1017
    return result.str();
60✔
1018
}
1019

1020
TEST_CASE("fs.path.itr - path iterators", "[filesystem][path][fs.path.itr]")
2✔
1021
{
1022
    CHECK(iterateResult(fs::path()).empty());
2✔
1023
    CHECK("." == iterateResult(fs::path(".")));
2✔
1024
    CHECK(".." == iterateResult(fs::path("..")));
2✔
1025
    CHECK("foo" == iterateResult(fs::path("foo")));
2✔
1026
    CHECK("/" == iterateResult(fs::path("/")));
2✔
1027
    CHECK("/,foo" == iterateResult(fs::path("/foo")));
2✔
1028
    CHECK("foo," == iterateResult(fs::path("foo/")));
2✔
1029
    CHECK("/,foo," == iterateResult(fs::path("/foo/")));
2✔
1030
    CHECK("foo,bar" == iterateResult(fs::path("foo/bar")));
2✔
1031
    CHECK("/,foo,bar" == iterateResult(fs::path("/foo/bar")));
2✔
1032
#ifndef USE_STD_FS
1033
    // ghc::filesystem enforces redundant slashes to be reduced to one
1034
    CHECK("/,foo,bar" == iterateResult(fs::path("///foo/bar")));
2✔
1035
#else
1036
    // typically std::filesystem keeps them
1037
    CHECK("///,foo,bar" == iterateResult(fs::path("///foo/bar")));
1038
#endif
1039
    CHECK("/,foo,bar," == iterateResult(fs::path("/foo/bar///")));
2✔
1040
    CHECK("foo,.,bar,..," == iterateResult(fs::path("foo/.///bar/../")));
2✔
1041
#ifdef GHC_OS_WINDOWS
1042
    CHECK("C:,/,foo" == iterateResult(fs::path("C:/foo")));
1043
#endif
1044

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

1082
    if (has_host_root_name_support()) {
2✔
1083
        CHECK("foo" == *(--fs::path("//host/foo").end()));
2✔
1084
        auto p = fs::path("//host/foo");
4✔
1085
        auto pi = p.end();
4✔
1086
        pi--;
2✔
1087
        CHECK("foo" == *pi);
2✔
1088
        CHECK("//host" == iterateResult(fs::path("//host")));
2✔
1089
        CHECK("//host,/,foo" == iterateResult(fs::path("//host/foo")));
2✔
1090
        CHECK("//host" == reverseIterateResult(fs::path("//host")));
2✔
1091
        CHECK("foo,/,//host" == reverseIterateResult(fs::path("//host/foo")));
2✔
1092
        {
1093
            fs::path p1 = "//host/foo/bar/test.txt";
4✔
1094
            fs::path p2;
4✔
1095
            for (auto pe : p1) {
12✔
1096
                p2 /= pe;
10✔
1097
            }
1098
            CHECK(p1 == p2);
2✔
1099
        }
1100
    }
1101
}
2✔
1102

1103
TEST_CASE("fs.path.nonmember - path non-member functions", "[filesystem][path][fs.path.nonmember]")
2✔
1104
{
1105
    fs::path p1("foo/bar");
4✔
1106
    fs::path p2("some/other");
4✔
1107
    fs::swap(p1, p2);
2✔
1108
    CHECK(p1 == "some/other");
2✔
1109
    CHECK(p2 == "foo/bar");
2✔
1110
    CHECK(hash_value(p1));
2✔
1111
    CHECK(p2 < p1);
2✔
1112
    CHECK(p2 <= p1);
2✔
1113
    CHECK(p1 <= p1);
2✔
1114
    CHECK(!(p1 < p2));
2✔
1115
    CHECK(!(p1 <= p2));
2✔
1116
    CHECK(p1 > p2);
2✔
1117
    CHECK(p1 >= p2);
2✔
1118
    CHECK(p1 >= p1);
2✔
1119
    CHECK(!(p2 > p1));
2✔
1120
    CHECK(!(p2 >= p1));
2✔
1121
    CHECK(p1 != p2);
2✔
1122
    CHECK(p1 / p2 == "some/other/foo/bar");
2✔
1123
}
2✔
1124

1125
TEST_CASE("fs.path.io - path inserter and extractor", "[filesystem][path][fs.path.io]")
2✔
1126
{
1127
    {
1128
        std::ostringstream os;
4✔
1129
        os << fs::path("/root/foo bar");
2✔
1130
#ifdef GHC_OS_WINDOWS
1131
        CHECK(os.str() == "\"\\\\root\\\\foo bar\"");
1132
#else
1133
        CHECK(os.str() == "\"/root/foo bar\"");
2✔
1134
#endif
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
    {
1147
        std::istringstream is("\"/root/foo bar\"");
6✔
1148
        fs::path p;
4✔
1149
        is >> p;
2✔
1150
        CHECK(p == fs::path("/root/foo bar"));
2✔
1151
        CHECK((is.flags() & std::ios_base::skipws) == std::ios_base::skipws);
2✔
1152
    }
1153
    {
1154
        std::istringstream is("\"/root/foo bar\"");
6✔
1155
        is >> std::noskipws;
2✔
1156
        fs::path p;
4✔
1157
        is >> p;
2✔
1158
        CHECK(p == fs::path("/root/foo bar"));
2✔
1159
        CHECK((is.flags() & std::ios_base::skipws) != std::ios_base::skipws);
2✔
1160
    }
1161
    {
1162
        std::istringstream is("\"/root/foo\\\"bar\"");
6✔
1163
        fs::path p;
4✔
1164
        is >> p;
2✔
1165
        CHECK(p == fs::path("/root/foo\"bar"));
2✔
1166
    }
1167
    {
1168
        std::istringstream is("/root/foo");
6✔
1169
        fs::path p;
4✔
1170
        is >> p;
2✔
1171
        CHECK(p == fs::path("/root/foo"));
2✔
1172
    }
1173
}
2✔
1174

1175
TEST_CASE("fs.path.factory - path factory functions", "[filesystem][path][fs.path.factory]")
2✔
1176
{
1177
    CHECK(fs::u8path("foo/bar") == fs::path("foo/bar"));
2✔
1178
    CHECK(fs::u8path("foo/bar") == fs::path("foo/bar"));
2✔
1179
    std::string str("/foo/bar/test.txt");
4✔
1180
    CHECK(fs::u8path(str.begin(), str.end()) == str);
2✔
1181
}
2✔
1182

1183
TEST_CASE("fs.class.filesystem_error - class filesystem_error", "[filesystem][filesystem_error][fs.class.filesystem_error]")
2✔
1184
{
1185
    std::error_code ec(1, std::system_category());
2✔
1186
    fs::filesystem_error fse("None", std::error_code());
6✔
1187
    fse = fs::filesystem_error("Some error", ec);
2✔
1188
    CHECK(fse.code().value() == 1);
2✔
1189
    CHECK(!std::string(fse.what()).empty());
2✔
1190
    CHECK(fse.path1().empty());
2✔
1191
    CHECK(fse.path2().empty());
2✔
1192
    fse = fs::filesystem_error("Some error", fs::path("foo/bar"), ec);
2✔
1193
    CHECK(!std::string(fse.what()).empty());
2✔
1194
    CHECK(fse.path1() == "foo/bar");
2✔
1195
    CHECK(fse.path2().empty());
2✔
1196
    fse = fs::filesystem_error("Some error", fs::path("foo/bar"), fs::path("some/other"), ec);
2✔
1197
    CHECK(!std::string(fse.what()).empty());
2✔
1198
    CHECK(fse.path1() == "foo/bar");
2✔
1199
    CHECK(fse.path2() == "some/other");
2✔
1200
}
2✔
1201

1202
constexpr fs::perms constExprOwnerAll()
1203
{
1204
    return fs::perms::owner_read | fs::perms::owner_write | fs::perms::owner_exec;
1205
}
1206

1207
TEST_CASE("fs.enum - enum class perms", "[filesystem][enum][fs.enum]")
2✔
1208
{
1209
    static_assert(constExprOwnerAll() == fs::perms::owner_all, "constexpr didn't result in owner_all");
1210
    CHECK((fs::perms::owner_read | fs::perms::owner_write | fs::perms::owner_exec) == fs::perms::owner_all);
2✔
1211
    CHECK((fs::perms::group_read | fs::perms::group_write | fs::perms::group_exec) == fs::perms::group_all);
2✔
1212
    CHECK((fs::perms::others_read | fs::perms::others_write | fs::perms::others_exec) == fs::perms::others_all);
2✔
1213
    CHECK((fs::perms::owner_all | fs::perms::group_all | fs::perms::others_all) == fs::perms::all);
2✔
1214
    CHECK((fs::perms::all | fs::perms::set_uid | fs::perms::set_gid | fs::perms::sticky_bit) == fs::perms::mask);
2✔
1215
}
2✔
1216

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

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

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

1443
TEST_CASE("fs.class.rec.dir.itr - class recursive_directory_iterator", "[filesystem][recursive_directory_iterator][fs.class.rec.dir.itr]")
2✔
1444
{
1445
    {
1446
        auto iter = fs::recursive_directory_iterator(".");
4✔
1447
        iter.pop();
2✔
1448
        CHECK(iter == fs::recursive_directory_iterator());
2✔
1449
    }
1450
    {
1451
        TemporaryDirectory t;
4✔
1452
        CHECK(fs::recursive_directory_iterator(t.path()) == fs::recursive_directory_iterator());
2✔
1453
        generateFile(t.path() / "test", 1234);
2✔
1454
        REQUIRE(fs::recursive_directory_iterator(t.path()) != fs::recursive_directory_iterator());
2✔
1455
        auto iter = fs::recursive_directory_iterator(t.path());
4✔
1456
        CHECK(iter->path().filename() == "test");
2✔
1457
        CHECK(iter->path() == t.path() / "test");
2✔
1458
        CHECK(!iter->is_symlink());
2✔
1459
        CHECK(iter->is_regular_file());
2✔
1460
        CHECK(!iter->is_directory());
2✔
1461
        CHECK(iter->file_size() == 1234);
2✔
1462
        CHECK(++iter == fs::recursive_directory_iterator());
2✔
1463
    }
1464

1465
    {
1466
        TemporaryDirectory t;
4✔
1467
        fs::path td = t.path() / "testdir";
4✔
1468
        fs::create_directories(td);
2✔
1469
        generateFile(td / "test", 1234);
2✔
1470
        REQUIRE(fs::recursive_directory_iterator(t.path()) != fs::recursive_directory_iterator());
2✔
1471
        auto iter = fs::recursive_directory_iterator(t.path());
4✔
1472

1473
        CHECK(iter->path().filename() == "testdir");
2✔
1474
        CHECK(iter->path() == td);
2✔
1475
        CHECK(!iter->is_symlink());
2✔
1476
        CHECK(!iter->is_regular_file());
2✔
1477
        CHECK(iter->is_directory());
2✔
1478

1479
        CHECK(++iter != fs::recursive_directory_iterator());
2✔
1480

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

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

1632
TEST_CASE("fs.op.absolute - absolute", "[filesystem][operations][fs.op.absolute]")
2✔
1633
{
1634
    CHECK(fs::absolute("") == fs::current_path() / "");
2✔
1635
    CHECK(fs::absolute(fs::current_path()) == fs::current_path());
2✔
1636
    CHECK(fs::absolute(".") == fs::current_path() / ".");
2✔
1637
    CHECK((fs::absolute("..") == fs::current_path().parent_path() || fs::absolute("..") == fs::current_path() / ".."));
2✔
1638
    CHECK(fs::absolute("foo") == fs::current_path() / "foo");
2✔
1639
    std::error_code ec;
2✔
1640
    CHECK(fs::absolute("", ec) == fs::current_path() / "");
2✔
1641
    CHECK(!ec);
2✔
1642
    CHECK(fs::absolute("foo", ec) == fs::current_path() / "foo");
2✔
1643
    CHECK(!ec);
2✔
1644
}
2✔
1645

1646
TEST_CASE("fs.op.canonical - canonical", "[filesystem][operations][fs.op.canonical]")
2✔
1647
{
1648
    CHECK_THROWS_AS(fs::canonical(""), fs::filesystem_error);
6✔
1649
    {
1650
        std::error_code ec;
2✔
1651
        CHECK(fs::canonical("", ec) == "");
2✔
1652
        CHECK(ec);
2✔
1653
    }
1654
    CHECK(fs::canonical(fs::current_path()) == fs::current_path());
2✔
1655

1656
    CHECK(fs::canonical(".") == fs::current_path());
2✔
1657
    CHECK(fs::canonical("..") == fs::current_path().parent_path());
2✔
1658
    CHECK(fs::canonical("/") == fs::current_path().root_path());
2✔
1659
    CHECK_THROWS_AS(fs::canonical("foo"), fs::filesystem_error);
6✔
1660
    {
1661
        std::error_code ec;
2✔
1662
        CHECK_NOTHROW(fs::canonical("foo", ec));
2✔
1663
        CHECK(ec);
2✔
1664
    }
1665
    {
1666
        TemporaryDirectory t(TempOpt::change_path);
4✔
1667
        auto dir = t.path() / "d0";
4✔
1668
        fs::create_directories(dir / "d1");
2✔
1669
        generateFile(dir / "f0");
2✔
1670
        fs::path rel(dir.filename());
4✔
1671
        CHECK(fs::canonical(dir) == dir);
2✔
1672
        CHECK(fs::canonical(rel) == dir);
2✔
1673
        CHECK(fs::canonical(dir / "f0") == dir / "f0");
2✔
1674
        CHECK(fs::canonical(rel / "f0") == dir / "f0");
2✔
1675
        CHECK(fs::canonical(rel / "./f0") == dir / "f0");
2✔
1676
        CHECK(fs::canonical(rel / "d1/../f0") == dir / "f0");
2✔
1677
    }
1678

1679
    if (is_symlink_creation_supported()) {
2✔
1680
        TemporaryDirectory t(TempOpt::change_path);
4✔
1681
        fs::create_directory(t.path() / "dir1");
2✔
1682
        generateFile(t.path() / "dir1/test1");
2✔
1683
        fs::create_directory(t.path() / "dir2");
2✔
1684
        fs::create_directory_symlink(t.path() / "dir1", t.path() / "dir2/dirSym");
2✔
1685
        CHECK(fs::canonical(t.path() / "dir2/dirSym/test1") == t.path() / "dir1/test1");
2✔
1686
    }
1687
}
2✔
1688

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

1758
TEST_CASE("fs.op.copy_file - copy_file", "[filesystem][operations][fs.op.copy_file]")
2✔
1759
{
1760
    TemporaryDirectory t(TempOpt::change_path);
4✔
1761
    std::error_code ec;
2✔
1762
    generateFile("foo", 100);
2✔
1763
    CHECK(!fs::exists("bar"));
2✔
1764
    CHECK(fs::copy_file("foo", "bar"));
2✔
1765
    CHECK(fs::exists("bar"));
2✔
1766
    CHECK(fs::file_size("foo") == fs::file_size("bar"));
2✔
1767
    CHECK(fs::copy_file("foo", "bar2", ec));
2✔
1768
    CHECK(!ec);
2✔
1769
    std::this_thread::sleep_for(std::chrono::seconds(1));
2✔
1770
    generateFile("foo2", 200);
2✔
1771
    CHECK(fs::copy_file("foo2", "bar", fs::copy_options::update_existing));
2✔
1772
    CHECK(fs::file_size("bar") == 200);
2✔
1773
    CHECK(!fs::copy_file("foo", "bar", fs::copy_options::update_existing));
2✔
1774
    CHECK(fs::file_size("bar") == 200);
2✔
1775
    CHECK(fs::copy_file("foo", "bar", fs::copy_options::overwrite_existing));
2✔
1776
    CHECK(fs::file_size("bar") == 100);
2✔
1777
    CHECK_THROWS_AS(fs::copy_file("foobar", "foobar2"), fs::filesystem_error);
8✔
1778
    CHECK_NOTHROW(fs::copy_file("foobar", "foobar2", ec));
2✔
1779
    CHECK(ec);
2✔
1780
    CHECK(!fs::exists("foobar"));
2✔
1781
}
2✔
1782

1783
TEST_CASE("fs.op.copy_symlink - copy_symlink", "[filesystem][operations][fs.op.copy_symlink]")
2✔
1784
{
1785
    TemporaryDirectory t(TempOpt::change_path);
4✔
1786
    std::error_code ec;
2✔
1787
    generateFile("foo");
2✔
1788
    fs::create_directory("dir");
2✔
1789
    if (is_symlink_creation_supported()) {
2✔
1790
        fs::create_symlink("foo", "sfoo");
2✔
1791
        fs::create_directory_symlink("dir", "sdir");
2✔
1792
        CHECK_NOTHROW(fs::copy_symlink("sfoo", "sfooc"));
2✔
1793
        CHECK(fs::exists("sfooc"));
2✔
1794
        CHECK_NOTHROW(fs::copy_symlink("sfoo", "sfooc2", ec));
2✔
1795
        CHECK(fs::exists("sfooc2"));
2✔
1796
        CHECK(!ec);
2✔
1797
        CHECK_NOTHROW(fs::copy_symlink("sdir", "sdirc"));
2✔
1798
        CHECK(fs::exists("sdirc"));
2✔
1799
        CHECK_NOTHROW(fs::copy_symlink("sdir", "sdirc2", ec));
2✔
1800
        CHECK(fs::exists("sdirc2"));
2✔
1801
        CHECK(!ec);
2✔
1802
    }
1803
    CHECK_THROWS_AS(fs::copy_symlink("bar", "barc"), fs::filesystem_error);
8✔
1804
    CHECK_NOTHROW(fs::copy_symlink("bar", "barc", ec));
2✔
1805
    CHECK(ec);
2✔
1806
}
2✔
1807

1808
TEST_CASE("fs.op.create_directories - create_directories", "[filesystem][operations][fs.op.create_directories]")
2✔
1809
{
1810
    TemporaryDirectory t;
4✔
1811
    fs::path p = t.path() / "testdir";
4✔
1812
    fs::path p2 = p / "nested";
4✔
1813
    REQUIRE(!fs::exists(p));
2✔
1814
    REQUIRE(!fs::exists(p2));
2✔
1815
    CHECK(fs::create_directories(p2));
2✔
1816
    CHECK(fs::is_directory(p));
2✔
1817
    CHECK(fs::is_directory(p2));
2✔
1818
    CHECK(!fs::create_directories(p2));
2✔
1819
#ifdef TEST_LWG_2935_BEHAVIOUR
1820
    INFO("This test expects LWG #2935 result conformance.");
1821
    p = t.path() / "testfile";
1822
    generateFile(p);
1823
    CHECK(fs::is_regular_file(p));
1824
    CHECK(!fs::is_directory(p));
1825
    bool created = false;
1826
    CHECK_NOTHROW((created = fs::create_directories(p)));
1827
    CHECK(!created);
1828
    CHECK(fs::is_regular_file(p));
1829
    CHECK(!fs::is_directory(p));
1830
    std::error_code ec;
1831
    CHECK_NOTHROW((created = fs::create_directories(p, ec)));
1832
    CHECK(!created);
1833
    CHECK(!ec);
1834
    CHECK(fs::is_regular_file(p));
1835
    CHECK(!fs::is_directory(p));
1836
    CHECK(!fs::create_directories(p, ec));
1837
#else
1838
    INFO("This test expects conformance with P1164R1. (implemented by GCC with issue #86910.)");
4✔
1839
    p = t.path() / "testfile";
2✔
1840
    generateFile(p);
2✔
1841
    CHECK(fs::is_regular_file(p));
2✔
1842
    CHECK(!fs::is_directory(p));
2✔
1843
    CHECK_THROWS_AS(fs::create_directories(p), fs::filesystem_error);
4✔
1844
    CHECK(fs::is_regular_file(p));
2✔
1845
    CHECK(!fs::is_directory(p));
2✔
1846
    std::error_code ec;
2✔
1847
    CHECK_NOTHROW(fs::create_directories(p, ec));
2✔
1848
    CHECK(ec);
2✔
1849
    CHECK(fs::is_regular_file(p));
2✔
1850
    CHECK(!fs::is_directory(p));
2✔
1851
    CHECK(!fs::create_directories(p, ec));
2✔
1852
#endif
1853
}
2✔
1854

1855
TEST_CASE("fs.op.create_directory - create_directory", "[filesystem][operations][fs.op.create_directory]")
2✔
1856
{
1857
    TemporaryDirectory t;
4✔
1858
    fs::path p = t.path() / "testdir";
4✔
1859
    REQUIRE(!fs::exists(p));
2✔
1860
    CHECK(fs::create_directory(p));
2✔
1861
    CHECK(fs::is_directory(p));
2✔
1862
    CHECK(!fs::is_regular_file(p));
2✔
1863
    CHECK(fs::create_directory(p / "nested", p));
2✔
1864
    CHECK(fs::is_directory(p / "nested"));
2✔
1865
    CHECK(!fs::is_regular_file(p / "nested"));
2✔
1866
#ifdef TEST_LWG_2935_BEHAVIOUR
1867
    INFO("This test expects LWG #2935 result conformance.");
1868
    p = t.path() / "testfile";
1869
    generateFile(p);
1870
    CHECK(fs::is_regular_file(p));
1871
    CHECK(!fs::is_directory(p));
1872
    bool created = false;
1873
    CHECK_NOTHROW((created = fs::create_directory(p)));
1874
    CHECK(!created);
1875
    CHECK(fs::is_regular_file(p));
1876
    CHECK(!fs::is_directory(p));
1877
    std::error_code ec;
1878
    CHECK_NOTHROW((created = fs::create_directory(p, ec)));
1879
    CHECK(!created);
1880
    CHECK(!ec);
1881
    CHECK(fs::is_regular_file(p));
1882
    CHECK(!fs::is_directory(p));
1883
    CHECK(!fs::create_directories(p, ec));
1884
#else
1885
    INFO("This test expects conformance with P1164R1. (implemented by GCC with issue #86910.)");
4✔
1886
    p = t.path() / "testfile";
2✔
1887
    generateFile(p);
2✔
1888
    CHECK(fs::is_regular_file(p));
2✔
1889
    CHECK(!fs::is_directory(p));
2✔
1890
    REQUIRE_THROWS_AS(fs::create_directory(p), fs::filesystem_error);
4✔
1891
    CHECK(fs::is_regular_file(p));
2✔
1892
    CHECK(!fs::is_directory(p));
2✔
1893
    std::error_code ec;
2✔
1894
    REQUIRE_NOTHROW(fs::create_directory(p, ec));
2✔
1895
    CHECK(ec);
2✔
1896
    CHECK(fs::is_regular_file(p));
2✔
1897
    CHECK(!fs::is_directory(p));
2✔
1898
    CHECK(!fs::create_directory(p, ec));
2✔
1899
#endif
1900
}
2✔
1901

1902
TEST_CASE("fs.op.create_directory_symlink - create_directory_symlink", "[filesystem][operations][fs.op.create_directory_symlink]")
2✔
1903
{
1904
    if (is_symlink_creation_supported()) {
2✔
1905
        TemporaryDirectory t;
4✔
1906
        fs::create_directory(t.path() / "dir1");
2✔
1907
        generateFile(t.path() / "dir1/test1");
2✔
1908
        fs::create_directory(t.path() / "dir2");
2✔
1909
        fs::create_directory_symlink(t.path() / "dir1", t.path() / "dir2/dirSym");
2✔
1910
        CHECK(fs::exists(t.path() / "dir2/dirSym"));
2✔
1911
        CHECK(fs::is_symlink(t.path() / "dir2/dirSym"));
2✔
1912
        CHECK(fs::exists(t.path() / "dir2/dirSym/test1"));
2✔
1913
        CHECK(fs::is_regular_file(t.path() / "dir2/dirSym/test1"));
2✔
1914
        CHECK_THROWS_AS(fs::create_directory_symlink(t.path() / "dir1", t.path() / "dir2/dirSym"), fs::filesystem_error);
12✔
1915
        std::error_code ec;
2✔
1916
        CHECK_NOTHROW(fs::create_directory_symlink(t.path() / "dir1", t.path() / "dir2/dirSym", ec));
2✔
1917
        CHECK(ec);
2✔
1918
    }
1919
}
2✔
1920

1921
TEST_CASE("fs.op.create_hard_link - create_hard_link", "[filesystem][operations][fs.op.create_hard_link]")
2✔
1922
{
1923
#ifndef GHC_OS_WEB
1924
    TemporaryDirectory t(TempOpt::change_path);
4✔
1925
    std::error_code ec;
2✔
1926
    generateFile("foo", 1234);
2✔
1927
    CHECK_NOTHROW(fs::create_hard_link("foo", "bar"));
2✔
1928
    CHECK(fs::exists("bar"));
2✔
1929
    CHECK(!fs::is_symlink("bar"));
2✔
1930
    CHECK_NOTHROW(fs::create_hard_link("foo", "bar2", ec));
2✔
1931
    CHECK(fs::exists("bar2"));
2✔
1932
    CHECK(!fs::is_symlink("bar2"));
2✔
1933
    CHECK(!ec);
2✔
1934
    CHECK_THROWS_AS(fs::create_hard_link("nofoo", "bar"), fs::filesystem_error);
8✔
1935
    CHECK_NOTHROW(fs::create_hard_link("nofoo", "bar", ec));
2✔
1936
    CHECK(ec);
2✔
1937
#endif
1938
}
2✔
1939

1940
TEST_CASE("fs.op.create_symlink - create_symlink", "[filesystem][operations][fs.op.create_symlink]")
2✔
1941
{
1942
    if (is_symlink_creation_supported()) {
2✔
1943
        TemporaryDirectory t;
4✔
1944
        fs::create_directory(t.path() / "dir1");
2✔
1945
        generateFile(t.path() / "dir1/test1");
2✔
1946
        fs::create_directory(t.path() / "dir2");
2✔
1947
        fs::create_symlink(t.path() / "dir1/test1", t.path() / "dir2/fileSym");
2✔
1948
        CHECK(fs::exists(t.path() / "dir2/fileSym"));
2✔
1949
        CHECK(fs::is_symlink(t.path() / "dir2/fileSym"));
2✔
1950
        CHECK(fs::exists(t.path() / "dir2/fileSym"));
2✔
1951
        CHECK(fs::is_regular_file(t.path() / "dir2/fileSym"));
2✔
1952
        CHECK_THROWS_AS(fs::create_symlink(t.path() / "dir1", t.path() / "dir2/fileSym"), fs::filesystem_error);
12✔
1953
        std::error_code ec;
2✔
1954
        CHECK_NOTHROW(fs::create_symlink(t.path() / "dir1", t.path() / "dir2/fileSym", ec));
2✔
1955
        CHECK(ec);
2✔
1956
    }
1957
}
2✔
1958

1959
TEST_CASE("fs.op.current_path - current_path", "[filesystem][operations][fs.op.current_path]")
2✔
1960
{
1961
    TemporaryDirectory t;
4✔
1962
    std::error_code ec;
2✔
1963
    fs::path p1 = fs::current_path();
4✔
1964
    CHECK_NOTHROW(fs::current_path(t.path()));
2✔
1965
    CHECK(p1 != fs::current_path());
2✔
1966
    CHECK_NOTHROW(fs::current_path(p1, ec));
2✔
1967
    CHECK(!ec);
2✔
1968
    CHECK_THROWS_AS(fs::current_path(t.path() / "foo"), fs::filesystem_error);
8✔
1969
    CHECK(p1 == fs::current_path());
2✔
1970
    CHECK_NOTHROW(fs::current_path(t.path() / "foo", ec));
2✔
1971
    CHECK(ec);
2✔
1972
}
2✔
1973

1974
TEST_CASE("fs.op.equivalent - equivalent", "[filesystem][operations][fs.op.equivalent]")
2✔
1975
{
1976
    TemporaryDirectory t(TempOpt::change_path);
4✔
1977
    generateFile("foo", 1234);
2✔
1978
    CHECK(fs::equivalent(t.path() / "foo", "foo"));
2✔
1979
    if (is_symlink_creation_supported()) {
2✔
1980
        std::error_code ec(42, std::system_category());
2✔
1981
        fs::create_symlink("foo", "foo2");
2✔
1982
        CHECK(fs::equivalent("foo", "foo2"));
2✔
1983
        CHECK(fs::equivalent("foo", "foo2", ec));
2✔
1984
        CHECK(!ec);
2✔
1985
    }
1986
#ifdef TEST_LWG_2937_BEHAVIOUR
1987
    INFO("This test expects LWG #2937 result conformance.");
4✔
1988
    std::error_code ec;
2✔
1989
    bool result = false;
2✔
1990
    REQUIRE_THROWS_AS(fs::equivalent("foo", "foo3"), fs::filesystem_error);
8✔
1991
    CHECK_NOTHROW(result = fs::equivalent("foo", "foo3", ec));
2✔
1992
    CHECK(!result);
2✔
1993
    CHECK(ec);
2✔
1994
    ec.clear();
2✔
1995
    CHECK_THROWS_AS(fs::equivalent("foo3", "foo"), fs::filesystem_error);
8✔
1996
    CHECK_NOTHROW(result = fs::equivalent("foo3", "foo", ec));
2✔
1997
    CHECK(!result);
2✔
1998
    CHECK(ec);
2✔
1999
    ec.clear();
2✔
2000
    CHECK_THROWS_AS(fs::equivalent("foo3", "foo4"), fs::filesystem_error);
8✔
2001
    CHECK_NOTHROW(result = fs::equivalent("foo3", "foo4", ec));
2✔
2002
    CHECK(!result);
2✔
2003
    CHECK(ec);
2✔
2004
#else
2005
    INFO("This test expects conformance predating LWG #2937 result.");
2006
    std::error_code ec;
2007
    bool result = false;
2008
    REQUIRE_NOTHROW(result = fs::equivalent("foo", "foo3"));
2009
    CHECK(!result);
2010
    CHECK_NOTHROW(result = fs::equivalent("foo", "foo3", ec));
2011
    CHECK(!result);
2012
    CHECK(!ec);
2013
    ec.clear();
2014
    CHECK_NOTHROW(result = fs::equivalent("foo3", "foo"));
2015
    CHECK(!result);
2016
    CHECK_NOTHROW(result = fs::equivalent("foo3", "foo", ec));
2017
    CHECK(!result);
2018
    CHECK(!ec);
2019
    ec.clear();
2020
    CHECK_THROWS_AS(result = fs::equivalent("foo4", "foo3"), fs::filesystem_error);
2021
    CHECK(!result);
2022
    CHECK_NOTHROW(result = fs::equivalent("foo4", "foo3", ec));
2023
    CHECK(!result);
2024
    CHECK(ec);
2025
#endif
2026
}
2✔
2027

2028
TEST_CASE("fs.op.exists - exists", "[filesystem][operations][fs.op.exists]")
2✔
2029
{
2030
    TemporaryDirectory t(TempOpt::change_path);
4✔
2031
    std::error_code ec;
2✔
2032
    CHECK(!fs::exists(""));
2✔
2033
    CHECK(!fs::exists("foo"));
2✔
2034
    CHECK(!fs::exists("foo", ec));
2✔
2035
    CHECK(!ec);
2✔
2036
    ec = std::error_code(42, std::system_category());
2✔
2037
    CHECK(!fs::exists("foo", ec));
2✔
2038
#if defined(__cpp_lib_char8_t) && !defined(GHC_FILESYSTEM_ENFORCE_CPP17_API)
2039
    CHECK(!fs::exists(u8"foo"));
1✔
2040
#endif
2041
    CHECK(!ec);
2✔
2042
    ec.clear();
2✔
2043
    CHECK(fs::exists(t.path()));
2✔
2044
    CHECK(fs::exists(t.path(), ec));
2✔
2045
    CHECK(!ec);
2✔
2046
    ec = std::error_code(42, std::system_category());
2✔
2047
    CHECK(fs::exists(t.path(), ec));
2✔
2048
    CHECK(!ec);
2✔
2049
#if defined(GHC_OS_WINDOWS) && !defined(GHC_FILESYSTEM_FWD)
2050
    if (::GetFileAttributesW(L"C:\\fs-test") != INVALID_FILE_ATTRIBUTES) {
2051
        CHECK(fs::exists("C:\\fs-test"));    
2052
    }
2053
#endif
2054
}
2✔
2055

2056
TEST_CASE("fs.op.file_size - file_size", "[filesystem][operations][fs.op.file_size]")
2✔
2057
{
2058
    TemporaryDirectory t(TempOpt::change_path);
4✔
2059
    std::error_code ec;
2✔
2060
    generateFile("foo", 0);
2✔
2061
    generateFile("bar", 1234);
2✔
2062
    CHECK(fs::file_size("foo") == 0);
2✔
2063
    ec = std::error_code(42, std::system_category());
2✔
2064
    CHECK(fs::file_size("foo", ec) == 0);
2✔
2065
    CHECK(!ec);
2✔
2066
    ec.clear();
2✔
2067
    CHECK(fs::file_size("bar") == 1234);
2✔
2068
    ec = std::error_code(42, std::system_category());
2✔
2069
    CHECK(fs::file_size("bar", ec) == 1234);
2✔
2070
    CHECK(!ec);
2✔
2071
    ec.clear();
2✔
2072
    CHECK_THROWS_AS(fs::file_size("foobar"), fs::filesystem_error);
6✔
2073
    CHECK(fs::file_size("foobar", ec) == static_cast<uintmax_t>(-1));
2✔
2074
    CHECK(ec);
2✔
2075
    ec.clear();
2✔
2076
}
2✔
2077

2078
#ifndef GHC_OS_WINDOWS
2079
static uintmax_t getHardlinkCount(const fs::path& p)
4✔
2080
{
2081
    struct stat st = {};
4✔
2082
    auto rc = ::lstat(p.c_str(), &st);
4✔
2083
    return rc == 0 ? st.st_nlink : ~0u;
4✔
2084
}
2085
#endif
2086

2087
TEST_CASE("fs.op.hard_link_count - hard_link_count", "[filesystem][operations][fs.op.hard_link_count]")
2✔
2088
{
2089
#ifndef GHC_OS_WEB
2090
    TemporaryDirectory t(TempOpt::change_path);
4✔
2091
    std::error_code ec;
2✔
2092
#ifdef GHC_OS_WINDOWS
2093
    // windows doesn't implement "."/".." as hardlinks, so it
2094
    // starts with 1 and subdirectories don't change the count
2095
    CHECK(fs::hard_link_count(t.path()) == 1);
2096
    fs::create_directory("dir");
2097
    CHECK(fs::hard_link_count(t.path()) == 1);
2098
#else
2099
    // unix/bsd/linux typically implements "."/".." as hardlinks
2100
    // so an empty dir has 2 (from parent and the ".") and
2101
    // adding a subdirectory adds one due to its ".."
2102
    CHECK(fs::hard_link_count(t.path()) == getHardlinkCount(t.path()));
2✔
2103
    fs::create_directory("dir");
2✔
2104
    CHECK(fs::hard_link_count(t.path()) == getHardlinkCount(t.path()));
2✔
2105
#endif
2106
    generateFile("foo");
2✔
2107
    CHECK(fs::hard_link_count(t.path() / "foo") == 1);
2✔
2108
    ec = std::error_code(42, std::system_category());
2✔
2109
    CHECK(fs::hard_link_count(t.path() / "foo", ec) == 1);
2✔
2110
    CHECK(!ec);
2✔
2111
    CHECK_THROWS_AS(fs::hard_link_count(t.path() / "bar"), fs::filesystem_error);
8✔
2112
    CHECK_NOTHROW(fs::hard_link_count(t.path() / "bar", ec));
2✔
2113
    CHECK(ec);
2✔
2114
    ec.clear();
2✔
2115
#else
2116
    WARN("Test for unsupportet features are disabled on JS/Wasm target.");
2117
#endif
2118
}
2✔
2119

2120
class FileTypeMixFixture
2121
{
2122
public:
2123
    FileTypeMixFixture()
16✔
2124
        : _t(TempOpt::change_path)
16✔
2125
        , _hasFifo(false)
2126
        , _hasSocket(false)
16✔
2127
    {
2128
        generateFile("regular");
16✔
2129
        fs::create_directory("directory");
16✔
2130
        if (is_symlink_creation_supported()) {
16✔
2131
            fs::create_symlink("regular", "file_symlink");
16✔
2132
            fs::create_directory_symlink("directory", "dir_symlink");
16✔
2133
        }
2134
#if !defined(GHC_OS_WINDOWS) && !defined(GHC_OS_WEB)
2135
        REQUIRE(::mkfifo("fifo", 0644) == 0);
16✔
2136
        _hasFifo = true;
16✔
2137
        struct ::sockaddr_un addr;
2138
        addr.sun_family = AF_UNIX;
16✔
2139
        std::strncpy(addr.sun_path, "socket", sizeof(addr.sun_path));
16✔
2140
        int fd = socket(PF_UNIX, SOCK_STREAM, 0);
16✔
2141
        bind(fd, (struct sockaddr*)&addr, sizeof addr);
16✔
2142
        _hasSocket = true;
16✔
2143
#endif
2144
    }
16✔
2145

2146
    ~FileTypeMixFixture() {}
16✔
2147

2148
    bool has_fifo() const { return _hasFifo; }
16✔
2149

2150
    bool has_socket() const { return _hasSocket; }
16✔
2151

2152
    fs::path block_path() const
32✔
2153
    {
2154
        std::error_code ec;
32✔
2155
        if (fs::exists("/dev/sda", ec)) {
32✔
2156
            return "/dev/sda";
32✔
2157
        }
2158
        else if (fs::exists("/dev/disk0", ec)) {
×
2159
            return "/dev/disk0";
×
2160
        }
2161
        return fs::path();
×
2162
    }
2163

2164
    fs::path character_path() const
32✔
2165
    {
2166
#ifndef GHC_OS_SOLARIS
2167
        std::error_code ec;
32✔
2168
        if (fs::exists("/dev/null", ec)) {
32✔
2169
            return "/dev/null";
32✔
2170
        }
2171
        else if (fs::exists("NUL", ec)) {
×
2172
            return "NUL";
×
2173
        }
2174
#endif
2175
        return fs::path();
×
2176
    }
2177
    fs::path temp_path() const { return _t.path(); }
2178

2179
private:
2180
    TemporaryDirectory _t;
2181
    bool _hasFifo;
2182
    bool _hasSocket;
2183
};
2184

2185
TEST_CASE_METHOD(FileTypeMixFixture, "fs.op.is_block_file - is_block_file", "[filesystem][operations][fs.op.is_block_file]")
2✔
2186
{
2187
    std::error_code ec;
2✔
2188
    CHECK(!fs::is_block_file("directory"));
2✔
2189
    CHECK(!fs::is_block_file("regular"));
2✔
2190
    if (is_symlink_creation_supported()) {
2✔
2191
        CHECK(!fs::is_block_file("dir_symlink"));
2✔
2192
        CHECK(!fs::is_block_file("file_symlink"));
2✔
2193
    }
2194
    CHECK((has_fifo() ? !fs::is_block_file("fifo") : true));
2✔
2195
    CHECK((has_socket() ? !fs::is_block_file("socket") : true));
2✔
2196
    CHECK((block_path().empty() ? true : fs::is_block_file(block_path())));
2✔
2197
    CHECK((character_path().empty() ? true : !fs::is_block_file(character_path())));
2✔
2198
    CHECK_NOTHROW(fs::is_block_file("notfound"));
2✔
2199
    CHECK_NOTHROW(fs::is_block_file("notfound", ec));
2✔
2200
    CHECK(ec);
2✔
2201
    ec.clear();
2✔
2202
    CHECK(!fs::is_block_file(fs::file_status(fs::file_type::none)));
2✔
2203
    CHECK(!fs::is_block_file(fs::file_status(fs::file_type::not_found)));
2✔
2204
    CHECK(!fs::is_block_file(fs::file_status(fs::file_type::regular)));
2✔
2205
    CHECK(!fs::is_block_file(fs::file_status(fs::file_type::directory)));
2✔
2206
    CHECK(!fs::is_block_file(fs::file_status(fs::file_type::symlink)));
2✔
2207
    CHECK(fs::is_block_file(fs::file_status(fs::file_type::block)));
2✔
2208
    CHECK(!fs::is_block_file(fs::file_status(fs::file_type::character)));
2✔
2209
    CHECK(!fs::is_block_file(fs::file_status(fs::file_type::fifo)));
2✔
2210
    CHECK(!fs::is_block_file(fs::file_status(fs::file_type::socket)));
2✔
2211
    CHECK(!fs::is_block_file(fs::file_status(fs::file_type::unknown)));
2✔
2212
}
2✔
2213

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

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

2272
TEST_CASE("fs.op.is_empty - is_empty", "[filesystem][operations][fs.op.is_empty]")
2✔
2273
{
2274
    TemporaryDirectory t(TempOpt::change_path);
4✔
2275
    std::error_code ec;
2✔
2276
    CHECK(fs::is_empty(t.path()));
2✔
2277
    CHECK(fs::is_empty(t.path(), ec));
2✔
2278
    CHECK(!ec);
2✔
2279
    generateFile("foo", 0);
2✔
2280
    generateFile("bar", 1234);
2✔
2281
    CHECK(fs::is_empty("foo"));
2✔
2282
    CHECK(fs::is_empty("foo", ec));
2✔
2283
    CHECK(!ec);
2✔
2284
    CHECK(!fs::is_empty("bar"));
2✔
2285
    CHECK(!fs::is_empty("bar", ec));
2✔
2286
    CHECK(!ec);
2✔
2287
    CHECK_THROWS_AS(fs::is_empty("foobar"), fs::filesystem_error);
6✔
2288
    bool result = false;
2✔
2289
    CHECK_NOTHROW(result = fs::is_empty("foobar", ec));
2✔
2290
    CHECK(!result);
2✔
2291
    CHECK(ec);
2✔
2292
}
2✔
2293

2294
TEST_CASE_METHOD(FileTypeMixFixture, "fs.op.is_fifo - is_fifo", "[filesystem][operations][fs.op.is_fifo]")
2✔
2295
{
2296
    std::error_code ec;
2✔
2297
    CHECK(!fs::is_fifo("directory"));
2✔
2298
    CHECK(!fs::is_fifo("regular"));
2✔
2299
    if (is_symlink_creation_supported()) {
2✔
2300
        CHECK(!fs::is_fifo("dir_symlink"));
2✔
2301
        CHECK(!fs::is_fifo("file_symlink"));
2✔
2302
    }
2303
    CHECK((has_fifo() ? fs::is_fifo("fifo") : true));
2✔
2304
    CHECK((has_socket() ? !fs::is_fifo("socket") : true));
2✔
2305
    CHECK((block_path().empty() ? true : !fs::is_fifo(block_path())));
2✔
2306
    CHECK((character_path().empty() ? true : !fs::is_fifo(character_path())));
2✔
2307
    CHECK_NOTHROW(fs::is_fifo("notfound"));
2✔
2308
    CHECK_NOTHROW(fs::is_fifo("notfound", ec));
2✔
2309
    CHECK(ec);
2✔
2310
    ec.clear();
2✔
2311
    CHECK(!fs::is_fifo(fs::file_status(fs::file_type::none)));
2✔
2312
    CHECK(!fs::is_fifo(fs::file_status(fs::file_type::not_found)));
2✔
2313
    CHECK(!fs::is_fifo(fs::file_status(fs::file_type::regular)));
2✔
2314
    CHECK(!fs::is_fifo(fs::file_status(fs::file_type::directory)));
2✔
2315
    CHECK(!fs::is_fifo(fs::file_status(fs::file_type::symlink)));
2✔
2316
    CHECK(!fs::is_fifo(fs::file_status(fs::file_type::block)));
2✔
2317
    CHECK(!fs::is_fifo(fs::file_status(fs::file_type::character)));
2✔
2318
    CHECK(fs::is_fifo(fs::file_status(fs::file_type::fifo)));
2✔
2319
    CHECK(!fs::is_fifo(fs::file_status(fs::file_type::socket)));
2✔
2320
    CHECK(!fs::is_fifo(fs::file_status(fs::file_type::unknown)));
2✔
2321
}
2✔
2322

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

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

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

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

2439
#ifndef GHC_OS_WEB
2440
static fs::file_time_type timeFromString(const std::string& str)
4✔
2441
{
2442
    struct ::tm tm;
2443
    ::memset(&tm, 0, sizeof(::tm));
4✔
2444
    std::istringstream is(str);
8✔
2445
    is >> std::get_time(&tm, "%Y-%m-%dT%H:%M:%S");
4✔
2446
    if (is.fail()) {
4✔
2447
        throw std::exception();
×
2448
    }
2449
    return from_time_t<fs::file_time_type>(std::mktime(&tm));
8✔
2450
}
2451
#endif
2452

2453
TEST_CASE("fs.op.last_write_time - last_write_time", "[filesystem][operations][fs.op.last_write_time]")
2✔
2454
{
2455
    TemporaryDirectory t(TempOpt::change_path);
4✔
2456
    std::error_code ec;
2✔
2457
    fs::file_time_type ft;
2✔
2458
    generateFile("foo");
2✔
2459
    auto now = fs::file_time_type::clock::now();
2✔
2460
    CHECK(std::abs(std::chrono::duration_cast<std::chrono::seconds>(fs::last_write_time(t.path()) - now).count()) < 3);
2✔
2461
    CHECK(std::abs(std::chrono::duration_cast<std::chrono::seconds>(fs::last_write_time("foo") - now).count()) < 3);
2✔
2462
    CHECK_THROWS_AS(fs::last_write_time("bar"), fs::filesystem_error);
6✔
2463
    CHECK_NOTHROW(ft = fs::last_write_time("bar", ec));
2✔
2464
    CHECK(ft == fs::file_time_type::min());
2✔
2465
    CHECK(ec);
2✔
2466
    ec.clear();
2✔
2467
    if (is_symlink_creation_supported()) {
2✔
2468
        std::this_thread::sleep_for(std::chrono::seconds(1));
2✔
2469
        fs::create_symlink("foo", "foo2");
2✔
2470
        ft = fs::last_write_time("foo");
2✔
2471
        // checks that the time of the symlink is fetched
2472
        CHECK(ft == fs::last_write_time("foo2"));
2✔
2473
    }
2474
#ifndef GHC_OS_WEB
2475
    auto nt = timeFromString("2015-10-21T04:30:00");
2✔
2476
    CHECK_NOTHROW(fs::last_write_time(t.path() / "foo", nt));
2✔
2477
    CHECK(std::abs(std::chrono::duration_cast<std::chrono::seconds>(fs::last_write_time("foo") - nt).count()) < 1);
2✔
2478
    nt = timeFromString("2015-10-21T04:29:00");
2✔
2479
    CHECK_NOTHROW(fs::last_write_time("foo", nt, ec));
2✔
2480
    CHECK(std::abs(std::chrono::duration_cast<std::chrono::seconds>(fs::last_write_time("foo") - nt).count()) < 1);
2✔
2481
    CHECK(!ec);
2✔
2482
    CHECK_THROWS_AS(fs::last_write_time("bar", nt), fs::filesystem_error);
6✔
2483
    CHECK_NOTHROW(fs::last_write_time("bar", nt, ec));
2✔
2484
    CHECK(ec);
2✔
2485
#endif
2486
}
2✔
2487

2488
TEST_CASE("fs.op.permissions - permissions", "[filesystem][operations][fs.op.permissions]")
2✔
2489
{
2490
    TemporaryDirectory t(TempOpt::change_path);
4✔
2491
    std::error_code ec;
2✔
2492
    generateFile("foo", 512);
2✔
2493
    auto allWrite = fs::perms::owner_write | fs::perms::group_write | fs::perms::others_write;
2✔
2494
    CHECK_NOTHROW(fs::permissions("foo", allWrite, fs::perm_options::remove));
2✔
2495
    CHECK((fs::status("foo").permissions() & fs::perms::owner_write) != fs::perms::owner_write);
2✔
2496
#if !defined(GHC_OS_WINDOWS)
2497
    if (geteuid() != 0)
2✔
2498
#endif
2499
    {
2500
        CHECK_THROWS_AS(fs::resize_file("foo", 1024), fs::filesystem_error);
6✔
2501
        CHECK(fs::file_size("foo") == 512);
2✔
2502
    }
2503
    CHECK_NOTHROW(fs::permissions("foo", fs::perms::owner_write, fs::perm_options::add));
2✔
2504
    CHECK((fs::status("foo").permissions() & fs::perms::owner_write) == fs::perms::owner_write);
2✔
2505
    CHECK_NOTHROW(fs::resize_file("foo", 2048));
2✔
2506
    CHECK(fs::file_size("foo") == 2048);
2✔
2507
    CHECK_THROWS_AS(fs::permissions("bar", fs::perms::owner_write, fs::perm_options::add), fs::filesystem_error);
6✔
2508
    CHECK_NOTHROW(fs::permissions("bar", fs::perms::owner_write, fs::perm_options::add, ec));
2✔
2509
    CHECK(ec);
2✔
2510
    CHECK_THROWS_AS(fs::permissions("bar", fs::perms::owner_write, static_cast<fs::perm_options>(0)), fs::filesystem_error);
6✔
2511
}
2✔
2512

2513
TEST_CASE("fs.op.proximate - proximate", "[filesystem][operations][fs.op.proximate]")
2✔
2514
{
2515
    std::error_code ec;
2✔
2516
    CHECK(fs::proximate("/a/d", "/a/b/c") == "../../d");
2✔
2517
    CHECK(fs::proximate("/a/d", "/a/b/c", ec) == "../../d");
2✔
2518
    CHECK(!ec);
2✔
2519
    CHECK(fs::proximate("/a/b/c", "/a/d") == "../b/c");
2✔
2520
    CHECK(fs::proximate("/a/b/c", "/a/d", ec) == "../b/c");
2✔
2521
    CHECK(!ec);
2✔
2522
    CHECK(fs::proximate("a/b/c", "a") == "b/c");
2✔
2523
    CHECK(fs::proximate("a/b/c", "a", ec) == "b/c");
2✔
2524
    CHECK(!ec);
2✔
2525
    CHECK(fs::proximate("a/b/c", "a/b/c/x/y") == "../..");
2✔
2526
    CHECK(fs::proximate("a/b/c", "a/b/c/x/y", ec) == "../..");
2✔
2527
    CHECK(!ec);
2✔
2528
    CHECK(fs::proximate("a/b/c", "a/b/c") == ".");
2✔
2529
    CHECK(fs::proximate("a/b/c", "a/b/c", ec) == ".");
2✔
2530
    CHECK(!ec);
2✔
2531
    CHECK(fs::proximate("a/b", "c/d") == "../../a/b");
2✔
2532
    CHECK(fs::proximate("a/b", "c/d", ec) == "../../a/b");
2✔
2533
    CHECK(!ec);
2✔
2534
#ifndef GHC_OS_WINDOWS
2535
    if (has_host_root_name_support()) {
2✔
2536
        CHECK(fs::proximate("//host1/a/d", "//host2/a/b/c") == "//host1/a/d");
2✔
2537
        CHECK(fs::proximate("//host1/a/d", "//host2/a/b/c", ec) == "//host1/a/d");
2✔
2538
        CHECK(!ec);
2✔
2539
    }
2540
#endif
2541
}
2✔
2542

2543
TEST_CASE("fs.op.read_symlink - read_symlink", "[filesystem][operations][fs.op.read_symlink]")
2✔
2544
{
2545
    if (is_symlink_creation_supported()) {
2✔
2546
        TemporaryDirectory t(TempOpt::change_path);
4✔
2547
        std::error_code ec;
2✔
2548
        generateFile("foo");
2✔
2549
        fs::create_symlink(t.path() / "foo", "bar");
2✔
2550
        CHECK(fs::read_symlink("bar") == t.path() / "foo");
2✔
2551
        CHECK(fs::read_symlink("bar", ec) == t.path() / "foo");
2✔
2552
        CHECK(!ec);
2✔
2553
        CHECK_THROWS_AS(fs::read_symlink("foobar"), fs::filesystem_error);
6✔
2554
        CHECK(fs::read_symlink("foobar", ec) == fs::path());
2✔
2555
        CHECK(ec);
2✔
2556
    }
2557
}
2✔
2558

2559
TEST_CASE("fs.op.relative - relative", "[filesystem][operations][fs.op.relative]")
2✔
2560
{
2561
    CHECK(fs::relative("/a/d", "/a/b/c") == "../../d");
2✔
2562
    CHECK(fs::relative("/a/b/c", "/a/d") == "../b/c");
2✔
2563
    CHECK(fs::relative("a/b/c", "a") == "b/c");
2✔
2564
    CHECK(fs::relative("a/b/c", "a/b/c/x/y") == "../..");
2✔
2565
    CHECK(fs::relative("a/b/c", "a/b/c") == ".");
2✔
2566
    CHECK(fs::relative("a/b", "c/d") == "../../a/b");
2✔
2567
    std::error_code ec;
2✔
2568
    CHECK(fs::relative(fs::current_path() / "foo", ec) == "foo");
2✔
2569
    CHECK(!ec);
2✔
2570
}
2✔
2571

2572
TEST_CASE("fs.op.remove - remove", "[filesystem][operations][fs.op.remove]")
2✔
2573
{
2574
    TemporaryDirectory t(TempOpt::change_path);
4✔
2575
    std::error_code ec;
2✔
2576
    generateFile("foo");
2✔
2577
    CHECK(fs::remove("foo"));
2✔
2578
    CHECK(!fs::exists("foo"));
2✔
2579
    CHECK(!fs::remove("foo"));
2✔
2580
    generateFile("foo");
2✔
2581
    CHECK(fs::remove("foo", ec));
2✔
2582
    CHECK(!fs::exists("foo"));
2✔
2583
    if (is_symlink_creation_supported()) {
2✔
2584
        generateFile("foo");
2✔
2585
        fs::create_symlink("foo", "bar");
2✔
2586
        CHECK(fs::exists(fs::symlink_status("bar")));
2✔
2587
        CHECK(fs::remove("bar", ec));
2✔
2588
        CHECK(fs::exists("foo"));
2✔
2589
        CHECK(!fs::exists(fs::symlink_status("bar")));
2✔
2590
    }
2591
    CHECK(!fs::remove("bar"));
2✔
2592
    CHECK(!fs::remove("bar", ec));
2✔
2593
    CHECK(!ec);
2✔
2594
}
2✔
2595

2596
TEST_CASE("fs.op.remove_all - remove_all", "[filesystem][operations][fs.op.remove_all]")
2✔
2597
{
2598
    TemporaryDirectory t(TempOpt::change_path);
4✔
2599
    std::error_code ec;
2✔
2600
    generateFile("foo");
2✔
2601
    CHECK(fs::remove_all("foo", ec) == 1);
2✔
2602
    CHECK(!ec);
2✔
2603
    ec.clear();
2✔
2604
    CHECK(fs::directory_iterator(t.path()) == fs::directory_iterator());
2✔
2605
    fs::create_directories("dir1/dir1a");
2✔
2606
    fs::create_directories("dir1/dir1b");
2✔
2607
    generateFile("dir1/dir1a/f1");
2✔
2608
    generateFile("dir1/dir1b/f2");
2✔
2609
    CHECK_NOTHROW(fs::remove_all("dir1/non-existing", ec));
2✔
2610
    CHECK(!ec);
2✔
2611
    CHECK(fs::remove_all("dir1/non-existing", ec) == 0);
2✔
2612
    if (is_symlink_creation_supported()) {
2✔
2613
        fs::create_directory_symlink("dir1", "dir1link");
2✔
2614
        CHECK(fs::remove_all("dir1link") == 1);
2✔
2615
    }
2616
    CHECK(fs::remove_all("dir1") == 5);
2✔
2617
    CHECK(fs::directory_iterator(t.path()) == fs::directory_iterator());
2✔
2618
}
2✔
2619

2620
TEST_CASE("fs.op.rename - rename", "[filesystem][operations][fs.op.rename]")
2✔
2621
{
2622
    TemporaryDirectory t(TempOpt::change_path);
4✔
2623
    std::error_code ec;
2✔
2624
    generateFile("foo", 123);
2✔
2625
    fs::create_directory("dir1");
2✔
2626
    CHECK_NOTHROW(fs::rename("foo", "bar"));
2✔
2627
    CHECK(!fs::exists("foo"));
2✔
2628
    CHECK(fs::exists("bar"));
2✔
2629
    CHECK_NOTHROW(fs::rename("dir1", "dir2"));
2✔
2630
    CHECK(fs::exists("dir2"));
2✔
2631
    generateFile("foo2", 42);
2✔
2632
    CHECK_NOTHROW(fs::rename("bar", "foo2"));
2✔
2633
    CHECK(fs::exists("foo2"));
2✔
2634
    CHECK(fs::file_size("foo2") == 123u);
2✔
2635
    CHECK(!fs::exists("bar"));
2✔
2636
    CHECK_NOTHROW(fs::rename("foo2", "foo", ec));
2✔
2637
    CHECK(!ec);
2✔
2638
    CHECK_THROWS_AS(fs::rename("foobar", "barfoo"), fs::filesystem_error);
8✔
2639
    CHECK_NOTHROW(fs::rename("foobar", "barfoo", ec));
2✔
2640
    CHECK(ec);
2✔
2641
    CHECK(!fs::exists("barfoo"));
2✔
2642
}
2✔
2643

2644
TEST_CASE("fs.op.resize_file - resize_file", "[filesystem][operations][fs.op.resize_file]")
2✔
2645
{
2646
    TemporaryDirectory t(TempOpt::change_path);
4✔
2647
    std::error_code ec;
2✔
2648
    generateFile("foo", 1024);
2✔
2649
    CHECK(fs::file_size("foo") == 1024);
2✔
2650
    CHECK_NOTHROW(fs::resize_file("foo", 2048));
2✔
2651
    CHECK(fs::file_size("foo") == 2048);
2✔
2652
    CHECK_NOTHROW(fs::resize_file("foo", 1000, ec));
2✔
2653
    CHECK(!ec);
2✔
2654
    CHECK(fs::file_size("foo") == 1000);
2✔
2655
    CHECK_THROWS_AS(fs::resize_file("bar", 2048), fs::filesystem_error);
6✔
2656
    CHECK(!fs::exists("bar"));
2✔
2657
    CHECK_NOTHROW(fs::resize_file("bar", 4096, ec));
2✔
2658
    CHECK(ec);
2✔
2659
    CHECK(!fs::exists("bar"));
2✔
2660
}
2✔
2661

2662
TEST_CASE("fs.op.space - space", "[filesystem][operations][fs.op.space]")
2✔
2663
{
2664
    {
2665
        fs::space_info si;
2666
        CHECK_NOTHROW(si = fs::space(fs::current_path()));
2✔
2667
        CHECK(si.capacity > 1024 * 1024);
2✔
2668
        CHECK(si.capacity > si.free);
2✔
2669
        CHECK(si.free >= si.available);
2✔
2670
    }
2671
    {
2672
        std::error_code ec;
2✔
2673
        fs::space_info si;
2674
        CHECK_NOTHROW(si = fs::space(fs::current_path(), ec));
2✔
2675
        CHECK(si.capacity > 1024 * 1024);
2✔
2676
        CHECK(si.capacity > si.free);
2✔
2677
        CHECK(si.free >= si.available);
2✔
2678
        CHECK(!ec);
2✔
2679
    }
2680
#ifndef GHC_OS_WEB // statvfs under emscripten always returns a result, so this tests would fail
2681
    {
2682
        std::error_code ec;
2✔
2683
        fs::space_info si;
2684
        CHECK_NOTHROW(si = fs::space("foobar42", ec));
2✔
2685
        CHECK(si.capacity == static_cast<uintmax_t>(-1));
2✔
2686
        CHECK(si.free == static_cast<uintmax_t>(-1));
2✔
2687
        CHECK(si.available == static_cast<uintmax_t>(-1));
2✔
2688
        CHECK(ec);
2✔
2689
    }
2690
    CHECK_THROWS_AS(fs::space("foobar42"), fs::filesystem_error);
6✔
2691
#endif
2692
}
2✔
2693

2694
TEST_CASE("fs.op.status - status", "[filesystem][operations][fs.op.status]")
2✔
2695
{
2696
    TemporaryDirectory t(TempOpt::change_path);
4✔
2697
    std::error_code ec;
2✔
2698
    fs::file_status fs;
4✔
2699
    CHECK_NOTHROW(fs = fs::status("foo"));
2✔
2700
    CHECK(fs.type() == fs::file_type::not_found);
2✔
2701
    CHECK(fs.permissions() == fs::perms::unknown);
2✔
2702
    CHECK_NOTHROW(fs = fs::status("bar", ec));
2✔
2703
    CHECK(fs.type() == fs::file_type::not_found);
2✔
2704
    CHECK(fs.permissions() == fs::perms::unknown);
2✔
2705
    CHECK(ec);
2✔
2706
    ec.clear();
2✔
2707
    fs = fs::status(t.path());
2✔
2708
    CHECK(fs.type() == fs::file_type::directory);
2✔
2709
    CHECK((fs.permissions() & (fs::perms::owner_read | fs::perms::owner_write)) == (fs::perms::owner_read | fs::perms::owner_write));
2✔
2710
    generateFile("foobar");
2✔
2711
    fs = fs::status(t.path() / "foobar");
2✔
2712
    CHECK(fs.type() == fs::file_type::regular);
2✔
2713
    CHECK((fs.permissions() & (fs::perms::owner_read | fs::perms::owner_write)) == (fs::perms::owner_read | fs::perms::owner_write));
2✔
2714
    if (is_symlink_creation_supported()) {
2✔
2715
        fs::create_symlink(t.path() / "foobar", t.path() / "barfoo");
2✔
2716
        fs = fs::status(t.path() / "barfoo");
2✔
2717
        CHECK(fs.type() == fs::file_type::regular);
2✔
2718
        CHECK((fs.permissions() & (fs::perms::owner_read | fs::perms::owner_write)) == (fs::perms::owner_read | fs::perms::owner_write));
2✔
2719
    }
2720
}
2✔
2721

2722
TEST_CASE("fs.op.status_known - status_known", "[filesystem][operations][fs.op.status_known]")
2✔
2723
{
2724
    CHECK(!fs::status_known(fs::file_status()));
2✔
2725
    CHECK(fs::status_known(fs::file_status(fs::file_type::not_found)));
2✔
2726
    CHECK(fs::status_known(fs::file_status(fs::file_type::regular)));
2✔
2727
    CHECK(fs::status_known(fs::file_status(fs::file_type::directory)));
2✔
2728
    CHECK(fs::status_known(fs::file_status(fs::file_type::symlink)));
2✔
2729
    CHECK(fs::status_known(fs::file_status(fs::file_type::character)));
2✔
2730
    CHECK(fs::status_known(fs::file_status(fs::file_type::fifo)));
2✔
2731
    CHECK(fs::status_known(fs::file_status(fs::file_type::socket)));
2✔
2732
    CHECK(fs::status_known(fs::file_status(fs::file_type::unknown)));
2✔
2733
}
2✔
2734

2735
TEST_CASE("fs.op.symlink_status - symlink_status", "[filesystem][operations][fs.op.symlink_status]")
2✔
2736
{
2737
    TemporaryDirectory t(TempOpt::change_path);
4✔
2738
    std::error_code ec;
2✔
2739
    fs::file_status fs;
4✔
2740
    CHECK_NOTHROW(fs = fs::symlink_status("foo"));
2✔
2741
    CHECK(fs.type() == fs::file_type::not_found);
2✔
2742
    CHECK(fs.permissions() == fs::perms::unknown);
2✔
2743
    CHECK_NOTHROW(fs = fs::symlink_status("bar", ec));
2✔
2744
    CHECK(fs.type() == fs::file_type::not_found);
2✔
2745
    CHECK(fs.permissions() == fs::perms::unknown);
2✔
2746
    CHECK(ec);
2✔
2747
    ec.clear();
2✔
2748
    fs = fs::symlink_status(t.path());
2✔
2749
    CHECK(fs.type() == fs::file_type::directory);
2✔
2750
    CHECK((fs.permissions() & (fs::perms::owner_read | fs::perms::owner_write)) == (fs::perms::owner_read | fs::perms::owner_write));
2✔
2751
    generateFile("foobar");
2✔
2752
    fs = fs::symlink_status(t.path() / "foobar");
2✔
2753
    CHECK(fs.type() == fs::file_type::regular);
2✔
2754
    CHECK((fs.permissions() & (fs::perms::owner_read | fs::perms::owner_write)) == (fs::perms::owner_read | fs::perms::owner_write));
2✔
2755
    if (is_symlink_creation_supported()) {
2✔
2756
        fs::create_symlink(t.path() / "foobar", t.path() / "barfoo");
2✔
2757
        fs = fs::symlink_status(t.path() / "barfoo");
2✔
2758
        CHECK(fs.type() == fs::file_type::symlink);
2✔
2759
    }
2760
}
2✔
2761

2762
TEST_CASE("fs.op.temp_dir_path - temporary_directory_path", "[filesystem][operations][fs.op.temp_dir_path]")
2✔
2763
{
2764
    std::error_code ec;
2✔
2765
    CHECK_NOTHROW(fs::exists(fs::temp_directory_path()));
2✔
2766
    CHECK_NOTHROW(fs::exists(fs::temp_directory_path(ec)));
2✔
2767
    CHECK(!fs::temp_directory_path().empty());
2✔
2768
    CHECK(!ec);
2✔
2769
}
2✔
2770

2771
TEST_CASE("fs.op.weakly_canonical - weakly_canonical", "[filesystem][operations][fs.op.weakly_canonical]")
2✔
2772
{
2773
    INFO("This might fail on std::implementations that return fs::current_path() for fs::canonical(\"\")");
4✔
2774
    CHECK(fs::weakly_canonical("") == ".");
2✔
2775
    if(fs::weakly_canonical("") == ".") {
2✔
2776
        CHECK(fs::weakly_canonical("foo/bar") == "foo/bar");
2✔
2777
        CHECK(fs::weakly_canonical("foo/./bar") == "foo/bar");
2✔
2778
        CHECK(fs::weakly_canonical("foo/../bar") == "bar");
2✔
2779
    }
2780
    else {
2781
        CHECK(fs::weakly_canonical("foo/bar") == fs::current_path() / "foo/bar");
×
2782
        CHECK(fs::weakly_canonical("foo/./bar") == fs::current_path() / "foo/bar");
×
2783
        CHECK(fs::weakly_canonical("foo/../bar") == fs::current_path() / "bar");
×
2784
    }
2785

2786
    {
2787
        TemporaryDirectory t(TempOpt::change_path);
4✔
2788
        auto dir = t.path() / "d0";
4✔
2789
        fs::create_directories(dir / "d1");
2✔
2790
        generateFile(dir / "f0");
2✔
2791
        fs::path rel(dir.filename());
4✔
2792
        CHECK(fs::weakly_canonical(dir) == dir);
2✔
2793
        CHECK(fs::weakly_canonical(rel) == dir);
2✔
2794
        CHECK(fs::weakly_canonical(dir / "f0") == dir / "f0");
2✔
2795
        CHECK(fs::weakly_canonical(dir / "f0/") == dir / "f0/");
2✔
2796
        CHECK(fs::weakly_canonical(dir / "f1") == dir / "f1");
2✔
2797
        CHECK(fs::weakly_canonical(rel / "f0") == dir / "f0");
2✔
2798
        CHECK(fs::weakly_canonical(rel / "f0/") == dir / "f0/");
2✔
2799
        CHECK(fs::weakly_canonical(rel / "f1") == dir / "f1");
2✔
2800
        CHECK(fs::weakly_canonical(rel / "./f0") == dir / "f0");
2✔
2801
        CHECK(fs::weakly_canonical(rel / "./f1") == dir / "f1");
2✔
2802
        CHECK(fs::weakly_canonical(rel / "d1/../f0") == dir / "f0");
2✔
2803
        CHECK(fs::weakly_canonical(rel / "d1/../f1") == dir / "f1");
2✔
2804
        CHECK(fs::weakly_canonical(rel / "d1/../f1/../f2") == dir / "f2");
2✔
2805
    }
2806
}
2✔
2807

2808
TEST_CASE("std::string_view support", "[filesystem][fs.string_view]")
2✔
2809
{
2810
#if defined(GHC_HAS_STD_STRING_VIEW) || defined(GHC_HAS_STD_EXPERIMENTAL_STRING_VIEW)
2811

2812
#if defined(GHC_HAS_STD_STRING_VIEW)
2813
    using namespace std::literals;
2814
    using string_view = std::string_view;
2815
    using wstring_view = std::wstring_view;
2816
#elif defined(GHC_HAS_STD_EXPERIMENTAL_STRING_VIEW)
2817
    using string_view = std::experimental::string_view;
2818
    using wstring_view = std::experimental::wstring_view;
2819
#endif
2820

2821
    {
2822
        std::string p("foo/bar");
4✔
2823
        string_view sv(p);
2✔
2824
        CHECK(fs::path(sv, fs::path::format::generic_format).generic_string() == "foo/bar");
2✔
2825
        fs::path p2("fo");
4✔
2826
        p2 += string_view("o");
2✔
2827
        CHECK(p2 == "foo");
2✔
2828
        CHECK(p2.compare(string_view("foo")) == 0);
2✔
2829
    }
2830
    {
2831
        auto p = fs::path{"XYZ"};
4✔
2832
        p /= string_view("Appendix");
2✔
2833
        CHECK(p == "XYZ/Appendix");
2✔
2834
    }
2835
    {
2836
        std::wstring p(L"foo/bar");
4✔
2837
        wstring_view sv(p);
2✔
2838
        CHECK(fs::path(sv, fs::path::format::generic_format).generic_string() == "foo/bar");
2✔
2839
        fs::path p2(L"fo");
4✔
2840
        p2 += wstring_view(L"o");
2✔
2841
        CHECK(p2 == "foo");
2✔
2842
        CHECK(p2.compare(wstring_view(L"foo")) == 0);
2✔
2843
    }
2844

2845
#else
2846
    WARN("std::string_view specific tests are empty without std::string_view.");
2847
#endif
2848
}
2✔
2849

2850
TEST_CASE("Windows: Long filename support", "[filesystem][path][fs.path.win.long]")
2✔
2851
{
2852
#ifdef GHC_OS_WINDOWS
2853
    TemporaryDirectory t(TempOpt::change_path);
2854
    char c = 'A';
2855
    fs::path dir{"\\\\?\\"};
2856
    dir += fs::current_path().u8string();
2857
    for (; c <= 'Z'; ++c) {
2858
        std::string part = std::string(16, c);
2859
        dir /= part;
2860
        CHECK_NOTHROW(fs::create_directory(dir));
2861
        CHECK(fs::exists(dir));
2862
        generateFile(dir / "f0");
2863
        REQUIRE(fs::exists(dir / "f0"));
2864
    }
2865
    CHECK(c > 'Z');
2866
    fs::remove_all(fs::current_path() / std::string(16, 'A'));
2867
    CHECK(!fs::exists(fs::current_path() / std::string(16, 'A')));
2868
    CHECK_NOTHROW(fs::create_directories(dir));
2869
    CHECK(fs::exists(dir));
2870
    generateFile(dir / "f0");
2871
    CHECK(fs::exists(dir / "f0"));
2872
#else
2873
    WARN("Windows specific tests are empty on non-Windows systems.");
2✔
2874
#endif
2875
}
2✔
2876

2877
TEST_CASE("Windows: path namespace handling", "[filesystem][path][fs.path.win.namespaces]")
2✔
2878
{
2879
#ifdef GHC_OS_WINDOWS
2880
    {
2881
        std::error_code ec;
2882
        fs::path p(R"(\\localhost\c$\Windows)");
2883
        auto symstat = fs::symlink_status(p, ec);
2884
        CHECK(!ec);
2885
        auto p2 = fs::canonical(p, ec);
2886
        CHECK(!ec);
2887
        CHECK(p2 == p);
2888
    }
2889
    
2890
    struct TestInfo
2891
    {
2892
        std::string _path;
2893
        std::string _string;
2894
        std::string _rootName;
2895
        std::string _rootPath;
2896
        std::string _iterateResult;
2897
    };
2898
    std::vector<TestInfo> variants = {
2899
        {R"(C:\Windows\notepad.exe)", R"(C:\Windows\notepad.exe)", "C:", "C:\\", "C:,/,Windows,notepad.exe"},
2900
#ifdef USE_STD_FS
2901
        {R"(\\?\C:\Windows\notepad.exe)", R"(\\?\C:\Windows\notepad.exe)", "\\\\?", "\\\\?\\", "//?,/,C:,Windows,notepad.exe"},
2902
        {R"(\??\C:\Windows\notepad.exe)", R"(\??\C:\Windows\notepad.exe)", "\\??", "\\??\\", "/??,/,C:,Windows,notepad.exe"},
2903
#else
2904
        {R"(\\?\C:\Windows\notepad.exe)", R"(\\?\C:\Windows\notepad.exe)", "C:", "C:\\", "//?/,C:,/,Windows,notepad.exe"},
2905
        {R"(\??\C:\Windows\notepad.exe)", R"(\??\C:\Windows\notepad.exe)", "C:", "C:\\", "/?\?/,C:,/,Windows,notepad.exe"},
2906
#endif
2907
        {R"(\\.\C:\Windows\notepad.exe)", R"(\\.\C:\Windows\notepad.exe)", "\\\\.", "\\\\.\\", "//.,/,C:,Windows,notepad.exe"},
2908
        {R"(\\?\HarddiskVolume1\Windows\notepad.exe)", R"(\\?\HarddiskVolume1\Windows\notepad.exe)", "\\\\?", "\\\\?\\", "//?,/,HarddiskVolume1,Windows,notepad.exe"},
2909
        {R"(\\?\Harddisk0Partition1\Windows\notepad.exe)", R"(\\?\Harddisk0Partition1\Windows\notepad.exe)", "\\\\?", "\\\\?\\", "//?,/,Harddisk0Partition1,Windows,notepad.exe"},
2910
        {R"(\\.\GLOBALROOT\Device\HarddiskVolume1\Windows\notepad.exe)", R"(\\.\GLOBALROOT\Device\HarddiskVolume1\Windows\notepad.exe)", "\\\\.", "\\\\.\\", "//.,/,GLOBALROOT,Device,HarddiskVolume1,Windows,notepad.exe"},
2911
        {R"(\\?\GLOBALROOT\Device\Harddisk0\Partition1\Windows\notepad.exe)", R"(\\?\GLOBALROOT\Device\Harddisk0\Partition1\Windows\notepad.exe)", "\\\\?", "\\\\?\\", "//?,/,GLOBALROOT,Device,Harddisk0,Partition1,Windows,notepad.exe"},
2912
        {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"},
2913
        {R"(\\LOCALHOST\C$\Windows\notepad.exe)", R"(\\LOCALHOST\C$\Windows\notepad.exe)", "\\\\LOCALHOST", "\\\\LOCALHOST\\", "//LOCALHOST,/,C$,Windows,notepad.exe"},
2914
        {R"(\\?\UNC\C$\Windows\notepad.exe)", R"(\\?\UNC\C$\Windows\notepad.exe)", "\\\\?", "\\\\?\\", "//?,/,UNC,C$,Windows,notepad.exe"},
2915
        {R"(\\?\GLOBALROOT\Device\Mup\C$\Windows\notepad.exe)", R"(\\?\GLOBALROOT\Device\Mup\C$\Windows\notepad.exe)", "\\\\?", "\\\\?\\", "//?,/,GLOBALROOT,Device,Mup,C$,Windows,notepad.exe"},
2916
    };
2917

2918
    for (auto ti : variants) {
2919
        INFO("Used path: " + ti._path);
2920
        auto p = fs::path(ti._path);
2921
        CHECK(p.string() == ti._string);
2922
        CHECK(p.is_absolute());
2923
        CHECK(p.root_name().string() == ti._rootName);
2924
        CHECK(p.root_path().string() == ti._rootPath);
2925
        CHECK(iterateResult(p) == ti._iterateResult);
2926
    }
2927
#else
2928
    WARN("Windows specific tests are empty on non-Windows systems.");
2✔
2929
#endif
2930
}
2✔
2931

2932
TEST_CASE("Windows: Mapped folders handling ", "[filesystem][fs.win][fs.win.mapped]")
2✔
2933
{
2934
#ifdef GHC_OS_WINDOWS
2935
    // this test expects a mapped volume on C:\\fs-test as is the case on the development test system
2936
    // does nothing on other systems
2937
    if (fs::exists("C:\\fs-test")) {
2938
        CHECK(fs::canonical("C:\\fs-test\\Test.txt").string() == "C:\\fs-test\\Test.txt");
2939
    }
2940
#else
2941
    WARN("Windows specific tests are empty on non-Windows systems.");
2✔
2942
#endif
2943
}
2✔
2944

2945
TEST_CASE("Windows: Deletion of Read-only Files", "[filesystem][fs.win][fs.win.remove]")
2✔
2946
{
2947
#ifdef GHC_OS_WINDOWS
2948
    TemporaryDirectory t(TempOpt::change_path);
2949
    std::error_code ec;
2950
    generateFile("foo", 512);
2951
    auto allWrite = fs::perms::owner_write | fs::perms::group_write | fs::perms::others_write;
2952
    CHECK_NOTHROW(fs::permissions("foo", allWrite, fs::perm_options::remove));
2953
    CHECK_NOTHROW(fs::remove("foo"));
2954
    CHECK(!fs::exists("foo"));
2955
#else
2956
    WARN("Windows specific tests are empty on non-Windows systems.");
2✔
2957
#endif
2958
}
2✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc