Codebase list ros-ros / d46534f
Fix build issue on Windows (#186) Johnson Shih authored 5 years ago Dirk Thomas committed 5 years ago
2 changed file(s) with 32 addition(s) and 10 deletion(s). Raw diff Collapse all Expand all
3030
3131 #include <gtest/gtest.h>
3232 #include <ros/package.h>
33 #include <sys/time.h>
3433
3534 using namespace ros;
3635
3131 #include <string>
3232 #include <vector>
3333 #include <stdlib.h>
34 #ifndef _WIN32
3435 #include <unistd.h>
36 #endif
37 #include <boost/filesystem.hpp>
3538 #include <boost/thread.hpp>
3639 #include <gtest/gtest.h>
3740 #include "ros/package.h"
41
42 #ifdef _WIN32
43 int setenv(const char *name, const char *value, int overwrite)
44 {
45 if(!overwrite)
46 {
47 size_t envsize = 0;
48 errno_t errcode = getenv_s(&envsize, NULL, 0, name);
49 if(errcode || envsize)
50 return errcode;
51 }
52 return _putenv_s(name, value);
53 }
54 #endif
3855
3956 void string_split(const std::string &s, std::vector<std::string> &t, const std::string &d)
4057 { t.clear();
4966 t.push_back(s.substr(start));
5067 }
5168
52 char g_rr_buf[1024];
69 std::string g_rr_path;
5370 void set_env_vars(void)
5471 {
5572 // Point ROS_PACKAGE_PATH at the roslib directory, and point
5673 // ROS_ROOT into an empty directory.
57 getcwd(g_rr_buf, sizeof(g_rr_buf));
58 setenv("ROS_PACKAGE_PATH", g_rr_buf, 1);
59 strncpy(g_rr_buf+strlen(g_rr_buf), "/tmp.XXXXXX", sizeof(g_rr_buf)-strlen(g_rr_buf)-1);
60 g_rr_buf[sizeof(g_rr_buf)-1] = '\0';
61 mkdtemp(g_rr_buf);
62 setenv("ROS_ROOT", g_rr_buf, 1);
74 char rr_buf[1024];
75 getcwd(rr_buf, sizeof(rr_buf));
76 setenv("ROS_PACKAGE_PATH", rr_buf, 1);
77
78 boost::filesystem::path temp_path = boost::filesystem::unique_path();
79 boost::filesystem::create_directories(temp_path);
80 g_rr_path = temp_path.string();
81 setenv("ROS_ROOT", g_rr_path.c_str(), 1);
6382 }
6483 void cleanup_env_vars(void)
6584 {
6685 // Remove the empty directory that we created in set_env_vars().
67 rmdir(g_rr_buf);
68 memset(g_rr_buf, 0, sizeof(g_rr_buf));
86 rmdir(g_rr_path.c_str());
87 g_rr_path.clear();
6988 }
7089
7190 TEST(roslib, commandListNames)
117136 for(int i=0;i<100;i++)
118137 {
119138 output = ros::package::command("plugins --attrib=foo roslib");
139 #ifndef _WIN32
120140 nanosleep(&ts, NULL);
141 #else
142 boost::this_thread::sleep(boost::posix_time::milliseconds(1));
143 #endif
121144 }
122145 }
123146