Fix Subprocess on Windows

This commit is contained in:
Sebastian Messmer 2021-12-22 21:18:02 +01:00
parent 4738c1ff78
commit fc906adff9
2 changed files with 14 additions and 4 deletions

View File

@ -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<string> &args, const string& input)
SubprocessResult Subprocess::call(const bf::path& executable, const vector<string>& 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{

View File

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