diff options
author | Egor Tensin <Egor.Tensin@gmail.com> | 2020-10-17 19:30:55 +0300 |
---|---|---|
committer | Egor Tensin <Egor.Tensin@gmail.com> | 2020-10-17 19:30:55 +0300 |
commit | 5cb737731a2d516259080522f938d7c54f650e1a (patch) | |
tree | 6d2b7672ff7347a942f4cecdde157b72e58eaecb | |
parent | echo: read if stdin if no args (diff) | |
download | winapi-common-5cb737731a2d516259080522f938d7c54f650e1a.tar.gz winapi-common-5cb737731a2d516259080522f938d7c54f650e1a.zip |
process_tests: add stdin redirection test
-rw-r--r-- | include/winapi/handle.hpp | 6 | ||||
-rw-r--r-- | test/unit_tests/process.cpp | 44 |
2 files changed, 35 insertions, 15 deletions
diff --git a/include/winapi/handle.hpp b/include/winapi/handle.hpp index 5f813bc..f6f4c11 100644 --- a/include/winapi/handle.hpp +++ b/include/winapi/handle.hpp @@ -13,6 +13,7 @@ #include <cstddef> #include <memory> +#include <string> #include <utility> namespace winapi { @@ -46,6 +47,11 @@ public: void write(const void*, std::size_t nb) const; void write(const Buffer& buffer) const; + template <typename CharT> + void write(const std::basic_string<CharT>& src) const { + write(src.c_str(), src.size() * sizeof(CharT)); + } + void inherit(bool yes = true) const; void dont_inherit() const { inherit(false); } diff --git a/test/unit_tests/process.cpp b/test/unit_tests/process.cpp index ced11ab..1c87b27 100644 --- a/test/unit_tests/process.cpp +++ b/test/unit_tests/process.cpp @@ -22,13 +22,6 @@ using namespace winapi::process; BOOST_AUTO_TEST_SUITE(process_tests) BOOST_FIXTURE_TEST_CASE(echo, WithEchoExe) { - const CommandLine cmd_line{get_echo_exe()}; - const auto process = Process::create(cmd_line); - process.wait(); - BOOST_TEST(process.get_exit_code() == 0); -} - -BOOST_FIXTURE_TEST_CASE(echo_with_args, WithEchoExe) { const CommandLine cmd_line{get_echo_exe(), {"1", "2", "3"}}; const auto process = Process::create(cmd_line); process.wait(); @@ -41,25 +34,46 @@ BOOST_FIXTURE_TEST_CASE(echo_stdout_to_pipe, WithEchoExe) { Pipe stdout_pipe; io.std_out = Stdout{stdout_pipe}; const auto process = Process::create(cmd_line, std::move(io)); - const auto output = stdout_pipe.read_end().read(); + const auto stdout16 = stdout_pipe.read_end().read(); process.wait(); BOOST_TEST(process.get_exit_code() == 0); - const auto utf8 = narrow(output); - BOOST_TEST(utf8 == "aaa\r\nbbb\r\nccc\r\n"); + const auto stdout8 = narrow(stdout16); + BOOST_TEST(stdout8 == "aaa\r\nbbb\r\nccc\r\n"); } BOOST_FIXTURE_TEST_CASE(echo_stdout_to_file, WithEchoExe) { + static const CanonicalPath stdout_path{"test.txt"}; + const RemoveFileGuard remove_stdout_file{stdout_path}; + const CommandLine cmd_line{get_echo_exe(), {"XXX", "YYY", "ZZZ"}}; Process::IO io; - const CanonicalPath stdout_path{"test.txt"}; - const RemoveFileGuard remove_stdout_file{stdout_path}; io.std_out = Stdout{stdout_path}; const auto process = Process::create(cmd_line, std::move(io)); process.wait(); BOOST_TEST(process.get_exit_code() == 0); - const auto output = File::open_r(stdout_path).read(); - const auto utf8 = narrow(output); - BOOST_TEST(utf8 == "XXX\r\nYYY\r\nZZZ\r\n"); + const auto stdout16 = File::open_r(stdout_path).read(); + const auto stdout8 = narrow(stdout16); + BOOST_TEST(stdout8 == "XXX\r\nYYY\r\nZZZ\r\n"); +} + +BOOST_FIXTURE_TEST_CASE(echo_stdin_from_file, WithEchoExe) { + static const CanonicalPath stdin_path{"test.txt"}; + const RemoveFileGuard remove_stdin_file{stdin_path}; + static const std::string stdin8{"123\r\n456\r\n"}; + const auto stdin16 = widen(stdin8); + File::open_w(stdin_path).write(stdin16); + + const CommandLine cmd_line{get_echo_exe()}; + Process::IO io; + Pipe stdout_pipe; + io.std_in = Stdin{stdin_path}; + io.std_out = Stdout{stdout_pipe}; + const auto process = Process::create(cmd_line, std::move(io)); + const auto stdout16 = stdout_pipe.read_end().read(); + process.wait(); + BOOST_TEST(process.get_exit_code() == 0); + const auto stdout8 = narrow(stdout16); + BOOST_TEST(stdout8 == stdin8); } BOOST_AUTO_TEST_SUITE_END() |