forked from degauden/Elements
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGetEnv_test.cpp
More file actions
187 lines (138 loc) · 4.66 KB
/
Copy pathGetEnv_test.cpp
File metadata and controls
187 lines (138 loc) · 4.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/**
* @file GetEnv_test.cpp
*
* @date Aug 27, 2015
* @author Hubert Degaudenzi
*
* @copyright 2012-2020 Euclid Science Ground Segment
*
* This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General
* Public License as published by the Free Software Foundation; either version 3.0 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include <cstdlib> // for getenv, setenv, unsetenv
#include <string> // for allocator, operator==, string
#include <boost/test/unit_test.hpp>
#include "ElementsKernel/System.h" // for getEnv, isEnvSet, setEnv, unSetEnv
using std::getenv; // standard
using std::string;
namespace Elements {
BOOST_AUTO_TEST_SUITE(GetEnv_test)
//-----------------------------------------------------------------------------
BOOST_AUTO_TEST_CASE(Raw_test) {
string path_var{};
const char* tmp = getenv("PATH");
if (tmp != nullptr) {
path_var = tmp;
}
// check that the variable exists
BOOST_CHECK(tmp != nullptr);
// check that it is not empty
BOOST_CHECK(not path_var.empty());
string not_existing_path{};
const char* tmp_2 = getenv("Jlhjdji43k");
if (tmp_2 != nullptr) {
not_existing_path = tmp_2;
}
// check that the variable doesn't exist
BOOST_CHECK(tmp_2 == nullptr);
// the value is also empty
BOOST_CHECK(not_existing_path.empty());
}
BOOST_AUTO_TEST_CASE(RawEmpty_test) {
const string var_name{"Dldoed7dja7c"};
// create empty test env variable
setenv(var_name.c_str(), "", 1);
string path_var{};
const char* tmp = getenv(var_name.c_str());
if (tmp != nullptr) {
path_var = tmp;
}
// check that the variable exists
BOOST_CHECK(tmp != nullptr);
// check that it is empty
BOOST_CHECK(path_var.empty());
// clean-up
unsetenv(var_name.c_str());
// the env variable has ceased to be
BOOST_CHECK(getenv(var_name.c_str()) == nullptr);
}
BOOST_AUTO_TEST_CASE(StringWrap_test) {
using System::getEnv;
using System::isEnvSet;
const string name_var{"PATH"};
const string path_var = getenv("PATH");
const string path_var2 = getEnv(name_var);
const string path_var3 = getEnv("PATH");
BOOST_CHECK_EQUAL(path_var, path_var2);
BOOST_CHECK_EQUAL(path_var, path_var3);
BOOST_CHECK(isEnvSet("PATH"));
BOOST_CHECK(isEnvSet(name_var));
}
BOOST_AUTO_TEST_CASE(EmptyEnv_test) {
using System::getEnv;
const string rnd_name{"Dldoed7dja7c"};
const string name_var{"PATH"};
string value_var{};
// the variable exists
BOOST_CHECK(getEnv(name_var, value_var));
// and it is not empty
BOOST_CHECK(not value_var.empty());
// the variable doesn't exists
BOOST_CHECK(not getEnv(rnd_name, value_var));
// and its value is empty
BOOST_CHECK(value_var.empty());
// create empty test env variable
setenv(rnd_name.c_str(), "", 1);
// the variable exists
BOOST_CHECK(getEnv(rnd_name, value_var));
// and its value is empty
BOOST_CHECK(value_var.empty());
// destroy the empty test env variable
unsetenv(rnd_name.c_str());
}
BOOST_AUTO_TEST_CASE(Set_test) {
using System::getEnv;
using System::isEnvSet;
using System::setEnv;
const string rnd_name{"Dldoed7dja7c"};
BOOST_CHECK(not isEnvSet(rnd_name));
const int r = setEnv(rnd_name, "");
BOOST_CHECK(r == 0);
BOOST_CHECK(isEnvSet(rnd_name));
BOOST_CHECK(getEnv(rnd_name).empty());
const int r2 = setEnv(rnd_name, "toto", false);
BOOST_CHECK(r2 == 0);
BOOST_CHECK(isEnvSet(rnd_name));
BOOST_CHECK(getEnv(rnd_name).empty());
const int r3 = setEnv(rnd_name, "titi");
BOOST_CHECK(r3 == 0);
BOOST_CHECK(isEnvSet(rnd_name));
BOOST_CHECK(getEnv(rnd_name) == "titi");
}
BOOST_AUTO_TEST_CASE(UnSet_test) {
using System::getEnv;
using System::isEnvSet;
using System::setEnv;
using System::unSetEnv;
const string rnd_name{"Dldoed7dja7c"};
const int r = setEnv(rnd_name, "");
BOOST_CHECK(r == 0);
BOOST_CHECK(isEnvSet(rnd_name));
BOOST_CHECK(getEnv(rnd_name) == "");
const int r2 = unSetEnv(rnd_name);
BOOST_CHECK(r2 == 0);
BOOST_CHECK(not isEnvSet(rnd_name));
BOOST_CHECK(getEnv(rnd_name) == "");
}
//-----------------------------------------------------------------------------
BOOST_AUTO_TEST_SUITE_END()
} // namespace Elements