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

gulrak / filesystem / 5012164383

pending completion
5012164383

push

github

Steffen Schuemann
head version bumped to wip version

3447 of 3620 relevant lines covered (95.22%)

436.15 hits per line

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

574
    CHECK(fs::path("/foo/bar.txt").replace_extension("odf") == "/foo/bar.odf");
2✔
575
    CHECK(fs::path("/foo/bar.txt").replace_extension() == "/foo/bar");
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
    CHECK(fs::path("/foo/bar.").replace_extension(".odf") == "/foo/bar.odf");
2✔
579
    CHECK(fs::path("/foo/bar/").replace_extension("odf") == "/foo/bar/.odf");
2✔
580

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

823
    // extension()
824
    CHECK(fs::path("/foo/bar.txt").extension() == ".txt");
2✔
825
    CHECK(fs::path("/foo/bar").extension() == "");
2✔
826
    CHECK(fs::path("/foo/.profile").extension() == "");
2✔
827
    CHECK(fs::path(".bar").extension() == "");
2✔
828
    CHECK(fs::path("..bar").extension() == ".bar");
2✔
829
    CHECK(fs::path("t:est.txt").extension() == ".txt");
2✔
830
    CHECK(fs::path("/foo/.").extension() == "");
2✔
831
    CHECK(fs::path("/foo/..").extension() == "");
2✔
832

833
    if (has_host_root_name_support()) {
2✔
834
        // //host-based root-names
835
        CHECK(fs::path("//host").root_name() == "//host");
2✔
836
        CHECK(fs::path("//host/foo").root_name() == "//host");
2✔
837
        CHECK(fs::path("//host").root_directory() == "");
2✔
838
        CHECK(fs::path("//host/foo").root_directory() == "/");
2✔
839
        CHECK(fs::path("//host").root_path() == "//host");
2✔
840
        CHECK(fs::path("//host/foo").root_path() == "//host/");
2✔
841
        CHECK(fs::path("//host").relative_path() == "");
2✔
842
        CHECK(fs::path("//host/foo").relative_path() == "foo");
2✔
843
        CHECK(fs::path("//host").parent_path() == "//host");
2✔
844
        CHECK(fs::path("//host/foo").parent_path() == "//host/");
2✔
845
        CHECK(fs::path("//host").filename() == "");
2✔
846
        CHECK(fs::path("//host/foo").filename() == "foo");
2✔
847
    }
848
}
2✔
849

850
TEST_CASE("fs.path.query - path query", "[fielsystem][path][fs.path.query]")
2✔
851
{
852
    // empty
853
    CHECK(fs::path("").empty());
2✔
854
    CHECK(!fs::path("foo").empty());
2✔
855

856
    // has_root_path()
857
    CHECK(!fs::path("foo").has_root_path());
2✔
858
    CHECK(!fs::path("foo/bar").has_root_path());
2✔
859
    CHECK(fs::path("/foo").has_root_path());
2✔
860
#ifdef GHC_OS_WINDOWS
861
    CHECK(fs::path("C:foo").has_root_path());
862
    CHECK(fs::path("C:/foo").has_root_path());
863
#endif
864

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

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

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

888
    // has_parent_path()
889
    CHECK(!fs::path("").has_parent_path());
2✔
890
    CHECK(!fs::path(".").has_parent_path());
2✔
891
    CHECK(!fs::path("..").has_parent_path());  // unintuitive but as defined in the standard
2✔
892
    CHECK(!fs::path("foo").has_parent_path());
2✔
893
    CHECK(fs::path("/").has_parent_path());
2✔
894
    CHECK(fs::path("/foo").has_parent_path());
2✔
895
    CHECK(fs::path("foo/").has_parent_path());
2✔
896
    CHECK(fs::path("/foo/").has_parent_path());
2✔
897

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

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

909
    // has_extension()
910
    CHECK(!fs::path("foo").has_extension());
2✔
911
    CHECK(fs::path("foo.bar").has_extension());
2✔
912
    CHECK(!fs::path(".profile").has_extension());
2✔
913

914
    // is_absolute()
915
    CHECK(!fs::path("foo/bar").is_absolute());
2✔
916
#ifdef GHC_OS_WINDOWS
917
    CHECK(!fs::path("/foo").is_absolute());
918
    CHECK(!fs::path("c:foo").is_absolute());
919
    CHECK(fs::path("c:/foo").is_absolute());
920
#else
921
    CHECK(fs::path("/foo").is_absolute());
2✔
922
#endif
923

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

934
    if (has_host_root_name_support()) {
2✔
935
        CHECK(fs::path("//host").has_root_name());
2✔
936
        CHECK(fs::path("//host/foo").has_root_name());
2✔
937
        CHECK(fs::path("//host").has_root_path());
2✔
938
        CHECK(fs::path("//host/foo").has_root_path());
2✔
939
        CHECK(!fs::path("//host").has_root_directory());
2✔
940
        CHECK(fs::path("//host/foo").has_root_directory());
2✔
941
        CHECK(!fs::path("//host").has_relative_path());
2✔
942
        CHECK(fs::path("//host/foo").has_relative_path());
2✔
943
        CHECK(fs::path("//host/foo").is_absolute());
2✔
944
        CHECK(!fs::path("//host/foo").is_relative());
2✔
945
    }
946
}
2✔
947

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

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

987
    // lexically_proximate()
988
    CHECK(fs::path("/a/d").lexically_proximate("/a/b/c") == "../../d");
2✔
989
    if (has_host_root_name_support()) {
2✔
990
        CHECK(fs::path("//host1/a/d").lexically_proximate("//host2/a/b/c") == "//host1/a/d");
2✔
991
    }
992
    CHECK(fs::path("a/d").lexically_proximate("/a/b/c") == "a/d");
2✔
993
#ifdef GHC_OS_WINDOWS
994
    CHECK(fs::path("c:/a/d").lexically_proximate("c:/a/b/c") == "../../d");
995
    CHECK(fs::path("c:/a/d").lexically_proximate("d:/a/b/c") == "c:/a/d");
996
    CHECK(fs::path("c:/foo").lexically_proximate("/bar") == "c:/foo");
997
    CHECK(fs::path("c:foo").lexically_proximate("c:/bar") == "c:foo");
998
    CHECK(fs::path("foo").lexically_proximate("/bar") == "foo");
999
#else
1000
    CHECK(fs::path("/foo").lexically_proximate("bar") == "/foo");
2✔
1001
    CHECK(fs::path("foo").lexically_proximate("/bar") == "foo");
2✔
1002
#endif
1003
}
2✔
1004

1005
static std::string iterateResult(const fs::path& path)
30✔
1006
{
1007
    std::ostringstream result;
60✔
1008
    for (fs::path::const_iterator i = path.begin(); i != path.end(); ++i) {
94✔
1009
        if (i != path.begin()) {
64✔
1010
            result << ",";
36✔
1011
        }
1012
        result << i->generic_string();
64✔
1013
    }
1014
    return result.str();
60✔
1015
}
1016

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

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

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

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

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

1140
TEST_CASE("fs.path.io - path inserter and extractor", "[filesystem][path][fs.path.io]")
2✔
1141
{
1142
    {
1143
        std::ostringstream os;
4✔
1144
        os << fs::path("/root/foo bar");
2✔
1145
#ifdef GHC_OS_WINDOWS
1146
        CHECK(os.str() == "\"\\\\root\\\\foo bar\"");
1147
#else
1148
        CHECK(os.str() == "\"/root/foo bar\"");
2✔
1149
#endif
1150
    }
1151
    {
1152
        std::ostringstream os;
4✔
1153
        os << fs::path("/root/foo\"bar");
2✔
1154
#ifdef GHC_OS_WINDOWS
1155
        CHECK(os.str() == "\"\\\\root\\\\foo\\\"bar\"");
1156
#else
1157
        CHECK(os.str() == "\"/root/foo\\\"bar\"");
2✔
1158
#endif
1159
    }
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
        CHECK((is.flags() & std::ios_base::skipws) == std::ios_base::skipws);
2✔
1167
    }
1168
    {
1169
        std::istringstream is("\"/root/foo bar\"");
6✔
1170
        is >> std::noskipws;
2✔
1171
        fs::path p;
4✔
1172
        is >> p;
2✔
1173
        CHECK(p == fs::path("/root/foo bar"));
2✔
1174
        CHECK((is.flags() & std::ios_base::skipws) != std::ios_base::skipws);
2✔
1175
    }
1176
    {
1177
        std::istringstream is("\"/root/foo\\\"bar\"");
6✔
1178
        fs::path p;
4✔
1179
        is >> p;
2✔
1180
        CHECK(p == fs::path("/root/foo\"bar"));
2✔
1181
    }
1182
    {
1183
        std::istringstream is("/root/foo");
6✔
1184
        fs::path p;
4✔
1185
        is >> p;
2✔
1186
        CHECK(p == fs::path("/root/foo"));
2✔
1187
    }
1188
}
2✔
1189

1190
TEST_CASE("fs.path.factory - path factory functions", "[filesystem][path][fs.path.factory]")
2✔
1191
{
1192
    CHECK(fs::u8path("foo/bar") == fs::path("foo/bar"));
2✔
1193
    CHECK(fs::u8path("foo/bar") == fs::path("foo/bar"));
2✔
1194
    std::string str("/foo/bar/test.txt");
4✔
1195
    CHECK(fs::u8path(str.begin(), str.end()) == str);
2✔
1196
}
2✔
1197

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

1217
static constexpr fs::perms constExprOwnerAll()
1218
{
1219
    return fs::perms::owner_read | fs::perms::owner_write | fs::perms::owner_exec;
1220
}
1221

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

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

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

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

