From fc906adff93b8788ad51d89ee9b6ae2c42967efb Mon Sep 17 00:00:00 2001 From: Sebastian Messmer Date: Wed, 22 Dec 2021 21:18:02 +0100 Subject: [PATCH] Fix Subprocess on Windows --- src/cpp-utils/process/subprocess.cpp | 15 +++++++++++---- test/cpp-utils/process/SubprocessTest.cpp | 3 +++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/cpp-utils/process/subprocess.cpp b/src/cpp-utils/process/subprocess.cpp index 4ce4aea2..e53be974 100644 --- a/src/cpp-utils/process/subprocess.cpp +++ b/src/cpp-utils/process/subprocess.cpp @@ -14,6 +14,12 @@ namespace bf = boost::filesystem; namespace ba = boost::asio; namespace bs = boost::system; +#if defined(_MSC_VER) +constexpr auto PIPE_CLOSED = ba::error::broken_pipe; +#else +constexpr auto PIPE_CLOSED = ba::error::eof; +#endif + namespace cpputils { namespace @@ -46,7 +52,7 @@ namespace cpputils output_.reserve(output_.size() + n); output_.insert(output_.end(), vOut_.begin(), vOut_.begin() + n); if (ec) { - if (ec != ba::error::eof) { + if (ec != PIPE_CLOSED) { throw SubprocessError(std::string() + "Error getting output from subprocess. Error code: " + std::to_string(ec.value()) + " : " + ec.message()); } } else { @@ -118,7 +124,7 @@ namespace cpputils return check_call(_find_executable(command), args, input); } - SubprocessResult Subprocess::call(const bf::path &executable, const vector &args, const string& input) + SubprocessResult Subprocess::call(const bf::path& executable, const vector& args, const string& input) { if (!bf::exists(executable)) { @@ -138,8 +144,8 @@ namespace cpputils bp::child child( bp::exe = executable.string(), bp::args(args), - bp::std_out > stdout_handler.pipe(), - bp::std_err > stderr_handler.pipe(), + bp::std_out > stdout_handler.pipe(), + bp::std_err > stderr_handler.pipe(), bp::std_in < stdin_handler.pipe() ); @@ -148,6 +154,7 @@ namespace cpputils stderr_handler.async_read(); ctx.run(); + child.wait(); return SubprocessResult{ diff --git a/test/cpp-utils/process/SubprocessTest.cpp b/test/cpp-utils/process/SubprocessTest.cpp index 07d24d6a..dcd3e4be 100644 --- a/test/cpp-utils/process/SubprocessTest.cpp +++ b/test/cpp-utils/process/SubprocessTest.cpp @@ -11,6 +11,7 @@ using std::string; namespace bf = boost::filesystem; // TODO Test passing input to stdin of processes +// TODO Test stderr #if defined(_MSC_VER) constexpr const char* NEWLINE = "\r\n"; @@ -163,8 +164,10 @@ TEST(SubprocessTest, Call_argumentwithspaces) EXPECT_EQ(std::string("hello") + NEWLINE + "world" + NEWLINE, Subprocess::check_call(exit_with_message_and_status(), {"0", "hello", "world"}, "").output_stdout); } +#if !defined(_MSC_VER) TEST(SubprocessTest, Call_withcommandfrompath) { // Test that we can call a system command without specifying the full path EXPECT_EQ("hello\n", Subprocess::check_call("echo", {"hello"}, "").output_stdout); } +#endif