1 // Copyright 2010 The Kyua Authors.
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/fs/path.hpp"
34 #include "utils/optional.ipp"
36 namespace fs = utils::fs;
38 using utils::optional;
41 ATF_TEST_CASE_WITHOUT_HEAD(getallenv);
42 ATF_TEST_CASE_BODY(getallenv)
44 utils::unsetenv("test-missing");
45 utils::setenv("test-empty", "");
46 utils::setenv("test-text", "some-value");
48 const std::map< std::string, std::string > allenv = utils::getallenv();
51 const std::map< std::string, std::string >::const_iterator iter =
52 allenv.find("test-missing");
53 ATF_REQUIRE(iter == allenv.end());
57 const std::map< std::string, std::string >::const_iterator iter =
58 allenv.find("test-empty");
59 ATF_REQUIRE(iter != allenv.end());
60 ATF_REQUIRE((*iter).second.empty());
64 const std::map< std::string, std::string >::const_iterator iter =
65 allenv.find("test-text");
66 ATF_REQUIRE(iter != allenv.end());
67 ATF_REQUIRE_EQ("some-value", (*iter).second);
70 if (utils::getenv("PATH")) {
71 const std::map< std::string, std::string >::const_iterator iter =
73 ATF_REQUIRE(iter != allenv.end());
74 ATF_REQUIRE_EQ(utils::getenv("PATH").get(), (*iter).second);
79 ATF_TEST_CASE_WITHOUT_HEAD(getenv);
80 ATF_TEST_CASE_BODY(getenv)
82 const optional< std::string > path = utils::getenv("PATH");
84 ATF_REQUIRE(!path.get().empty());
86 ATF_REQUIRE(!utils::getenv("__UNDEFINED_VARIABLE__"));
90 ATF_TEST_CASE_WITHOUT_HEAD(getenv_with_default);
91 ATF_TEST_CASE_BODY(getenv_with_default)
93 ATF_REQUIRE("don't use" !=
94 utils::getenv_with_default("PATH", "don't use"));
97 utils::getenv_with_default("__UNDEFINED_VARIABLE__", "foo"));
101 ATF_TEST_CASE_WITHOUT_HEAD(get_home__ok);
102 ATF_TEST_CASE_BODY(get_home__ok)
104 const fs::path home("/foo/bar");
105 utils::setenv("HOME", home.str());
106 const optional< fs::path > computed = utils::get_home();
107 ATF_REQUIRE(computed);
108 ATF_REQUIRE_EQ(home, computed.get());
112 ATF_TEST_CASE_WITHOUT_HEAD(get_home__missing);
113 ATF_TEST_CASE_BODY(get_home__missing)
115 utils::unsetenv("HOME");
116 ATF_REQUIRE(!utils::get_home());
120 ATF_TEST_CASE_WITHOUT_HEAD(get_home__invalid);
121 ATF_TEST_CASE_BODY(get_home__invalid)
123 utils::setenv("HOME", "");
124 ATF_REQUIRE(!utils::get_home());
128 ATF_TEST_CASE_WITHOUT_HEAD(setenv);
129 ATF_TEST_CASE_BODY(setenv)
131 ATF_REQUIRE(utils::getenv("PATH"));
132 const std::string oldval = utils::getenv("PATH").get();
133 utils::setenv("PATH", "foo-bar");
134 ATF_REQUIRE(utils::getenv("PATH").get() != oldval);
135 ATF_REQUIRE_EQ("foo-bar", utils::getenv("PATH").get());
137 ATF_REQUIRE(!utils::getenv("__UNDEFINED_VARIABLE__"));
138 utils::setenv("__UNDEFINED_VARIABLE__", "foo2-bar2");
139 ATF_REQUIRE_EQ("foo2-bar2", utils::getenv("__UNDEFINED_VARIABLE__").get());
143 ATF_TEST_CASE_WITHOUT_HEAD(unsetenv);
144 ATF_TEST_CASE_BODY(unsetenv)
146 ATF_REQUIRE(utils::getenv("PATH"));
147 utils::unsetenv("PATH");
148 ATF_REQUIRE(!utils::getenv("PATH"));
152 ATF_INIT_TEST_CASES(tcs)
154 ATF_ADD_TEST_CASE(tcs, getallenv);
156 ATF_ADD_TEST_CASE(tcs, getenv);
158 ATF_ADD_TEST_CASE(tcs, getenv_with_default);
160 ATF_ADD_TEST_CASE(tcs, get_home__ok);
161 ATF_ADD_TEST_CASE(tcs, get_home__missing);
162 ATF_ADD_TEST_CASE(tcs, get_home__invalid);
164 ATF_ADD_TEST_CASE(tcs, setenv);
166 ATF_ADD_TEST_CASE(tcs, unsetenv);