UniEvent

 view release on metacpan or  search on metacpan

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

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

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

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

// no tests for chown

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

TEST("mkdtemp") {
    Test t;
    auto ret = Fs::mkdtemp(t.path("tmpXXXXXX")).value();
    CHECK(ret != "");
    CHECK(Fs::isdir(ret));
}

TEST("mkstemp") {
    Test t;
    auto ret = Fs::mkstemp(t.path("tmpXXXXXX")).value();
    CHECK(ret.path != "");
    CHECK(Fs::exists(ret.path));
    Fs::write(ret.fd, "hello world");
    Fs::close(ret.fd);
    CHECK(Fs::stat(ret.path).value().size == 11);
}

}

namespace test_async {

TEST_PREFIX("fs-async: ", "[fs]");

TEST("fs request") {
    Test t(2000);
    auto req = Fs::mkdtemp(t.path("tmpXXXXXX"), [](auto&&...){}, t.loop);
    CHECK(req->active());
    t.run();
    CHECK_FALSE(req->active());
}

TEST("mkdir") {
    Test t(10000, 1);
    SECTION("ok") {
        Fs::mkdir(t.dir, 0755, t.success, t.loop);
        t.run();
        CHECK(Fs::isdir(t.dir));
    }
    SECTION("err") {

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

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

TEST("chmod") {
    if (win32) return;
    Test t(10000, 1);
    Fs::touch(t.file, 0644);
    SECTION("path") {
        Fs::chmod(t.file, 0666, t.success, t.loop);
        t.run();
        CHECK(Fs::stat(t.file).value().perms() == 0666);
    }
    SECTION("fd") {
        auto fd = Fs::open(t.file, Fs::OpenFlags::RDONLY).value();
        Fs::chmod(fd, 0600, t.success, t.loop);
        t.run();
        CHECK(Fs::stat(t.file).value().perms() == 0600);
        Fs::close(fd);
    }
}

TEST("touch") {
    Test t(10000, 1);
    t.set_expected(2);
    Fs::touch(t.file, 0644, t.success, t.loop);
    t.run();
    auto s = Fs::stat(t.file).value();
    auto mtime = s.mtime;
    auto atime = s.atime;
    std::this_thread::sleep_for(std::chrono::milliseconds(1));

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

TEST("utime") {
    Test t(10000, 1);
    Fs::touch(t.file);
    SECTION("path") {
        Fs::utime(t.file, 1000, 1000, t.success, t.loop);
        t.run();
        CHECK(Fs::stat(t.file).value().atime.get() == 1000);
        CHECK(Fs::stat(t.file).value().mtime.get() == 1000);
    }
    if (!win32)
    SECTION("fd") {
        auto fd = Fs::open(t.file, Fs::OpenFlags::RDONLY).value();
        Fs::utime(fd, 2000, 2000, t.success, t.loop);
        t.run();
        Fs::close(fd);
        CHECK(Fs::stat(t.file).value().atime.get() == 2000);
        CHECK(Fs::stat(t.file).value().mtime.get() == 2000);
    }
}

// no tests for chown

TEST("rename") {
    Test t(10000, 1);
    Fs::touch(t.file);
    Fs::rename(t.file, t.file2, t.success, t.loop);
    t.run();
    CHECK(!Fs::exists(t.file));
    CHECK(Fs::isfile(t.file2));
}

TEST("mkdtemp") {
    Test t(2000, 1);
    Fs::mkdtemp(t.path("tmpXXXXXX"), [&](auto& path, auto& err, auto&){
        REQUIRE(!err);
        CHECK(path != "");
        CHECK(Fs::isdir(path));
        t.happens();
    }, t.loop);
    t.run();
}

TEST("mkstemp") {
    Test t(2000, 1);
    Fs::mkstemp(t.path("tmpXXXXXX"), [&](auto& path, auto fd, auto& err, auto&){
        REQUIRE(!err);
        CHECK(path != "");
        CHECK(Fs::exists(path));
        t.happens();

        Fs::write(fd, "hello world");
        Fs::close(fd);
        CHECK(Fs::stat(path).value().size == 11);
    }, t.loop);
    t.run();
}

}



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