From 36427c0966faa7aecd586b397ed9b845f18172f5 Mon Sep 17 00:00:00 2001 From: Lexi Winter Date: Mon, 30 Jun 2025 09:10:16 +0100 Subject: more refactoring; add stat() --- nihil.posix/stat.test.cc | 59 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 nihil.posix/stat.test.cc (limited to 'nihil.posix/stat.test.cc') diff --git a/nihil.posix/stat.test.cc b/nihil.posix/stat.test.cc new file mode 100644 index 0000000..cf1e29c --- /dev/null +++ b/nihil.posix/stat.test.cc @@ -0,0 +1,59 @@ +// This source code is released into the public domain. + +import nihil.error; +import nihil.posix; + +#include + +#include + +namespace { + +SCENARIO("nihil::stat() on an existing file") +{ + GIVEN("A call to stat() on /etc/passwd") + { + auto sb = nihil::stat("/etc/passwd"); + + THEN("The returned struct is correct") + { + REQUIRE(sb); + REQUIRE(S_ISREG(sb->st_mode)); + } + } +} + +SCENARIO("nihil::stat() on a non-existent file") +{ + GIVEN("A call to stat() on /nonesuchfile") + { + auto sb = nihil::stat("/nonesuchfile"); + + THEN("std::errc::no_such_file_or_directory is returned") + { + REQUIRE(!sb); + REQUIRE(sb.error() == std::errc::no_such_file_or_directory); + } + } +} + +SCENARIO("nihil::stat() on an open file descriptor") +{ + GIVEN("An fd referring to /etc/password") + { + auto fd = nihil::open("/etc/passwd", nihil::open_read).value(); + + WHEN("nihil::stat() is called on the fd") + { + auto sb = nihil::stat(fd); + + THEN("The returned struct is correct") + { + REQUIRE(sb); + REQUIRE(S_ISREG(sb->st_mode)); + } + } + } +} + +} // anonymous namespace -- cgit v1.2.3