extensions.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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/process/error.hpp>
  13. #include <system_error>
  14. #include <boost/bind.hpp>
  15. #include <boost/ref.hpp>
  16. #include <boost/process/child.hpp>
  17. #include <boost/process/extend.hpp>
  18. namespace bp = boost::process;
  19. struct run_exe
  20. {
  21. std::string exe;
  22. template<typename T>
  23. void operator()(T &e) const
  24. {
  25. e.exe = exe.c_str();
  26. }
  27. };
  28. struct set_on_error
  29. {
  30. mutable std::error_code ec;
  31. template<typename T>
  32. void operator()(T &, const std::error_code & ec) const
  33. {
  34. this->ec = ec;
  35. }
  36. };
  37. BOOST_AUTO_TEST_CASE(extensions)
  38. {
  39. using boost::unit_test::framework::master_test_suite;
  40. run_exe re;
  41. re.exe = master_test_suite().argv[1];
  42. set_on_error se;
  43. std::error_code ec;
  44. bp::child c(
  45. "Wrong-Command",
  46. "test",
  47. bp::extend::on_setup=re,
  48. bp::extend::on_error=se,
  49. bp::ignore_error
  50. );
  51. BOOST_CHECK(!se.ec);
  52. }
  53. namespace ex = boost::process::extend;
  54. std::string st = "not called";
  55. struct overload_handler : ex::handler
  56. {
  57. template <class Char, class Sequence>
  58. void on_setup(ex::windows_executor<Char, Sequence>& exec) const
  59. {
  60. st = "windows";
  61. const char* env = exec.env;
  62. }
  63. template <class Sequence>
  64. void on_setup(ex::posix_executor<Sequence>& exec) const
  65. {
  66. st = "posix";
  67. char** env = exec.env;
  68. }
  69. };
  70. BOOST_AUTO_TEST_CASE(overload)
  71. {
  72. bp::child c(
  73. overload_handler(),
  74. bp::ignore_error
  75. );
  76. #if defined(BOOST_WINDOWS_API)
  77. BOOST_CHECK_EQUAL(st, "windows");
  78. #else
  79. BOOST_CHECK_EQUAL(st, "posix");
  80. #endif
  81. }