source: trunk/debathena/third/schroot/test/sbuild-environment.cc @ 24167

Revision 24167, 9.3 KB checked in by broder, 15 years ago (diff)
Import schroot upstream into subversion.
Line 
1/* Copyright © 2006-2007  Roger Leigh <rleigh@debian.org>
2 *
3 * schroot is free software: you can redistribute it and/or modify it
4 * under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * schroot is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program.  If not, see
15 * <http://www.gnu.org/licenses/>.
16 *
17 *********************************************************************/
18
19#include <sbuild/sbuild-environment.h>
20#include <sbuild/sbuild-util.h>
21
22#include <iostream>
23#include <sstream>
24
25#include <cppunit/extensions/HelperMacros.h>
26
27using namespace CppUnit;
28
29class test_environment : public TestFixture
30{
31  CPPUNIT_TEST_SUITE(test_environment);
32  CPPUNIT_TEST(test_construction);
33  CPPUNIT_TEST(test_add_strv);
34  CPPUNIT_TEST(test_add_env);
35  CPPUNIT_TEST(test_add_value);
36  CPPUNIT_TEST(test_add_string_pair);
37  CPPUNIT_TEST(test_add_template);
38  CPPUNIT_TEST(test_add_string);
39  CPPUNIT_TEST(test_add_empty_implicit_remove);
40  CPPUNIT_TEST(test_remove_strv);
41  CPPUNIT_TEST(test_remove_env);
42  CPPUNIT_TEST(test_remove_value);
43  CPPUNIT_TEST(test_remove_string);
44  CPPUNIT_TEST(test_get_value);
45  CPPUNIT_TEST(test_get_strv);
46  CPPUNIT_TEST(test_operator_plus);
47  CPPUNIT_TEST(test_operator_plus_equals);
48  CPPUNIT_TEST(test_operator_minus);
49  CPPUNIT_TEST(test_operator_minus_equals);
50  CPPUNIT_TEST(test_add_filter);
51  CPPUNIT_TEST(test_filter);
52  CPPUNIT_TEST(test_output);
53  CPPUNIT_TEST_SUITE_END();
54
55protected:
56  sbuild::environment *env;
57  sbuild::environment *half_env;
58
59public:
60  test_environment():
61    TestFixture(),
62    env()
63  {}
64
65  virtual ~test_environment()
66  {}
67
68  void setUp()
69  {
70    this->env = new sbuild::environment;
71    this->env->add(std::make_pair("TERM", "wy50"));
72    this->env->add(std::make_pair("SHELL", "/bin/sh"));
73    this->env->add(std::make_pair("USER", "root"));
74    this->env->add(std::make_pair("COLUMNS", "80"));
75
76    this->half_env = new sbuild::environment;
77    this->half_env->add(std::make_pair("TERM", "wy50"));
78    this->half_env->add(std::make_pair("USER", "root"));
79  }
80
81  static void add_examples(sbuild::environment& env)
82  {
83    sbuild::environment::size_type size = env.size();
84    env.add(std::make_pair("TERM", "wy50"));
85    env.add(std::make_pair("SHELL", "/bin/sh"));
86    env.add(std::make_pair("USER", "root"));
87    env.add(std::make_pair("COLUMNS", "80"));
88    CPPUNIT_ASSERT(env.size() == size + 4);
89  }
90
91  static void add_simple_examples(sbuild::environment& env)
92  {
93    sbuild::environment::size_type size = env.size();
94    env.add(std::make_pair("TERM", "wy50"));
95    env.add(std::make_pair("USER", "root"));
96    CPPUNIT_ASSERT(env.size() == size + 2);
97  }
98
99  void tearDown()
100  {
101    delete this->env;
102    delete this->half_env;
103  }
104
105  void
106  test_construction()
107  {
108    const char *items[] = {"TERM=wy50", "SHELL=/bin/sh",
109                           "USER=root", "COLUMNS=80", 0};
110    sbuild::environment e(const_cast<char **>(&items[0]));
111
112    CPPUNIT_ASSERT(e.size() == 4);
113
114    CPPUNIT_ASSERT(e == *this->env);
115  }
116
117  void
118  test_add_strv()
119  {
120    const char *items[] = {"TERM=wy50", "SHELL=/bin/sh",
121                           "USER=root", "COLUMNS=80", 0};
122    sbuild::environment e;
123    e.add(const_cast<char **>(&items[0]));
124
125    CPPUNIT_ASSERT(e.size() == 4);
126
127    CPPUNIT_ASSERT(e == *this->env);
128  }
129
130  void
131  test_add_env()
132  {
133    sbuild::environment e;
134    e.add(*this->env);
135
136    CPPUNIT_ASSERT(e == *this->env);
137  }
138
139  void
140  test_add_value()
141  {
142    sbuild::environment e;
143    e.add(sbuild::environment::value_type("TERM", "wy50"));
144    e.add(sbuild::environment::value_type("SHELL", "/bin/sh"));
145    e.add(sbuild::environment::value_type("USER", "root"));
146    e.add(sbuild::environment::value_type("COLUMNS", "80"));
147
148    CPPUNIT_ASSERT(e == *this->env);
149  }
150
151  void
152  test_add_string_pair()
153  {
154    sbuild::environment e;
155    e.add("TERM", "wy50");
156    e.add("SHELL", "/bin/sh");
157    e.add("USER", "root");
158    e.add("COLUMNS", "80");
159
160    CPPUNIT_ASSERT(e == *this->env);
161  }
162
163  void
164  test_add_template()
165  {
166    sbuild::environment e;
167    e.add("TERM", "wy50");
168    e.add("SHELL", "/bin/sh");
169    e.add("USER", std::string("root"));
170    e.add("COLUMNS", 80);
171
172    CPPUNIT_ASSERT(e == *this->env);
173  }
174
175  void
176  test_add_string()
177  {
178    sbuild::environment e;
179    e.add("TERM=wy50");
180    e.add("SHELL=/bin/sh");
181    e.add("USER=root");
182    e.add("COLUMNS=80");
183
184    CPPUNIT_ASSERT(e == *this->env);
185  }
186
187  void
188  test_add_empty_implicit_remove()
189  {
190    sbuild::environment e;
191    e.add("TERM=wy50");
192    e.add("USER=root");
193
194    this->env->add("COLUMNS=");
195    this->env->add(sbuild::environment::value_type("SHELL", ""));
196
197    CPPUNIT_ASSERT(this->env->size() == 2);
198    CPPUNIT_ASSERT(e == *this->env);
199  }
200
201  void
202  test_remove_strv()
203  {
204    const char *items[] = {"SHELL=/bin/bash",
205                           "COLUMNS=160", 0};
206    this->env->remove(const_cast<char **>(&items[0]));
207
208    CPPUNIT_ASSERT(this->env->size() == 2);
209    CPPUNIT_ASSERT(*this->env == *this->half_env);
210  }
211
212  void
213  test_remove_env()
214  {
215    sbuild::environment e;
216    e.add("SHELL=/bin/bash");
217    e.add("COLUMNS=160");
218
219    this->env->remove(e);
220
221    CPPUNIT_ASSERT(*this->env == *this->half_env);
222  }
223
224  void
225  test_remove_value()
226  {
227    this->env->remove(sbuild::environment::value_type("SHELL", "/bin/bash"));
228    this->env->remove(sbuild::environment::value_type("COLUMNS", "160"));
229
230    CPPUNIT_ASSERT(*this->env == *this->half_env);
231  }
232
233  void
234  test_remove_string()
235  {
236    this->env->remove("SHELL=/bin/bash");
237    this->env->remove("COLUMNS=160");
238
239    CPPUNIT_ASSERT(*this->env == *this->half_env);
240  }
241
242  void test_get_value()
243  {
244    std::string value;
245    CPPUNIT_ASSERT(this->env->get("TERM", value) && value == "wy50");
246    CPPUNIT_ASSERT(this->env->get("SHELL", value) && value == "/bin/sh");
247    CPPUNIT_ASSERT(this->env->get("USER", value) && value == "root");
248    CPPUNIT_ASSERT(this->env->get("COLUMNS", value) && value == "80");
249    // Check failure doesn't overwrite value.
250    CPPUNIT_ASSERT(!this->env->get("MUSTFAIL", value) && value == "80");
251
252    // Check getting templated types.
253    int tval;
254    CPPUNIT_ASSERT(this->env->get("COLUMNS", tval) && tval == 80);
255  }
256
257  void test_get_strv()
258  {
259    char **strv = this->env->get_strv();
260
261    int size = 0;
262    for (char **ev = strv; ev != 0 && *ev != 0; ++ev, ++size);
263
264    CPPUNIT_ASSERT(size == 4);
265    CPPUNIT_ASSERT(std::string(strv[0]) == "COLUMNS=80");
266    CPPUNIT_ASSERT(std::string(strv[1]) == "SHELL=/bin/sh");
267    CPPUNIT_ASSERT(std::string(strv[2]) == "TERM=wy50");
268    CPPUNIT_ASSERT(std::string(strv[3]) == "USER=root");
269
270    sbuild::strv_delete(strv);
271  }
272
273  void test_operator_plus()
274  {
275    sbuild::environment e;
276    e.add("SHELL=/bin/sh");
277    e.add("COLUMNS=80");
278
279    sbuild::environment result;
280    result = *this->half_env + e;
281    CPPUNIT_ASSERT(result == *this->env);
282
283    sbuild::environment e2;
284    e2 = *this->half_env + "SHELL=/bin/sh";
285    e2 = e2 + sbuild::environment::value_type("COLUMNS", "80");
286    CPPUNIT_ASSERT(e2 == *this->env);
287  }
288
289  void test_operator_plus_equals()
290  {
291    sbuild::environment e;
292    e.add("SHELL=/bin/sh");
293    e.add("COLUMNS=80");
294
295    sbuild::environment result(*this->half_env);
296    result += e;
297    CPPUNIT_ASSERT(result == *this->env);
298
299    sbuild::environment e2(*this->half_env);
300    e2 += "SHELL=/bin/sh";
301    // TODO: Why does calling direct fail?
302    sbuild::environment::value_type val("COLUMNS", "80");
303    e2 += val;
304    CPPUNIT_ASSERT(e2 == *this->env);
305  }
306
307  void test_operator_minus()
308  {
309    sbuild::environment e;
310    e.add("SHELL=/bin/sh");
311    e.add("COLUMNS=80");
312
313    sbuild::environment result;
314    result = *this->env - e;
315    CPPUNIT_ASSERT(result == *this->half_env);
316
317    sbuild::environment e2;
318    e2 = *this->env - "SHELL=/bin/sh";
319    e2 = e2 - sbuild::environment::value_type("COLUMNS", "80");
320    CPPUNIT_ASSERT(e2 == *this->half_env);
321  }
322
323  void test_operator_minus_equals()
324  {
325    sbuild::environment e;
326    e.add("SHELL=/bin/sh");
327    e.add("COLUMNS=80");
328
329    sbuild::environment result(*this->env);
330    result -= e;
331    CPPUNIT_ASSERT(result == *this->half_env);
332
333    sbuild::environment e2(*this->env);
334    e2 -= "SHELL=/bin/sh";
335    // TODO: Why does calling direct fail?
336    sbuild::environment::value_type val("COLUMNS", "80");
337    e2 -= val;
338    CPPUNIT_ASSERT(e2 == *this->half_env);
339  }
340
341  void test_add_filter()
342  {
343    sbuild::regex f("^FOO|BAR$");
344
345    sbuild::environment e;
346    e.set_filter(f);
347
348    CPPUNIT_ASSERT(f.compare(e.get_filter()) == 0);
349  }
350
351  void test_filter()
352  {
353    sbuild::regex f("^FOO|BAR$");
354
355    sbuild::environment e;
356    e.set_filter(f);
357
358    e.add("FOO=bar");
359    e.add("BAR=baz");
360    e.add("BAZ=bat");
361    e.add("BAT=bah");
362
363    std::string value;
364    CPPUNIT_ASSERT(e.get("FOO", value) == false);
365    CPPUNIT_ASSERT(e.get("BAR", value) == false);
366    CPPUNIT_ASSERT(e.get("BAZ", value) && value == "bat");
367    CPPUNIT_ASSERT(e.get("BAT", value) && value == "bah");
368  }
369
370  void test_output()
371  {
372    std::ostringstream os;
373    os << *this->env;
374
375    CPPUNIT_ASSERT(os.str() ==
376                   "COLUMNS=80\n"
377                   "SHELL=/bin/sh\n"
378                   "TERM=wy50\n"
379                   "USER=root\n");
380  }
381};
382
383CPPUNIT_TEST_SUITE_REGISTRATION(test_environment);
Note: See TracBrowser for help on using the repository browser.