test_sr_description_urdf.cpp
Go to the documentation of this file.
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2008, Willow Garage, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 * * Neither the name of the Willow Garage nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *********************************************************************/
34 
39 #include <gtest/gtest.h>
40 #include <cstdlib>
41 
42 #include <dirent.h>
43 #include <sys/types.h>
44 #include <sys/param.h>
45 #include <sys/stat.h>
46 #include <unistd.h>
47 #include <stdio.h>
48 #include <string>
49 
50 #include <iostream>
51 
52 int runExternalProcess(const std::string &executable, const std::string &args)
53 {
54  return system((executable + " " + args).c_str());
55 }
56 
57 std::string getCommandOutput(std::string cmd)
58 {
59  std::string data;
60  FILE * stream;
61 
62  stream = popen(cmd.c_str(), "r");
63  if (stream)
64  {
65  char buffer[MAXPATHLEN];
66  while (!feof(stream))
67  if (fgets(buffer, MAXPATHLEN, stream) != NULL) data.append(buffer);
68  pclose(stream);
69  }
70  // keep anything before first cr/lf
71  unsigned pos = data.find_first_of('\n');
72  return data.substr(0, pos);
73 }
74 
75 int walker(char *result, int *test_result)
76 {
77  std::string package_path = getCommandOutput("rospack find sr_description");
78 
79  if (package_path.find("sr_description") == std::string::npos)
80  {
81  printf("cannot find package in path %s\n", package_path.c_str());
82  *test_result = 1;
83  return 1;
84  }
85  else
86  {
87  printf("sr_description robots path : %s\n", (package_path+"/robots").c_str());
88  }
89 
90  DIR *d;
91  struct dirent *dir;
92  d = opendir((package_path+"/robots").c_str());
93  if (d == NULL)
94  {
95  printf("no robots found\n");
96  *test_result = 1;
97  return 1;
98  }
99  while ((dir = readdir(d)))
100  {
101  if (strcmp(dir->d_name, ".") == 0 ||
102  strcmp(dir->d_name, "..") == 0)
103  {
104  continue;
105  }
106  if (dir->d_type != DT_DIR)
107  {
108  std::string dir_name = dir->d_name;
109  if (dir_name.find(std::string(".urdf.xacro")) == dir_name.size()-11)
110  {
111  printf("\n\ntesting: %s\n", (package_path+"/robots/"+dir_name).c_str());
112  printf("xacro --inorder %s/robots/%s > "
113  "`rospack find sr_description`/test/tmp.urdf",
114  package_path.c_str(), dir_name.c_str() );
115  runExternalProcess("xacro --inorder",
116  package_path+"/robots/"+dir_name+" > `rospack find sr_description`/test/tmp.urdf");
117  // check urdf structure
118  *test_result = *test_result || runExternalProcess("check_urdf", "`rospack find sr_description`/test/tmp.urdf");
119  printf("\n looking for unexpanded xacro tags\n");
120  // check for unexpanded xacros
121  *test_result = *test_result ||
122  !runExternalProcess("grep", "'<xacro:' `rospack find sr_description`/test/tmp.urdf");
123  }
124  if (*test_result != 0)
125  return *result == 0;
126  }
127  }
128  closedir(d);
129  return *result == 0;
130 }
131 
132 TEST(URDF, CorrectFormat)
133 {
134  int test_result = 0;
135 
136  char buf[MAXPATHLEN] = { 0 };
137  if (walker(buf, &test_result) == 0)
138  {
139  printf("Found: %s\n", buf);
140  }
141  else
142  {
143  puts("Not found");
144  }
145 
146  EXPECT_EQ(test_result, 0);
147 }
148 
149 int main(int argc, char **argv)
150 {
151  testing::InitGoogleTest(&argc, argv);
152  return RUN_ALL_TESTS();
153 }
d
int runExternalProcess(const std::string &executable, const std::string &args)
TEST(URDF, CorrectFormat)
int walker(char *result, int *test_result)
int main(int argc, char **argv)
std::string getCommandOutput(std::string cmd)


sr_description
Author(s): Ugo Cupcic
autogenerated on Tue Oct 13 2020 03:12:33