1468
TEST_CASE("fs.class.rec.dir.itr - class recursive_directory_iterator", "[filesystem][recursive_directory_iterator][fs.class.rec.dir.itr]")
2✔
1469
{
1470
    {
1471
        auto iter = fs::recursive_directory_iterator(".");
4✔
1472
        iter.pop();
2✔
1473
        CHECK(iter == fs::recursive_directory_iterator());
2✔
1474
    }
1475
    {
1476
        TemporaryDirectory t;
4✔
1477
        CHECK(fs::recursive_directory_iterator(t.path()) == fs::recursive_directory_iterator());
2✔
1478
        generateFile(t.path() / "test", 1234);
2✔
1479
        REQUIRE(fs::recursive_directory_iterator(t.path()) != fs::recursive_directory_iterator());
2✔
1480
        auto iter = fs::recursive_directory_iterator(t.path());
4✔
1481
        CHECK(iter->path().filename() == "test");
2✔
1482
        CHECK(iter->path() == t.path() / "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
        CHECK(++iter == fs::recursive_directory_iterator());
2✔
1488
    }
1489

1490
    {
1491
        TemporaryDirectory t;
4✔
1492
        fs::path td = t.path() / "testdir";
4✔
1493
        fs::create_directories(td);
2✔
1494
        generateFile(td / "test", 1234);
2✔
1495
        REQUIRE(fs::recursive_directory_iterator(t.path()) != fs::recursive_directory_iterator());
2✔
1496
        auto iter = fs::recursive_directory_iterator(t.path());
4✔
1497

1498
        CHECK(iter->path().filename() == "testdir");
2✔
1499
        CHECK(iter->path() == td);
2✔
1500
        CHECK(!iter->is_symlink());
2✔
1501
        CHECK(!iter->is_regular_file());
2✔
1502
        CHECK(iter->is_directory());
2✔
1503

1504
        CHECK(++iter != fs::recursive_directory_iterator());
2✔
1505

1506
        CHECK(iter->path().filename() == "test");
2✔
1507
        CHECK(iter->path() == td / "test");
2✔
1508
        CHECK(!iter->is_symlink());
2✔
1509
        CHECK(iter->is_regular_file());
2✔
1510
        CHECK(!iter->is_directory());
2✔
1511
        CHECK(iter->file_size() == 1234);
2✔
1512

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

1657
TEST_CASE("fs.op.absolute - absolute", "[filesystem][operations][fs.op.absolute]")
2✔
1658
{
1659
    CHECK(fs::absolute("") == fs::current_path() / "");
2✔
1660
    CHECK(fs::absolute(fs::current_path()) == fs::current_path());
2✔
1661
    CHECK(fs::absolute(".") == fs::current_path() / ".");
2✔
1662
    CHECK((fs::absolute("..") == fs::current_path().parent_path() || fs::absolute("..") == fs::current_path() / ".."));
2✔
1663
    CHECK(fs::absolute("foo") == fs::current_path() / "foo");
2✔
1664
    std::error_code ec;
2✔
1665
    CHECK(fs::absolute("", ec) == fs::current_path() / "");
2✔
1666
    CHECK(!ec);
2✔
1667
    CHECK(fs::absolute("foo", ec) == fs::current_path() / "foo");
2✔
1668
    CHECK(!ec);
2✔
1669
}
2✔
1670

1671
TEST_CASE("fs.op.canonical - canonical", "[filesystem][operations][fs.op.canonical]")
2✔
1672
{
1673
    CHECK_THROWS_AS(fs::canonical(""), fs::filesystem_error);
6✔
1674
    {
1675
        std::error_code ec;
2✔
1676
        CHECK(fs::canonical("", ec) == "");
2✔
1677
        CHECK(ec);
2✔
1678
    }
1679
    CHECK(fs::canonical(fs::current_path()) == fs::current_path());
2✔
1680

1681
    CHECK(fs::canonical(".") == fs::current_path());
2✔
1682
    CHECK(fs::canonical("..") == fs::current_path().parent_path());
2✔
1683
    CHECK(fs::canonical("/") == fs::current_path().root_path());
2✔
1684
    CHECK_THROWS_AS(fs::canonical("foo"), fs::filesystem_error);
6✔
1685
    {
1686
        std::error_code ec;
2✔
1687
        CHECK_NOTHROW(fs::canonical("foo", ec));
2✔
1688
        CHECK(ec);
2✔
1689
    }
1690
    {
1691
        TemporaryDirectory t(TempOpt::change_path);
4✔
1692
        auto dir = t.path() / "d0";
4✔
1693
        fs::create_directories(dir / "d1");
2✔
1694
        generateFile(dir / "f0");
2✔
1695
        fs::path rel(dir.filename());
4✔
1696
        CHECK(fs::canonical(dir) == dir);
2✔
1697
        CHECK(fs::canonical(rel) == dir);
2✔
1698
        CHECK(fs::canonical(dir / "f0") == dir / "f0");
2✔
1699
        CHECK(fs::canonical(rel / "f0") == dir / "f0");
2✔
1700
        CHECK(fs::canonical(rel / "./f0") == dir / "f0");
2✔
1701
        CHECK(fs::canonical(rel / "d1/../f0") == dir / "f0");
2✔
1702
    }
1703

1704
    if (is_symlink_creation_supported()) {
2✔
1705
        TemporaryDirectory t(TempOpt::change_path);
4✔
1706
        fs::create_directory(t.path() / "dir1");
2✔
1707
        generateFile(t.path() / "dir1/test1");
2✔
1708
        fs::create_directory(t.path() / "dir2");
2✔
1709
        fs::create_directory_symlink(t.path() / "dir1", t.path() / "dir2/dirSym");
2✔
1710
        CHECK(fs::canonical(t.path() / "dir2/dirSym/test1") == t.path() / "dir1/test1");
2✔
1711
    }
1712
}
2✔
1713

1714
TEST_CASE("fs.op.copy - copy", "[filesystem][operations][fs.op.copy]")
2✔
1715
{
1716
    {
1717
        TemporaryDirectory t(TempOpt::change_path);
4✔
1718
        std::error_code ec;
2✔
1719
        fs::create_directory("dir1");
2✔
1720
        generateFile("dir1/file1");
2✔
1721
        generateFile("dir1/file2");
2✔
1722
        fs::create_directory("dir1/dir2");
2✔
1723
        generateFile("dir1/dir2/file3");
2✔
1724
        CHECK_NOTHROW(fs::copy("dir1", "dir3"));
2✔
1725
        CHECK(fs::exists("dir3/file1"));
2✔
1726
        CHECK(fs::exists("dir3/file2"));
2✔
1727
        CHECK(!fs::exists("dir3/dir2"));
2✔
1728
        CHECK_NOTHROW(fs::copy("dir1", "dir4", fs::copy_options::recursive, ec));
2✔
1729
        CHECK(!ec);
2✔
1730
        CHECK(fs::exists("dir4/file1"));
2✔
1731
        CHECK(fs::exists("dir4/file2"));
2✔
1732
        CHECK(fs::exists("dir4/dir2/file3"));
2✔
1733
        fs::create_directory("dir5");
2✔
1734
        generateFile("dir5/file1");
2✔
1735
        CHECK_THROWS_AS(fs::copy("dir1/file1", "dir5/file1"), fs::filesystem_error);
8✔
1736
        CHECK_NOTHROW(fs::copy("dir1/file1", "dir5/file1", fs::copy_options::skip_existing));
2✔
1737
    }
1738
    if (is_symlink_creation_supported()) {
2✔
1739
        TemporaryDirectory t(TempOpt::change_path);
4✔
1740
        std::error_code ec;
2✔
1741
        fs::create_directory("dir1");
2✔
1742
        generateFile("dir1/file1");
2✔
1743
        generateFile("dir1/file2");
2✔
1744
        fs::create_directory("dir1/dir2");
2✔
1745
        generateFile("dir1/dir2/file3");
2✔
1746
#ifdef TEST_LWG_2682_BEHAVIOUR
1747
        REQUIRE_THROWS_AS(fs::copy("dir1", "dir3", fs::copy_options::create_symlinks | fs::copy_options::recursive), fs::filesystem_error);
8✔
1748
#else
1749
        REQUIRE_NOTHROW(fs::copy("dir1", "dir3", fs::copy_options::create_symlinks | fs::copy_options::recursive));
1750
        CHECK(!ec);
1751
        CHECK(fs::exists("dir3/file1"));
1752
        CHECK(fs::is_symlink("dir3/file1"));
1753
        CHECK(fs::exists("dir3/file2"));
1754
        CHECK(fs::is_symlink("dir3/file2"));
1755
        CHECK(fs::exists("dir3/dir2/file3"));
1756
        CHECK(fs::is_symlink("dir3/dir2/file3"));
1757
#endif
1758
    }
1759
#ifndef GHC_OS_WEB
1760
    {
1761
        TemporaryDirectory t(TempOpt::change_path);
4✔
1762
        std::error_code ec;
2✔
1763
        fs::create_directory("dir1");
2✔
1764
        generateFile("dir1/file1");
2✔
1765
        generateFile("dir1/file2");
2✔
1766
        fs::create_directory("dir1/dir2");
2✔
1767
        generateFile("dir1/dir2/file3");
2✔
1768
        auto f1hl = fs::hard_link_count("dir1/file1");
2✔
1769
        auto f2hl = fs::hard_link_count("dir1/file2");
2✔
1770
        auto f3hl = fs::hard_link_count("dir1/dir2/file3");
2✔
1771
        CHECK_NOTHROW(fs::copy("dir1", "dir3", fs::copy_options::create_hard_links | fs::copy_options::recursive, ec));
2✔
1772
        REQUIRE(!ec);
2✔
1773
        CHECK(fs::exists("dir3/file1"));
2✔
1774
        CHECK(fs::hard_link_count("dir1/file1") == f1hl + 1);
2✔
1775
        CHECK(fs::exists("dir3/file2"));
2✔
1776
        CHECK(fs::hard_link_count("dir1/file2") == f2hl + 1);
2✔
1777
        CHECK(fs::exists("dir3/dir2/file3"));
2✔
1778
        CHECK(fs::hard_link_count("dir1/dir2/file3") == f3hl + 1);
2✔
1779
    }
1780
#endif
1781
}
2✔
1782

1783
TEST_CASE("fs.op.copy_file - copy_file", "[filesystem][operations][fs.op.copy_file]")
2✔
1784
{
1785
    TemporaryDirectory t(TempOpt::change_path);
4✔
1786
    std::error_code ec;
2✔
1787
    generateFile("foo", 100);
2✔
1788
    CHECK(!fs::exists("bar"));
2✔
1789
    CHECK(fs::copy_file("foo", "bar"));
2✔
1790
    CHECK(fs::exists("bar"));
2✔
1791
    CHECK(fs::file_size("foo") == fs::file_size("bar"));
2✔
1792
    CHECK(fs::copy_file("foo", "bar2", ec));
2✔
1793
    CHECK(!ec);
2✔
1794
    std::this_thread::sleep_for(std::chrono::seconds(1));
2✔
1795
    generateFile("foo2", 200);
2✔
1796
    CHECK(fs::copy_file("foo2", "bar", fs::copy_options::update_existing));
2✔
1797
    CHECK(fs::file_size("bar") == 200);
2✔
1798
    CHECK(!fs::copy_file("foo", "bar", fs::copy_options::update_existing));
2✔
1799
    CHECK(fs::file_size("bar") == 200);
2✔
1800
    CHECK(fs::copy_file("foo", "bar", fs::copy_options::overwrite_existing));
2✔
1801
    CHECK(fs::file_size("bar") == 100);
2✔
1802
    CHECK_THROWS_AS(fs::copy_file("foobar", "foobar2"), fs::filesystem_error);
8✔
1803
    CHECK_NOTHROW(fs::copy_file("foobar", "foobar2", ec));
2✔
1804
    CHECK(ec);
2✔
1805
    CHECK(!fs::exists("foobar"));
2✔
1806
    fs::path file1("temp1.txt");
4✔
1807
    fs::path file2("temp2.txt");
4✔
1808
    generateFile(file1, 200);
2✔
1809
    generateFile(file2, 200);
2✔
1810
    auto allWrite = fs::perms::owner_write | fs::perms::group_write | fs::perms::others_write;
2✔
1811
    CHECK_NOTHROW(fs::permissions(file1, allWrite, fs::perm_options::remove));
2✔
1812
    CHECK((fs::status(file1).permissions() & fs::perms::owner_write) != fs::perms::owner_write);
2✔
1813
    CHECK_NOTHROW(fs::permissions(file2, allWrite, fs::perm_options::add));
2✔
1814
    CHECK((fs::status(file2).permissions() & fs::perms::owner_write) == fs::perms::owner_write);
2✔
1815
    fs::copy_file(file1, file2, fs::copy_options::overwrite_existing);
2✔
1816
    CHECK((fs::status(file2).permissions() & fs::perms::owner_write) != fs::perms::owner_write);
2✔
1817
    CHECK_NOTHROW(fs::permissions(file1, allWrite, fs::perm_options::add));
2✔
1818
    CHECK_NOTHROW(fs::permissions(file2, allWrite, fs::perm_options::add));
2✔
1819
}
2✔
1820

1821
TEST_CASE("fs.op.copy_symlink - copy_symlink", "[filesystem][operations][fs.op.copy_symlink]")
2✔
1822
{
1823
    TemporaryDirectory t(TempOpt::change_path);
4✔
1824
    std::error_code ec;
2✔
1825
    generateFile("foo");
2✔
1826
    fs::create_directory("dir");
2✔
1827
    if (is_symlink_creation_supported()) {
2✔
1828
        fs::create_symlink("foo", "sfoo");
2✔
1829
        fs::create_directory_symlink("dir", "sdir");
2✔
1830
        CHECK_NOTHROW(fs::copy_symlink("sfoo", "sfooc"));
2✔
1831
        CHECK(fs::exists("sfooc"));
2✔
1832
        CHECK_NOTHROW(fs::copy_symlink("sfoo", "sfooc2", ec));
2✔
1833
        CHECK(fs::exists("sfooc2"));
2✔
1834
        CHECK(!ec);
2✔
1835
        CHECK_NOTHROW(fs::copy_symlink("sdir", "sdirc"));
2✔
1836
        CHECK(fs::exists("sdirc"));
2✔
1837
        CHECK_NOTHROW(fs::copy_symlink("sdir", "sdirc2", ec));
2✔
1838
        CHECK(fs::exists("sdirc2"));
2✔
1839
        CHECK(!ec);
2✔
1840
    }
1841
    CHECK_THROWS_AS(fs::copy_symlink("bar", "barc"), fs::filesystem_error);
8✔
1842
    CHECK_NOTHROW(fs::copy_symlink("bar", "barc", ec));
2✔
1843
    CHECK(ec);
2✔
1844
}
2✔
1845

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

1893
TEST_CASE("fs.op.create_directory - create_directory", "[filesystem][operations][fs.op.create_directory]")
2✔
1894
{
1895
    TemporaryDirectory t;
4✔
1896
    fs::path p = t.path() / "testdir";
4✔
1897
    REQUIRE(!fs::exists(p));
2✔
1898
    CHECK(fs::create_directory(p));
2✔
1899
    CHECK(fs::is_directory(p));
2✔
1900
    CHECK(!fs::is_regular_file(p));
2✔
1901
    CHECK(fs::create_directory(p / "nested", p));
2✔
1902
    CHECK(fs::is_directory(p / "nested"));
2✔
1903
    CHECK(!fs::is_regular_file(p / "nested"));
2✔
1904
#ifdef TEST_LWG_2935_BEHAVIOUR
1905
    INFO("This test expects LWG #2935 result conformance.");
1906
    p = t.path() / "testfile";
1907
    generateFile(p);
1908
    CHECK(fs::is_regular_file(p));
1909
    CHECK(!fs::is_directory(p));
1910
    bool created = false;
1911
    CHECK_NOTHROW((created = fs::create_directory(p)));
1912
    CHECK(!created);
1913
    CHECK(fs::is_regular_file(p));
1914
    CHECK(!fs::is_directory(p));
1915
    std::error_code ec;
1916
    CHECK_NOTHROW((created = fs::create_directory(p, ec)));
1917
    CHECK(!created);
1918
    CHECK(!ec);
1919
    CHECK(fs::is_regular_file(p));
1920
    CHECK(!fs::is_directory(p));
1921
    CHECK(!fs::create_directories(p, ec));
1922
#else
1923
    INFO("This test expects conformance with P1164R1. (implemented by GCC with issue #86910.)");
4✔
1924
    p = t.path() / "testfile";
2✔
1925
    generateFile(p);
2✔
1926
    CHECK(fs::is_regular_file(p));
2✔
1927
    CHECK(!fs::is_directory(p));
2✔
1928
    REQUIRE_THROWS_AS(fs::create_directory(p), fs::filesystem_error);
4✔
1929
    CHECK(fs::is_regular_file(p));
2✔
1930
    CHECK(!fs::is_directory(p));
2✔
1931
    std::error_code ec;
2✔
1932
    REQUIRE_NOTHROW(fs::create_directory(p, ec));
2✔
1933
    CHECK(ec);
2✔
1934
    CHECK(fs::is_regular_file(p));
2✔
1935
    CHECK(!fs::is_directory(p));
2✔
1936
    CHECK(!fs::create_directory(p, ec));
2✔
1937
#endif
1938
}
2✔
1939

1940
TEST_CASE("fs.op.create_directory_symlink - create_directory_symlink", "[filesystem][operations][fs.op.create_directory_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_directory_symlink(t.path() / "dir1", t.path() / "dir2/dirSym");
2✔
1948
        CHECK(fs::exists(t.path() / "dir2/dirSym"));
2✔
1949
        CHECK(fs::is_symlink(t.path() / "dir2/dirSym"));
2✔
1950
        CHECK(fs::exists(t.path() / "dir2/dirSym/test1"));
2✔
1951
        CHECK(fs::is_regular_file(t.path() / "dir2/dirSym/test1"));
2✔
1952
        CHECK_THROWS_AS(fs::create_directory_symlink(t.path() / "dir1", t.path() / "dir2/dirSym"), fs::filesystem_error);
12✔
1953
        std::error_code ec;
2✔
1954
        CHECK_NOTHROW(fs::create_directory_symlink(t.path() / "dir1", t.path() / "dir2/dirSym", ec));
2✔
1955
        CHECK(ec);
2✔
1956
    }
1957
}
2✔
1958

1959
TEST_CASE("fs.op.create_hard_link - create_hard_link", "[filesystem][operations][fs.op.create_hard_link]")
2✔
1960
{
1961
#ifndef GHC_OS_WEB
1962
    TemporaryDirectory t(TempOpt::change_path);
4✔
1963
    std::error_code ec;
2✔
1964
    generateFile("foo", 1234);
2✔
1965
    CHECK_NOTHROW(fs::create_hard_link("foo", "bar"));
2✔
1966
    CHECK(fs::exists("bar"));
2✔
1967
    CHECK(!fs::is_symlink("bar"));
2✔
1968
    CHECK_NOTHROW(fs::create_hard_link("foo", "bar2", ec));
2✔
1969
    CHECK(fs::exists("bar2"));
2✔
1970
    CHECK(!fs::is_symlink("bar2"));
2✔
1971
    CHECK(!ec);
2✔
1972
    CHECK_THROWS_AS(fs::create_hard_link("nofoo", "bar"), fs::filesystem_error);
8✔
1973
    CHECK_NOTHROW(fs::create_hard_link("nofoo", "bar", ec));
2✔
1974
    CHECK(ec);
2✔
1975
#endif
1976
}
2✔
1977

1978
TEST_CASE("fs.op.create_symlink - create_symlink", "[filesystem][operations][fs.op.create_symlink]")
2✔
1979
{
1980
    if (is_symlink_creation_supported()) {
2✔
1981
        TemporaryDirectory t;
4✔
1982
        fs::create_directory(t.path() / "dir1");
2✔
1983
        generateFile(t.path() / "dir1/test1");
2✔
1984
        fs::create_directory(t.path() / "dir2");
2✔
1985
        fs::create_symlink(t.path() / "dir1/test1", t.path() / "dir2/fileSym");
2✔
1986
        CHECK(fs::exists(t.path() / "dir2/fileSym"));
2✔
1987
        CHECK(fs::is_symlink(t.path() / "dir2/fileSym"));
2✔
1988
        CHECK(fs::exists(t.path() / "dir2/fileSym"));
2✔
1989
        CHECK(fs::is_regular_file(t.path() / "dir2/fileSym"));
2✔
1990
        CHECK_THROWS_AS(fs::create_symlink(t.path() / "dir1", t.path() / "dir2/fileSym"), fs::filesystem_error);
12✔
1991
        std::error_code ec;
2✔
1992
        CHECK_NOTHROW(fs::create_symlink(t.path() / "dir1", t.path() / "dir2/fileSym", ec));
2✔
1993
        CHECK(ec);
2✔
1994
    }
1995
}
2✔
1996

1997
TEST_CASE("fs.op.current_path - current_path", "[filesystem][operations][fs.op.current_path]")
2✔
1998
{
1999
    TemporaryDirectory t;
4✔
2000
    std::error_code ec;
2✔
2001
    fs::path p1 = fs::current_path();
4✔
2002
    CHECK_NOTHROW(fs::current_path(t.path()));
2✔
2003
    CHECK(p1 != fs::current_path());
2✔
2004
    CHECK_NOTHROW(fs::current_path(p1, ec));
2✔
2005
    CHECK(!ec);
2✔
2006
    CHECK_THROWS_AS(fs::current_path(t.path() / "foo"), fs::filesystem_error);
8✔
2007
    CHECK(p1 == fs::current_path());
2✔
2008
    CHECK_NOTHROW(fs::current_path(t.path() / "foo", ec));
2✔
2009
    CHECK(ec);
2✔
2010
}
2✔
2011

2012
TEST_CASE("fs.op.equivalent - equivalent", "[filesystem][operations][fs.op.equivalent]")
2✔
2013
{
2014
    TemporaryDirectory t(TempOpt::change_path);
4✔
2015
    generateFile("foo", 1234);
2✔
2016
    CHECK(fs::equivalent(t.path() / "foo", "foo"));
2✔
2017
    if (is_symlink_creation_supported()) {
2✔
2018
        std::error_code ec(42, std::system_category());
2✔
2019
        fs::create_symlink("foo", "foo2");
2✔
2020
        CHECK(fs::equivalent("foo", "foo2"));
2✔
2021
        CHECK(fs::equivalent("foo", "foo2", ec));
2✔
2022
        CHECK(!ec);
2✔
2023
    }
2024
#ifdef TEST_LWG_2937_BEHAVIOUR
2025
    INFO("This test expects LWG #2937 result conformance.");
4✔
2026
    std::error_code ec;
2✔
2027
    bool result = false;
2✔
2028
    REQUIRE_THROWS_AS(fs::equivalent("foo", "foo3"), fs::filesystem_error);
8✔
2029
    CHECK_NOTHROW(result = fs::equivalent("foo", "foo3", ec));
2✔
2030
    CHECK(!result);
2✔
2031
    CHECK(ec);
2✔
2032
    ec.clear();
2✔
2033
    CHECK_THROWS_AS(fs::equivalent("foo3", "foo"), fs::filesystem_error);
8✔
2034
    CHECK_NOTHROW(result = fs::equivalent("foo3", "foo", ec));
2✔
2035
    CHECK(!result);
2✔
2036
    CHECK(ec);
2✔
2037
    ec.clear();
2✔
2038
    CHECK_THROWS_AS(fs::equivalent("foo3", "foo4"), fs::filesystem_error);
8✔
2039
    CHECK_NOTHROW(result = fs::equivalent("foo3", "foo4", ec));
2✔
2040
    CHECK(!result);
2✔
2041
    CHECK(ec);
2✔
2042
#else
2043
    INFO("This test expects conformance predating LWG #2937 result.");
2044
    std::error_code ec;
2045
    bool result = false;
2046
    REQUIRE_NOTHROW(result = fs::equivalent("foo", "foo3"));
2047
    CHECK(!result);
2048
    CHECK_NOTHROW(result = fs::equivalent("foo", "foo3", ec));
2049
    CHECK(!result);
2050
    CHECK(!ec);
2051
    ec.clear();
2052
    CHECK_NOTHROW(result = fs::equivalent("foo3", "foo"));
2053
    CHECK(!result);
2054
    CHECK_NOTHROW(result = fs::equivalent("foo3", "foo", ec));
2055
    CHECK(!result);
2056
    CHECK(!ec);
2057
    ec.clear();
2058
    CHECK_THROWS_AS(result = fs::equivalent("foo4", "foo3"), fs::filesystem_error);
2059
    CHECK(!result);
2060
    CHECK_NOTHROW(result = fs::equivalent("foo4", "foo3", ec));
2061
    CHECK(!result);
2062
    CHECK(ec);
2063
#endif
2064
}
2✔
2065

2066
TEST_CASE("fs.op.exists - exists", "[filesystem][operations][fs.op.exists]")
2✔
2067
{
2068
    TemporaryDirectory t(TempOpt::change_path);
4✔
2069
    std::error_code ec;
2✔
2070
    CHECK(!fs::exists(""));
2✔
2071
    CHECK(!fs::exists("foo"));
2✔
2072
    CHECK(!fs::exists("foo", ec));
2✔
2073
    CHECK(!ec);
2✔
2074
    ec = std::error_code(42, std::system_category());
2✔
2075
    CHECK(!fs::exists("foo", ec));
2✔
2076
#if defined(__cpp_lib_char8_t) && !defined(GHC_FILESYSTEM_ENFORCE_CPP17_API)
2077
    CHECK(!fs::exists(u8"foo"));
1✔
2078
#endif
2079
    CHECK(!ec);
2✔
2080
    ec.clear();
2✔
2081
    CHECK(fs::exists(t.path()));
2✔
2082
    CHECK(fs::exists(t.path(), ec));
2✔
2083
    CHECK(!ec);
2✔
2084
    ec = std::error_code(42, std::system_category());
2✔
2085
    CHECK(fs::exists(t.path(), ec));
2✔
2086
    CHECK(!ec);
2✔
2087
#if defined(GHC_OS_WINDOWS) && !defined(GHC_FILESYSTEM_FWD)
2088
    if (::GetFileAttributesW(L"C:\\fs-test") != INVALID_FILE_ATTRIBUTES) {
2089
        CHECK(fs::exists("C:\\fs-test"));    
2090
    }
2091
#endif
2092
}
2✔
2093

2094
TEST_CASE("fs.op.file_size - file_size", "[filesystem][operations][fs.op.file_size]")
2✔
2095
{
2096
    TemporaryDirectory t(TempOpt::change_path);
4✔
2097
    std::error_code ec;
2✔
2098
    generateFile("foo", 0);
2✔
2099
    generateFile("bar", 1234);
2✔
2100
    CHECK(fs::file_size("foo") == 0);
2✔
2101
    ec = std::error_code(42, std::system_category());
2✔
2102
    CHECK(fs::file_size("foo", ec) == 0);
2✔
2103
    CHECK(!ec);
2✔
2104
    ec.clear();
2✔
2105
    CHECK(fs::file_size("bar") == 1234);
2✔
2106
    ec = std::error_code(42, std::system_category());
2✔
2107
    CHECK(fs::file_size("bar", ec) == 1234);
2✔
2108
    CHECK(!ec);
2✔
2109
    ec.clear();
2✔
2110
    CHECK_THROWS_AS(fs::file_size("foobar"), fs::filesystem_error);
6✔
2111
    CHECK(fs::file_size("foobar", ec) == static_cast<uintmax_t>(-1));
2✔
2112
    CHECK(ec);
2✔
2113
    ec.clear();
2✔
2114
}
2✔
2115

2116
#ifndef GHC_OS_WINDOWS
2117
static uintmax_t getHardlinkCount(const fs::path& p)
4✔
2118
{
2119
    struct stat st = {};
4✔
2120
    auto rc = ::lstat(p.c_str(), &st);
4✔
2121
    return rc == 0 ? st.st_nlink : ~0u;
4✔
2122
}
2123
#endif
2124

2125
TEST_CASE("fs.op.hard_link_count - hard_link_count", "[filesystem][operations][fs.op.hard_link_count]")
2✔
2126
{
2127
#ifndef GHC_OS_WEB
2128
    TemporaryDirectory t(TempOpt::change_path);
4✔
2129
    std::error_code ec;
2✔
2130
#ifdef GHC_OS_WINDOWS
2131
    // windows doesn't implement "."/".." as hardlinks, so it
2132
    // starts with 1 and subdirectories don't change the count
2133
    CHECK(fs::hard_link_count(t.path()) == 1);
2134
    fs::create_directory("dir");
2135
    CHECK(fs::hard_link_count(t.path()) == 1);
2136
#else
2137
    // unix/bsd/linux typically implements "."/".." as hardlinks
2138
    // so an empty dir has 2 (from parent and the ".") and
2139
    // adding a subdirectory adds one due to its ".."
2140
    CHECK(fs::hard_link_count(t.path()) == getHardlinkCount(t.path()));
2✔
2141
    fs::create_directory("dir");
2✔
2142
    CHECK(fs::hard_link_count(t.path()) == getHardlinkCount(t.path()));
2✔
2143
#endif
2144
    generateFile("foo");
2✔
2145
    CHECK(fs::hard_link_count(t.path() / "foo") == 1);
2✔
2146
    ec = std::error_code(42, std::system_category());
2✔
2147
    CHECK(fs::hard_link_count(t.path() / "foo", ec) == 1);
2✔
2148
    CHECK(!ec);
2✔
2149
    CHECK_THROWS_AS(fs::hard_link_count(t.path() / "bar"), fs::filesystem_error);
8✔
2150
    CHECK_NOTHROW(fs::hard_link_count(t.path() / "bar", ec));
2✔
2151
    CHECK(ec);
2✔
2152
    ec.clear();
2✔
2153
#else
2154
    WARN("Test for unsupportet features are disabled on JS/Wasm target.");
2155
#endif
2156
}
2✔
2157

2158
class FileTypeMixFixture
2159
{
2160
public:
2161
    FileTypeMixFixture()
16✔
2162
        : _t(TempOpt::change_path)
16✔
2163
        , _hasFifo(false)
2164
        , _hasSocket(false)
16✔
2165
    {
2166
        generateFile("regular");
16✔
2167
        fs::create_directory("directory");
16✔
2168
        if (is_symlink_creation_supported()) {
16✔
2169
            fs::create_symlink("regular", "file_symlink");
16✔
2170
            fs::create_directory_symlink("directory", "dir_symlink");
16✔
2171
        }
2172
#if !defined(GHC_OS_WINDOWS) && !defined(GHC_OS_WEB)
2173
        REQUIRE(::mkfifo("fifo", 0644) == 0);
16✔
2174
        _hasFifo = true;
16✔
2175
        struct ::sockaddr_un addr;
2176
        addr.sun_family = AF_UNIX;
16✔
2177
        std::strncpy(addr.sun_path, "socket", sizeof(addr.sun_path));
16✔
2178
        int fd = socket(PF_UNIX, SOCK_STREAM, 0);
16✔
2179
        bind(fd, (struct sockaddr*)&addr, sizeof addr);
16✔
2180
        _hasSocket = true;
16✔
2181
#endif
2182
    }
16✔
2183

2184
    ~FileTypeMixFixture() {}
16✔
2185

2186
    bool has_fifo() const { return _hasFifo; }
16✔
2187

2188
    bool has_socket() const { return _hasSocket; }
16✔
2189

2190
    fs::path block_path() const
32✔
2191
    {
2192
        std::error_code ec;
32✔
2193
        if (fs::exists("/dev/sda", ec)) {
32✔
2194
            return "/dev/sda";
32✔
2195
        }
2196
        else if (fs::exists("/dev/disk0", ec)) {
×
2197
            return "/dev/disk0";
×
2198
        }
2199
        return fs::path();
×
2200
    }
2201

2202
    fs::path character_path() const
32✔
2203
    {
2204
#ifndef GHC_OS_SOLARIS
2205
        std::error_code ec;
32✔
2206
        if (fs::exists("/dev/null", ec)) {
32✔
2207
            return "/dev/null";
32✔
2208
        }
2209
        else if (fs::exists("NUL", ec)) {
×
2210
            return "NUL";
×
2211
        }
2212
#endif
2213
        return fs::path();
×
2214
    }
2215
    fs::path temp_path() const { return _t.path(); }
2216

2217
private:
2218
    TemporaryDirectory _t;
2219
    bool _hasFifo;
2220
    bool _hasSocket;
2221
};
2222

2223
TEST_CASE_METHOD(FileTypeMixFixture, "fs.op.is_block_file - is_block_file", "[filesystem][operations][fs.op.is_block_file]")
2✔
2224
{
2225
    std::error_code ec;
2✔
2226
    CHECK(!fs::is_block_file("directory"));
2✔
2227
    CHECK(!fs::is_block_file("regular"));
2✔
2228
    if (is_symlink_creation_supported()) {
2✔
2229
        CHECK(!fs::is_block_file("dir_symlink"));
2✔
2230
        CHECK(!fs::is_block_file("file_symlink"));
2✔
2231
    }
2232
    CHECK((has_fifo() ? !fs::is_block_file("fifo") : true));
2✔
2233
    CHECK((has_socket() ? !fs::is_block_file("socket") : true));
2✔
2234
    CHECK((block_path().empty() ? true : fs::is_block_file(block_path())));
2✔
2235
    CHECK((character_path().empty() ? true : !fs::is_block_file(character_path())));
2✔
2236
    CHECK_NOTHROW(fs::is_block_file("notfound"));
2✔
2237
    CHECK_NOTHROW(fs::is_block_file("notfound", ec));
2✔
2238
    CHECK(ec);
2✔
2239
    ec.clear();
2✔
2240
    CHECK(!fs::is_block_file(fs::file_status(fs::file_type::none)));
2✔
2241
    CHECK(!fs::is_block_file(fs::file_status(fs::file_type::not_found)));
2✔
2242
    CHECK(!fs::is_block_file(fs::file_status(fs::file_type::regular)));
2✔
2243
    CHECK(!fs::is_block_file(fs::file_status(fs::file_type::directory)));
2✔
2244
    CHECK(!fs::is_block_file(fs::file_status(fs::file_type::symlink)));
2✔
2245
    CHECK(fs::is_block_file(fs::file_status(fs::file_type::block)));
2✔
2246
    CHECK(!fs::is_block_file(fs::file_status(fs::file_type::character)));
2✔
2247
    CHECK(!fs::is_block_file(fs::file_status(fs::file_type::fifo)));
2✔
2248
    CHECK(!fs::is_block_file(fs::file_status(fs::file_type::socket)));
2✔
2249
    CHECK(!fs::is_block_file(fs::file_status(fs::file_type::unknown)));
2✔
2250
}
2✔
2251

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

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

2310
TEST_CASE("fs.op.is_empty - is_empty", "[filesystem][operations][fs.op.is_empty]")
2✔
2311
{
2312
    TemporaryDirectory t(TempOpt::change_path);
4✔
2313
    std::error_code ec;
2✔
2314
    CHECK(fs::is_empty(t.path()));
2✔
2315
    CHECK(fs::is_empty(t.path(), ec));
2✔
2316
    CHECK(!ec);
2✔
2317
    generateFile("foo", 0);
2✔
2318
    generateFile("bar", 1234);
2✔
2319
    CHECK(fs::is_empty("foo"));
2✔
2320
    CHECK(fs::is_empty("foo", ec));
2✔
2321
    CHECK(!ec);
2✔
2322
    CHECK(!fs::is_empty("bar"));
2✔
2323
    CHECK(!fs::is_empty("bar", ec));
2✔
2324
    CHECK(!ec);
2✔
2325
    CHECK_THROWS_AS(fs::is_empty("foobar"), fs::filesystem_error);
6✔
2326
    bool result = false;
2✔
2327
    CHECK_NOTHROW(result = fs::is_empty("foobar", ec));
2✔
2328
    CHECK(!result);
2✔
2329
    CHECK(ec);
2✔
2330
}
2✔
2331

2332
TEST_CASE_METHOD(FileTypeMixFixture, "fs.op.is_fifo - is_fifo", "[filesystem][operations][fs.op.is_fifo]")
2✔
2333
{
2334
    std::error_code ec;
2✔
2335
    CHECK(!fs::is_fifo("directory"));
2✔
2336
    CHECK(!fs::is_fifo("regular"));
2✔
2337
    if (is_symlink_creation_supported()) {
2✔
2338
        CHECK(!fs::is_fifo("dir_symlink"));
2✔
2339
        CHECK(!fs::is_fifo("file_symlink"));
2✔
2340
    }
2341
    CHECK((has_fifo() ? fs::is_fifo("fifo") : true));
2✔
2342
    CHECK((has_socket() ? !fs::is_fifo("socket") : true));
2✔
2343
    CHECK((block_path().empty() ? true : !fs::is_fifo(block_path())));
2✔
2344
    CHECK((character_path().empty() ? true : !fs::is_fifo(character_path())));
2✔
2345
    CHECK_NOTHROW(fs::is_fifo("notfound"));
2✔
2346
    CHECK_NOTHROW(fs::is_fifo("notfound", ec));
2✔
2347
    CHECK(ec);
2✔
2348
    ec.clear();
2✔
2349
    CHECK(!fs::is_fifo(fs::file_status(fs::file_type::none)));
2✔
2350
    CHECK(!fs::is_fifo(fs::file_status(fs::file_type::not_found)));
2✔
2351
    CHECK(!fs::is_fifo(fs::file_status(fs::file_type::regular)));
2✔
2352
    CHECK(!fs::is_fifo(fs::file_status(fs::file_type::directory)));
2✔
2353
    CHECK(!fs::is_fifo(fs::file_status(fs::file_type::symlink)));
2✔
2354
    CHECK(!fs::is_fifo(fs::file_status(fs::file_type::block)));
2✔
2355
    CHECK(!fs::is_fifo(fs::file_status(fs::file_type::character)));
2✔
2356
    CHECK(fs::is_fifo(fs::file_status(fs::file_type::fifo)));
2✔
2357
    CHECK(!fs::is_fifo(fs::file_status(fs::file_type::socket)));
2✔
2358
    CHECK(!fs::is_fifo(fs::file_status(fs::file_type::unknown)));
2✔
2359
}
2✔
2360

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

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

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

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

2477
#ifndef GHC_OS_WEB
2478
static fs::file_time_type timeFromString(const std::string& str)
4✔
2479
{
2480
    struct ::tm tm;
2481
    ::memset(&tm, 0, sizeof(::tm));
4✔
2482
    std::istringstream is(str);
8✔
2483
    is >> std::get_time(&tm, "%Y-%m-%dT%H:%M:%S");
4✔
2484
    if (is.fail()) {
4✔
2485
        throw std::exception();
×
2486
    }
2487
    return from_time_t<fs::file_time_type>(std::mktime(&tm));
8✔
2488
}
2489
#endif
2490

2491
TEST_CASE("fs.op.last_write_time - last_write_time", "[filesystem][operations][fs.op.last_write_time]")
2✔
2492
{
2493
    TemporaryDirectory t(TempOpt::change_path);
4✔
2494
    std::error_code ec;
2✔
2495
    fs::file_time_type ft;
2✔
2496
    generateFile("foo");
2✔
2497
    auto now = fs::file_time_type::clock::now();
2✔
2498
    CHECK(std::abs(std::chrono::duration_cast<std::chrono::seconds>(fs::last_write_time(t.path()) - now).count()) < 3);
2✔
2499
    CHECK(std::abs(std::chrono::duration_cast<std::chrono::seconds>(fs::last_write_time("foo") - now).count()) < 3);
2✔
2500
    CHECK_THROWS_AS(fs::last_write_time("bar"), fs::filesystem_error);
6✔
2501
    CHECK_NOTHROW(ft = fs::last_write_time("bar", ec));
2✔
2502
    CHECK(ft == fs::file_time_type::min());
2✔
2503
    CHECK(ec);
2✔
2504
    ec.clear();
2✔
2505
    if (is_symlink_creation_supported()) {
2✔
2506
        std::this_thread::sleep_for(std::chrono::seconds(1));
2✔
2507
        fs::create_symlink("foo", "foo2");
2✔
2508
        ft = fs::last_write_time("foo");
2✔
2509
        // checks that the time of the symlink is fetched
2510
        CHECK(ft == fs::last_write_time("foo2"));
2✔
2511
    }
2512
#ifndef GHC_OS_WEB
2513
    auto nt = timeFromString("2015-10-21T04:30:00");
2✔
2514
    CHECK_NOTHROW(fs::last_write_time(t.path() / "foo", nt));
2✔
2515
    CHECK(std::abs(std::chrono::duration_cast<std::chrono::seconds>(fs::last_write_time("foo") - nt).count()) < 1);
2✔
2516
    nt = timeFromString("2015-10-21T04:29:00");
2✔
2517
    CHECK_NOTHROW(fs::last_write_time("foo", nt, ec));
2✔
2518
    CHECK(std::abs(std::chrono::duration_cast<std::chrono::seconds>(fs::last_write_time("foo") - nt).count()) < 1);
2✔
2519
    CHECK(!ec);
2✔
2520
    CHECK_THROWS_AS(fs::last_write_time("bar", nt), fs::filesystem_error);
6✔
2521
    CHECK_NOTHROW(fs::last_write_time("bar", nt, ec));
2✔
2522
    CHECK(ec);
2✔
2523
#endif
2524
}
2✔
2525

2526
TEST_CASE("fs.op.permissions - permissions", "[filesystem][operations][fs.op.permissions]")
2✔
2527
{
2528
    TemporaryDirectory t(TempOpt::change_path);
4✔
2529
    std::error_code ec;
2✔
2530
    generateFile("foo", 512);
2✔
2531
    auto allWrite = fs::perms::owner_write | fs::perms::group_write | fs::perms::others_write;
2✔
2532
    CHECK_NOTHROW(fs::permissions("foo", allWrite, fs::perm_options::remove));
2✔
2533
    CHECK((fs::status("foo").permissions() & fs::perms::owner_write) != fs::perms::owner_write);
2✔
2534
#if !defined(GHC_OS_WINDOWS)
2535
    if (geteuid() != 0)
2✔
2536
#endif
2537
    {
2538
        CHECK_THROWS_AS(fs::resize_file("foo", 1024), fs::filesystem_error);
6✔
2539
        CHECK(fs::file_size("foo") == 512);
2✔
2540
    }
2541
    CHECK_NOTHROW(fs::permissions("foo", fs::perms::owner_write, fs::perm_options::add));
2✔
2542
    CHECK((fs::status("foo").permissions() & fs::perms::owner_write) == fs::perms::owner_write);
2✔
2543
    CHECK_NOTHROW(fs::resize_file("foo", 2048));
2✔
2544
    CHECK(fs::file_size("foo") == 2048);
2✔
2545
    CHECK_THROWS_AS(fs::permissions("bar", fs::perms::owner_write, fs::perm_options::add), fs::filesystem_error);
6✔
2546
    CHECK_NOTHROW(fs::permissions("bar", fs::perms::owner_write, fs::perm_options::add, ec));
2✔
2547
    CHECK(ec);
2✔
2548
    CHECK_THROWS_AS(fs::permissions("bar", fs::perms::owner_write, static_cast<fs::perm_options>(0)), fs::filesystem_error);
6✔
2549
}
2✔
2550

2551
TEST_CASE("fs.op.proximate - proximate", "[filesystem][operations][fs.op.proximate]")
2✔
2552
{
2553
    std::error_code ec;
2✔
2554
    CHECK(fs::proximate("/a/d", "/a/b/c") == "../../d");
2✔
2555
    CHECK(fs::proximate("/a/d", "/a/b/c", ec) == "../../d");
2✔
2556
    CHECK(!ec);
2✔
2557
    CHECK(fs::proximate("/a/b/c", "/a/d") == "../b/c");
2✔
2558
    CHECK(fs::proximate("/a/b/c", "/a/d", ec) == "../b/c");
2✔
2559
    CHECK(!ec);
2✔
2560
    CHECK(fs::proximate("a/b/c", "a") == "b/c");
2✔
2561
    CHECK(fs::proximate("a/b/c", "a", ec) == "b/c");
2✔
2562
    CHECK(!ec);
2✔
2563
    CHECK(fs::proximate("a/b/c", "a/b/c/x/y") == "../..");
2✔
2564
    CHECK(fs::proximate("a/b/c", "a/b/c/x/y", ec) == "../..");
2✔
2565
    CHECK(!ec);
2✔
2566
    CHECK(fs::proximate("a/b/c", "a/b/c") == ".");
2✔
2567
    CHECK(fs::proximate("a/b/c", "a/b/c", ec) == ".");
2✔
2568
    CHECK(!ec);
2✔
2569
    CHECK(fs::proximate("a/b", "c/d") == "../../a/b");
2✔
2570
    CHECK(fs::proximate("a/b", "c/d", ec) == "../../a/b");
2✔
2571
    CHECK(!ec);
2✔
2572
#ifndef GHC_OS_WINDOWS
2573
    if (has_host_root_name_support()) {
2✔
2574
        CHECK(fs::proximate("//host1/a/d", "//host2/a/b/c") == "//host1/a/d");
2✔
2575
        CHECK(fs::proximate("//host1/a/d", "//host2/a/b/c", ec) == "//host1/a/d");
2✔
2576
        CHECK(!ec);
2✔
2577
    }
2578
#endif
2579
}
2✔
2580

2581
TEST_CASE("fs.op.read_symlink - read_symlink", "[filesystem][operations][fs.op.read_symlink]")
2✔
2582
{
2583
    if (is_symlink_creation_supported()) {
2✔
2584
        TemporaryDirectory t(TempOpt::change_path);
4✔
2585
        std::error_code ec;
2✔
2586
        generateFile("foo");
2✔
2587
        fs::create_symlink(t.path() / "foo", "bar");
2✔
2588
        CHECK(fs::read_symlink("bar") == t.path() / "foo");
2✔
2589
        CHECK(fs::read_symlink("bar", ec) == t.path() / "foo");
2✔
2590
        CHECK(!ec);
2✔
2591
        CHECK_THROWS_AS(fs::read_symlink("foobar"), fs::filesystem_error);
6✔
2592
        CHECK(fs::read_symlink("foobar", ec) == fs::path());
2✔
2593
        CHECK(ec);
2✔
2594
    }
2595
}
2✔
2596

2597
TEST_CASE("fs.op.relative - relative", "[filesystem][operations][fs.op.relative]")
2✔
2598
{
2599
    CHECK(fs::relative("/a/d", "/a/b/c") == "../../d");
2✔
2600
    CHECK(fs::relative("/a/b/c", "/a/d") == "../b/c");
2✔
2601
    CHECK(fs::relative("a/b/c", "a") == "b/c");
2✔
2602
    CHECK(fs::relative("a/b/c", "a/b/c/x/y") == "../..");
2✔
2603
    CHECK(fs::relative("a/b/c", "a/b/c") == ".");
2✔
2604
    CHECK(fs::relative("a/b", "c/d") == "../../a/b");
2✔
2605
    std::error_code ec;
2✔
2606
    CHECK(fs::relative(fs::current_path() / "foo", ec) == "foo");
2✔
2607
    CHECK(!ec);
2✔
2608
}
2✔
2609

2610
TEST_CASE("fs.op.remove - remove", "[filesystem][operations][fs.op.remove]")
2✔
2611
{
2612
    TemporaryDirectory t(TempOpt::change_path);
4✔
2613
    std::error_code ec;
2✔
2614
    generateFile("foo");
2✔
2615
    CHECK(fs::remove("foo"));
2✔
2616
    CHECK(!fs::exists("foo"));
2✔
2617
    CHECK(!fs::remove("foo"));
2✔
2618
    generateFile("foo");
2✔
2619
    CHECK(fs::remove("foo", ec));
2✔
2620
    CHECK(!fs::exists("foo"));
2✔
2621
    if (is_symlink_creation_supported()) {
2✔
2622
        generateFile("foo");
2✔
2623
        fs::create_symlink("foo", "bar");
2✔
2624
        CHECK(fs::exists(fs::symlink_status("bar")));
2✔
2625
        CHECK(fs::remove("bar", ec));
2✔
2626
        CHECK(fs::exists("foo"));
2✔
2627
        CHECK(!fs::exists(fs::symlink_status("bar")));
2✔
2628
    }
2629
    CHECK(!fs::remove("bar"));
2✔
2630
    CHECK(!fs::remove("bar", ec));
2✔
2631
    CHECK(!ec);
2✔
2632
}
2✔
2633

2634
TEST_CASE("fs.op.remove_all - remove_all", "[filesystem][operations][fs.op.remove_all]")
2✔
2635
{
2636
    TemporaryDirectory t(TempOpt::change_path);
4✔
2637
    std::error_code ec;
2✔
2638
    generateFile("foo");
2✔
2639
    CHECK(fs::remove_all("foo", ec) == 1);
2✔
2640
    CHECK(!ec);
2✔
2641
    ec.clear();
2✔
2642
    CHECK(fs::directory_iterator(t.path()) == fs::directory_iterator());
2✔
2643
    fs::create_directories("dir1/dir1a");
2✔
2644
    fs::create_directories("dir1/dir1b");
2✔
2645
    generateFile("dir1/dir1a/f1");
2✔
2646
    generateFile("dir1/dir1b/f2");
2✔
2647
    CHECK_NOTHROW(fs::remove_all("dir1/non-existing", ec));
2✔
2648
    CHECK(!ec);
2✔
2649
    CHECK(fs::remove_all("dir1/non-existing", ec) == 0);
2✔
2650
    if (is_symlink_creation_supported()) {
2✔
2651
        fs::create_directory_symlink("dir1", "dir1link");
2✔
2652
        CHECK(fs::remove_all("dir1link") == 1);
2✔
2653
    }
2654
    CHECK(fs::remove_all("dir1") == 5);
2✔
2655
    CHECK(fs::directory_iterator(t.path()) == fs::directory_iterator());
2✔
2656
}
2✔
2657

2658
TEST_CASE("fs.op.rename - rename", "[filesystem][operations][fs.op.rename]")
2✔
2659
{
2660
    TemporaryDirectory t(TempOpt::change_path);
4✔
2661
    std::error_code ec;
2✔
2662
    generateFile("foo", 123);
2✔
2663
    fs::create_directory("dir1");
2✔
2664
    CHECK_NOTHROW(fs::rename("foo", "bar"));
2✔
2665
    CHECK(!fs::exists("foo"));
2✔
2666
    CHECK(fs::exists("bar"));
2✔
2667
    CHECK_NOTHROW(fs::rename("dir1", "dir2"));
2✔
2668
    CHECK(fs::exists("dir2"));
2✔
2669
    generateFile("foo2", 42);
2✔
2670
    CHECK_NOTHROW(fs::rename("bar", "foo2"));
2✔
2671
    CHECK(fs::exists("foo2"));
2✔
2672
    CHECK(fs::file_size("foo2") == 123u);
2✔
2673
    CHECK(!fs::exists("bar"));
2✔
2674
    CHECK_NOTHROW(fs::rename("foo2", "foo", ec));
2✔
2675
    CHECK(!ec);
2✔
2676
    CHECK_THROWS_AS(fs::rename("foobar", "barfoo"), fs::filesystem_error);
8✔
2677
    CHECK_NOTHROW(fs::rename("foobar", "barfoo", ec));
2✔
2678
    CHECK(ec);
2✔
2679
    CHECK(!fs::exists("barfoo"));
2✔
2680
}
2✔
2681

2682
TEST_CASE("fs.op.resize_file - resize_file", "[filesystem][operations][fs.op.resize_file]")
2✔
2683
{
2684
    TemporaryDirectory t(TempOpt::change_path);
4✔
2685
    std::error_code ec;
2✔
2686
    generateFile("foo", 1024);
2✔
2687
    CHECK(fs::file_size("foo") == 1024);
2✔
2688
    CHECK_NOTHROW(fs::resize_file("foo", 2048));
2✔
2689
    CHECK(fs::file_size("foo") == 2048);
2✔
2690
    CHECK_NOTHROW(fs::resize_file("foo", 1000, ec));
2✔
2691
    CHECK(!ec);
2✔
2692
    CHECK(fs::file_size("foo") == 1000);
2✔
2693
    CHECK_THROWS_AS(fs::resize_file("bar", 2048), fs::filesystem_error);
6✔
2694
    CHECK(!fs::exists("bar"));
2✔
2695
    CHECK_NOTHROW(fs::resize_file("bar", 4096, ec));
2✔
2696
    CHECK(ec);
2✔
2697
    CHECK(!fs::exists("bar"));
2✔
2698
}
2✔
2699

2700
TEST_CASE("fs.op.space - space", "[filesystem][operations][fs.op.space]")
2✔
2701
{
2702
    {
2703
        fs::space_info si;
2704
        CHECK_NOTHROW(si = fs::space(fs::current_path()));
2✔
2705
        CHECK(si.capacity > 1024 * 1024);
2✔
2706
        CHECK(si.capacity > si.free);
2✔
2707
        CHECK(si.free >= si.available);
2✔
2708
    }
2709
    {
2710
        std::error_code ec;
2✔
2711
        fs::space_info si;
2712
        CHECK_NOTHROW(si = fs::space(fs::current_path(), ec));
2✔
2713
        CHECK(si.capacity > 1024 * 1024);
2✔
2714
        CHECK(si.capacity > si.free);
2✔
2715
        CHECK(si.free >= si.available);
2✔
2716
        CHECK(!ec);
2✔
2717
    }
2718
#ifndef GHC_OS_WEB // statvfs under emscripten always returns a result, so this tests would fail
2719
    {
2720
        std::error_code ec;
2✔
2721
        fs::space_info si;
2722
        CHECK_NOTHROW(si = fs::space("foobar42", ec));
2✔
2723
        CHECK(si.capacity == static_cast<uintmax_t>(-1));
2✔
2724
        CHECK(si.free == static_cast<uintmax_t>(-1));
2✔
2725
        CHECK(si.available == static_cast<uintmax_t>(-1));
2✔
2726
        CHECK(ec);
2✔
2727
    }
2728
    CHECK_THROWS_AS(fs::space("foobar42"), fs::filesystem_error);
6✔
2729
#endif
2730
}
2✔
2731

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

2760
TEST_CASE("fs.op.status_known - status_known", "[filesystem][operations][fs.op.status_known]")
2✔
2761
{
2762
    CHECK(!fs::status_known(fs::file_status()));
2✔
2763
    CHECK(fs::status_known(fs::file_status(fs::file_type::not_found)));
2✔
2764
    CHECK(fs::status_known(fs::file_status(fs::file_type::regular)));
2✔
2765
    CHECK(fs::status_known(fs::file_status(fs::file_type::directory)));
2✔
2766
    CHECK(fs::status_known(fs::file_status(fs::file_type::symlink)));
2✔
2767
    CHECK(fs::status_known(fs::file_status(fs::file_type::character)));
2✔
2768
    CHECK(fs::status_known(fs::file_status(fs::file_type::fifo)));
2✔
2769
    CHECK(fs::status_known(fs::file_status(fs::file_type::socket)));
2✔
2770
    CHECK(fs::status_known(fs::file_status(fs::file_type::unknown)));
2✔
2771
}
2✔
2772

2773
TEST_CASE("fs.op.symlink_status - symlink_status", "[filesystem][operations][fs.op.symlink_status]")
2✔
2774
{
2775
    TemporaryDirectory t(TempOpt::change_path);
4✔
2776
    std::error_code ec;
2✔
2777
    fs::file_status fs;
4✔
2778
    CHECK_NOTHROW(fs = fs::symlink_status("foo"));
2✔
2779
    CHECK(fs.type() == fs::file_type::not_found);
2✔
2780
    CHECK(fs.permissions() == fs::perms::unknown);
2✔
2781
    CHECK_NOTHROW(fs = fs::symlink_status("bar", ec));
2✔
2782
    CHECK(fs.type() == fs::file_type::not_found);
2✔
2783
    CHECK(fs.permissions() == fs::perms::unknown);
2✔
2784
    CHECK(ec);
2✔
2785
    ec.clear();
2✔
2786
    fs = fs::symlink_status(t.path());
2✔
2787
    CHECK(fs.type() == fs::file_type::directory);
2✔
2788
    CHECK((fs.permissions() & (fs::perms::owner_read | fs::perms::owner_write)) == (fs::perms::owner_read | fs::perms::owner_write));
2✔
2789
    generateFile("foobar");
2✔
2790
    fs = fs::symlink_status(t.path() / "foobar");
2✔
2791
    CHECK(fs.type() == fs::file_type::regular);
2✔
2792
    CHECK((fs.permissions() & (fs::perms::owner_read | fs::perms::owner_write)) == (fs::perms::owner_read | fs::perms::owner_write));
2✔
2793
    if (is_symlink_creation_supported()) {
2✔
2794
        fs::create_symlink(t.path() / "foobar", t.path() / "barfoo");
2✔
2795
        fs = fs::symlink_status(t.path() / "barfoo");
2✔
2796
        CHECK(fs.type() == fs::file_type::symlink);
2✔
2797
    }
2798
}
2✔
2799

2800
TEST_CASE("fs.op.temp_dir_path - temporary_directory_path", "[filesystem][operations][fs.op.temp_dir_path]")
2✔
2801
{
2802
    std::error_code ec;
2✔
2803
    CHECK_NOTHROW(fs::exists(fs::temp_directory_path()));
2✔
2804
    CHECK_NOTHROW(fs::exists(fs::temp_directory_path(ec)));
2✔
2805
    CHECK(!fs::temp_directory_path().empty());
2✔
2806
    CHECK(!ec);
2✔
2807
}
2✔
2808

2809
TEST_CASE("fs.op.weakly_canonical - weakly_canonical", "[filesystem][operations][fs.op.weakly_canonical]")
2✔
2810
{
2811
    INFO("This might fail on std::implementations that return fs::current_path() for fs::canonical(\"\")");
4✔
2812
    CHECK(fs::weakly_canonical("") == ".");
2✔
2813
    if(fs::weakly_canonical("") == ".") {
2✔
2814
        CHECK(fs::weakly_canonical("foo/bar") == "foo/bar");
2✔
2815
        CHECK(fs::weakly_canonical("foo/./bar") == "foo/bar");
2✔
2816
        CHECK(fs::weakly_canonical("foo/../bar") == "bar");
2✔
2817
    }
2818
    else {
2819
        CHECK(fs::weakly_canonical("foo/bar") == fs::current_path() / "foo/bar");
×
2820
        CHECK(fs::weakly_canonical("foo/./bar") == fs::current_path() / "foo/bar");
×
2821
        CHECK(fs::weakly_canonical("foo/../bar") == fs::current_path() / "bar");
×
2822
    }
2823

2824
    {
2825
        TemporaryDirectory t(TempOpt::change_path);
4✔
2826
        auto dir = t.path() / "d0";
4✔
2827
        fs::create_directories(dir / "d1");
2✔
2828
        generateFile(dir / "f0");
2✔
2829
        fs::path rel(dir.filename());
4✔
2830
        CHECK(fs::weakly_canonical(dir) == dir);
2✔
2831
        CHECK(fs::weakly_canonical(rel) == dir);
2✔
2832
        CHECK(fs::weakly_canonical(dir / "f0") == dir / "f0");
2✔
2833
        CHECK(fs::weakly_canonical(dir / "f0/") == dir / "f0/");
2✔
2834
        CHECK(fs::weakly_canonical(dir / "f1") == dir / "f1");
2✔
2835
        CHECK(fs::weakly_canonical(rel / "f0") == dir / "f0");
2✔
2836
        CHECK(fs::weakly_canonical(rel / "f0/") == dir / "f0/");
2✔
2837
        CHECK(fs::weakly_canonical(rel / "f1") == dir / "f1");
2✔
2838
        CHECK(fs::weakly_canonical(rel / "./f0") == dir / "f0");
2✔
2839
        CHECK(fs::weakly_canonical(rel / "./f1") == dir / "f1");
2✔
2840
        CHECK(fs::weakly_canonical(rel / "d1/../f0") == dir / "f0");
2✔
2841
        CHECK(fs::weakly_canonical(rel / "d1/../f1") == dir / "f1");
2✔
2842
        CHECK(fs::weakly_canonical(rel / "d1/../f1/../f2") == dir / "f2");
2✔
2843
    }
2844
}
2✔
2845

2846
TEST_CASE("std::string_view support", "[filesystem][fs.string_view]")
2✔
2847
{
2848
#if defined(GHC_HAS_STD_STRING_VIEW) || defined(GHC_HAS_STD_EXPERIMENTAL_STRING_VIEW)
2849

2850
#if defined(GHC_HAS_STD_STRING_VIEW)
2851
    using namespace std::literals;
2852
    using string_view = std::string_view;
2853
    using wstring_view = std::wstring_view;
2854
#elif defined(GHC_HAS_STD_EXPERIMENTAL_STRING_VIEW)
2855
    using string_view = std::experimental::string_view;
2856
    using wstring_view = std::experimental::wstring_view;
2857
#endif
2858

2859
    {
2860
        std::string p("foo/bar");
4✔
2861
        string_view sv(p);
2✔
2862
        CHECK(fs::path(sv, fs::path::format::generic_format).generic_string() == "foo/bar");
2✔
2863
        fs::path p2("fo");
4✔
2864
        p2 += string_view("o");
2✔
2865
        CHECK(p2 == "foo");
2✔
2866
        CHECK(p2.compare(string_view("foo")) == 0);
2✔
2867
    }
2868
    {
2869
        auto p = fs::path{"XYZ"};
4✔
2870
        p /= string_view("Appendix");
2✔
2871
        CHECK(p == "XYZ/Appendix");
2✔
2872
    }
2873
    {
2874
        std::wstring p(L"foo/bar");
4✔
2875
        wstring_view sv(p);
2✔
2876
        CHECK(fs::path(sv, fs::path::format::generic_format).generic_string() == "foo/bar");
2✔
2877
        fs::path p2(L"fo");
4✔
2878
        p2 += wstring_view(L"o");
2✔
2879
        CHECK(p2 == "foo");
2✔
2880
        CHECK(p2.compare(wstring_view(L"foo")) == 0);
2✔
2881
    }
2882

2883
#else
2884
    WARN("std::string_view specific tests are empty without std::string_view.");
2885
#endif
2886
}
2✔
2887

2888
TEST_CASE("Windows: Long filename support", "[filesystem][path][fs.path.win.long]")
2✔
2889
{
2890
#ifdef GHC_OS_WINDOWS
2891
    TemporaryDirectory t(TempOpt::change_path);
2892
    char c = 'A';
2893
    fs::path dir{"\\\\?\\"};
2894
    dir += fs::current_path().u8string();
2895
    for (; c <= 'Z'; ++c) {
2896
        std::string part = std::string(16, c);
2897
        dir /= part;
2898
        CHECK_NOTHROW(fs::create_directory(dir));
2899
        CHECK(fs::exists(dir));
2900
        generateFile(dir / "f0");
2901
        REQUIRE(fs::exists(dir / "f0"));
2902
    }
2903
    CHECK(c > 'Z');
2904
    fs::remove_all(fs::current_path() / std::string(16, 'A'));
2905
    CHECK(!fs::exists(fs::current_path() / std::string(16, 'A')));
2906
    CHECK_NOTHROW(fs::create_directories(dir));
2907
    CHECK(fs::exists(dir));
2908
    generateFile(dir / "f0");
2909
    CHECK(fs::exists(dir / "f0"));
2910
#else
2911
    WARN("Windows specific tests are empty on non-Windows systems.");
2✔
2912
#endif
2913
}
2✔
2914

2915
TEST_CASE("Windows: path namespace handling", "[filesystem][path][fs.path.win.namespaces]")
2✔
2916
{
2917
#ifdef GHC_OS_WINDOWS
2918
    {
2919
        std::error_code ec;
2920
        fs::path p(R"(\\localhost\c$\Windows)");
2921
        auto symstat = fs::symlink_status(p, ec);
2922
        CHECK(!ec);
2923
        auto p2 = fs::canonical(p, ec);
2924
        CHECK(!ec);
2925
        CHECK(p2 == p);
2926
    }
2927
    
2928
    struct TestInfo
2929
    {
2930
        std::string _path;
2931
        std::string _string;
2932
        std::string _rootName;
2933
        std::string _rootPath;
2934
        std::string _iterateResult;
2935
    };
2936
    std::vector<TestInfo> variants = {
2937
        {R"(C:\Windows\notepad.exe)", R"(C:\Windows\notepad.exe)", "C:", "C:\\", "C:,/,Windows,notepad.exe"},
2938
#ifdef USE_STD_FS
2939
        {R"(\\?\C:\Windows\notepad.exe)", R"(\\?\C:\Windows\notepad.exe)", "\\\\?", "\\\\?\\", "//?,/,C:,Windows,notepad.exe"},
2940
        {R"(\??\C:\Windows\notepad.exe)", R"(\??\C:\Windows\notepad.exe)", "\\??", "\\??\\", "/??,/,C:,Windows,notepad.exe"},
2941
#else
2942
        {R"(\\?\C:\Windows\notepad.exe)", R"(\\?\C:\Windows\notepad.exe)", "C:", "C:\\", "//?/,C:,/,Windows,notepad.exe"},
2943
        {R"(\??\C:\Windows\notepad.exe)", R"(\??\C:\Windows\notepad.exe)", "C:", "C:\\", "/?\?/,C:,/,Windows,notepad.exe"},
2944
#endif
2945
        {R"(\\.\C:\Windows\notepad.exe)", R"(\\.\C:\Windows\notepad.exe)", "\\\\.", "\\\\.\\", "//.,/,C:,Windows,notepad.exe"},
2946
        {R"(\\?\HarddiskVolume1\Windows\notepad.exe)", R"(\\?\HarddiskVolume1\Windows\notepad.exe)", "\\\\?", "\\\\?\\", "//?,/,HarddiskVolume1,Windows,notepad.exe"},
2947
        {R"(\\?\Harddisk0Partition1\Windows\notepad.exe)", R"(\\?\Harddisk0Partition1\Windows\notepad.exe)", "\\\\?", "\\\\?\\", "//?,/,Harddisk0Partition1,Windows,notepad.exe"},
2948
        {R"(\\.\GLOBALROOT\Device\HarddiskVolume1\Windows\notepad.exe)", R"(\\.\GLOBALROOT\Device\HarddiskVolume1\Windows\notepad.exe)", "\\\\.", "\\\\.\\", "//.,/,GLOBALROOT,Device,HarddiskVolume1,Windows,notepad.exe"},
2949
        {R"(\\?\GLOBALROOT\Device\Harddisk0\Partition1\Windows\notepad.exe)", R"(\\?\GLOBALROOT\Device\Harddisk0\Partition1\Windows\notepad.exe)", "\\\\?", "\\\\?\\", "//?,/,GLOBALROOT,Device,Harddisk0,Partition1,Windows,notepad.exe"},
2950
        {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"},
2951
        {R"(\\LOCALHOST\C$\Windows\notepad.exe)", R"(\\LOCALHOST\C$\Windows\notepad.exe)", "\\\\LOCALHOST", "\\\\LOCALHOST\\", "//LOCALHOST,/,C$,Windows,notepad.exe"},
2952
        {R"(\\?\UNC\C$\Windows\notepad.exe)", R"(\\?\UNC\C$\Windows\notepad.exe)", "\\\\?", "\\\\?\\", "//?,/,UNC,C$,Windows,notepad.exe"},
2953
        {R"(\\?\GLOBALROOT\Device\Mup\C$\Windows\notepad.exe)", R"(\\?\GLOBALROOT\Device\Mup\C$\Windows\notepad.exe)", "\\\\?", "\\\\?\\", "//?,/,GLOBALROOT,Device,Mup,C$,Windows,notepad.exe"},
2954
    };
2955

2956
    for (auto ti : variants) {
2957
        INFO("Used path: " + ti._path);
2958
        auto p = fs::path(ti._path);
2959
        CHECK(p.string() == ti._string);
2960
        CHECK(p.is_absolute());
2961
        CHECK(p.root_name().string() == ti._rootName);
2962
        CHECK(p.root_path().string() == ti._rootPath);
2963
        CHECK(iterateResult(p) == ti._iterateResult);
2964
    }
2965
#else
2966
    WARN("Windows specific tests are empty on non-Windows systems.");
2✔
2967
#endif
2968
}
2✔
2969

2970
TEST_CASE("Windows: Mapped folders handling ", "[filesystem][fs.win][fs.win.mapped]")
2✔
2971
{
2972
#ifdef GHC_OS_WINDOWS
2973
    // this test expects a mapped volume on C:\\fs-test as is the case on the development test system
2974
    // does nothing on other systems
2975
    if (fs::exists("C:\\fs-test")) {
2976
        CHECK(fs::canonical("C:\\fs-test\\Test.txt").string() == "C:\\fs-test\\Test.txt");
2977
    }
2978
#else
2979
    WARN("Windows specific tests are empty on non-Windows systems.");
2✔
2980
#endif
2981
}
2✔
2982

2983
TEST_CASE("Windows: Deletion of Read-only Files", "[filesystem][fs.win][fs.win.remove]")
2✔
2984
{
2985
#ifdef GHC_OS_WINDOWS
2986
    TemporaryDirectory t(TempOpt::change_path);
2987
    std::error_code ec;
2988
    generateFile("foo", 512);
2989
    auto allWrite = fs::perms::owner_write | fs::perms::group_write | fs::perms::others_write;
2990
    CHECK_NOTHROW(fs::permissions("foo", allWrite, fs::perm_options::remove));
2991
    CHECK_NOTHROW(fs::remove("foo"));
2992
    CHECK(!fs::exists("foo"));
2993
#else
2994
    WARN("Windows specific tests are empty on non-Windows systems.");
2✔
2995
#endif
2996
}
2✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc