Example #1
0
    def test_add_variant_with_dependencies(self, tmpdir):
        path = Path(str(tmpdir)) / 'fake.jar'
        project = Project.from_dir(Path('/path/to/project'), version='1.3.4')

        path.write_text("Testing\n", encoding='utf-8')

        with Options(project=project):
            module_data = _create_module_data()

        dependency = Dependency('dep', {
            'location': 'remote',
            'version': '1.2.3',
            'scope': 'scope'
        })

        transient = dependency.derive_from('group', 'name', '1.2.4')
        transient.transient = True
        path_sets = [
            DependencyPathSet(dependency, path),
            DependencyPathSet(transient, path)
        ]

        _add_variant(module_data, API_ELEMENTS, path, {}, 'library', 'api',
                     None, path_sets)

        self._compare_to_file(module_data, 'variant_2')
Example #2
0
    def test_secondary_files(self):
        dep = _make_dep()
        primary_path = Path('path/to/file.txt')
        secondary_path = Path('path/to/file_2.txt')
        file_set = DependencyPathSet(dep, primary_path)

        with pytest.raises(AttributeError) as info:
            print(file_set.source_path)

        assert info.value.args[
            0] == "'DependencyPathSet' object has no attribute 'source_path'"

        file_set.add_secondary_path('source_path', secondary_path)

        assert file_set.source_path is secondary_path
Example #3
0
    def test_dependency_file_construction(self):
        path = Path('path/to/file.txt')
        dep = _make_dep()
        file_set = DependencyPathSet(dep, path)

        assert file_set.dependency == dep
        assert file_set.primary_path is path
Example #4
0
def _use_pom_resolution(context: DependencyContext, dependency: Dependency, classified_name: str, base_name: str) \
        -> Optional[DependencyPathSet]:
    """
    A function that resolves Java dependencies using POM files (the old way).

    :param context: the current dependency context in play.
    :param dependency: the dependency we are to resolve.
    :param classified_name: the classified base name for the main asset.
    :param base_name: the base name for file assets.
    :return: the appropriate dependency path set or ``None``.
    """
    jar_file = context.to_local_path(dependency, f'{classified_name}.jar')

    if not jar_file:
        return None

    # Ok, we're good to go.
    # First, let's see if there's a POM file that can tell us about dependencies.
    pom_file = context.to_local_path(dependency, f'{base_name}.pom')

    if pom_file and not dependency.ignore_transients:
        read_pom_for_dependencies(pom_file, context, dependency)

    # Now, let's create and load up our result:
    result = DependencyPathSet(dependency, jar_file)

    _try_to_add_secondary_path(context, dependency, 'sources', f'{base_name}-sources.jar', result)
    _try_to_add_secondary_path(context, dependency, 'javadoc', f'{base_name}-javadoc.jar', result)

    return result
Example #5
0
def _try_to_add_secondary_path(context: DependencyContext, dependency: Dependency, key: str, name: str,
                               path_set: DependencyPathSet, signatures: Optional[Dict[str, str]] = None):
    """
    A function that attempts to load a secondary path (sources or javadoc) and, if
    successful, adds them to the given path set.

    :param context: the current dependency context in play.
    :param dependency: the dependency we are to resolve.
    :param key: the key by which the secondary path will be known.
    :param name: the name of the secondary path.
    :param path_set: the path set to add a successfully isolated path to.
    :param signatures: the set of signatures to verify against (if any).
    """
    path = context.to_local_path(dependency, name, signatures)

    if path:
        path_set.add_secondary_path(key, path)
Example #6
0
    def test_add_class_path(self):
        dep = Dependency('dep', {
            'location': 'local',
            'version': '4.5.6',
            'scope': 'scope'
        })
        dps_list = [
            DependencyPathSet(dep, Path('a.jar')),
            DependencyPathSet(dep, Path('b.jar')),
            DependencyPathSet(dep, Path('c.jar'))
        ]
        expected_class_path = os.pathsep.join(['a.jar', 'b.jar', 'c.jar'])
        options = []

        add_class_path(options, dps_list)

        assert options == ['--class-path', expected_class_path]
Example #7
0
    def test_build_options_with_class_path(self):
        """Make sure we build options correctly with no extra class path."""
        classes_dir = Path('classes')
        dep = Dependency('dep', {
            'location': 'local',
            'version': '1.2.3',
            'scope': 'scope'
        })
        dps_list = [
            DependencyPathSet(dep, Path('a.jar')),
            DependencyPathSet(dep, Path('b.jar')),
            DependencyPathSet(dep, Path('c.jar'))
        ]
        expected_class_path = os.pathsep.join(['a.jar', 'b.jar', 'c.jar'])
        options = _build_compiler_options(classes_dir, dps_list)

        assert options == ['-d', 'classes', '--class-path', expected_class_path]
Example #8
0
    def test_resolve_duplicates(self):
        dep = _make_dep()
        path = Path('/path/to/file.txt')
        language = Language({}, 'lang')
        path_set = DependencyPathSet(dep, path)
        context = DependencyContext([dep, dep], language,
                                    Configuration({}, [], None))

        language.resolver = MagicMock(return_value=path_set)

        assert context.resolve() == [path_set]

        language.resolver.assert_called_once_with(context, dep)
Example #9
0
    def test_resolve_version_mismatch(self):
        dep1 = _make_dep(version='1.2.3')
        dep2 = _make_dep(version='4.5.6')
        path = Path('/path/to/file.txt')
        language = Language({}, 'lang')
        path_set = DependencyPathSet(dep1, path)
        context = DependencyContext([dep1, dep2], language,
                                    Configuration({}, [], None))

        language.resolver = MagicMock(return_value=path_set)

        with pytest.raises(ValueError) as info:
            context.resolve()

        assert info.value.args[0] == 'The same library, name:name, is required at two different versions, 1.2.3 vs. ' \
                                     '4.5.6.'

        language.resolver.assert_called_once_with(context, dep1)
Example #10
0
def _use_module_resolution(context: DependencyContext, dependency: Dependency, module_path: Path) \
        -> Optional[DependencyPathSet]:
    """
    A function that resolves Java dependencies using module files (the new way).

    :param context: the current dependency context in play.
    :param dependency: the dependency we are to resolve.
    :param module_path: the local path to our module file.
    :return: the appropriate dependency path set or ``None``.
    """
    module_data = ModuleData.from_path(module_path)
    variant = module_data.get_variant(API_ELEMENTS)

    # If there's not an API variant, then we really can't do anything.
    if not variant:
        return None

    jar_variant_file = variant.files[0]
    jar_file = context.to_local_path(dependency, jar_variant_file.name, jar_variant_file.signatures)

    # If we couldn't get the jar file, report same.
    if not jar_file:
        return None

    # Ok, let's process any dependencies that may be involved.
    if not dependency.ignore_transients:
        for transient_dependency in variant.dependencies:
            context.add_dependency(transient_dependency.as_dependency(dependency))

    # Now, let's create and load up our result:
    result = DependencyPathSet(dependency, jar_file)

    _try_for_variant(context, dependency, module_data, SOURCE_ELEMENTS, result)
    _try_for_variant(context, dependency, module_data, JAVADOC_ELEMENTS, result)

    return result