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
14#include <boost/process/args.hpp>
15#include <boost/process/child.hpp>
16#include <boost/process/env.hpp>
17#include <boost/process/environment.hpp>
18#include <boost/process/error.hpp>
19#include <boost/process/io.hpp>
20
21#include <boost/algorithm/string/predicate.hpp>
22
23#include <boost/system/error_code.hpp>
24
25#include <boost/program_options/environment_iterator.hpp>
26#include <string>
27#include <stdlib.h>
28#include <list>
29
30namespace bp = boost::process;
31
32BOOST_AUTO_TEST_CASE(inherit_env, *boost::unit_test::timeout(2))
33{
34 using boost::unit_test::framework::master_test_suite;
35
36 bp::ipstream st;
37
38 std::error_code ec;
39
40 bp::child c(
41 master_test_suite().argv[1],
42 "test", "--query", "PATH",
43 bp::std_out>st,
44 ec
45 );
46 BOOST_REQUIRE(!ec);
47
48 std::string s;
49
50 std::getline(is&: st, str&: s);
51
52 auto path = boost::this_process::environment()["PATH"].to_string();
53
54 std::cout << "Path : '" << path << "'" << std::endl;
55 std::cout << "Value: '" << s << "'" << std::endl;
56
57 if(!path.empty())
58 {
59 auto size = (path.size() < s.size()) ? path.size() : s.size();
60
61 BOOST_CHECK_EQUAL_COLLECTIONS(
62 s.begin(), s. begin() + size,
63 path.begin(), path.begin() + size
64 );
65 }
66 else
67 BOOST_CHECK(boost::starts_with(s, "************** empty environment **************"));
68 c.wait();
69}
70
71
72BOOST_AUTO_TEST_CASE(inherit_mod_env, *boost::unit_test::timeout(2))
73{
74 using boost::unit_test::framework::master_test_suite;
75
76 auto ie = boost::this_process::environment();
77 std::string value = "TestString";
78 ie["BOOST_PROCESS_TEST_1"] = value;
79
80 {
81 auto ie2 = boost::this_process::environment();
82 auto val = ie2["BOOST_PROCESS_TEST_1"];
83 auto st = val.to_string();
84
85 BOOST_CHECK_EQUAL_COLLECTIONS(
86 st.begin(), st.end(),
87 value.begin(), value.end()
88 );
89 }
90 bp::ipstream st;
91
92 std::error_code ec;
93 bp::child c(
94 master_test_suite().argv[1],
95 "test", "--query", "BOOST_PROCESS_TEST_1",
96 bp::std_out>st,
97 ec
98 );
99 BOOST_REQUIRE(!ec);
100
101 std::string s;
102
103 std::getline(is&: st, str&: s);
104
105 auto size = (value.size() < s.size()) ? value.size() : s.size();
106
107 BOOST_CHECK_EQUAL_COLLECTIONS(
108 s.begin(), s. begin() + size,
109 value.begin(), value.begin() + size
110 );
111 c.wait();
112}
113
114
115BOOST_AUTO_TEST_CASE(modifided_env, *boost::unit_test::timeout(2))
116{
117 using boost::unit_test::framework::master_test_suite;
118
119 bp::ipstream st;
120
121 boost::process::environment env = boost::this_process::environment(); //empty env, that would fail.
122 std::string value = "TestString";
123 env["BOOST_PROCESS_TEST_2"] = value;
124
125
126 std::error_code ec;
127 bp::child c(
128 master_test_suite().argv[1],
129 "test", "--query", "BOOST_PROCESS_TEST_2",
130 bp::std_out>st,
131 env,
132 ec
133 );
134 BOOST_REQUIRE(!ec);
135 BOOST_REQUIRE(boost::this_process::environment().count(value) == 0);
136
137 std::string s;
138 std::getline(is&: st, str&: s);
139
140 BOOST_CHECK(boost::algorithm::starts_with(s, "TestString"));
141 c.wait();
142}
143
144
145BOOST_AUTO_TEST_CASE(append, *boost::unit_test::timeout(5))
146{
147 using boost::unit_test::framework::master_test_suite;
148
149 bp::ipstream st;
150 BOOST_TEST_PASSPOINT();
151 bp::environment e = boost::this_process::environment();
152
153 std::error_code ec;
154 BOOST_REQUIRE_GE(e.size(), 1u);
155
156 std::list<std::string> arg = {"test", "--query", "BOOST_PROCESS_TEST_3"};
157 bp::child c(
158 master_test_suite().argv[1],
159 bp::env["BOOST_PROCESS_TEST_3"]="some_string",
160 bp::env=e,
161 bp::env["BOOST_PROCESS_TEST_3"]=boost::none,
162 bp::env["BOOST_PROCESS_TEST_3"]+="some_fictional_path_42",
163 bp::env["BOOST_PROCESS_TEST_3"]+={"other", "next"},
164 bp::args=arg,
165 bp::std_out>st,
166 ec
167 );
168
169 BOOST_REQUIRE(!ec);
170 BOOST_WARN(c.running());
171 std::string s;
172
173 std::getline(is&: st, str&: s);
174#if defined(BOOST_WINDOWS_API)
175 BOOST_CHECK(boost::starts_with(s, "some_fictional_path_42;other;next"));
176#else
177 BOOST_CHECK(boost::starts_with(s, "some_fictional_path_42:other:next"));
178#endif
179 c.wait();
180}
181

source code of boost/libs/process/test/env.cpp