From e9d2f022cd31a35046c2ef28b2117640d7fac83c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20BOULMIER?= Date: Thu, 7 Mar 2019 01:42:10 -0500 Subject: [PATCH] add command unit tests --- include/components/controller.hpp | 1 + include/utils/command.hpp | 2 +- tests/CMakeLists.txt | 1 + tests/unit_tests/utils/command.cpp | 57 ++++++++++++++++++++++++++++++ tests/unit_tests/utils/file.cpp | 3 +- 5 files changed, 61 insertions(+), 3 deletions(-) create mode 100644 tests/unit_tests/utils/command.cpp diff --git a/include/components/controller.hpp b/include/components/controller.hpp index f37efd85..cf21b871 100644 --- a/include/components/controller.hpp +++ b/include/components/controller.hpp @@ -5,6 +5,7 @@ #include #include "common.hpp" +#include "components/types.hpp" #include "events/signal_fwd.hpp" #include "events/signal_receiver.hpp" #include "events/types.hpp" diff --git a/include/utils/command.hpp b/include/utils/command.hpp index 1619e3e3..bc3f8537 100644 --- a/include/utils/command.hpp +++ b/include/utils/command.hpp @@ -77,7 +77,7 @@ class command { string m_cmd; pid_t m_forkpid{}; - int m_forkstatus{}; + int m_forkstatus = - 1; }; template <> diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 6dffb6e8..dfad6588 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -48,6 +48,7 @@ function(add_unit_test source_file) endfunction() add_unit_test(utils/color) +add_unit_test(utils/command) add_unit_test(utils/math unit_tests) add_unit_test(utils/memory unit_tests) add_unit_test(utils/scope unit_tests) diff --git a/tests/unit_tests/utils/command.cpp b/tests/unit_tests/utils/command.cpp new file mode 100644 index 00000000..2283515e --- /dev/null +++ b/tests/unit_tests/utils/command.cpp @@ -0,0 +1,57 @@ +#include "utils/command.hpp" +#include "common/test.hpp" + +#include + +using namespace polybar; + +TEST(Command, status) { + // Test for command::exec(bool); + { + auto cmd = command_util::make_command("echo polybar > /dev/null"); + int status = cmd->exec(); + + EXPECT_EQ(status, EXIT_SUCCESS); + } + + // Test for command::exec(bool); + { + auto cmd = command_util::make_command("echo polybar"); + int status = cmd->exec(); + + EXPECT_EQ(status, EXIT_SUCCESS); + } +} + +TEST(Command, status_async) { + { + auto cmd = command_util::make_command("echo polybar > /dev/null"); + EXPECT_EQ(cmd->exec(false), EXIT_SUCCESS); + + cmd->wait(); + + EXPECT_FALSE(cmd->is_running()); + EXPECT_EQ(cmd->get_exit_status(), EXIT_SUCCESS); + } +} + +TEST(Command, output) { + auto cmd = command_util::make_command("echo polybar"); + string str; + cmd->exec(false); + cmd->tail([&str](string&& string) { str = string; }); + cmd->wait(); + + EXPECT_EQ(str, "polybar"); +} + +TEST(Command, readline) { + auto cmd = command_util::make_command("read text;echo $text"); + + string str; + cmd->exec(false); + cmd->writeline("polybar"); + cmd->tail([&str](string&& string) { str = string; }); + + EXPECT_EQ(str, "polybar"); +} diff --git a/tests/unit_tests/utils/file.cpp b/tests/unit_tests/utils/file.cpp index ebea3c21..1c303dc3 100644 --- a/tests/unit_tests/utils/file.cpp +++ b/tests/unit_tests/utils/file.cpp @@ -8,10 +8,9 @@ using namespace polybar; TEST(File, expand) { - auto cmd = command_util::make_command("echo $HOME"); + auto cmd = command_util::make_command("echo $HOME"); cmd->exec(); cmd->tail([](string home) { EXPECT_EQ(home + "/test", file_util::expand("~/test")); }); } -