diff --git a/subprocess.hpp b/subprocess.hpp index 16b1ab6..62cfa92 100755 --- a/subprocess.hpp +++ b/subprocess.hpp @@ -1535,7 +1535,7 @@ inline void Popen::execute_process() noexcept(false) ZeroMemory(&siStartInfo, sizeof(STARTUPINFOW)); siStartInfo.cb = sizeof(STARTUPINFOW); - siStartInfo.hStdError = this->stream_.g_hChildStd_OUT_Wr; + siStartInfo.hStdError = this->stream_.g_hChildStd_ERR_Wr; siStartInfo.hStdOutput = this->stream_.g_hChildStd_OUT_Wr; siStartInfo.hStdInput = this->stream_.g_hChildStd_IN_Rd; diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index ffc4eab..f61e5d2 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -19,3 +19,17 @@ foreach(test_file IN LISTS test_files) COPYONLY ) endforeach() + + +set(TEST_REDIRECTION_PYTHON_SCRIPT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/test_redirection.py) +configure_file( + ${CMAKE_CURRENT_SOURCE_DIR}/test_redirection.cc.in + ${CMAKE_CURRENT_BINARY_DIR}/test_redirection.cc + @ONLY +) +add_executable(test_redirection ${CMAKE_CURRENT_BINARY_DIR}/test_redirection.cc) +target_link_libraries(test_redirection PRIVATE subprocess) +add_test( + NAME test_redirection + COMMAND $ +) diff --git a/test/test_redirection.cc.in b/test/test_redirection.cc.in new file mode 100644 index 0000000..787ecc9 --- /dev/null +++ b/test/test_redirection.cc.in @@ -0,0 +1,23 @@ +#include + +#include +#include +#include + +using namespace subprocess; + +int main() { + auto p = Popen("python3 @TEST_REDIRECTION_PYTHON_SCRIPT_PATH@", output{PIPE}, error{PIPE}); + OutBuffer out_buf; + ErrBuffer err_buf; + std::tie(out_buf, err_buf) = p.communicate(); + std::string out{out_buf.buf.data()}; + std::string err{err_buf.buf.data()}; + + if (out.find("Hello message.") == std::string::npos) return EXIT_FAILURE; + if (err.find("Hello message.") != std::string::npos) return EXIT_FAILURE; + if (out.find("Error report.") != std::string::npos) return EXIT_FAILURE; + if (err.find("Error report.") == std::string::npos) return EXIT_FAILURE; + + return EXIT_SUCCESS; +} diff --git a/test/test_redirection.py b/test/test_redirection.py new file mode 100755 index 0000000..afa9d88 --- /dev/null +++ b/test/test_redirection.py @@ -0,0 +1,7 @@ +#!/usr/bin/env python3 + +import sys + +if __name__ == "__main__": + print("Hello message.") + print("Error report.", file=sys.stderr)