aboutsummaryrefslogtreecommitdiffstats
path: root/nihil.posix/stat.test.cc
blob: cb199f67dd4f01d282a52c40a11c0995ffaec092 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// This source code is released into the public domain.

import nihil.std;
import nihil.posix;
import nihil.util;

#include <sys/stat.h>

#include <catch2/catch_test_macros.hpp>

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