Fix redirection from StdError on Windows (#96)

* Fix redirection from StdError on Windows

* test: Add cross-platform test for error redirection
This commit is contained in:
Hennadii Stepanov 2023-12-03 12:40:57 +00:00 committed by GitHub
parent 06858e5fd7
commit 40bcc2daa9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 45 additions and 1 deletions

View File

@ -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;

View File

@ -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 $<TARGET_FILE:test_redirection>
)

View File

@ -0,0 +1,23 @@
#include <subprocess.hpp>
#include <cstdlib>
#include <string>
#include <tuple>
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;
}

7
test/test_redirection.py Executable file
View File

@ -0,0 +1,7 @@
#!/usr/bin/env python3
import sys
if __name__ == "__main__":
print("Hello message.")
print("Error report.", file=sys.stderr)