utest.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008, Willow Garage, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
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 Willow Garage, Inc. nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 /* Author: Brian Gerkey */
31 
32 #include <stdexcept> // for std::runtime_error
33 #include <string>
34 #include <vector>
35 #include <stdlib.h>
36 #include <unistd.h>
37 #include <stdio.h>
38 #include <time.h>
39 #include <gtest/gtest.h>
40 #include <boost/algorithm/string.hpp>
41 #include <boost/filesystem.hpp>
42 #include "rospack/rospack.h"
43 #include "utils.h"
44 
45 
46 TEST(rospack, reentrant)
47 {
49  std::string output;
50  int ret = rp.run(std::string("plugins --attrib=foo --top=precedence1 roslang"));
51  ASSERT_EQ(ret, 0);
52  ret = rp.run(std::string("find roslang"));
53  ASSERT_EQ(ret, 0);
54  ret = rp.run(std::string("list-names"));
55  ASSERT_EQ(ret, 0);
56  std::vector<std::string> output_list;
57  output = rp.getOutput();
58  boost::trim(output);
59  boost::split(output_list, output, boost::is_any_of("\n"));
60  ASSERT_EQ((int)output_list.size(), 4);
61  ret = rp.run(std::string("list"));
62  ASSERT_EQ(ret, 0);
63  output = rp.getOutput();
64  boost::trim(output);
65  boost::split(output_list, output, boost::is_any_of("\n"));
66  ASSERT_EQ((int)output_list.size(), 4);
67  std::vector<std::string> path_name;
68  boost::split(path_name, output_list[0], boost::is_any_of(" "));
69  ASSERT_EQ((int)path_name.size(), 2);
70 }
71 
72 TEST(rospack, multiple_rospack_objects)
73 {
75  std::string output;
76  int ret = rp.run(std::string("plugins --attrib=foo --top=precedence1 roslang"));
77  ASSERT_EQ(ret, 0);
78  ret = rp.run(std::string("find roslang"));
79  ASSERT_EQ(ret, 0);
80  ret = rp.run(std::string("list-names"));
81  ASSERT_EQ(ret, 0);
82  std::vector<std::string> output_list;
83  output = rp.getOutput();
84  boost::trim(output);
85  boost::split(output_list, output, boost::is_any_of("\n"));
86  ASSERT_EQ((int)output_list.size(), 4);
87  ret = rp.run(std::string("list"));
88  ASSERT_EQ(ret, 0);
89  output = rp.getOutput();
90  boost::trim(output);
91  boost::split(output_list, output, boost::is_any_of("\n"));
92  ASSERT_EQ((int)output_list.size(), 4);
93  std::vector<std::string> path_name;
94  boost::split(path_name, output_list[0], boost::is_any_of(" "));
95  ASSERT_EQ((int)path_name.size(), 2);
96 
97  rospack::ROSPack rp2;
98  ret = rp2.run(std::string("plugins --attrib=foo --top=precedence1 roslang"));
99  ASSERT_EQ(ret, 0);
100  ret = rp2.run(std::string("find roslang"));
101  ASSERT_EQ(ret, 0);
102  ret = rp2.run(std::string("list-names"));
103  ASSERT_EQ(ret, 0);
104  output_list.clear();
105  output = rp2.getOutput();
106  boost::trim(output);
107  boost::split(output_list, output, boost::is_any_of("\n"));
108  ASSERT_EQ((int)output_list.size(), 4);
109  ret = rp2.run(std::string("list"));
110  ASSERT_EQ(ret, 0);
111  output = rp2.getOutput();
112  boost::trim(output);
113  boost::split(output_list, output, boost::is_any_of("\n"));
114  ASSERT_EQ((int)output_list.size(), 4);
115  path_name.clear();
116  boost::split(path_name, output_list[0], boost::is_any_of(" "));
117  ASSERT_EQ((int)path_name.size(), 2);
118 }
119 
121 {
122  std::string input = "foo foo bar bat bar foobar batbar";
123  std::string truth = "foo bar bat foobar batbar";
124  std::string output;
125  rospack::deduplicate_tokens(input, false, output);
126  ASSERT_EQ(truth, output);
127 }
128 
129 // Test that env var changes between runs still produce the right results.
130 TEST(rospack, env_change)
131 {
132  // Get old path for resetting later, to avoid cross-talk with other tests
133  char* oldrpp = getenv("ROS_PACKAGE_PATH");
134  rospack::ROSPack rp;
135 
136  // Case 1: RPP=/test2
137  char buf[1024];
138  std::string rr = std::string(getcwd(buf, sizeof(buf))) + "/test2";
139  setenv("ROS_PACKAGE_PATH", rr.c_str(), 1);
140  std::vector<std::string> test_pkgs;
141  test_pkgs.push_back("precedence1");
142  test_pkgs.push_back("precedence2");
143  test_pkgs.push_back("precedence3");
144  test_pkgs.push_back("roslang");
145  std::string output;
146  int ret = rp.run(std::string("list-names"));
147  EXPECT_EQ(ret, 0);
148  std::vector<std::string> output_list;
149  output = rp.getOutput();
150  boost::trim(output);
151  boost::split(output_list, output, boost::is_any_of("\n"));
152  // Check that we get back the right list of packages (which could be in any
153  // order).
154  EXPECT_EQ(output_list.size(), test_pkgs.size());
155  for(std::vector<std::string>::const_iterator it = output_list.begin();
156  it != output_list.end();
157  ++it)
158  {
159  bool found = false;
160  for(std::vector<std::string>::const_iterator it2 = test_pkgs.begin();
161  it2 != test_pkgs.end();
162  ++it2)
163  {
164  if(*it == *it2)
165  {
166  EXPECT_FALSE(found);
167  found = true;
168  }
169  }
170  EXPECT_TRUE(found);
171  }
172 
173  // Case 2: RPP=/test3
174  rr = std::string(getcwd(buf, sizeof(buf))) + "/test3";
175  setenv("ROS_PACKAGE_PATH", rr.c_str(), 1);
176  test_pkgs.clear();
177  test_pkgs.push_back("precedence1");
178  test_pkgs.push_back("precedence2");
179  test_pkgs.push_back("precedence3");
180  ret = rp.run(std::string("list-names"));
181  EXPECT_EQ(ret, 0);
182  output_list.clear();
183  output = rp.getOutput();
184  boost::trim(output);
185  boost::split(output_list, output, boost::is_any_of("\n"));
186  // Check that we get back the right list of packages (which could be in any
187  // order).
188  EXPECT_EQ(output_list.size(), test_pkgs.size());
189  for(std::vector<std::string>::const_iterator it = output_list.begin();
190  it != output_list.end();
191  ++it)
192  {
193  bool found = false;
194  for(std::vector<std::string>::const_iterator it2 = test_pkgs.begin();
195  it2 != test_pkgs.end();
196  ++it2)
197  {
198  if(*it == *it2)
199  {
200  EXPECT_FALSE(found);
201  found = true;
202  }
203  }
204  EXPECT_TRUE(found);
205  }
206 
207  // Case 3: RPP=/test_empty
208  rr = std::string(getcwd(buf, sizeof(buf))) + "/test_empty";
209  setenv("ROS_PACKAGE_PATH", rr.c_str(), 1);
210  ret = rp.run(std::string("list-names"));
211  EXPECT_EQ(ret, 0);
212  output = rp.getOutput();
213  boost::trim(output);
214  EXPECT_EQ(output, std::string());
215 
216  // Reset old path, for other tests
217  setenv("ROS_PACKAGE_PATH", oldrpp, 1);
218 }
219 
220 int main(int argc, char **argv)
221 {
222  // Quiet some warnings
223  (void)rospack::ROSPACK_NAME;
225 
226  char buf[1024];
227  std::string rr = std::string(getcwd(buf, sizeof(buf))) + "/test2";
228  setenv("ROS_PACKAGE_PATH", rr.c_str(), 1);
229  char path[PATH_MAX];
230  if(getcwd(path,sizeof(path)))
231  {
232  boost::filesystem::path p(path);
233  p = p.parent_path();
234  std::string newpath = p.string();
235  char* oldpath = getenv("PATH");
236  if(oldpath)
237  newpath += std::string(":") + oldpath;
238  setenv("PATH", newpath.c_str(), 1);
239  }
240 
241  testing::InitGoogleTest(&argc, argv);
242  return RUN_ALL_TESTS();
243 }
void deduplicate_tokens(const std::string &instring, bool last, std::string &outstring)
Definition: utils.cpp:43
TEST(rospack, reentrant)
Definition: utest.cpp:46
int main(int argc, char **argv)
Definition: utest.cpp:220
static const char * ROSSTACK_NAME
Definition: utils.h:43
static const char * ROSPACK_NAME
Definition: utils.h:42
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).


rospack
Author(s): Brian Gerkey, Morgan Quigley, Dirk Thomas
autogenerated on Thu Dec 10 2020 03:42:11