aboutsummaryrefslogtreecommitdiffstats
path: root/nihil.posix/find_in_path.test.cc
blob: 2c07ce2f9b910e401aec487b11c74ce8a2b2b1f5 (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
61
62
63
64
65
66
67
68
69
70
71
// This source code is released into the public domain.

#include <catch2/catch_test_macros.hpp>

import nihil.std;
import nihil.core;
import nihil.posix;

namespace {

SCENARIO("nihil::find_in_path can find /bin/sh in the default path")
{
	GIVEN ("A call to find_in_path for /bin/sh") {
		auto ret = nihil::find_in_path("sh");

		THEN ("The return value should be /bin/sh") {
			REQUIRE(ret);
			REQUIRE(ret.value() == "/bin/sh");
		}
	}
}

SCENARIO("nihil::find_in_path works with an explicit path")
{
	GIVEN ("A call to find_in_path with an explicit path") {
		auto ret = nihil::find_in_path("sh", "/bin:/usr/bin");

		THEN ("The return value should be /bin/sh") {
			REQUIRE(ret);
			REQUIRE(ret.value() == "/bin/sh");
		}
	}
}

SCENARIO("nihil::find_in_path works with an absolute path")
{
	GIVEN ("A call to find_in_path with an absolute filename and an empty path") {
		auto ret = nihil::find_in_path("/bin/sh", "");

		THEN ("The return value should be /bin/sh") {
			REQUIRE(ret);
			REQUIRE(ret.value() == "/bin/sh");
		}
	}
}

SCENARIO("nihil::find_in_path returns ENOENT when the executable is not found")
{
	GIVEN ("A call to find_in_path for a non-existent executable") {
		auto ret = nihil::find_in_path("nihil_no_such_executable");

		THEN ("The return value should be std::errc::no_such_file_or_directory") {
			REQUIRE(!ret);
			REQUIRE(ret.error() == std::errc::no_such_file_or_directory);
		}
	}
}

SCENARIO("nihil::find_in_path returns errors from access(2)")
{
	GIVEN ("A call to find_in_path for a non-executable file") {
		auto ret = nihil::find_in_path("passwd", "/etc");

		THEN ("The return value should be std::errc::permission_denied") {
			REQUIRE(!ret);
			REQUIRE(ret.error() == std::errc::permission_denied);
		}
	}
}

} // anonymous namespace