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.packages
00041 import rosunit
00042
00043
00044 class RoslibPackagesTest(unittest.TestCase):
00045
00046 def test_list_pkgs_by_path(self):
00047 from roslib.packages import list_pkgs_by_path
00048
00049 d = roslib.packages.get_pkg_dir('test_roslib')
00050
00051 os.chdir(d)
00052
00053 self.assertEquals(['test_roslib'], list_pkgs_by_path('.'))
00054 self.assertEquals(set(['bar', 'foo']), set(list_pkgs_by_path(os.path.join('test', 'package_tests'))))
00055 self.assertEquals(set(['bar', 'foo']), set(list_pkgs_by_path(os.path.join('test', 'package_tests', 'p1'))))
00056 self.assertEquals(['foo'], list_pkgs_by_path(os.path.join('test', 'package_tests', 'p1', 'foo')))
00057
00058 self.assertEquals([], list_pkgs_by_path(os.path.join('bin')))
00059
00060
00061 def test_list_pkgs(self):
00062
00063 import roslib.rospack
00064 import roslib.packages
00065 pkgs = [s.split()[0] for s in roslib.rospack.rospackexec(['list']).split('\n')]
00066 retval = roslib.packages.list_pkgs()
00067 self.assertEquals(set(pkgs), set(retval), set(pkgs) ^ set(retval))
00068
00069
00070 retval = roslib.packages.list_pkgs()
00071 self.assertEquals(set(pkgs), set(retval), set(pkgs) ^ set(retval))
00072
00073
00074 d = roslib.packages.get_pkg_dir('test_roslib')
00075 d = os.path.join(d, 'test', 'package_tests')
00076
00077
00078 paths = [os.path.join(d, p) for p in ['p1', 'p2', 'p3']]
00079 cache = {}
00080 packages = roslib.packages.list_pkgs(pkg_dirs=paths, cache=cache)
00081 self.assert_('foo' in packages)
00082 self.assert_('bar' in packages)
00083
00084 foo_p = os.path.join(d, 'p1', 'foo')
00085 bar_p = os.path.join(d, 'p1', 'bar')
00086 self.assertEquals(foo_p, cache['foo'][0])
00087 self.assertEquals(bar_p, cache['bar'][0])
00088
00089
00090 paths = [os.path.join(d, p) for p in ['p2', 'p3', 'p1']]
00091 cache = {}
00092 packages = roslib.packages.list_pkgs(pkg_dirs=paths, cache=cache)
00093 self.assert_('foo' in packages)
00094 self.assert_('bar' in packages)
00095
00096 foo_p = os.path.join(d, 'p2', 'foo')
00097 bar_p = os.path.join(d, 'p1', 'bar')
00098 self.assertEquals(foo_p, cache['foo'][0])
00099 self.assertEquals(bar_p, cache['bar'][0])
00100
00101 def test_find_node(self):
00102 import roslib.packages
00103 d = roslib.packages.get_pkg_dir('test_roslib')
00104 p = os.path.join(d, 'test', 'fake_node.py')
00105 self.assertEquals(p, roslib.packages.find_node('test_roslib', 'fake_node.py'))
00106
00107 self.assertEquals(None, roslib.packages.find_node('test_roslib', 'not_a_node'))
00108
00109 def test_get_pkg_dir(self):
00110 import roslib.packages
00111 import roslib.rospack
00112 path = roslib.rospack.rospackexec(['find', 'test_roslib'])
00113 self.assertEquals(path, roslib.packages.get_pkg_dir('test_roslib'))
00114 try:
00115 self.assertEquals(path, roslib.packages.get_pkg_dir('fake_test_roslib'))
00116 self.fail("should have raised")
00117 except roslib.packages.InvalidROSPkgException: pass
00118
00119 def test_get_dir_pkg(self):
00120 import roslib.packages
00121 import roslib.rospack
00122 path = os.path.realpath(roslib.rospack.rospackexec(['find', 'test_roslib']))
00123
00124 res = roslib.packages.get_dir_pkg(path)
00125 res = (os.path.realpath(res[0]), res[1])
00126 self.assertEquals((path, 'test_roslib'), res)
00127 res = roslib.packages.get_dir_pkg(os.path.join(path, 'test'))
00128 res = (os.path.realpath(res[0]), res[1])
00129 self.assertEquals((path, 'test_roslib'), res)
00130
00131
00132 self.assertEquals((None, None), roslib.packages.get_dir_pkg(os.path.dirname(path)))
00133
00134 def test_ROSPackages(self):
00135 from roslib.packages import ROSPackages
00136 rp = ROSPackages()
00137
00138 try:
00139 rp.load_manifests('test_roslib')
00140 self.fail("should have raised")
00141 except TypeError:
00142 pass
00143
00144
00145 x = rp.depends1(['test_roslib'])
00146 self.assertEquals(['test_roslib'], x.keys())
00147 self.assertEquals(set(['roslib', 'rosunit']), set(x['test_roslib']))
00148 x = rp.depends1(['test_rospack'])
00149 self.assertEquals(['test_rospack'], x.keys())
00150 self.assertEquals(set(['rospack', 'rosunit']), set(x['test_rospack']))
00151
00152 rp = ROSPackages()
00153 x = rp.depends1(['test_roslib', 'test_rospack'])
00154 self.assertEquals(set(['test_rospack', 'test_roslib']), set(x.keys()))
00155 self.assertEquals(set(['rospack', 'rosunit']), set(x['test_rospack']))
00156 self.assertEquals(set(['roslib', 'rosunit']), set(x['test_roslib']))
00157
00158
00159 test_roslib_depends = ['rospack', 'roslib', 'rosunit']
00160 x = rp.depends(['test_roslib'])
00161 self.assertEquals(['test_roslib'], x.keys())
00162 s1 = set(test_roslib_depends)
00163 s2 = set(x['test_roslib'])
00164 self.assertEquals(s1, s2, s1^s2)
00165
00166
00167 no_depends = ['mk', 'rospack', 'rosbuild']
00168 for p in no_depends:
00169 self.assertEquals({p: []}, rp.depends1([p]))
00170 self.assertEquals({p: []}, rp.depends([p]))
00171
00172 rp = ROSPackages()
00173 v = dict((p, []) for p in no_depends)
00174 self.assertEquals(v, rp.depends1(no_depends))
00175
00176 self.assertEquals(v, rp.depends1(no_depends))
00177
00178
00179
00180 roslib_rosdeps0 = ['python', 'python-yaml', 'boost']
00181 roslib_rosdeps = roslib_rosdeps0
00182 rosbuild_rosdeps = ['bzip2']
00183
00184 rp = ROSPackages()
00185
00186 x = rp.rosdeps0(['mk'])
00187 self.assertEquals(['mk'], x.keys())
00188 self.assertEquals(set(), set(x['mk']))
00189
00190 x = rp.rosdeps0(['roslib'])
00191 self.assertEquals(['roslib'], x.keys())
00192 self.assertEquals(set(roslib_rosdeps0), set(x['roslib']))
00193
00194 x = rp.rosdeps0(['rosbuild'])
00195 self.assertEquals(['rosbuild'], x.keys())
00196 self.assertEquals(set(rosbuild_rosdeps), set(x['rosbuild']))
00197
00198
00199 p = 'rosunit'
00200 rosunit_rosdeps = set(roslib_rosdeps + ['gtest'])
00201 x = rp.rosdeps([p])
00202 self.assertEquals([p], x.keys())
00203 self.assertEquals(rosunit_rosdeps, set(x[p]))
00204 x = rp.rosdeps(['rosunit', 'mk'])
00205 self.assertEquals(set(['rosunit', 'mk']), set(x.keys()))
00206 self.assertEquals(rosunit_rosdeps, set(x['rosunit']))
00207 self.assertEquals([], x['mk'])
00208
00209 rp = ROSPackages()
00210 no_rosdeps = ['mk', 'rospack']
00211 no_rosdeps0 = no_rosdeps + ['test_roslib']
00212 for p in no_rosdeps0:
00213 self.assertEquals({p: []}, rp.rosdeps0([p]))
00214 if p in no_rosdeps:
00215 self.assertEquals({p: []}, rp.rosdeps([p]))
00216
00217 rp = ROSPackages()
00218 v = dict((p, []) for p in no_rosdeps)
00219 self.assertEquals(v, rp.rosdeps0(no_rosdeps))
00220 if p in no_rosdeps:
00221 self.assertEquals({p: []}, rp.rosdeps([p]))
00222
00223 self.assertEquals(v, rp.rosdeps0(no_rosdeps))
00224 if p in no_rosdeps:
00225 self.assertEquals({p: []}, rp.rosdeps([p]))
00226
00227
00228 def test_get_package_paths(self):
00229 from roslib.packages import get_package_paths
00230
00231 s = os.pathsep
00232 tests = [
00233 ('', []),
00234 (s, []),
00235 (s+s+s+s+s, []),
00236 (s+s+s+s+'/fake/kwc/ros-pkg'+s+s+s, ['/fake/kwc/ros-pkg']),
00237 (s.join(['/fake/kwc/ros-pkg', '/fake/wg-ros-pkg']), ['/fake/kwc/ros-pkg', '/fake/wg-ros-pkg']),
00238 ]
00239 for rpp, v in tests:
00240 rr = os.getcwd()
00241 for ros_root_required in [True, False]:
00242 env = { 'ROS_PACKAGE_PATH' : rpp}
00243
00244 if ros_root_required:
00245 try:
00246 get_package_paths(ros_root_required, env)
00247 self.fail("should have failed")
00248 except roslib.rosenv.ROSEnvException:
00249 pass
00250 else:
00251 paths = get_package_paths(ros_root_required, env)
00252 self.assertEquals(v, paths)
00253
00254
00255 env['ROS_ROOT'] = rr
00256 paths = get_package_paths(ros_root_required, env)
00257 self.assertEquals(v + [rr], paths)
00258
00259
00260 def test__platform_supported(self):
00261 self.assertTrue(roslib.packages._platform_supported(os.path.join(roslib.packages.get_pkg_dir("test_roslib"), "test", "platform_supported.manifest.xml"), "test_os", "test_version"))
00262 self.assertFalse(roslib.packages._platform_supported(os.path.join(roslib.packages.get_pkg_dir("test_roslib"), "test", "platform_supported.manifest.xml"), "test_os", "not_test_version"))
00263 self.assertFalse(roslib.packages._platform_supported(os.path.join(roslib.packages.get_pkg_dir("test_roslib"), "test", "platform_supported.manifest.xml"), "not_test_os", "test_version"))
00264 self.assertFalse(roslib.packages._platform_supported(os.path.join(roslib.packages.get_pkg_dir("test_roslib"), "test", "platform_supported.manifest.xml"), "not_test_os", "not_test_version"))
00265
00266 def test_platform_supported_tripwire(self):
00267 self.assertFalse(roslib.packages.platform_supported("test_roslib", "nonextant_os", "noextant_version"))
00268
00269 def test_current_platform_supported_tripwire(self):
00270 roslib.packages.current_platform_supported("test_roslib")
00271
00272
00273
00274 if __name__ == '__main__':
00275 rosunit.unitrun('test_roslib', 'test_packages', RoslibPackagesTest, coverage_packages=['roslib.packages'])
00276