def test_only_name_and_version(self): """Test only with name and version.""" # no index and no git dep = Dependency(self.package_name, self.version) assert dep.get_pip_install_args() == [ f"{self.package_name}{self.version}", ]
def _install_dependency(dependency_name: str, dependency: Dependency): click.echo("Installing {}...".format(pprint.pformat(dependency_name))) try: index = dependency.get("index", None) git_url = dependency.get("git", None) revision = dependency.get("ref", "") version_constraint = dependency.get("version", "") command = [sys.executable, "-m", "pip", "install"] if git_url is not None: command += ["-i", index] if index is not None else [] command += [ "git+" + git_url + "@" + revision + "#egg=" + dependency_name ] else: command += ["-i", index] if index is not None else [] command += [dependency_name + version_constraint] logger.debug("Calling '{}'".format(" ".join(command))) return_code = _run_install_subprocess(command) if return_code == 1: # try a second time return_code = _run_install_subprocess(command) assert return_code == 0, "Return code != 0." except Exception as e: raise AEAException( "An error occurred while installing {}, {}: {}".format( dependency_name, dependency, str(e)))
def test_dependency_from_json_fail_more_than_one_key(): """Test failure of Dependency.from_json due to more than one key at the top level.""" bad_obj = {"field_1": {}, "field_2": {}} keys = set(bad_obj.keys()) with pytest.raises(ValueError, match=f"Only one key allowed, found {keys}"): Dependency.from_json(bad_obj)
def test_name_version_index(self): """Test the method with name, version and index.""" dep = Dependency(self.package_name, self.version, self.custom_index) assert dep.get_pip_install_args() == [ "-i", self.custom_index, f"{self.package_name}{self.version}", ]
def test_is_simple_dep(): """Test the `is_simple_dep` function.""" dependency_a = Dependency("name", "==0.1.0") assert is_simple_dep(dependency_a), "Should be a simple dependency." dependency_b = Dependency("name") assert is_simple_dep(dependency_b), "Should be a simple dependency." dependency_c = Dependency("name", "==0.1.0", "pypi") assert not is_simple_dep( dependency_c), "Should not be a simple dependency."
def merge_dependencies(dep1: Dependencies, dep2: Dependencies) -> Dependencies: """ Merge two groups of dependencies. If some of them are not "simple" (see above), we just filter them out. :param dep1: the first operand :param dep2: the second operand. :return: the merged dependencies. """ result: Dependencies result = { pkg_name: info for pkg_name, info in dep1.items() if is_simple_dep(info) } for pkg_name, info in dep2.items(): if not is_simple_dep(info): continue new_specifier = SpecifierSet(info.version) old_specifier = (SpecifierSet(result[pkg_name].version) if pkg_name in result else SpecifierSet("")) combined_specifier = and_(new_specifier, old_specifier) new_info = Dependency( name=info.name, version=combined_specifier, index=info.index, git=info.git, ref=info.ref, ) result[pkg_name] = new_info return result
def test_install_dependency(self): """Test dependencies installed.""" package_name = "async_generator" dependency = Dependency(package_name, "==1.10") sys.modules.pop(package_name, None) run_install_subprocess( [sys.executable, "-m", "pip", "uninstall", package_name, "-y"] ) try: import_module(package_name) raise Exception("should not be raised") except ModuleNotFoundError: pass builder = AEABuilder.from_aea_project(Path(self._get_cwd())) with patch( "aea.aea_builder._DependenciesManager.pypi_dependencies", {"package_name": dependency}, ): builder.install_pypi_dependencies() import_module(package_name) sys.modules.pop(package_name) run_install_subprocess( [sys.executable, "-m", "pip", "uninstall", package_name, "-y"] ) try: import_module(package_name) raise Exception("should not be raised") except ModuleNotFoundError: pass
def test_dependency_to_string(): """Test dependency.__str__ method.""" dependency = Dependency("package_1", "==0.1.0", "https://index.com", "https://some-repo.git", "branch") assert ( str(dependency) == "Dependency(name='package_1', version='==0.1.0', index='https://index.com', git='https://some-repo.git', ref='branch')" )
def test__install_dependency_ok(self, *mocks): """Test for install_dependency method ok.""" result = mock.Mock() result.returncode = 0 with mock.patch("aea.helpers.install_dependency.subprocess.Popen", return_value=result): install_dependency("test", Dependency("test", "==10.0.0"), mock.Mock())
def test__install_dependency_fails(self, *mocks): """Test for install_dependency method fails.""" result = mock.Mock() result.returncode = 1 with mock.patch("aea.helpers.install_dependency.subprocess.Popen", return_value=result): with self.assertRaises(AEAException): install_dependency("test", Dependency("test", "==10.0.0"), mock.Mock())
def test_component_add_bad_dep(): """Test component load failed cause dependency.""" builder = AEABuilder() builder.set_name("aea_1") builder.add_private_key("fetchai") connection = _make_dummy_connection() connection.configuration.pypi_dependencies = { "something": Dependency("something", "==0.1.0") } builder.add_component_instance(connection) a_protocol = Protocol( ProtocolConfig("a_protocol", "author", "0.1.0"), DefaultMessage ) a_protocol.configuration.pypi_dependencies = { "something": Dependency("something", "==0.2.0") } with pytest.raises( AEAException, match=r"Conflict on package something: specifier set .*" ): builder.add_component_instance(a_protocol)
def test_dependencies_from_to_json(): """Test serialization and deserialization of Dependencies object.""" version_str = "==0.1.0" git_url = "https://some-git-repo.git" branch = "some-branch" dep1 = Dependency("package_1", version_str, DEFAULT_PYPI_INDEX_URL, git_url, branch) dep2 = Dependency("package_2", version_str) expected_obj = {"package_1": dep1, "package_2": dep2} expected_obj_json = dependencies_to_json(expected_obj) assert expected_obj_json == { "package_1": { "version": "==0.1.0", "index": DEFAULT_PYPI_INDEX_URL, "git": git_url, "ref": branch, }, "package_2": { "version": version_str }, } actual_obj = dependencies_from_json(expected_obj_json) assert expected_obj == actual_obj
def test_merge_dependencies(): """Test the 'merge_dependencies' function.""" dependencies_a = { "package_1": Dependency("package_1", "==0.1.0"), "package_2": Dependency("package_2", "==0.3.0"), "package_3": Dependency("package_3", "==0.2.0", "https://pypi.org"), } dependencies_b = { "package_1": Dependency("package_1", "==0.1.0"), "package_2": Dependency("package_2", "==0.2.0"), "package_4": Dependency("package_4", "==0.1.0", "https://pypi.org"), } expected_merged_dependencies = { "package_1": Dependency("package_1", "==0.1.0"), "package_2": Dependency("package_2", "==0.2.0,==0.3.0"), } assert expected_merged_dependencies == merge_dependencies( dependencies_a, dependencies_b)
def test_name_version_index_git_ref(self): """Test the method when name, version, index, git and ref fields are provided.""" dep = Dependency(self.package_name, self.version, self.custom_index, self.git_url, self.ref) git_url = f"git+{self.git_url}@{self.ref}#egg={self.package_name}" assert dep.get_pip_install_args() == ["-i", self.custom_index, git_url]
def test_dependency_from_json_fail_not_allowed_keys(): """Test failure of Dependency.from_json due to unallowed keys""" bad_obj = {"field_1": {"not-allowed-key": "value"}} with pytest.raises(ValueError, match="Not allowed keys: {'not-allowed-key'}"): Dependency.from_json(bad_obj)
def test_to_set_specifier(): """Test the 'to_set_specifier' function.""" dependency_a = Dependency("name", "==0.1.0") assert to_set_specifier(dependency_a) == SpecifierSet("==0.1.0")