00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #include <stdexcept>
00033 #include <string>
00034 #include <vector>
00035 #include <stdlib.h>
00036 #include <unistd.h>
00037 #include <stdio.h>
00038 #include <time.h>
00039 #include <gtest/gtest.h>
00040 #include <boost/algorithm/string.hpp>
00041 #include <boost/filesystem.hpp>
00042 #include "rospack/rospack.h"
00043 #include "utils.h"
00044
00045
00046 TEST(rospack, reentrant)
00047 {
00048 rospack::ROSPack rp;
00049 std::string output;
00050 int ret = rp.run(std::string("plugins --attrib=foo --top=precedence1 roslang"));
00051 ASSERT_EQ(ret, 0);
00052 ret = rp.run(std::string("find roslang"));
00053 ASSERT_EQ(ret, 0);
00054 ret = rp.run(std::string("list-names"));
00055 ASSERT_EQ(ret, 0);
00056 std::vector<std::string> output_list;
00057 output = rp.getOutput();
00058 boost::trim(output);
00059 boost::split(output_list, output, boost::is_any_of("\n"));
00060 ASSERT_EQ((int)output_list.size(), 4);
00061 ret = rp.run(std::string("list"));
00062 ASSERT_EQ(ret, 0);
00063 output = rp.getOutput();
00064 boost::trim(output);
00065 boost::split(output_list, output, boost::is_any_of("\n"));
00066 ASSERT_EQ((int)output_list.size(), 4);
00067 std::vector<std::string> path_name;
00068 boost::split(path_name, output_list[0], boost::is_any_of(" "));
00069 ASSERT_EQ((int)path_name.size(), 2);
00070 }
00071
00072 TEST(rospack, multiple_rospack_objects)
00073 {
00074 rospack::ROSPack rp;
00075 std::string output;
00076 int ret = rp.run(std::string("plugins --attrib=foo --top=precedence1 roslang"));
00077 ASSERT_EQ(ret, 0);
00078 ret = rp.run(std::string("find roslang"));
00079 ASSERT_EQ(ret, 0);
00080 ret = rp.run(std::string("list-names"));
00081 ASSERT_EQ(ret, 0);
00082 std::vector<std::string> output_list;
00083 output = rp.getOutput();
00084 boost::trim(output);
00085 boost::split(output_list, output, boost::is_any_of("\n"));
00086 ASSERT_EQ((int)output_list.size(), 4);
00087 ret = rp.run(std::string("list"));
00088 ASSERT_EQ(ret, 0);
00089 output = rp.getOutput();
00090 boost::trim(output);
00091 boost::split(output_list, output, boost::is_any_of("\n"));
00092 ASSERT_EQ((int)output_list.size(), 4);
00093 std::vector<std::string> path_name;
00094 boost::split(path_name, output_list[0], boost::is_any_of(" "));
00095 ASSERT_EQ((int)path_name.size(), 2);
00096
00097 rospack::ROSPack rp2;
00098 ret = rp2.run(std::string("plugins --attrib=foo --top=precedence1 roslang"));
00099 ASSERT_EQ(ret, 0);
00100 ret = rp2.run(std::string("find roslang"));
00101 ASSERT_EQ(ret, 0);
00102 ret = rp2.run(std::string("list-names"));
00103 ASSERT_EQ(ret, 0);
00104 output_list.clear();
00105 output = rp2.getOutput();
00106 boost::trim(output);
00107 boost::split(output_list, output, boost::is_any_of("\n"));
00108 ASSERT_EQ((int)output_list.size(), 4);
00109 ret = rp2.run(std::string("list"));
00110 ASSERT_EQ(ret, 0);
00111 output = rp2.getOutput();
00112 boost::trim(output);
00113 boost::split(output_list, output, boost::is_any_of("\n"));
00114 ASSERT_EQ((int)output_list.size(), 4);
00115 path_name.clear();
00116 boost::split(path_name, output_list[0], boost::is_any_of(" "));
00117 ASSERT_EQ((int)path_name.size(), 2);
00118 }
00119
00120 TEST(rospack, deduplicate_tokens)
00121 {
00122 std::string input = "foo foo bar bat bar foobar batbar";
00123 std::string truth = "foo bar bat foobar batbar";
00124 std::string output;
00125 rospack::deduplicate_tokens(input, false, output);
00126 ASSERT_EQ(truth, output);
00127 }
00128
00129
00130 TEST(rospack, env_change)
00131 {
00132
00133 char* oldrpp = getenv("ROS_PACKAGE_PATH");
00134 rospack::ROSPack rp;
00135
00136
00137 char buf[1024];
00138 std::string rr = std::string(getcwd(buf, sizeof(buf))) + "/test2";
00139 setenv("ROS_PACKAGE_PATH", rr.c_str(), 1);
00140 std::vector<std::string> test_pkgs;
00141 test_pkgs.push_back("precedence1");
00142 test_pkgs.push_back("precedence2");
00143 test_pkgs.push_back("precedence3");
00144 test_pkgs.push_back("roslang");
00145 std::string output;
00146 int ret = rp.run(std::string("list-names"));
00147 EXPECT_EQ(ret, 0);
00148 std::vector<std::string> output_list;
00149 output = rp.getOutput();
00150 boost::trim(output);
00151 boost::split(output_list, output, boost::is_any_of("\n"));
00152
00153
00154 EXPECT_EQ(output_list.size(), test_pkgs.size());
00155 for(std::vector<std::string>::const_iterator it = output_list.begin();
00156 it != output_list.end();
00157 ++it)
00158 {
00159 bool found = false;
00160 for(std::vector<std::string>::const_iterator it2 = test_pkgs.begin();
00161 it2 != test_pkgs.end();
00162 ++it2)
00163 {
00164 if(*it == *it2)
00165 {
00166 EXPECT_FALSE(found);
00167 found = true;
00168 }
00169 }
00170 EXPECT_TRUE(found);
00171 }
00172
00173
00174 rr = std::string(getcwd(buf, sizeof(buf))) + "/test3";
00175 setenv("ROS_PACKAGE_PATH", rr.c_str(), 1);
00176 test_pkgs.clear();
00177 test_pkgs.push_back("precedence1");
00178 test_pkgs.push_back("precedence2");
00179 test_pkgs.push_back("precedence3");
00180 ret = rp.run(std::string("list-names"));
00181 EXPECT_EQ(ret, 0);
00182 output_list.clear();
00183 output = rp.getOutput();
00184 boost::trim(output);
00185 boost::split(output_list, output, boost::is_any_of("\n"));
00186
00187
00188 EXPECT_EQ(output_list.size(), test_pkgs.size());
00189 for(std::vector<std::string>::const_iterator it = output_list.begin();
00190 it != output_list.end();
00191 ++it)
00192 {
00193 bool found = false;
00194 for(std::vector<std::string>::const_iterator it2 = test_pkgs.begin();
00195 it2 != test_pkgs.end();
00196 ++it2)
00197 {
00198 if(*it == *it2)
00199 {
00200 EXPECT_FALSE(found);
00201 found = true;
00202 }
00203 }
00204 EXPECT_TRUE(found);
00205 }
00206
00207
00208 rr = std::string(getcwd(buf, sizeof(buf))) + "/test_empty";
00209 setenv("ROS_PACKAGE_PATH", rr.c_str(), 1);
00210 ret = rp.run(std::string("list-names"));
00211 EXPECT_EQ(ret, 0);
00212 output = rp.getOutput();
00213 boost::trim(output);
00214 EXPECT_EQ(output, std::string());
00215
00216
00217 setenv("ROS_PACKAGE_PATH", oldrpp, 1);
00218 }
00219
00220 int main(int argc, char **argv)
00221 {
00222
00223 (void)rospack::ROSPACK_NAME;
00224 (void)rospack::ROSSTACK_NAME;
00225
00226 char buf[1024];
00227 std::string rr = std::string(getcwd(buf, sizeof(buf))) + "/test2";
00228 setenv("ROS_PACKAGE_PATH", rr.c_str(), 1);
00229 char path[PATH_MAX];
00230 if(getcwd(path,sizeof(path)))
00231 {
00232 boost::filesystem::path p(path);
00233 p = p.parent_path();
00234 std::string newpath = p.string();
00235 char* oldpath = getenv("PATH");
00236 if(oldpath)
00237 newpath += std::string(":") + oldpath;
00238 setenv("PATH", newpath.c_str(), 1);
00239 }
00240
00241 testing::InitGoogleTest(&argc, argv);
00242 return RUN_ALL_TESTS();
00243 }