Skip to content

Commit

Permalink
Print warning when get_package_share_directory() does not exist (Fix #74
Browse files Browse the repository at this point in the history
) (#77)

* Fix #74

Signed-off-by: David V. Lu <davidvlu@gmail.com>

* Fix tests, change variable name, add documentation

Signed-off-by: David V. Lu <davidvlu@gmail.com>
  • Loading branch information
DLu committed Jun 18, 2021
1 parent e04af7b commit f214964
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 5 deletions.
17 changes: 13 additions & 4 deletions ament_index_python/ament_index_python/packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import os
import pathlib
import re
import warnings

from .resources import get_resource
from .resources import get_resources
Expand Down Expand Up @@ -61,7 +62,7 @@ def get_package_prefix(package_name):
return package_prefix


def get_package_share_directory(package_name):
def get_package_share_directory(package_name, print_warning=True):
"""
Return the share directory of the given package.
Expand All @@ -71,14 +72,18 @@ def get_package_share_directory(package_name):
the package's share directory.
:param str package_name: name of the package to locate
:param bool print_warning: if true, print a warning if the directory does not exist
:returns: share directory of the package
:raises: :exc:`PackageNotFoundError` if the package is not found
:raises: :exc:`ValueError` if the package name is invalid
"""
return os.path.join(get_package_prefix(package_name), 'share', package_name)
path = os.path.join(get_package_prefix(package_name), 'share', package_name)
if print_warning and not os.path.exists(path):
warnings.warn(f'Share directory for {package_name} ({path}) does not exist.', stacklevel=2)
return path


def get_package_share_path(package_name):
def get_package_share_path(package_name, print_warning=True):
"""
Return the share directory of the given package as a pathlib.Path.
Expand All @@ -89,7 +94,11 @@ def get_package_share_path(package_name):
`get_package_share_path('foo') / 'urdf/robot.urdf'`
:param str package_name: name of the package to locate
:param bool print_warning: if true, print a warning if the path does not exist
:returns: share directory of the package as a pathlib.Path
:raises: :exc:`PackageNotFoundError` if the package is not found
"""
return pathlib.Path(get_package_share_directory(package_name))
path = pathlib.Path(get_package_share_directory(package_name, print_warning=False))
if print_warning and not path.exists():
warnings.warn(f'Share path for {package_name} ({path}) does not exist.', stacklevel=2)
return path
Empty file.
Empty file.
Empty file.
Empty file.
15 changes: 14 additions & 1 deletion ament_index_python/test/test_ament_index_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ def get_package_prefix_basename(package_name):
get_package_prefix(name)

with pytest.raises(ValueError):
# An absolue path is not a valid package name
# An absolute path is not a valid package name
extant_absolute_path = os.path.abspath(__file__)
get_package_prefix(extant_absolute_path)

Expand Down Expand Up @@ -228,6 +228,12 @@ def get_package_share_directory_test(package_name, expect_prefix):
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'])
Expand All @@ -247,6 +253,12 @@ def get_package_share_path_test(package_name, expect_prefix):
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([])
Expand Down Expand Up @@ -299,6 +311,7 @@ def test_main_tool(capsys):
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
Expand Down

0 comments on commit f214964

Please sign in to comment.