From a632bb8e796be52929f1541d305910d704e55076 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Fri, 16 Oct 2020 10:09:55 +0300 Subject: Process: support pipe redirection --- test/unit_tests/CMakeLists.txt | 2 +- test/unit_tests/process.cpp | 51 +++++++++++++++++++++++++++++------------- 2 files changed, 37 insertions(+), 16 deletions(-) (limited to 'test/unit_tests') diff --git a/test/unit_tests/CMakeLists.txt b/test/unit_tests/CMakeLists.txt index 33a6101..4580a57 100644 --- a/test/unit_tests/CMakeLists.txt +++ b/test/unit_tests/CMakeLists.txt @@ -1,6 +1,6 @@ file(GLOB unit_tests_src "*.cpp") add_executable(unit_tests ${unit_tests_src}) -target_link_libraries(unit_tests PRIVATE winapi_common) +target_link_libraries(unit_tests PRIVATE winapi_common winapi_utf8) set_target_properties(unit_tests PROPERTIES OUTPUT_NAME winapi-common-unit-tests) find_package(Boost REQUIRED COMPONENTS unit_test_framework) diff --git a/test/unit_tests/process.cpp b/test/unit_tests/process.cpp index 69327e1..57deef0 100644 --- a/test/unit_tests/process.cpp +++ b/test/unit_tests/process.cpp @@ -4,52 +4,73 @@ // Distributed under the MIT License. #include +#include #include +#include #include #include #include +#include + +using namespace winapi; namespace { -class WhereTestExe { +class WithEchoExe { public: - WhereTestExe() : m_test_exe(find_test_exe()) {} + WithEchoExe() : m_echo_exe(find_echo_exe()) {} - const std::string& get_test_exe() { return m_test_exe; } + const std::string& get_echo_exe() { return m_echo_exe; } private: - static std::string find_test_exe() { - static const std::string param_prefix{"--test_exe="}; - const auto cmd_line = winapi::CommandLine::query(); + static std::string find_echo_exe() { + static const std::string prefix{"--echo_exe="}; + return find_param_value(prefix); + } + + static std::string find_param_value(const std::string& param_prefix) { + const auto cmd_line = CommandLine::query(); const auto& args = cmd_line.get_args(); for (const auto& arg : args) { if (arg.rfind(param_prefix, 0) == 0) { return arg.substr(param_prefix.length()); } } - throw std::runtime_error{"couldn't find parameter --test_exe"}; + throw std::runtime_error{"couldn't find parameter " + param_prefix}; } - const std::string m_test_exe; + const std::string m_echo_exe; }; } // namespace BOOST_AUTO_TEST_SUITE(process_tests) -BOOST_AUTO_TEST_CASE(create_dir) { - const winapi::CommandLine cmd_line{"cmd.exe", {"/c", "dir"}}; - auto process = winapi::Process::create(cmd_line); +BOOST_FIXTURE_TEST_CASE(create_echo, WithEchoExe) { + const CommandLine cmd_line{get_echo_exe()}; + auto process = Process::create(cmd_line); + process.wait(); + BOOST_TEST(true, "Successfully created test process"); +} + +BOOST_FIXTURE_TEST_CASE(create_echo_with_args, WithEchoExe) { + const CommandLine cmd_line{get_echo_exe(), {"1", "2", "3"}}; + auto process = Process::create(cmd_line); process.wait(); } -BOOST_FIXTURE_TEST_CASE(create_test_exe, WhereTestExe) { - const winapi::CommandLine cmd_line{get_test_exe()}; - auto process = winapi::Process::create(cmd_line); +BOOST_FIXTURE_TEST_CASE(create_echo_pipe, WithEchoExe) { + const CommandLine cmd_line{get_echo_exe(), {"1", "2", "3"}}; + Process::IO io; + Pipe stdout_pipe; + io.std_out = process::Stdout{stdout_pipe}; + auto process = Process::create(cmd_line, std::move(io)); + const auto output = stdout_pipe.read_end().read(); process.wait(); - BOOST_TEST(true, "Successfully created test process"); + const auto utf8 = narrow(output); + BOOST_TEST(utf8 == "1\r\n2\r\n3\r\n"); } BOOST_AUTO_TEST_SUITE_END() -- cgit v1.2.3