41 #include <gtest/gtest.h> 42 #include <boost/algorithm/string.hpp> 43 #include <boost/filesystem.hpp> 48 int setenv(
const char *name,
const char *value,
int overwrite)
53 errno_t errcode = getenv_s(&envsize, NULL, 0, name);
54 if(errcode || envsize)
57 return _putenv_s(name, value);
65 int ret = rp.
run(std::string(
"plugins --attrib=foo --top=precedence1 roslang"));
67 ret = rp.
run(std::string(
"find roslang"));
69 ret = rp.
run(std::string(
"list-names"));
71 std::vector<std::string> output_list;
74 boost::split(output_list, output, boost::is_any_of(
"\n"));
75 ASSERT_EQ((
int)output_list.size(), 4);
76 ret = rp.
run(std::string(
"list"));
80 boost::split(output_list, output, boost::is_any_of(
"\n"));
81 ASSERT_EQ((
int)output_list.size(), 4);
82 std::vector<std::string> path_name;
83 boost::split(path_name, output_list[0], boost::is_any_of(
" "));
84 ASSERT_EQ((
int)path_name.size(), 2);
91 int ret = rp.
run(std::string(
"plugins --attrib=foo --top=precedence1 roslang"));
93 ret = rp.
run(std::string(
"find roslang"));
95 ret = rp.
run(std::string(
"list-names"));
97 std::vector<std::string> output_list;
100 boost::split(output_list, output, boost::is_any_of(
"\n"));
101 ASSERT_EQ((
int)output_list.size(), 4);
102 ret = rp.
run(std::string(
"list"));
106 boost::split(output_list, output, boost::is_any_of(
"\n"));
107 ASSERT_EQ((
int)output_list.size(), 4);
108 std::vector<std::string> path_name;
109 boost::split(path_name, output_list[0], boost::is_any_of(
" "));
110 ASSERT_EQ((
int)path_name.size(), 2);
113 ret = rp2.
run(std::string(
"plugins --attrib=foo --top=precedence1 roslang"));
115 ret = rp2.run(std::string(
"find roslang"));
117 ret = rp2.run(std::string(
"list-names"));
120 output = rp2.getOutput();
122 boost::split(output_list, output, boost::is_any_of(
"\n"));
123 ASSERT_EQ((
int)output_list.size(), 4);
124 ret = rp2.run(std::string(
"list"));
126 output = rp2.getOutput();
128 boost::split(output_list, output, boost::is_any_of(
"\n"));
129 ASSERT_EQ((
int)output_list.size(), 4);
131 boost::split(path_name, output_list[0], boost::is_any_of(
" "));
132 ASSERT_EQ((
int)path_name.size(), 2);
137 std::string input =
"foo foo bar bat bar foobar batbar";
138 std::string truth =
"foo bar bat foobar batbar";
141 ASSERT_EQ(truth, output);
148 char* oldrpp = getenv(
"ROS_PACKAGE_PATH");
153 std::string rr = std::string(getcwd(buf,
sizeof(buf))) +
"/test2";
154 setenv(
"ROS_PACKAGE_PATH", rr.c_str(), 1);
155 std::vector<std::string> test_pkgs;
156 test_pkgs.push_back(
"precedence1");
157 test_pkgs.push_back(
"precedence2");
158 test_pkgs.push_back(
"precedence3");
159 test_pkgs.push_back(
"roslang");
161 int ret = rp.
run(std::string(
"list-names"));
163 std::vector<std::string> output_list;
166 boost::split(output_list, output, boost::is_any_of(
"\n"));
169 EXPECT_EQ(output_list.size(), test_pkgs.size());
170 for(std::vector<std::string>::const_iterator it = output_list.begin();
171 it != output_list.end();
175 for(std::vector<std::string>::const_iterator it2 = test_pkgs.begin();
176 it2 != test_pkgs.end();
189 rr = std::string(getcwd(buf,
sizeof(buf))) +
"/test3";
190 setenv(
"ROS_PACKAGE_PATH", rr.c_str(), 1);
192 test_pkgs.push_back(
"precedence1");
193 test_pkgs.push_back(
"precedence2");
194 test_pkgs.push_back(
"precedence3");
195 ret = rp.
run(std::string(
"list-names"));
200 boost::split(output_list, output, boost::is_any_of(
"\n"));
203 EXPECT_EQ(output_list.size(), test_pkgs.size());
204 for(std::vector<std::string>::const_iterator it = output_list.begin();
205 it != output_list.end();
209 for(std::vector<std::string>::const_iterator it2 = test_pkgs.begin();
210 it2 != test_pkgs.end();
223 rr = std::string(getcwd(buf,
sizeof(buf))) +
"/test_empty";
224 setenv(
"ROS_PACKAGE_PATH", rr.c_str(), 1);
225 ret = rp.
run(std::string(
"list-names"));
229 EXPECT_EQ(output, std::string());
232 setenv(
"ROS_PACKAGE_PATH", oldrpp, 1);
235 int main(
int argc,
char **argv)
242 std::string rr = std::string(getcwd(buf,
sizeof(buf))) +
"/test2";
243 setenv(
"ROS_PACKAGE_PATH", rr.c_str(), 1);
244 char *path = getcwd(NULL, 0);
247 boost::filesystem::path p(path);
249 std::string newpath = p.string();
250 char* oldpath = getenv(
"PATH");
252 newpath += std::string(
":") + oldpath;
253 setenv(
"PATH", newpath.c_str(), 1);
257 testing::InitGoogleTest(&argc, argv);
258 return RUN_ALL_TESTS();
void deduplicate_tokens(const std::string &instring, bool last, std::string &outstring)
int main(int argc, char **argv)
static const char * ROSSTACK_NAME
static const char * ROSPACK_NAME
int run(int argc, char **argv)
Run rospack with the given arguments. Call getOutput() to get the result.
std::string getOutput()
Get the output from the last successful run() call.
Backward compatibility API for librospack (DEPRECATED).