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 import roslib; roslib.load_manifest('test_roslib')
00033
00034 import os
00035 import sys
00036 import unittest
00037
00038 import roslib.rosenv
00039 import rosunit
00040
00041 class EnvTest(unittest.TestCase):
00042
00043 def test_get_ros_root(self):
00044 from roslib.rosenv import get_ros_root
00045 self.assertEquals(None, get_ros_root(required=False, env={}))
00046 self.assertEquals(None, get_ros_root(False, {}))
00047 try:
00048 get_ros_root(required=True, env={})
00049 self.fail("get_ros_root should have failed")
00050 except: pass
00051
00052 env = {'ROS_ROOT': '/fake/path'}
00053 self.assertEquals('/fake/path', get_ros_root(required=False, env=env))
00054 try:
00055 get_ros_root(required=True, env=env)
00056 self.fail("get_ros_root should have failed")
00057 except: pass
00058
00059 real_ros_root = get_ros_root(required=True)
00060
00061
00062 p = os.path.join(real_ros_root, 'Makefile')
00063 env = {'ROS_ROOT': p}
00064 self.assertEquals(p, get_ros_root(required=False, env=env))
00065 try:
00066 get_ros_root(required=True, env=env)
00067 self.fail("get_ros_root should have failed")
00068 except: pass
00069
00070 def test_get_ros_package_path(self):
00071 from roslib.rosenv import get_ros_package_path
00072 self.assertEquals(None, get_ros_package_path(required=False, env={}))
00073 self.assertEquals(None, get_ros_package_path(False, {}))
00074 try:
00075 get_ros_package_path(required=True, env={})
00076 self.fail("get_ros_package_path should have raised")
00077 except: pass
00078 env = {'ROS_PACKAGE_PATH': ':'}
00079 self.assertEquals(':', get_ros_package_path(True, env=env))
00080 self.assertEquals(':', get_ros_package_path(False, env=env))
00081
00082
00083
00084 self.assertEquals(os.environ.get('ROS_PACKAGE_PATH', None), get_ros_package_path(required=False))
00085
00086 def test_get_ros_master_uri(self):
00087 from roslib.rosenv import get_master_uri
00088 self.assertEquals(None, get_master_uri(required=False, env={}))
00089 self.assertEquals(None, get_master_uri(False, {}))
00090 try:
00091 get_master_uri(required=True, env={})
00092 self.fail("get_ros_package_path should have raised")
00093 except: pass
00094 env = {'ROS_MASTER_URI': 'http://localhost:1234'}
00095 self.assertEquals('http://localhost:1234', get_master_uri(True, env=env))
00096 self.assertEquals('http://localhost:1234', get_master_uri(False, env=env))
00097
00098 argv = ['__master:=http://localhost:5678']
00099 self.assertEquals('http://localhost:5678', get_master_uri(False, env=env, argv=argv))
00100
00101 try:
00102 argv = ['__master:=http://localhost:5678:=http://localhost:1234']
00103 get_master_uri(required=False, env=env, argv=argv)
00104 self.fail("should have thrown")
00105 except roslib.rosenv.ROSEnvException: pass
00106
00107 try:
00108 argv = ['__master:=']
00109 get_master_uri(False, env=env, argv=argv)
00110 self.fail("should have thrown")
00111 except roslib.rosenv.ROSEnvException: pass
00112
00113
00114 self.assertEquals(os.environ.get('ROS_MASTER_URI', None), get_master_uri(required=False))
00115
00116 def test_get_log_dir(self):
00117 from roslib.roslogging import get_log_dir
00118 from roslib.rosenv import get_ros_root
00119 import tempfile, os
00120 base = tempfile.gettempdir()
00121 ros_log_dir = os.path.join(base, 'ros_log_dir')
00122 ros_home_dir = os.path.join(base, 'ros_home_dir')
00123 home_dir = os.path.expanduser('~')
00124
00125
00126 env = {'ROS_ROOT': get_ros_root(), 'ROS_LOG_DIR': ros_log_dir, 'ROS_HOME': ros_home_dir }
00127 self.assertEquals(ros_log_dir, get_log_dir(env=env))
00128
00129 env = {'ROS_ROOT': get_ros_root(), 'ROS_HOME': ros_home_dir }
00130 self.assertEquals(os.path.join(ros_home_dir, 'log'), get_log_dir(env=env))
00131
00132 env = {'ROS_ROOT': get_ros_root()}
00133 self.assertEquals(os.path.join(home_dir, '.ros', 'log'), get_log_dir(env=env))
00134
00135
00136 self.assert_(get_log_dir() is not None)
00137
00138 def test_get_test_results_dir(self):
00139 from roslib.rosenv import get_ros_root, get_test_results_dir
00140 import tempfile, os
00141 base = tempfile.gettempdir()
00142 ros_test_results_dir = os.path.join(base, 'ros_test_results_dir')
00143 ros_home_dir = os.path.join(base, 'ros_home_dir')
00144 home_dir = os.path.expanduser('~')
00145
00146
00147 env = {'ROS_ROOT': get_ros_root(), 'ROS_TEST_RESULTS_DIR': ros_test_results_dir, 'ROS_HOME': ros_home_dir }
00148 self.assertEquals(ros_test_results_dir, get_test_results_dir(env=env))
00149
00150 env = {'ROS_ROOT': get_ros_root(), 'ROS_HOME': ros_home_dir }
00151 self.assertEquals(os.path.join(ros_home_dir, 'test_results'), get_test_results_dir(env=env))
00152
00153 env = {'ROS_ROOT': get_ros_root()}
00154 self.assertEquals(os.path.join(home_dir, '.ros', 'test_results'), get_test_results_dir(env=env))
00155
00156
00157 self.assert_(get_test_results_dir() is not None)
00158
00159 def test_get_ros_home(self):
00160 from roslib.rosenv import get_ros_root, get_ros_home
00161 import tempfile, os
00162 base = tempfile.gettempdir()
00163 ros_home_dir = os.path.join(base, 'ros_home_dir')
00164 home_dir = os.path.expanduser('~')
00165
00166
00167 env = {'ROS_ROOT': get_ros_root(), 'ROS_HOME': ros_home_dir }
00168 self.assertEquals(ros_home_dir, get_ros_home(env=env))
00169
00170 env = {'ROS_ROOT': get_ros_root()}
00171 self.assertEquals(os.path.join(home_dir, '.ros'), get_ros_home(env=env))
00172
00173
00174 self.assert_(get_ros_home() is not None)
00175
00176 def test_on_ros_path(self):
00177 from roslib.rosenv import on_ros_path, get_ros_root, resolve_paths, get_ros_package_path
00178
00179 self.assert_(on_ros_path(get_ros_root()))
00180
00181
00182 self.assert_(on_ros_path('.'))
00183 self.assert_(on_ros_path('test_roslib_rosenv.py'))
00184
00185 paths = resolve_paths(get_ros_package_path()).split(os.pathsep)
00186 for p in paths:
00187 self.assert_(on_ros_path(p), "failed: %s, [%s]"%(p, paths))
00188
00189 self.failIf(on_ros_path(os.tempnam()))
00190
00191 if __name__ == '__main__':
00192 rosunit.unitrun('test_roslib', 'test_env', EnvTest, coverage_packages=['roslib.rosenv'])
00193