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