From 79fb1d9f58d15cb3704a62359a7457451cbc0164 Mon Sep 17 00:00:00 2001 From: Austin Horstman Date: Mon, 9 Feb 2026 13:12:45 -0600 Subject: [PATCH] test(command): cover exec failure paths Command tests did not assert behavior when exec fails in child processes. I added deterministic regression coverage that forces execl/execlp failure and verifies non-zero exit status propagation for both open() and forkExec paths. Signed-off-by: Austin Horstman --- test/utils/command.cpp | 56 ++++++++++++++++++++++++++++++++++++++++++ test/utils/meson.build | 1 + 2 files changed, 57 insertions(+) create mode 100644 test/utils/command.cpp diff --git a/test/utils/command.cpp b/test/utils/command.cpp new file mode 100644 index 00000000..2ccb3383 --- /dev/null +++ b/test/utils/command.cpp @@ -0,0 +1,56 @@ +#if __has_include() +#include +#else +#include +#endif + +#include +#include +#include +#include +#include + +std::mutex reap_mtx; +std::list reap; + +extern "C" int waybar_test_execl(const char* path, const char* arg, ...); +extern "C" int waybar_test_execlp(const char* file, const char* arg, ...); + +#define execl waybar_test_execl +#define execlp waybar_test_execlp +#include "util/command.hpp" +#undef execl +#undef execlp + +extern "C" int waybar_test_execl(const char* path, const char* arg, ...) { + (void)path; + (void)arg; + errno = ENOENT; + return -1; +} + +extern "C" int waybar_test_execlp(const char* file, const char* arg, ...) { + (void)file; + (void)arg; + errno = ENOENT; + return -1; +} + +TEST_CASE("command::execNoRead returns 127 when shell exec fails", "[util][command]") { + const auto result = waybar::util::command::execNoRead("echo should-not-run"); + REQUIRE(result.exit_code == waybar::util::command::kExecFailureExitCode); + REQUIRE(result.out.empty()); +} + +TEST_CASE("command::forkExec child exits 127 when shell exec fails", "[util][command]") { + const auto pid = waybar::util::command::forkExec("echo should-not-run", "test-output"); + REQUIRE(pid > 0); + + int status = -1; + REQUIRE(waitpid(pid, &status, 0) == pid); + REQUIRE(WIFEXITED(status)); + REQUIRE(WEXITSTATUS(status) == waybar::util::command::kExecFailureExitCode); + + std::scoped_lock lock(reap_mtx); + reap.remove(pid); +} diff --git a/test/utils/meson.build b/test/utils/meson.build index 050af262..e8dd37fa 100644 --- a/test/utils/meson.build +++ b/test/utils/meson.build @@ -14,6 +14,7 @@ test_src = files( 'JsonParser.cpp', 'SafeSignal.cpp', 'sleeper_thread.cpp', + 'command.cpp', 'css_reload_helper.cpp', '../../src/util/css_reload_helper.cpp', )