From 359fbb0b9592892ba5945cd202ea4bfe38e40ff2 Mon Sep 17 00:00:00 2001 From: Marcus Holland-Moritz Date: Sun, 25 Jun 2023 11:45:19 +0200 Subject: [PATCH] folly::Subprocess -> boost::process --- test/dwarfs_tools.cpp | 49 ++++++++++++++++++++++++++++++------------- 1 file changed, 34 insertions(+), 15 deletions(-) diff --git a/test/dwarfs_tools.cpp b/test/dwarfs_tools.cpp index f125bac3..f777b579 100644 --- a/test/dwarfs_tools.cpp +++ b/test/dwarfs_tools.cpp @@ -21,8 +21,10 @@ #include #include +#include #include #include +#include #include #include @@ -30,11 +32,14 @@ #include #include +#include +#include + #include #include +#include #include -#include #include #include @@ -65,8 +70,9 @@ pid_t get_dwarfs_pid(std::filesystem::path const& path) { return folly::to(std::string_view(attr_buf.data(), attr_len)); } -void append_arg(std::vector& args, std::string const& arg) { - args.emplace_back(arg); +void append_arg(std::vector& args, + std::filesystem::path const& arg) { + args.emplace_back(arg.string()); } void append_arg(std::vector& args, @@ -74,22 +80,39 @@ void append_arg(std::vector& args, args.insert(args.end(), more.begin(), more.end()); } +template +void append_arg(std::vector& args, T const& arg) { + args.emplace_back(arg); +} + template -folly::Subprocess make_subprocess(Args&&... args) { +std::tuple run(Args&&... args) { + namespace bp = boost::process; + std::vector cmdline; (append_arg(cmdline, std::forward(args)), ...); - return folly::Subprocess( - cmdline, folly::Subprocess::Options().pipeStdout().pipeStderr()); + + boost::asio::io_service ios; + std::future out, err; + + bp::child c(bp::args(cmdline), bp::std_in.close(), bp::std_out > out, + bp::std_err > err, ios); + + ios.run(); + c.wait(); + + return {out.get(), err.get(), c.exit_code()}; } template std::optional check_run(Args&&... args) { - auto proc = make_subprocess(std::forward(args)...); - const auto [out, err] = proc.communicate(); - if (auto ec = proc.wait().exitStatus(); ec != 0) { + auto const [out, err, ec] = run(std::forward(args)...); + + if (ec != 0) { std::cerr << "stdout:\n" << out << "\nstderr:\n" << err << std::endl; return std::nullopt; } + return out; } @@ -277,13 +300,9 @@ TEST(tools, everything) { } { - auto proc = make_subprocess(driver, image_hdr, mountpoint); + auto const [out, err, ec] = run(driver, image_hdr, mountpoint); - const auto [out, err] = proc.communicate(); - - EXPECT_NE(0, proc.wait().exitStatus()) << "stdout:\n" - << out << "\nstderr:\n" - << err; + EXPECT_NE(0, ec) << "stdout:\n" << out << "\nstderr:\n" << err; } unsigned const combinations = 1 << all_options.size();