示例#1
0
def test_discover_packages():
    # check without any extensions
    with patch(
            'colcon_core.package_discovery.get_package_discovery_extensions',
            return_value={},
    ) as get_extensions:
        with patch('colcon_core.package_discovery.logger.warning') as warn:
            descs = discover_packages(None, None)
    assert get_extensions.call_count == 1
    warn.assert_called_once_with('No package discovery extensions found')
    assert descs == set()

    with EntryPointContext(
            extension1=Extension1,
            extension2=Extension2,
            extension3=Extension3,
            extension4=Extension4,
    ):
        extensions = get_package_discovery_extensions()
        assert len(extensions) == 4

        # check without any parameters
        extensions['extension1'].discover = Mock(
            return_value={PackageDescriptor('/extension1/pkg1')})
        extensions['extension2'].discover = Mock(
            return_value={PackageDescriptor('/extension2/pkg1')})

        descs = discover_packages(None, None, discovery_extensions=extensions)
        assert len(descs) == 2
        expected_path = '/extension1/pkg1'.replace('/', os.sep)
        assert expected_path in (str(d.path) for d in descs)
        expected_path = '/extension2/pkg1'.replace('/', os.sep)
        assert expected_path in (str(d.path) for d in descs)

        # check with parameters
        extensions['extension3'].has_parameters = Mock(return_value=True)
        extensions['extension3'].discover = Mock(
            return_value={
                PackageDescriptor('/extension3/pkg1'),
                PackageDescriptor('/extension3/pkg2')
            })

        descs = discover_packages(None, None, discovery_extensions=extensions)
        assert len(descs) == 2
        expected_path = '/extension3/pkg1'.replace('/', os.sep)
        assert expected_path in (str(d.path) for d in descs)
        expected_path = '/extension3/pkg2'.replace('/', os.sep)
        assert expected_path in (str(d.path) for d in descs)
示例#2
0
def get_package_descriptors(args, *, additional_argument_names=None):
    """
    Get the package descriptors.

    The overview of the process:
    * Discover the package descriptors using the package discovery and
      identification extensions
    * Check is the passed package selection arguments have valid values
    * Augment the package descriptors

    :param additional_argument_names: A list of additional arguments to
      consider
    :returns: set of
      :py:class:`colcon_core.package_descriptor.PackageDescriptor`
    :rtype: set
    """
    extensions = get_package_identification_extensions()
    descriptors = discover_packages(args, extensions)

    pkg_names = {d.name for d in descriptors}
    _check_package_selection_parameters(args, pkg_names)

    augment_packages(descriptors,
                     additional_argument_names=additional_argument_names)
    return descriptors
示例#3
0
def package_name_completer(prefix, **kwargs):
    """Callable returning a list of packages names."""
    from colcon_core.package_discovery import discover_packages
    from colcon_core.package_identification \
        import get_package_identification_extensions
    # discover packages
    extensions = get_package_identification_extensions()
    pkgs = discover_packages(kwargs.get('parsed_args'), extensions)
    # return package names starting with the given prefix
    pkg_names = [pkg.name for pkg in pkgs]
    return (name for name in pkg_names if name.startswith(prefix))
示例#4
0
def get_package_path(package_name, args):
    """
    Get the relative package path.

    Use the `package_discovery` extension to find package path.

    :param str package_name: The name of the package to find path
    :param args: The positional arguments to `add_argument()`
    :returns: The package path or None
    """
    extensions = get_package_identification_extensions()
    descriptors = discover_packages(args, extensions)
    package_path = ''
    for pkg in descriptors:
        if (pkg.name == args.package_name):
            package_path = pkg.path
    return package_path