XS-libunievent

 view release on metacpan or  search on metacpan

libunievent/tests/fs.cc  view on Meta::CPAN

        CHECK(Fs::stat(file).value().size == 0);
    }

    if (!win32) // it seems win32 ignores chmod
    SECTION("chmod") {
        Fs::touch(file, 0644);
        SECTION("path") {
            Fs::chmod(file, 0666);
            CHECK(Fs::stat(file).value().perms() == 0666);
        }
        SECTION("fd") {
            auto fd = Fs::open(file, Fs::OpenFlags::RDONLY).value();
            Fs::chmod(fd, 0600);
            CHECK(Fs::stat(file).value().perms() == 0600);
            Fs::close(fd);
        }
    }

    SECTION("touch") {
        SECTION("non-existant") {
            CHECK(Fs::touch(file));
            CHECK(Fs::isfile(file));
        }
        SECTION("exists") {
            CHECK(Fs::touch(file));
            auto s = Fs::stat(file).value();
            auto mtime = s.mtime;
            auto atime = s.atime;
            std::this_thread::sleep_for(std::chrono::milliseconds(1));
            CHECK(Fs::touch(file));
            CHECK(Fs::isfile(file));
            s = Fs::stat(file).value();
            CHECK(s.mtime > mtime);
            CHECK(s.atime > atime);
        }
    }

    SECTION("utime") {
        SECTION("non-existant") {
            auto ret = Fs::utime(file, 1000, 1000);
            REQUIRE(!ret);
            CHECK(ret.error() == std::errc::no_such_file_or_directory);
        }
        SECTION("path") {
            Fs::touch(file);
            CHECK(Fs::utime(file, 1000, 1000));
            CHECK(Fs::stat(file).value().atime.get() == 1000);
            CHECK(Fs::stat(file).value().mtime.get() == 1000);
        }
        if (!win32) // win32 can't set utime via descriptor
            SECTION("fd") {
            Fs::touch(file);
            auto fd = Fs::open(file, Fs::OpenFlags::RDONLY).value();
            CHECK(Fs::utime(fd, 2000, 2000));
            Fs::close(fd);
            CHECK(Fs::stat(file).value().atime.get() == 2000);
            CHECK(Fs::stat(file).value().mtime.get() == 2000);
        }
    }

    // no tests for chown

    SECTION("rename") {
        SECTION("non-existant") {
            Fs::touch(file2);
            auto ret = Fs::rename(file, file2);
            REQUIRE(!ret);
            CHECK(ret.error() == std::errc::no_such_file_or_directory);
        }
        SECTION("exists file") {
            Fs::touch(file);
            CHECK(Fs::rename(file, file2));
            CHECK(Fs::isfile(file2));
        }
        SECTION("exists dir") {
            Fs::mkdir(dir);
            CHECK(Fs::rename(dir, dir2));
            CHECK(Fs::isdir(dir2));
        }
    }
}

TEST_CASE("fs-async", "[fs]") {
    VarDir vdir;
    AsyncTest test(10000, 1);
    auto l       = test.loop;
    auto p       = [&](string_view s) { return vdir.path(s); };
    auto file    = p("file");
    auto file2   = p("file2");
    auto dir     = p("dir");
    auto dir2    = p("dir2");
    auto success = [&](auto err, auto) { CHECK(!err); test.happens(); };
    auto fail    = [&](auto err, auto) { CHECK(err); test.happens(); };

    SECTION("mkdir") {
        SECTION("ok") {
            Fs::mkdir(dir, 0755, success, l);
            l->run();
            CHECK(Fs::isdir(dir));
        }
        SECTION("err") {
            Fs::mkdir(dir);
            Fs::mkdir(dir, 0755, [&](auto& err, auto) {
                test.happens();
                CHECK(err == std::errc::file_exists);
            }, l);
        }
    }

    SECTION("rmdir") {
        SECTION("err") {
            Fs::rmdir(dir, [&](auto& err, auto) {
                test.happens();
                CHECK(err == std::errc::no_such_file_or_directory);
            }, l);
        }
        SECTION("ok") {
            Fs::mkdir(dir);
            Fs::rmdir(dir, success, l);
            l->run();
            CHECK(!Fs::exists(dir));

libunievent/tests/fs.cc  view on Meta::CPAN

        CHECK(Fs::stat(file).value().size == 5);

        Fs::truncate(file, 0, success, l);
        l->run();
        CHECK(Fs::stat(file).value().size == 0);
    }

    if (!win32)
    SECTION("chmod") {
        Fs::touch(file, 0644);
        SECTION("path") {
            Fs::chmod(file, 0666, success, l);
            l->run();
            CHECK(Fs::stat(file).value().perms() == 0666);
        }
        SECTION("fd") {
            auto fd = Fs::open(file, Fs::OpenFlags::RDONLY).value();
            Fs::chmod(fd, 0600, success, l);
            l->run();
            CHECK(Fs::stat(file).value().perms() == 0600);
            Fs::close(fd);
        }
    }

    SECTION("touch") {
        test.set_expected(2);
        Fs::touch(file, 0644, success, l);
        l->run();
        auto s = Fs::stat(file).value();
        auto mtime = s.mtime;
        auto atime = s.atime;
        std::this_thread::sleep_for(std::chrono::milliseconds(1));

        Fs::touch(file, 0644, success, l);
        l->run();
        CHECK(Fs::isfile(file));
        s = Fs::stat(file).value();
        CHECK(s.mtime > mtime);
        CHECK(s.atime > atime);
    }

    SECTION("utime") {
        Fs::touch(file);
        SECTION("path") {
            Fs::utime(file, 1000, 1000, success, l);
            l->run();
            CHECK(Fs::stat(file).value().atime.get() == 1000);
            CHECK(Fs::stat(file).value().mtime.get() == 1000);
        }
        if (!win32)
        SECTION("fd") {
            auto fd = Fs::open(file, Fs::OpenFlags::RDONLY).value();
            Fs::utime(fd, 2000, 2000, success, l);
            l->run();
            Fs::close(fd);
            CHECK(Fs::stat(file).value().atime.get() == 2000);
            CHECK(Fs::stat(file).value().mtime.get() == 2000);
        }
    }

    // no tests for chown

    SECTION("rename") {
        Fs::touch(file);
        Fs::rename(file, file2, success, l);
        l->run();
        CHECK(!Fs::exists(file));
        CHECK(Fs::isfile(file2));
    }

    l->run();
}



( run in 0.619 second using v1.01-cache-2.11-cpan-5511b514fd6 )