aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorLexi Winter <lexi@le-fay.org>2025-06-21 15:23:05 +0100
committerLexi Winter <lexi@le-fay.org>2025-06-21 15:23:05 +0100
commit75c6b5fee029ec95e7e45e18525e3e78b9616f48 (patch)
tree7ac4e4b8e2e09eb9151e0d756e0173c47bc64484 /tests
parentca90e97a9da4457790262c53515cb5ab58a2ce4f (diff)
downloadnihil-75c6b5fee029ec95e7e45e18525e3e78b9616f48.tar.gz
nihil-75c6b5fee029ec95e7e45e18525e3e78b9616f48.tar.bz2
add spawn
Diffstat (limited to 'tests')
-rw-r--r--tests/CMakeLists.txt1
-rw-r--r--tests/spawn.cc65
2 files changed, 66 insertions, 0 deletions
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index 13974aa..abeab88 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -10,6 +10,7 @@ add_executable(nihil.test
guard.cc
next_word.cc
skipws.cc
+ spawn.cc
tabulate.cc)
target_link_libraries(nihil.test PRIVATE
diff --git a/tests/spawn.cc b/tests/spawn.cc
new file mode 100644
index 0000000..455223e
--- /dev/null
+++ b/tests/spawn.cc
@@ -0,0 +1,65 @@
+/*
+ * This source code is released into the public domain.
+ */
+
+#include <catch2/catch_test_macros.hpp>
+
+import nihil;
+
+TEST_CASE("spawn: system", "[spawn]") {
+ using namespace nihil;
+ auto output = std::string();
+ auto result = spawn(shell("x=1; echo $x"),
+ capture(stdout_fileno, output)).wait();
+
+ REQUIRE(result.okay());
+ REQUIRE(output == "1\n");
+}
+
+TEST_CASE("spawn: execv", "[spawn]") {
+ using namespace nihil;
+ auto output = std::string();
+ auto args = argv::from_args({"sh", "-c", "x=1; echo $x"});
+ auto result = spawn(execv("/bin/sh", std::move(args)),
+ capture(stdout_fileno, output)).wait();
+
+ REQUIRE(result.okay());
+ REQUIRE(output == "1\n");
+}
+
+TEST_CASE("spawn: execvp", "[spawn]") {
+ using namespace nihil;
+ auto output = std::string();
+ auto args = argv::from_args({"sh", "-c", "x=1; echo $x"});
+ auto result = spawn(execvp("sh", std::move(args)),
+ capture(stdout_fileno, output)).wait();
+
+ REQUIRE(result.okay());
+ REQUIRE(output == "1\n");
+}
+
+TEST_CASE("spawn: execl", "[spawn]") {
+ using namespace nihil;
+ auto output = std::string();
+ auto result = spawn(execl("/bin/sh", "sh", "-c", "x=1; echo $x"),
+ capture(stdout_fileno, output)).wait();
+
+ REQUIRE(result.okay());
+ REQUIRE(output == "1\n");
+}
+
+TEST_CASE("spawn: execlp", "[spawn]") {
+ using namespace nihil;
+ auto output = std::string();
+ auto result = spawn(execlp("sh", "sh", "-c", "x=1; echo $x"),
+ capture(stdout_fileno, output)).wait();
+
+ REQUIRE(result.okay());
+ REQUIRE(output == "1\n");
+}
+
+TEST_CASE("spawn: execlp failure", "[spawn]") {
+ using namespace nihil;
+ REQUIRE_THROWS_AS(execlp("lfjail_nonesuch_executable", "x"),
+ executable_not_found);
+}