From 9b3873c4f0127902b6829a03c281a3b545ee1d91 Mon Sep 17 00:00:00 2001 From: Karthikeyan Singaravelan Date: Fri, 6 May 2022 23:57:22 +0530 Subject: [PATCH] Move exception raising to glob_targets to cover all commands. --- dvc/repo/pull.py | 4 ---- dvc/repo/push.py | 9 +-------- dvc/utils/__init__.py | 9 ++++++++- tests/func/test_add.py | 6 ++++++ 4 files changed, 15 insertions(+), 13 deletions(-) diff --git a/dvc/repo/pull.py b/dvc/repo/pull.py index 3639a89b6..b4f472933 100644 --- a/dvc/repo/pull.py +++ b/dvc/repo/pull.py @@ -1,7 +1,6 @@ import logging from typing import TYPE_CHECKING, Optional -from dvc.exceptions import GlobDoesNotMatchError from dvc.repo import locked from dvc.utils import glob_targets @@ -32,9 +31,6 @@ def pull( expanded_targets = glob_targets(targets, glob=glob) - if targets and not expanded_targets: - raise GlobDoesNotMatchError(targets) - processed_files_count = self.fetch( expanded_targets, jobs, diff --git a/dvc/repo/push.py b/dvc/repo/push.py index 2d9a7fbd1..25a9b3692 100644 --- a/dvc/repo/push.py +++ b/dvc/repo/push.py @@ -1,10 +1,6 @@ from typing import TYPE_CHECKING, Optional -from dvc.exceptions import ( - FileTransferError, - GlobDoesNotMatchError, - UploadError, -) +from dvc.exceptions import FileTransferError, UploadError from ..utils import glob_targets from . import locked @@ -39,9 +35,6 @@ def push( expanded_targets = glob_targets(targets, glob=glob) - if targets and not expanded_targets: - raise GlobDoesNotMatchError(targets) - used = self.used_objs( expanded_targets, all_branches=all_branches, diff --git a/dvc/utils/__init__.py b/dvc/utils/__init__.py index add978e05..c0e6ead55 100644 --- a/dvc/utils/__init__.py +++ b/dvc/utils/__init__.py @@ -476,17 +476,24 @@ def is_exec(mode): def glob_targets(targets, glob=True, recursive=True): + from dvc.exceptions import GlobDoesNotMatchError + if not glob: return targets from glob import iglob - return [ + results = [ exp_target for target in targets for exp_target in iglob(target, recursive=recursive) ] + if not results: + raise GlobDoesNotMatchError(targets) + + return results + def error_handler(func): def wrapper(*args, **kwargs): diff --git a/tests/func/test_add.py b/tests/func/test_add.py index dcd2bed64..d9f0275e2 100644 --- a/tests/func/test_add.py +++ b/tests/func/test_add.py @@ -16,6 +16,7 @@ from dvc.data.db import ODBManager from dvc.dvcfile import DVC_FILE_SUFFIX from dvc.exceptions import ( DvcException, + GlobDoesNotMatchError, InvalidArgumentError, OutputDuplicationError, OverlappingOutputPathsError, @@ -292,6 +293,11 @@ def test_add_filtered_files_in_dir( assert stage.outs[0].def_path in expected_def_paths +def test_add_glob_no_result_exception(dvc): + with pytest.raises(GlobDoesNotMatchError): + dvc.add(["invalid*"], glob=True) + + class TestAddExternal(TestAdd): @pytest.fixture def hash_name(self):