system_test1.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 <iostream>
  14. #include <boost/asio.hpp>
  15. #include <boost/chrono.hpp>
  16. #include <boost/algorithm/string/predicate.hpp>
  17. #include <boost/asio/steady_timer.hpp>
  18. #include <boost/process/error.hpp>
  19. #include <boost/process/io.hpp>
  20. #include <boost/process/args.hpp>
  21. #include <boost/process/system.hpp>
  22. #include <boost/process/async_pipe.hpp>
  23. #include <boost/process/async.hpp>
  24. #include <system_error>
  25. #include <boost/filesystem.hpp>
  26. #include <string>
  27. #include <chrono>
  28. #include <istream>
  29. #include <cstdlib>
  30. #if defined(BOOST_WINDOWS_API)
  31. # include <windows.h>
  32. typedef boost::asio::windows::stream_handle pipe_end;
  33. #elif defined(BOOST_POSIX_API)
  34. # include <sys/wait.h>
  35. # include <unistd.h>
  36. typedef boost::asio::posix::stream_descriptor pipe_end;
  37. #endif
  38. namespace fs = boost::filesystem;
  39. namespace bp = boost::process;
  40. BOOST_AUTO_TEST_CASE(system_exit_code, *boost::unit_test::timeout(5))
  41. {
  42. using boost::unit_test::framework::master_test_suite;
  43. std::error_code ec;
  44. //I need to spawn a thread for that to work
  45. BOOST_CHECK_EQUAL(
  46. bp::system(
  47. master_test_suite().argv[1],
  48. "test", "--exit-code", "123", ec), 123);
  49. BOOST_CHECK(!ec);
  50. }
  51. BOOST_AUTO_TEST_CASE(implicit_async_io, *boost::unit_test::timeout(2))
  52. {
  53. using boost::unit_test::framework::master_test_suite;
  54. std::future<std::string> fut;
  55. std::error_code ec;
  56. int res = bp::system(
  57. master_test_suite().argv[1],
  58. "test", "--echo-stdout", "abc",
  59. bp::std_out > fut,
  60. ec
  61. );
  62. BOOST_REQUIRE(!ec);
  63. BOOST_REQUIRE(fut.valid());
  64. BOOST_CHECK_EQUAL(res, 0);
  65. BOOST_CHECK(boost::starts_with(
  66. fut.get(), "abc"));
  67. }