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"));
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"));
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);
238 void trimAll(std::vector<std::string> &inout)
240 for (ssize_t i = inout.size() - 1; i >= 0; --i)
242 boost::trim(inout[i]);
244 if (inout[i].empty()) inout.erase(inout.begin() + i);
248 ::testing::AssertionResult outputEqual(std::string output,
const std::string &expected,
bool trim =
true)
250 if (trim) boost::trim(output);
251 if (output == expected) return ::testing::AssertionSuccess();
252 return ::testing::AssertionFailure() <<
"Output did not match: " << output;
255 ::testing::AssertionResult outputEqual(std::string output,
const std::vector<std::string> &expected,
bool trim =
true)
258 std::vector<std::string> output_lines;
259 boost::split( output_lines, output, [](
char c) {
return c ==
'\n'; });
260 if (trim) trimAll( output_lines);
261 if ( output_lines.size() != expected.size())
262 return ::testing::AssertionFailure() <<
"output line count did not match expected: " << output_lines.size() << std::endl << boost::join( output_lines,
"\n");
263 for (
size_t i = 0; i < output_lines.size(); ++i)
265 for (
size_t j = i + 1; j < output_lines.size(); ++j)
267 if ( output_lines[i] == output_lines[j])
268 return ::testing::AssertionFailure() <<
"output contained a duplicate: " << output_lines[i];
271 for (
auto &dep : output_lines)
274 for (
auto &line : expected)
276 if (line != dep)
continue;
281 return ::testing::AssertionFailure() <<
"output contained '" << dep <<
"' which was not expected!";
283 return ::testing::AssertionSuccess();
286 ::testing::AssertionResult outputEndsWith(std::string output,
const std::vector<std::string> &expected,
bool trim =
true)
289 std::vector<std::string> output_lines;
290 boost::split( output_lines, output, [](
char c) {
return c ==
'\n' || c ==
' '; });
291 if (trim) trimAll( output_lines);
292 if (output_lines.size() != expected.size())
293 return ::testing::AssertionFailure() <<
"output line count did not match expected: " << output_lines.size() << std::endl << boost::join( output_lines,
"\n");
294 for (
size_t i = 0; i < output_lines.size(); ++i)
296 for (
size_t j = i + 1; j < output_lines.size(); ++j)
298 if (output_lines[i] == output_lines[j])
299 return ::testing::AssertionFailure() <<
"output contained a duplicate: " << output_lines[i];
302 for (
auto &dep : output_lines)
305 for (
auto &line : expected)
307 if (dep.length() < line.length())
continue;
308 if (line != dep.substr(dep.length() - line.length()))
continue;
313 return ::testing::AssertionFailure() <<
"output contained '" << dep <<
"' which was not expected!";
315 return ::testing::AssertionSuccess();
322 char* oldrpp = getenv(
"ROS_PACKAGE_PATH");
325 std::string rr = std::string(getcwd(buf,
sizeof(buf))) +
"/test_errors";
326 setenv(
"ROS_PACKAGE_PATH", rr.c_str(), 1);
329 int ret = rp.
run(
"deps valid_package");
331 EXPECT_TRUE(outputEqual(rp.
getOutput(), std::vector<std::string> {
"base",
"base2"}));
334 ret = rp.
run(
"deps invalid_package_one");
336 EXPECT_TRUE(outputEqual(rp.
getOutput(), std::vector<std::string> {
"valid_package",
"base",
"base2"} ));
339 ret = rp.
run(
"deps invalid_package_two");
341 EXPECT_TRUE(outputEqual(rp.
getOutput(), {
"invalid_package_one",
"valid_package",
"base",
"base2",
"another_valid_package"}));
344 ret = rp.
run(
"depends1 direct_deps_valid");
346 EXPECT_TRUE(outputEqual(rp.
getOutput(), std::vector<std::string> {
"valid_package",
"invalid_package_one"}));
349 ret = rp.
run(
"depends-manifests invalid_package_one");
351 EXPECT_TRUE(outputEndsWith(rp.
getOutput(), std::vector<std::string> {
"valid_package/manifest.xml",
"base/manifest.xml",
"base2/manifest.xml"}));
354 ret = rp.
run(
"depends-indent invalid_package_one");
356 EXPECT_TRUE(outputEqual(rp.
getOutput(), std::vector<std::string> {
"valid_package",
" base",
" base2"},
false));
359 ret = rp.
run(
"depends-why --target=base invalid_package_two");
361 EXPECT_TRUE(outputEqual(rp.
getOutput(), std::vector<std::string> {
"Dependency chains from invalid_package_two to base:",
362 "* invalid_package_two -> invalid_package_one -> valid_package -> base"}));
365 ret = rp.
run(
"depends-on base");
367 EXPECT_TRUE(outputEqual(rp.
getOutput(), {
"direct_deps_valid",
"valid_package",
"invalid_package_one",
"invalid_package_two",
"invalid_roslang_package"}));
370 ret = rp.
run(
"depends-on1 base");
372 EXPECT_TRUE(outputEqual(rp.
getOutput(),
"valid_package"));
374 ret = rp.
run(
"depends-on1 valid_package");
376 EXPECT_TRUE(outputEqual(rp.
getOutput(), std::vector<std::string> {
"direct_deps_valid",
"invalid_package_one"}));
379 setenv(
"ROS_PACKAGE_PATH", oldrpp, 1);
382 int main(
int argc,
char **argv)
389 std::string rr = std::string(getcwd(buf,
sizeof(buf))) +
"/test2";
390 setenv(
"ROS_PACKAGE_PATH", rr.c_str(), 1);
391 char *path = getcwd(NULL, 0);
394 boost::filesystem::path p(path);
396 std::string newpath = p.string();
397 char* oldpath = getenv(
"PATH");
399 newpath += std::string(
":") + oldpath;
400 setenv(
"PATH", newpath.c_str(), 1);
404 testing::InitGoogleTest(&argc, argv);
405 return RUN_ALL_TESTS();