From 034cd404a129103a8dd7747e6bd00ffd5550da93 Mon Sep 17 00:00:00 2001 From: Lexi Winter Date: Mon, 30 Jun 2025 07:51:23 +0100 Subject: refactoring --- nihil.posix/execlp.test.cc | 67 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 nihil.posix/execlp.test.cc (limited to 'nihil.posix/execlp.test.cc') diff --git a/nihil.posix/execlp.test.cc b/nihil.posix/execlp.test.cc new file mode 100644 index 0000000..cedf871 --- /dev/null +++ b/nihil.posix/execlp.test.cc @@ -0,0 +1,67 @@ +/* + * This source code is released into the public domain. + */ + +#include + +import nihil.posix; + +namespace { + +SCENARIO("nihil::execlp() can be used to spawn a shell") +{ + GIVEN("An execlp object") + { + auto exec = nihil::execlp("sh", "sh", "-c", "x=1; echo $x"); + + THEN("sh was found in $PATH") + { + if (!exec) + FAIL(exec.error()); + } + + WHEN("The shell is executed") + { + auto output = std::string(); + auto capture = nihil::make_capture(nihil::stdout_fileno, output).value(); + auto status = nihil::spawn(exec.value(), capture).value().wait().value(); + + THEN("The exit code is 0") + { + REQUIRE(status.status() == 0); + } + AND_THEN("The expected output was captured") + { + REQUIRE(output == "1\n"); + } + } + } +} + +SCENARIO("nihil::execlp() returns the shell's exit code") +{ + GIVEN("An execlp object") + { + auto exec = nihil::execlp("sh", "sh", "-c", "x=42; exit $x"); + + THEN("sh was found in $PATH") + { + if (!exec) + FAIL(exec.error()); + } + + WHEN("The shell is executed") + { + auto output = std::string(); + auto capture = nihil::make_capture(nihil::stdout_fileno, output).value(); + auto status = nihil::spawn(exec.value(), capture).value().wait().value(); + + THEN("The exit code is 1") + { + REQUIRE(status.status() == 42); + } + } + } +} + +} // anonymous namespace -- cgit v1.2.3