def _init_projects( self, projects: Sequence[Union[ProjectEntryDict, ProjectEntry, Dict[str, str]]] ) -> Dict[str, ProjectEntry]: """Iterate over projects from manifest and initialize ProjectEntries from it. Args: projects (Sequence[Union[ProjectEntryDict, ProjectEntry, Dict[str, str]]]): Iterable with projects Raises: RuntimeError: Project unknown Returns: Dict[str, ProjectEntry]: Dictionary with key: Name of project, Value: ProjectEntry """ _projects: Dict[str, ProjectEntry] = {} for project in projects: if isinstance(project, dict): last_project = _projects[ project["name"]] = ProjectEntry.from_yaml( project, self._default_remote) elif isinstance(project, ProjectEntry): last_project = _projects[project.name] = ProjectEntry.copy( project, self._default_remote) else: raise RuntimeError(f"{project} has unknown type") if last_project.remote: last_project.set_remote(self._remotes[last_project.remote]) return _projects
def __init__(self, manifest: ManifestDict) -> None: """Create the manifest.""" self.__version: str = manifest.get("version", self.CURRENT_VERSION) self._remotes, default_remotes = self._determine_remotes( manifest["remotes"]) if not default_remotes: default_remotes = list(self._remotes.values())[0:1] self._projects: Dict[str, ProjectEntry] = {} for project in manifest["projects"]: if isinstance(project, dict): last_project = self._projects[ project["name"]] = ProjectEntry.from_yaml( project, default_remotes[0]) elif isinstance(project, ProjectEntry): last_project = self._projects[ project.name] = ProjectEntry.copy(project, default_remotes[0]) else: raise RuntimeError(f"{project} has unknown type") if last_project.remote: last_project.set_remote(self._remotes[last_project.remote])
def _import_from_git() -> Sequence[ProjectEntry]: projects: List[ProjectEntry] = [] toplevel: str = "" for submodule in GitRepo.submodules(): projects.append( ProjectEntry({ "name": submodule.name, "revision": submodule.sha, "url": submodule.url, "dst": submodule.path, "branch": submodule.branch, "tag": submodule.tag, })) logger.info(f"Found {submodule.name}") if not toplevel: toplevel = submodule.toplevel elif toplevel != submodule.toplevel: raise RuntimeError( "Recursive submodules not (yet) supported. Check manifest!") if os.path.realpath(toplevel) != os.getcwd(): logger.warning("\n".join(( f'The toplevel directory is in "{toplevel}"', f'"dfetch import" was called from "{os.getcwd()}"', "All projects paths will be relative to the current directory dfetch is running!", ))) return projects
def test_are_there_local_changes(name, hash_in_metadata, current_hash, expectation): with patch("dfetch.project.vcs.hash_directory") as mocked_hash_directory: with patch("dfetch.project.vcs.VCS._on_disk_hash") as mocked_on_disk_hash: vcs = ConcreteVCS(ProjectEntry({"name": "proj1"})) mocked_on_disk_hash.return_value = hash_in_metadata mocked_hash_directory.return_value = current_hash assert expectation == vcs._are_there_local_changes()
def test_get_childmanifests(name, manifest_paths) -> None: parent = ProjectEntry({"name": "parent"}) with patch("dfetch.manifest.manifest.find_file") as find_file_mock: with patch("dfetch.manifest.validate.validate"): with patch("dfetch.manifest.manifest.Manifest"): find_file_mock.return_value = manifest_paths found_childmanifests = get_childmanifests([parent]) assert len(found_childmanifests) == len(manifest_paths) for path, result in zip(manifest_paths, found_childmanifests): assert os.path.realpath(path) == result[1]
def test_check_wanted_with_local(name, given_on_disk, given_wanted, expect_wanted, expect_have): with patch("dfetch.project.vcs.os.path.exists") as mocked_path_exists: with patch("dfetch.project.vcs.Metadata.from_file") as mocked_metadata: vcs = ConcreteVCS(ProjectEntry({"name": "proj1"})) mocked_path_exists.return_value = bool(given_on_disk) mocked_metadata().version = given_on_disk vcs._wanted_version = given_wanted wanted, have = vcs.check_wanted_with_local() assert wanted == expect_wanted assert have == expect_have
def _import_from_svn() -> Sequence[ProjectEntry]: projects: List[ProjectEntry] = [] for external in SvnRepo.externals(): projects.append( ProjectEntry({ "name": external.name, "revision": external.revision, "url": external.url, "dst": external.path, "branch": external.branch, "tag": external.tag, "src": external.src, })) logger.info(f"Found {external.name}") return projects
def test_projectentry_revision(): assert ProjectEntry({ "name": "SomeProject", "revision": "123" }).revision == "123"
], ) def test_check_path(name, cmd_result, expectation): with patch("dfetch.project.git.run_on_cmdline") as run_on_cmdline_mock: run_on_cmdline_mock.side_effect = cmd_result assert GitRepo.check_path() == expectation @pytest.mark.parametrize( "name, project, cmd_result, expectation", [ ("SSH url", ProjectEntry({ "name": "sshProject", "url": "some.git" }), [], True), ( "http url", ProjectEntry({ "name": "httpProject", "url": "some/bla" }), ["Yep!"], True, ), ( "Failed command", ProjectEntry({ "name": "proj1", "url": "some/bla"
def test_projectentry_source(): assert ProjectEntry({ "name": "SomeProject", "src": "SomePath" }).source == "SomePath"
], ) def test_check_path(name, cmd_result, expectation): with patch("dfetch.project.svn.run_on_cmdline") as run_on_cmdline_mock: run_on_cmdline_mock.side_effect = cmd_result assert SvnRepo.check_path() == expectation @pytest.mark.parametrize( "name, project, cmd_result, expectation", [ ("Ok url", ProjectEntry({ "name": "proj1", "url": "some_url" }), ["Yep!"], True), ( "Failed command", ProjectEntry({ "name": "proj2", "url": "some_url" }), [SubprocessCommandError], False, ), ( "No svn", ProjectEntry({ "name": "proj3", "url": "some_url"
def test_check_overlapping_destinations(name, real_path, destinations): with pytest.raises(RuntimeError): Update._check_overlapping_destination( ProjectEntry.from_yaml({"name": "a"}), destinations, real_path)
def test_check_path_traversal(name, real_path): with pytest.raises(RuntimeError): Update._check_path_traversal(ProjectEntry.from_yaml({"name": "a"}), real_path, "/somewhere/somewhere")
def test_projectentry_vcs(): assert ProjectEntry({"name": "SomeProject", "vcs": "git"}).vcs == "git"
def test_projectentry_patch(): assert (ProjectEntry({ "name": "SomeProject", "patch": "diff.patch" }).patch == "diff.patch")
def test_projectentry_remote(): assert (ProjectEntry({ "name": "SomeProject", "remote": "SomeRemote" }).remote == "SomeRemote")
def test_projectentry_name(): assert ProjectEntry({"name": "SomeProject"}).name == "SomeProject"
def test_projectentry_as_str(): assert (str(ProjectEntry( {"name": "SomeProject"})) == "SomeProject latest SomeProject")
def test_projectentry_as_yaml(): assert ProjectEntry({ "name": "SomeProject" }).as_yaml() == { "name": "SomeProject" }