aboutsummaryrefslogtreecommitdiffstats
path: root/nihil.posix/find_in_path.test.cc
diff options
context:
space:
mode:
authorLexi Winter <lexi@le-fay.org>2025-06-30 09:10:16 +0100
committerLexi Winter <lexi@le-fay.org>2025-06-30 09:10:16 +0100
commit36427c0966faa7aecd586b397ed9b845f18172f5 (patch)
tree0d86c7ca9ac9e2e768b54d491bf596fb84f5e45a /nihil.posix/find_in_path.test.cc
parent034cd404a129103a8dd7747e6bd00ffd5550da93 (diff)
downloadnihil-36427c0966faa7aecd586b397ed9b845f18172f5.tar.gz
nihil-36427c0966faa7aecd586b397ed9b845f18172f5.tar.bz2
more refactoring; add stat()
Diffstat (limited to 'nihil.posix/find_in_path.test.cc')
-rw-r--r--nihil.posix/find_in_path.test.cc70
1 files changed, 70 insertions, 0 deletions
diff --git a/nihil.posix/find_in_path.test.cc b/nihil.posix/find_in_path.test.cc
new file mode 100644
index 0000000..b2f6240
--- /dev/null
+++ b/nihil.posix/find_in_path.test.cc
@@ -0,0 +1,70 @@
+// This source code is released into the public domain.
+
+#include <catch2/catch_test_macros.hpp>
+
+import nihil.error;
+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