spawn.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright (c) 2006, 2007 Julio M. Merino Vidal
  2. // Copyright (c) 2008 Ilya Sokolov, Boris Schaeling
  3. // Copyright (c) 2009 Boris Schaeling
  4. // Copyright (c) 2010 Felipe Tanus, Boris Schaeling
  5. // Copyright (c) 2011, 2012 Jeff Flinn, Boris Schaeling
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. #define BOOST_TEST_MAIN
  10. #define BOOST_TEST_IGNORE_SIGCHLD
  11. #include <boost/test/included/unit_test.hpp>
  12. #include <boost/system/error_code.hpp>
  13. #include <boost/asio.hpp>
  14. #include <boost/algorithm/string/predicate.hpp>
  15. #include <boost/process/error.hpp>
  16. #include <boost/process/io.hpp>
  17. #include <boost/process/args.hpp>
  18. #include <boost/process/spawn.hpp>
  19. #include <boost/process/async_pipe.hpp>
  20. #include <boost/process/async.hpp>
  21. #include <system_error>
  22. #include <boost/filesystem.hpp>
  23. #include <string>
  24. #include <istream>
  25. #include <cstdlib>
  26. #if defined(BOOST_WINDOWS_API)
  27. # include <windows.h>
  28. typedef boost::asio::windows::stream_handle pipe_end;
  29. #elif defined(BOOST_POSIX_API)
  30. # include <sys/wait.h>
  31. # include <unistd.h>
  32. typedef boost::asio::posix::stream_descriptor pipe_end;
  33. #endif
  34. namespace fs = boost::filesystem;
  35. namespace bp = boost::process;
  36. BOOST_AUTO_TEST_CASE(sync_spawn, *boost::unit_test::timeout(5))
  37. {
  38. using boost::unit_test::framework::master_test_suite;
  39. bp::ipstream is;
  40. std::error_code ec;
  41. bp::spawn(
  42. master_test_suite().argv[1],
  43. bp::args+={"test", "--echo-stdout", "hello"},
  44. bp::std_out > is,
  45. ec
  46. );
  47. BOOST_CHECK(!ec);
  48. std::string s;
  49. BOOST_TEST_CHECKPOINT("Starting read");
  50. is >> s;
  51. BOOST_TEST_CHECKPOINT("Finished read");
  52. BOOST_CHECK_EQUAL(s, "hello");
  53. }
  54. struct read_handler
  55. {
  56. boost::asio::streambuf &buffer_;
  57. read_handler(boost::asio::streambuf &buffer) : buffer_(buffer) {}
  58. void operator()(const boost::system::error_code &ec, std::size_t size)
  59. {
  60. std::istream is(&buffer_);
  61. std::string line;
  62. std::getline(is, line);
  63. BOOST_CHECK(boost::algorithm::starts_with(line, "abc"));
  64. }
  65. };
  66. BOOST_AUTO_TEST_CASE(async_spawn, *boost::unit_test::timeout(2))
  67. {
  68. using boost::unit_test::framework::master_test_suite;
  69. boost::asio::io_context io_context;
  70. bp::async_pipe p(io_context);
  71. std::error_code ec;
  72. bp::spawn(
  73. master_test_suite().argv[1],
  74. "test", "--echo-stdout", "abc",
  75. bp::std_out > p,
  76. ec
  77. );
  78. BOOST_REQUIRE(!ec);
  79. boost::asio::streambuf buffer;
  80. boost::asio::async_read_until(p, buffer, '\n',
  81. read_handler(buffer));
  82. io_context.run();
  83. }