1 // Copyright 2010 Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of Google Inc. nor the names of its contributors
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include "utils/env.hpp"
31 #include <atf-c++.hpp>
33 #include "utils/optional.ipp"
35 using utils::optional
;
38 ATF_TEST_CASE_WITHOUT_HEAD(getallenv
);
39 ATF_TEST_CASE_BODY(getallenv
)
41 utils::unsetenv("test-missing");
42 utils::setenv("test-empty", "");
43 utils::setenv("test-text", "some-value");
45 const std::map
< std::string
, std::string
> allenv
= utils::getallenv();
48 const std::map
< std::string
, std::string
>::const_iterator iter
=
49 allenv
.find("test-missing");
50 ATF_REQUIRE(iter
== allenv
.end());
54 const std::map
< std::string
, std::string
>::const_iterator iter
=
55 allenv
.find("test-empty");
56 ATF_REQUIRE(iter
!= allenv
.end());
57 ATF_REQUIRE((*iter
).second
.empty());
61 const std::map
< std::string
, std::string
>::const_iterator iter
=
62 allenv
.find("test-text");
63 ATF_REQUIRE(iter
!= allenv
.end());
64 ATF_REQUIRE_EQ("some-value", (*iter
).second
);
67 if (utils::getenv("PATH")) {
68 const std::map
< std::string
, std::string
>::const_iterator iter
=
70 ATF_REQUIRE(iter
!= allenv
.end());
71 ATF_REQUIRE_EQ(utils::getenv("PATH").get(), (*iter
).second
);
76 ATF_TEST_CASE_WITHOUT_HEAD(getenv
);
77 ATF_TEST_CASE_BODY(getenv
)
79 const optional
< std::string
> path
= utils::getenv("PATH");
81 ATF_REQUIRE(!path
.get().empty());
83 ATF_REQUIRE(!utils::getenv("__UNDEFINED_VARIABLE__"));
87 ATF_TEST_CASE_WITHOUT_HEAD(getenv_with_default
);
88 ATF_TEST_CASE_BODY(getenv_with_default
)
90 ATF_REQUIRE("don't use" !=
91 utils::getenv_with_default("PATH", "don't use"));
94 utils::getenv_with_default("__UNDEFINED_VARIABLE__", "foo"));
98 ATF_TEST_CASE_WITHOUT_HEAD(setenv
);
99 ATF_TEST_CASE_BODY(setenv
)
101 ATF_REQUIRE(utils::getenv("PATH"));
102 const std::string oldval
= utils::getenv("PATH").get();
103 utils::setenv("PATH", "foo-bar");
104 ATF_REQUIRE(utils::getenv("PATH").get() != oldval
);
105 ATF_REQUIRE_EQ("foo-bar", utils::getenv("PATH").get());
107 ATF_REQUIRE(!utils::getenv("__UNDEFINED_VARIABLE__"));
108 utils::setenv("__UNDEFINED_VARIABLE__", "foo2-bar2");
109 ATF_REQUIRE_EQ("foo2-bar2", utils::getenv("__UNDEFINED_VARIABLE__").get());
113 ATF_TEST_CASE_WITHOUT_HEAD(unsetenv
);
114 ATF_TEST_CASE_BODY(unsetenv
)
116 ATF_REQUIRE(utils::getenv("PATH"));
117 utils::unsetenv("PATH");
118 ATF_REQUIRE(!utils::getenv("PATH"));
122 ATF_INIT_TEST_CASES(tcs
)
124 ATF_ADD_TEST_CASE(tcs
, getallenv
);
125 ATF_ADD_TEST_CASE(tcs
, getenv
);
126 ATF_ADD_TEST_CASE(tcs
, getenv_with_default
);
127 ATF_ADD_TEST_CASE(tcs
, setenv
);
128 ATF_ADD_TEST_CASE(tcs
, unsetenv
);