1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
|
# Copyright 2015 Open Source Robotics Foundation, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
from pathlib import Path, PurePath
from ament_index_python import get_package_prefix
from ament_index_python import get_package_share_directory
from ament_index_python import get_package_share_path
from ament_index_python import get_packages_with_prefixes
from ament_index_python import get_resource
from ament_index_python import get_resource_types
from ament_index_python import get_resources
from ament_index_python import get_search_paths
from ament_index_python import has_resource
from ament_index_python import InvalidResourceNameError
from ament_index_python import InvalidResourceTypeNameError
from ament_index_python import PackageNotFoundError
from ament_index_python.cli import main
from ament_index_python.cli import resource_name_completer
from ament_index_python.cli import resource_type_completer
import pytest
def set_ament_prefix_path(subfolders):
paths = []
base_path = Path(__file__).parent
for subfolder in subfolders:
path = base_path / subfolder
if path.is_dir():
paths.append(str(path))
ament_prefix_path = os.pathsep.join(paths)
os.environ['AMENT_PREFIX_NO_IMPLICIT_USR'] = '1'
os.environ['AMENT_PREFIX_PATH'] = ament_prefix_path
def test_empty_search_paths():
set_ament_prefix_path([])
with pytest.raises(EnvironmentError):
get_search_paths()
def test_implicit_usr():
set_ament_prefix_path([])
os.environ['AMENT_PREFIX_NO_IMPLICIT_USR'] = ''
search_paths = get_search_paths()
assert search_paths == ["/usr"]
def test_search_paths():
set_ament_prefix_path(['prefix1', 'prefix2'])
search_paths = get_search_paths()
assert len(search_paths) == 2, 'Expected two search paths'
def test_not_existing_search_paths():
set_ament_prefix_path(['prefix1', 'not_existing_prefix'])
search_paths = get_search_paths()
assert len(search_paths) == 1, 'Expected one search paths'
def test_unknown_resources():
set_ament_prefix_path(['prefix1'])
resources = get_resources('unknown_resource_type')
assert len(resources) == 0, 'Expected no resources'
def test_invalid_resources():
set_ament_prefix_path(['prefix1'])
invalid_resource_type_names = [
'/invalid/name', 'invalid/name', '\\invalid\\name', 'invalid\\name']
for name in invalid_resource_type_names:
with pytest.raises(InvalidResourceTypeNameError):
resources = get_resources(name)
assert len(resources) == 0, 'Expected no resources'
with pytest.raises(InvalidResourceTypeNameError):
exists = has_resource(name, 'example_resource')
assert not exists, 'Resource should not exist'
with pytest.raises(InvalidResourceTypeNameError):
get_resource(name, 'example_resource')
def test_resources():
set_ament_prefix_path(['prefix1'])
resources = get_resources('resource_type1')
assert len(resources) == 2, 'Expected two resources'
assert set(resources.keys()) == {'foo', 'bar'}, 'Expected different resources'
def test_resources_overlay():
set_ament_prefix_path(['prefix1', 'prefix2'])
resources = get_resources('resource_type2')
assert len(resources) == 2, 'Expected two resource'
assert set(resources.keys()) == {'foo', 'bar'}, 'Expected different resources'
def test_resources_underlay():
set_ament_prefix_path(['prefix1', 'prefix2'])
resources = get_resources('resource_type3')
assert len(resources) == 1, 'Expected one resource'
assert set(resources.keys()) == {'bar'}, 'Expected different resources'
def test_unknown_resource():
set_ament_prefix_path(['prefix1'])
exists = has_resource('resource_type4', 'bar')
assert not exists, 'Resource should not exist'
with pytest.raises(LookupError):
get_resource('resource_type4', 'bar')
def test_invalid_resource_names():
set_ament_prefix_path(['prefix1'])
invalid_resource_names = [
'/invalid/name', 'invalid/name', '\\invalid\\name', 'invalid\\name']
for name in invalid_resource_names:
with pytest.raises(InvalidResourceNameError):
exists = has_resource('resource_type4', name)
assert not exists, 'Resource should not exist'
with pytest.raises(InvalidResourceNameError):
get_resource('resource_type4', name)
def test_absolute_path_resource():
extant_absolute_path = os.path.abspath(__file__)
set_ament_prefix_path(['prefix1'])
with pytest.raises(InvalidResourceNameError):
exists = has_resource('resource_type4', str(extant_absolute_path))
assert not exists, 'Resource should not exist'
with pytest.raises(InvalidResourceNameError):
get_resource('resource_type4', str(extant_absolute_path))
def test_resource():
set_ament_prefix_path(['prefix1'])
exists = has_resource('resource_type4', 'foo')
assert exists, 'Resource should exist'
resource, prefix = get_resource('resource_type4', 'foo')
assert resource == 'foo', 'Expected different content'
assert PurePath(prefix).name == 'prefix1', 'Expected different prefix'
def test_resource_overlay():
set_ament_prefix_path(['prefix1', 'prefix2'])
resource, prefix = get_resource('resource_type5', 'foo')
assert resource == 'foo1', 'Expected different content'
assert PurePath(prefix).name == 'prefix1', 'Expected different prefix'
def test_get_packages_with_prefixes():
set_ament_prefix_path(['prefix1', 'prefix2'])
packages = get_packages_with_prefixes()
assert 'foo' in packages, "Expected to find 'foo'"
assert PurePath(packages['foo']).name == 'prefix1', "Expected to find 'foo' in 'prefix1'"
assert 'bar' in packages, "Expected to find 'bar'"
assert PurePath(packages['bar']).name == 'prefix1', "Expected to find 'bar' in 'prefix1'"
assert 'baz' in packages, "Expected to find 'baz'"
assert PurePath(packages['baz']).name == 'prefix2', "Expected to find 'baz' in 'prefix2'"
os.environ['AMENT_PREFIX_PATH'] = '/path/does/not/exist'
assert not get_packages_with_prefixes(), 'Expected to find no packages'
def test_get_package_prefix():
set_ament_prefix_path(['prefix1', 'prefix2'])
def get_package_prefix_basename(package_name):
return PurePath(get_package_prefix(package_name)).name
assert get_package_prefix_basename('foo') == 'prefix1', "Expected 'foo' in 'prefix1'"
# found in both prefix1 and prefix2, but prefix1 is ahead on the APP
assert get_package_prefix_basename('bar') == 'prefix1', "Expected 'bar' in 'prefix2'"
assert get_package_prefix_basename('baz') == 'prefix2', "Expected 'baz' in 'prefix2'"
with pytest.raises(PackageNotFoundError):
get_package_prefix('does_not_exist')
assert issubclass(PackageNotFoundError, KeyError)
invalid_package_names = [
'_package', 'package a', 'package/a', 'package.a']
for name in invalid_package_names:
with pytest.raises(ValueError):
get_package_prefix(name)
with pytest.raises(ValueError):
# An absolute path is not a valid package name
extant_absolute_path = os.path.abspath(__file__)
get_package_prefix(extant_absolute_path)
def test_get_package_share_directory():
set_ament_prefix_path(['prefix1', 'prefix2'])
def get_package_share_directory_test(package_name, expect_prefix):
full_share_dir = get_package_share_directory(package_name)
left_over, dirname = os.path.split(full_share_dir)
assert dirname == package_name, f"Expected package name '{package_name}'"
left_over, dirname = os.path.split(left_over)
assert dirname == 'share', "Expected 'share'"
left_over, dirname = os.path.split(left_over)
assert dirname == expect_prefix, f"Expected '{expect_prefix}'"
get_package_share_directory_test('foo', 'prefix1')
# found in both prefix1 and prefix2, but prefix1 is ahead on the APP
get_package_share_directory_test('bar', 'prefix1')
get_package_share_directory_test('baz', 'prefix2')
with pytest.raises(PackageNotFoundError):
get_package_share_directory('does_not_exist')
with pytest.raises(ValueError):
get_package_share_directory('/invalid/package/name')
with pytest.warns(UserWarning):
# Package exists, but should print warning because there is no share dir
get_package_share_directory('trogdor')
get_package_share_directory('trogdor', print_warning=False)
def test_get_package_share_path():
set_ament_prefix_path(['prefix1', 'prefix2'])
def get_package_share_path_test(package_name, expect_prefix):
my_path = get_package_share_path(package_name)
assert len(my_path.parts) >= 3
assert my_path.parts[-1] == package_name, f"Expected package name '{package_name}'"
assert my_path.parts[-2] == 'share', "Expected 'share'"
assert my_path.parts[-3] == expect_prefix, f"Expected '{expect_prefix}'"
get_package_share_path_test('foo', 'prefix1')
# found in both prefix1 and prefix2, but prefix1 is ahead on the APP
get_package_share_path_test('bar', 'prefix1')
get_package_share_path_test('baz', 'prefix2')
with pytest.raises(PackageNotFoundError):
get_package_share_path('does_not_exist')
with pytest.warns(UserWarning):
# Package exists, but should print warning because there is no share dir
get_package_share_path('trogdor')
get_package_share_path('trogdor', print_warning=False)
def test_get_resource_types():
set_ament_prefix_path([])
with pytest.raises(EnvironmentError):
get_resource_types()
set_ament_prefix_path(['prefix1', 'prefix2'])
resources = get_resource_types()
assert resources == {
'resource_type1',
'resource_type2',
'resource_type3',
'resource_type4',
'resource_type5',
'packages'
}, ('Expected resources to be: resource_type1, resource_type2, resource_type3, '
'resource_type4, resource_type5 and packages')
set_ament_prefix_path(['prefix1'])
resources = get_resource_types()
assert resources == {
'resource_type1',
'resource_type2',
'resource_type4',
'resource_type5',
'packages'
}, ('Expected resources to be: resource_type1, resource_type2, resource_type4, '
'resource_type5 and packages')
@pytest.mark.skip(reason='test is broken')
def test_main_tool(capsys):
set_ament_prefix_path(['prefix1', 'prefix2'])
base_path = Path(__file__).parent
main()
captured = capsys.readouterr()
expected_result = (
'packages\n'
'resource_type1\n'
'resource_type2\n'
'resource_type3\n'
'resource_type4\n'
'resource_type5\n'
)
assert captured.out == expected_result
main(argv=['packages'])
captured = capsys.readouterr()
expected_result = '\n'.join([
f"bar\t{base_path / 'prefix1'}",
f"baz\t{base_path / 'prefix2'}",
f"foo\t{base_path / 'prefix1'}",
f"trogdor\t{base_path / 'prefix1'}",
''
])
assert captured.out == expected_result
main(argv=['packages', 'bar'])
captured = capsys.readouterr()
expected_result = str(base_path / 'prefix1\n')
assert captured.out == expected_result
main(argv=['resource_type4', 'foo'])
captured = capsys.readouterr()
expected_result = f"{base_path / 'prefix1'}\n<<<\nfoo\n>>>\n"
assert captured.out == expected_result
result = main(argv=['packages', 'not_available'])
captured = capsys.readouterr()
expected_result = "Could not find the resource 'not_available' of type 'packages'"
assert result == expected_result
def test_autocomplete():
set_ament_prefix_path(['prefix1', 'prefix2'])
result = sorted(resource_type_completer('res'))
expected_result = [
'resource_type1',
'resource_type2',
'resource_type3',
'resource_type4',
'resource_type5'
]
assert result == expected_result
class arguments():
resource_type = 'packages'
result = sorted(resource_name_completer('ba', arguments))
expected_result = ['bar', 'baz']
assert result == expected_result
setattr(arguments, 'resource_type', None)
result = sorted(resource_name_completer('ba', arguments))
expected_result = []
assert result == expected_result
|