diff --git a/dvc/remote/gs.py b/dvc/remote/gs.py index d732f6045..190c52176 100644 --- a/dvc/remote/gs.py +++ b/dvc/remote/gs.py @@ -163,14 +163,20 @@ class RemoteGS(RemoteBASE): dir_path = path_info / "" return bool(list(self._list_paths(dir_path, max_items=1))) - def exists(self, path_info): - blob = self.gs.bucket(path_info.bucket).blob(path_info.path) - if blob.exists(): - return True + def isfile(self, path_info): + if path_info.path.endswith("/"): + return False - # if the blob does not exist, it could be a part of a directory path - # eg: if `data/file.txt` exists, check for `data` should return True - return self.isdir(path_info / "") + blob = self.gs.bucket(path_info.bucket).blob(path_info.path) + return blob.exists() + + def exists(self, path_info): + """Check if the blob exists. If it does not exist, + it could be a part of a directory path. + + eg: if `data/file.txt` exists, check for `data` should return True + """ + return True if self.isfile(path_info) else self.isdir(path_info / "") def _upload(self, from_file, to_info, name=None, no_progress_bar=True): bucket = self.gs.bucket(to_info.bucket) diff --git a/tests/unit/remote/test_remote_dir.py b/tests/unit/remote/test_remote_dir.py index 9213a670f..e0e48d1f7 100644 --- a/tests/unit/remote/test_remote_dir.py +++ b/tests/unit/remote/test_remote_dir.py @@ -106,3 +106,25 @@ def test_makedirs(remote): remote.makedirs(empty_dir) assert remote.exists(empty_dir) assert remote.isdir(empty_dir) + + +@pytest.mark.parametrize("remote", [GCP], indirect=True) +def test_isfile(remote): + test_cases = [ + (False, "empty_dir/"), + (True, "empty_file"), + (True, "foo"), + (True, "data/alice"), + (True, "data/alpha"), + (True, "data/subdir/1"), + (True, "data/subdir/2"), + (True, "data/subdir/3"), + (False, "data/subdir/empty_dir/"), + (True, "data/subdir/empty_file"), + (False, "something-that-does-not-exist"), + (False, "data/subdir/empty-file/"), + (False, "empty_dir"), + ] + + for expected, path in test_cases: + assert remote.isfile(remote.path_info / path) == expected