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 struct
00036 import sys
00037 import unittest
00038
00039 import rosunit
00040
00041 class RoslibStacksTest(unittest.TestCase):
00042
00043 def test_packages_of_unary(self):
00044 from roslib.stacks import packages_of
00045
00046 test_dir = os.path.join(roslib.packages.get_pkg_dir('test_roslib'), 'test', 'stack_tests_unary')
00047 env = os.environ.copy()
00048 env['ROS_PACKAGE_PATH'] = test_dir
00049 for s in ['foo', 'bar']:
00050 self.assertEquals([s], packages_of(s, env=env))
00051
00052 def test_packages_of(self):
00053 from roslib.stacks import packages_of
00054 pkgs = packages_of('ros')
00055 for p in ['test_roslib', 'rosbuild', 'rospack']:
00056 self.assert_(p in pkgs, p)
00057
00058 pkgs = packages_of('ros')
00059 for p in ['test_roslib', 'rosbuild', 'rospack']:
00060 self.assert_(p in pkgs, p)
00061
00062 try:
00063 packages_of(None)
00064 self.fail("should have raised ValueError")
00065 except ValueError: pass
00066
00067
00068 test_dir = os.path.join(roslib.packages.get_pkg_dir('test_roslib'), 'test', 'stack_tests', 's1')
00069 env = os.environ.copy()
00070 env['ROS_PACKAGE_PATH'] = test_dir
00071 self.assertEquals(set(['foo_pkg', 'foo_pkg_2']), set(packages_of('foo', env=env)))
00072
00073
00074 def test_stack_of(self):
00075 import roslib.packages
00076 from roslib.stacks import stack_of
00077 self.assertEquals('ros', stack_of('test_roslib'))
00078
00079 self.assertEquals('ros', stack_of('test_roslib'))
00080 try:
00081 stack_of('fake_test_roslib')
00082 self.fail("should have failed")
00083 except roslib.packages.InvalidROSPkgException:
00084 pass
00085
00086
00087 test_dir = os.path.join(roslib.packages.get_pkg_dir('test_roslib'), 'test', 'stack_tests_unary')
00088 env = os.environ.copy()
00089 env['ROS_PACKAGE_PATH'] = test_dir
00090 for s in ['foo', 'bar']:
00091 self.assertEquals(s, stack_of(s, env=env))
00092
00093 def test_list_stacks(self):
00094 from roslib.stacks import list_stacks
00095 l = list_stacks()
00096 self.assert_('ros' in l)
00097
00098
00099 from roslib.rospack import rosstackexec
00100 l2 = [x for x in rosstackexec(['list']).split('\n') if x]
00101 l2 = [x.split()[0] for x in l2]
00102 self.assertEquals(set(l), set(l2), set(l) ^ set(l2))
00103
00104
00105 test_dir = os.path.join(roslib.packages.get_pkg_dir('test_roslib'), 'test', 'stack_tests', 's1')
00106 env = os.environ.copy()
00107 env['ROS_PACKAGE_PATH'] = test_dir
00108 self.assertEquals(set(['ros', 'foo', 'bar']), set(list_stacks(env=env)))
00109
00110
00111 def test_list_stacks_by_path(self):
00112 from roslib.stacks import list_stacks_by_path
00113 import roslib.rosenv
00114
00115
00116 rr = roslib.rosenv.get_ros_root()
00117 self.assertEquals(['ros'], list_stacks_by_path(rr))
00118 stacks = []
00119 self.assertEquals(['ros'], list_stacks_by_path(rr, stacks))
00120 self.assertEquals(['ros'], stacks)
00121 self.assertEquals(['ros'], list_stacks_by_path(rr, stacks))
00122
00123 stacks.extend(['fake_stack', 'fake_stack2'])
00124 self.assertEquals(['ros', 'fake_stack', 'fake_stack2'], list_stacks_by_path(rr, stacks))
00125
00126 cache = {}
00127 self.assertEquals(['ros'], list_stacks_by_path(rr, cache=cache))
00128 self.assertEquals({'ros': rr}, cache)
00129
00130
00131 test_dir = os.path.join(roslib.packages.get_pkg_dir('test_roslib'), 'test', 'stack_tests')
00132 self.assertEquals(set(['bar', 'foo']), set(list_stacks_by_path(test_dir)))
00133
00134 test_dir = os.path.join(roslib.packages.get_pkg_dir('test_roslib'), 'test', 'stack_tests', 's1')
00135 self.assertEquals(set(['bar', 'foo']), set(list_stacks_by_path(test_dir)))
00136
00137 test_dir = os.path.join(roslib.packages.get_pkg_dir('test_roslib'), 'test', 'stack_tests', 's1', 'bar')
00138 self.assertEquals(['bar'], list_stacks_by_path(test_dir))
00139
00140
00141
00142 test_dir = os.path.join(roslib.packages.get_pkg_dir('test_roslib'), 'test', 'stack_tests2')
00143 self.assertEquals(set(['foo', 'bar']), set(list_stacks_by_path(test_dir)))
00144
00145 def test_list_stacks_by_path_unary(self):
00146 from roslib.stacks import list_stacks_by_path
00147
00148 test_dir = os.path.join(roslib.packages.get_pkg_dir('test_roslib'), 'test', 'stack_tests_unary')
00149 self.assertEquals(set(['bar', 'foo', 'baz']), set(list_stacks_by_path(test_dir)))
00150
00151 def test_get_stack_dir_unary(self):
00152
00153
00154 d = roslib.packages.get_pkg_dir('test_roslib')
00155 d = os.path.join(d, 'test', 'stack_tests_unary')
00156 s1_d = os.path.join(d, 's1')
00157 rpp = os.environ.get(roslib.rosenv.ROS_PACKAGE_PATH, None)
00158 try:
00159 paths = [d]
00160 os.environ[roslib.rosenv.ROS_PACKAGE_PATH] = os.pathsep.join(paths)
00161 self.assertEquals(os.path.join(s1_d, 'foo'), roslib.stacks.get_stack_dir('foo'))
00162 self.assertEquals(os.path.join(s1_d, 'bar'), roslib.stacks.get_stack_dir('bar'))
00163 self.assertEquals(os.path.join(s1_d, 'baz'), roslib.stacks.get_stack_dir('baz'))
00164 finally:
00165
00166 if rpp is not None:
00167 os.environ[roslib.rosenv.ROS_PACKAGE_PATH] = rpp
00168 else:
00169 del os.environ[roslib.rosenv.ROS_PACKAGE_PATH]
00170
00171 def test_get_stack_dir(self):
00172 import roslib.rosenv
00173 import roslib.packages
00174 from roslib.stacks import get_stack_dir, InvalidROSStackException, list_stacks
00175 self.assertEquals(roslib.rosenv.get_ros_root(), get_stack_dir('ros'))
00176 try:
00177 get_stack_dir('non_existent')
00178 self.fail("should have raised")
00179 except roslib.stacks.InvalidROSStackException:
00180 pass
00181
00182
00183 stacks = list_stacks()
00184 from roslib.rospack import rosstackexec
00185 for s in stacks:
00186 d1 = get_stack_dir(s)
00187 d2 = rosstackexec(['find', s])
00188 self.assertEquals(d1, d2, "%s vs %s"%(d1, d2))
00189
00190
00191
00192 rpp = os.environ.get(roslib.rosenv.ROS_PACKAGE_PATH, None)
00193 try:
00194 d = roslib.packages.get_pkg_dir('test_roslib')
00195 d = os.path.join(d, 'test', 'stack_tests')
00196
00197
00198 print "s1/s2/s3"
00199 paths = [os.path.join(d, p) for p in ['s1', 's2', 's3']]
00200 os.environ[roslib.rosenv.ROS_PACKAGE_PATH] = os.pathsep.join(paths)
00201
00202 for i in xrange(2):
00203 stacks = roslib.stacks.list_stacks()
00204 self.assert_('foo' in stacks)
00205 self.assert_('bar' in stacks)
00206
00207 foo_p = os.path.join(d, 's1', 'foo')
00208 bar_p = os.path.join(d, 's1', 'bar')
00209 self.assertEquals(foo_p, roslib.stacks.get_stack_dir('foo'))
00210 self.assertEquals(bar_p, roslib.stacks.get_stack_dir('bar'))
00211
00212
00213 print "s2/s3/s1"
00214
00215 paths = [os.path.join(d, p) for p in ['s2', 's3', 's1']]
00216 os.environ[roslib.rosenv.ROS_PACKAGE_PATH] = os.pathsep.join(paths)
00217 stacks = roslib.stacks.list_stacks()
00218 self.assert_('foo' in stacks)
00219 self.assert_('bar' in stacks)
00220
00221 foo_p = os.path.join(d, 's2', 'foo')
00222 bar_p = os.path.join(d, 's1', 'bar')
00223 self.assertEquals(foo_p, roslib.stacks.get_stack_dir('foo'))
00224 self.assertEquals(bar_p, roslib.stacks.get_stack_dir('bar'))
00225 finally:
00226
00227 if rpp is not None:
00228 os.environ[roslib.rosenv.ROS_PACKAGE_PATH] = rpp
00229 else:
00230 del os.environ[roslib.rosenv.ROS_PACKAGE_PATH]
00231
00232 def test_expand_to_packages_unary(self):
00233
00234 test_dir = os.path.join(roslib.packages.get_pkg_dir('test_roslib'), 'test', 'stack_tests_unary')
00235
00236 env = os.environ.copy()
00237 env[roslib.rosenv.ROS_PACKAGE_PATH] = test_dir
00238
00239 from roslib.stacks import expand_to_packages
00240 self.assertEquals((['foo'], []), expand_to_packages(['foo'], env=env))
00241 self.assertEquals((['foo', 'bar'], []), expand_to_packages(['foo', 'bar'], env=env))
00242
00243 def test_expand_to_packages(self):
00244 from roslib.stacks import expand_to_packages
00245 try:
00246
00247
00248
00249 self.assertEquals(([], []), expand_to_packages('ros'))
00250 self.fail("expand_to_packages should only take in a list of strings")
00251 except ValueError: pass
00252
00253 self.assertEquals(([], []), expand_to_packages([]))
00254 self.assertEquals((['rosmake', 'test_roslib', 'roslib'], []), expand_to_packages(['rosmake', 'test_roslib', 'roslib']))
00255 self.assertEquals(([], ['bogus_one', 'bogus_two']), expand_to_packages(['bogus_one', 'bogus_two']))
00256
00257 self.assertEquals(([], ['bogus_one', 'bogus_two']), expand_to_packages(['bogus_one', 'bogus_two']))
00258
00259
00260 valid, invalid = expand_to_packages(['ros', 'bogus_one'])
00261 self.assertEquals(['bogus_one'], invalid)
00262 check = ['rosbuild', 'rospack', 'rosunit', 'test_roslib']
00263 for c in check:
00264 self.assert_(c in valid, "expected [%s] to be in ros expansion"%c)
00265
00266 def test_get_stack_version(self):
00267 from roslib.stacks import get_stack_version
00268
00269 test_dir = os.path.join(roslib.packages.get_pkg_dir('test_roslib'), 'test', 'stack_tests', 's1')
00270 env = os.environ.copy()
00271 env[roslib.rosenv.ROS_PACKAGE_PATH] = test_dir
00272
00273
00274 self.assertEquals('1.6.0-manifest', roslib.stacks.get_stack_version('foo', env=env))
00275
00276 self.assertEquals('1.5.0-cmake', roslib.stacks.get_stack_version('bar', env=env))
00277
00278 if 0:
00279 test_dir = os.path.join(roslib.packages.get_pkg_dir('test_roslib'), 'test', 'stack_tests_unary')
00280 env = os.environ.copy()
00281 env[roslib.rosenv.ROS_PACKAGE_PATH] = test_dir
00282
00283 if __name__ == '__main__':
00284 rosunit.unitrun('test_roslib', 'test_stacks', RoslibStacksTest, coverage_packages=['roslib.stacks'])
00285