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 roslib.rosenv
00040 import roslib.stacks
00041 import rosunit
00042
00043 class RoslibStacksTest(unittest.TestCase):
00044
00045 def test_packages_of(self):
00046 from roslib.stacks import packages_of
00047 pkgs = packages_of('ros')
00048 for p in ['test_roslib', 'rosbuild', 'rospack']:
00049 self.assert_(p in pkgs, p)
00050
00051 pkgs = packages_of('ros')
00052 for p in ['test_roslib', 'rosbuild', 'rospack']:
00053 self.assert_(p in pkgs, p)
00054
00055 try:
00056 packages_of(None)
00057 self.fail("should have raised ValueError")
00058 except ValueError: pass
00059
00060 def test_stack_of(self):
00061 import roslib.packages
00062 from roslib.stacks import stack_of
00063 self.assertEquals('ros', stack_of('test_roslib'))
00064
00065 self.assertEquals('ros', stack_of('test_roslib'))
00066 try:
00067 stack_of('fake_test_roslib')
00068 self.fail("should have failed")
00069 except roslib.packages.InvalidROSPkgException:
00070 pass
00071
00072 def test_list_stacks(self):
00073 from roslib.stacks import list_stacks
00074 l = list_stacks()
00075 self.assert_('ros' in l)
00076
00077
00078 from roslib.rospack import rosstackexec
00079 l2 = [x for x in rosstackexec(['list']).split('\n') if x]
00080 l2 = [x.split()[0] for x in l2]
00081 self.assertEquals(set(l), set(l2), set(l) ^ set(l2))
00082
00083 def test_list_stacks_by_path(self):
00084 from roslib.stacks import list_stacks_by_path
00085
00086
00087 rr = roslib.rosenv.get_ros_root()
00088 self.assertEquals(['ros'], list_stacks_by_path(rr))
00089 stacks = []
00090 self.assertEquals(['ros'], list_stacks_by_path(rr, stacks))
00091 self.assertEquals(['ros'], stacks)
00092 self.assertEquals(['ros'], list_stacks_by_path(rr, stacks))
00093
00094 stacks.extend(['fake_stack', 'fake_stack2'])
00095 self.assertEquals(['ros', 'fake_stack', 'fake_stack2'], list_stacks_by_path(rr, stacks))
00096
00097 cache = {}
00098 self.assertEquals(['ros'], list_stacks_by_path(rr, cache=cache))
00099 self.assertEquals({'ros': rr}, cache)
00100
00101
00102 test_dir = os.path.join(roslib.packages.get_pkg_dir('test_roslib'), 'test', 'stack_tests')
00103 self.assertEquals(set(['bar', 'foo']), set(list_stacks_by_path(test_dir)))
00104
00105 test_dir = os.path.join(roslib.packages.get_pkg_dir('test_roslib'), 'test', 'stack_tests', 's1')
00106 self.assertEquals(set(['bar', 'foo']), set(list_stacks_by_path(test_dir)))
00107
00108 test_dir = os.path.join(roslib.packages.get_pkg_dir('test_roslib'), 'test', 'stack_tests', 's1', 'bar')
00109 self.assertEquals(['bar'], list_stacks_by_path(test_dir))
00110
00111
00112
00113 test_dir = os.path.join(roslib.packages.get_pkg_dir('test_roslib'), 'test', 'stack_tests2')
00114 self.assertEquals(set(['foo', 'bar']), set(list_stacks_by_path(test_dir)))
00115
00116 def test_get_stack_dir(self):
00117 import roslib.rosenv
00118 import roslib.packages
00119 from roslib.stacks import get_stack_dir, InvalidROSStackException, list_stacks
00120 self.assertEquals(roslib.rosenv.get_ros_root(), get_stack_dir('ros'))
00121 try:
00122 get_stack_dir('non_existent')
00123 self.fail("should have raised")
00124 except roslib.stacks.InvalidROSStackException:
00125 pass
00126
00127
00128 stacks = list_stacks()
00129 from roslib.rospack import rosstackexec
00130 for s in stacks:
00131 d1 = get_stack_dir(s)
00132 d2 = rosstackexec(['find', s])
00133 self.assertEquals(d1, d2, "%s vs %s"%(d1, d2))
00134
00135
00136
00137 rpp = os.environ.get(roslib.rosenv.ROS_PACKAGE_PATH, None)
00138 try:
00139 d = roslib.packages.get_pkg_dir('test_roslib')
00140 d = os.path.join(d, 'test', 'stack_tests')
00141
00142
00143 print "s1/s2/s3"
00144 paths = [os.path.join(d, p) for p in ['s1', 's2', 's3']]
00145 os.environ[roslib.rosenv.ROS_PACKAGE_PATH] = os.pathsep.join(paths)
00146
00147 for i in xrange(2):
00148 stacks = roslib.stacks.list_stacks()
00149 self.assert_('foo' in stacks)
00150 self.assert_('bar' in stacks)
00151
00152 foo_p = os.path.join(d, 's1', 'foo')
00153 bar_p = os.path.join(d, 's1', 'bar')
00154 self.assertEquals(foo_p, roslib.stacks.get_stack_dir('foo'))
00155 self.assertEquals(bar_p, roslib.stacks.get_stack_dir('bar'))
00156
00157
00158 print "s2/s3/s1"
00159
00160 paths = [os.path.join(d, p) for p in ['s2', 's3', 's1']]
00161 os.environ[roslib.rosenv.ROS_PACKAGE_PATH] = os.pathsep.join(paths)
00162 stacks = roslib.stacks.list_stacks()
00163 self.assert_('foo' in stacks)
00164 self.assert_('bar' in stacks)
00165
00166 foo_p = os.path.join(d, 's2', 'foo')
00167 bar_p = os.path.join(d, 's1', 'bar')
00168 self.assertEquals(foo_p, roslib.stacks.get_stack_dir('foo'))
00169 self.assertEquals(bar_p, roslib.stacks.get_stack_dir('bar'))
00170 finally:
00171
00172 if rpp is not None:
00173 os.environ[roslib.rosenv.ROS_PACKAGE_PATH] = rpp
00174 else:
00175 del os.environ[roslib.rosenv.ROS_PACKAGE_PATH]
00176
00177 def test_expand_to_packages(self):
00178 from roslib.stacks import expand_to_packages
00179 try:
00180
00181
00182
00183 self.assertEquals(([], []), expand_to_packages('ros'))
00184 self.fail("expand_to_packages should only take in a list of strings")
00185 except ValueError: pass
00186
00187 self.assertEquals(([], []), expand_to_packages([]))
00188 self.assertEquals((['rosmake', 'test_roslib', 'roslib'], []), expand_to_packages(['rosmake', 'test_roslib', 'roslib']))
00189 self.assertEquals(([], ['bogus_one', 'bogus_two']), expand_to_packages(['bogus_one', 'bogus_two']))
00190
00191 self.assertEquals(([], ['bogus_one', 'bogus_two']), expand_to_packages(['bogus_one', 'bogus_two']))
00192
00193
00194 valid, invalid = expand_to_packages(['ros', 'bogus_one'])
00195 self.assertEquals(['bogus_one'], invalid)
00196 check = ['rosbuild', 'rospack', 'rosunit', 'test_roslib']
00197 for c in check:
00198 self.assert_(c in valid, "expected [%s] to be in ros expansion"%c)
00199
00200 if __name__ == '__main__':
00201 rosunit.unitrun('test_roslib', 'test_stacks', RoslibStacksTest, coverage_packages=['roslib.stacks'])
00202