From cc5ba985b0a1f1f0893c6f703b0329b41bc83e2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20M=C3=A9ndez?= Date: Wed, 30 Jun 2021 08:49:15 +0200 Subject: [PATCH] import: error message when imported file does not exist (#6240) * Fix exception handling * Replace exceptions with PathMissingError --- dvc/dependency/repo.py | 20 +++++++++++++------- tests/func/test_import.py | 8 ++++---- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/dvc/dependency/repo.py b/dvc/dependency/repo.py index 3e5d81134..a9bbdcbf4 100644 --- a/dvc/dependency/repo.py +++ b/dvc/dependency/repo.py @@ -97,7 +97,7 @@ class RepoDependency(Dependency): self, **kwargs ) -> Dict[Optional["ObjectDB"], Set["HashFile"]]: from dvc.config import NoRemoteError - from dvc.exceptions import NoOutputOrStageError + from dvc.exceptions import NoOutputOrStageError, PathMissingError from dvc.objects.db.git import GitObjectDB from dvc.objects.stage import stage @@ -126,12 +126,18 @@ class RepoDependency(Dependency): except (NoRemoteError, NoOutputOrStageError): pass - staged_obj = stage( - local_odb, - path_info, - repo.repo_fs, - local_odb.fs.PARAM_CHECKSUM, - ) + try: + staged_obj = stage( + local_odb, + path_info, + repo.repo_fs, + local_odb.fs.PARAM_CHECKSUM, + ) + except FileNotFoundError as exc: + raise PathMissingError( + self.def_path, self.def_repo[self.PARAM_URL] + ) from exc + self._staged_objs[rev] = staged_obj git_odb = GitObjectDB(repo.repo_fs, repo.root_dir) used_objs[git_odb].add(staged_obj) diff --git a/tests/func/test_import.py b/tests/func/test_import.py index 1e68fe005..372af19ac 100644 --- a/tests/func/test_import.py +++ b/tests/func/test_import.py @@ -8,7 +8,7 @@ from mock import patch import dvc.data_cloud as cloud from dvc.config import NoRemoteError from dvc.dvcfile import Dvcfile -from dvc.exceptions import DownloadError +from dvc.exceptions import DownloadError, PathMissingError from dvc.objects.db import ODBManager from dvc.stage.exceptions import StagePathNotFoundError from dvc.system import System @@ -289,7 +289,7 @@ def test_push_wildcard_from_bare_git_repo( dvc_repo = make_tmp_dir("dvc-repo", scm=True, dvc=True) with dvc_repo.chdir(): dvc_repo.dvc.imp(os.fspath(tmp_dir), "dirextra") - with pytest.raises(FileNotFoundError): + with pytest.raises(PathMissingError): dvc_repo.dvc.imp(os.fspath(tmp_dir), "dir123") @@ -347,11 +347,11 @@ def test_pull_non_workspace(tmp_dir, scm, dvc, erepo_dir): def test_import_non_existing(erepo_dir, tmp_dir, dvc): - with pytest.raises(FileNotFoundError): + with pytest.raises(PathMissingError): tmp_dir.dvc.imp(os.fspath(erepo_dir), "invalid_output") # https://github.com/iterative/dvc/pull/2837#discussion_r352123053 - with pytest.raises(FileNotFoundError): + with pytest.raises(PathMissingError): tmp_dir.dvc.imp(os.fspath(erepo_dir), "/root/", "root")