Skip to content

Commit 40bcc2d

Browse files
authored
Fix redirection from StdError on Windows (arun11299#96)
* Fix redirection from StdError on Windows * test: Add cross-platform test for error redirection
1 parent 06858e5 commit 40bcc2d

File tree

4 files changed

+45
-1
lines changed

4 files changed

+45
-1
lines changed

subprocess.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1535,7 +1535,7 @@ inline void Popen::execute_process() noexcept(false)
15351535
ZeroMemory(&siStartInfo, sizeof(STARTUPINFOW));
15361536
siStartInfo.cb = sizeof(STARTUPINFOW);
15371537

1538-
siStartInfo.hStdError = this->stream_.g_hChildStd_OUT_Wr;
1538+
siStartInfo.hStdError = this->stream_.g_hChildStd_ERR_Wr;
15391539
siStartInfo.hStdOutput = this->stream_.g_hChildStd_OUT_Wr;
15401540
siStartInfo.hStdInput = this->stream_.g_hChildStd_IN_Rd;
15411541

test/CMakeLists.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,17 @@ foreach(test_file IN LISTS test_files)
1919
COPYONLY
2020
)
2121
endforeach()
22+
23+
24+
set(TEST_REDIRECTION_PYTHON_SCRIPT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/test_redirection.py)
25+
configure_file(
26+
${CMAKE_CURRENT_SOURCE_DIR}/test_redirection.cc.in
27+
${CMAKE_CURRENT_BINARY_DIR}/test_redirection.cc
28+
@ONLY
29+
)
30+
add_executable(test_redirection ${CMAKE_CURRENT_BINARY_DIR}/test_redirection.cc)
31+
target_link_libraries(test_redirection PRIVATE subprocess)
32+
add_test(
33+
NAME test_redirection
34+
COMMAND $<TARGET_FILE:test_redirection>
35+
)

test/test_redirection.cc.in

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <subprocess.hpp>
2+
3+
#include <cstdlib>
4+
#include <string>
5+
#include <tuple>
6+
7+
using namespace subprocess;
8+
9+
int main() {
10+
auto p = Popen("python3 @TEST_REDIRECTION_PYTHON_SCRIPT_PATH@", output{PIPE}, error{PIPE});
11+
OutBuffer out_buf;
12+
ErrBuffer err_buf;
13+
std::tie(out_buf, err_buf) = p.communicate();
14+
std::string out{out_buf.buf.data()};
15+
std::string err{err_buf.buf.data()};
16+
17+
if (out.find("Hello message.") == std::string::npos) return EXIT_FAILURE;
18+
if (err.find("Hello message.") != std::string::npos) return EXIT_FAILURE;
19+
if (out.find("Error report.") != std::string::npos) return EXIT_FAILURE;
20+
if (err.find("Error report.") == std::string::npos) return EXIT_FAILURE;
21+
22+
return EXIT_SUCCESS;
23+
}

test/test_redirection.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env python3
2+
3+
import sys
4+
5+
if __name__ == "__main__":
6+
print("Hello message.")
7+
print("Error report.", file=sys.stderr)

0 commit comments

Comments
 (0)