Skip to content

Commit 6ba7b1d

Browse files
committed
gs: adjust tests
2 parents 4f011f6 + 24d3a87 commit 6ba7b1d

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

dvc/remote/gs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ def exists(self, path_info):
176176
177177
eg: if `data/file.txt` exists, check for `data` should return True
178178
"""
179-
return self.isfile(path_info) or self.isdir(path_info / "")
179+
return self.isfile(path_info) or self.isdir(path_info)
180180

181181
def _upload(self, from_file, to_info, name=None, no_progress_bar=True):
182182
bucket = self.gs.bucket(to_info.bucket)

dvc/remote/s3.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import os
66
import threading
77

8+
from botocore.exceptions import ClientError
89
from funcy import cached_property, wrap_prop
910

1011
from dvc.config import Config
@@ -207,10 +208,26 @@ def _list_paths(self, path_info, max_items=None):
207208
def list_cache_paths(self):
208209
return self._list_paths(self.path_info)
209210

211+
def isfile(self, path_info):
212+
if path_info.path.endswith("/"):
213+
return False
214+
215+
try:
216+
self.s3.head_object(Bucket=path_info.bucket, Key=path_info.path)
217+
except ClientError as exc:
218+
if exc.response["Error"]["Code"] != "404":
219+
raise
220+
return False
221+
222+
return True
223+
210224
def exists(self, path_info):
211-
dir_path = path_info / ""
212-
fname = next(self._list_paths(path_info, max_items=1), "")
213-
return path_info.path == fname or fname.startswith(dir_path.path)
225+
"""Check if the blob exists. If it does not exist,
226+
it could be a part of a directory path.
227+
228+
eg: if `data/file.txt` exists, check for `data` should return True
229+
"""
230+
return self.isfile(path_info) or self.isdir(path_info)
214231

215232
def makedirs(self, path_info):
216233
# We need to support creating empty directories, which means
@@ -279,7 +296,7 @@ def _generate_download_url(self, path_info, expires=3600):
279296
)
280297

281298
def walk_files(self, path_info, max_items=None):
282-
for fname in self._list_paths(path_info, max_items):
299+
for fname in self._list_paths(path_info / "", max_items):
283300
if fname.endswith("/"):
284301
continue
285302

tests/unit/remote/test_remote_dir.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
remotes = [GCP, S3Mocked]
99

1010
FILE_WITH_CONTENTS = {
11+
"data1.txt": "",
1112
"empty_dir/": "",
1213
"empty_file": "",
1314
"foo": "foo",
1415
"data/alice": "alice",
1516
"data/alpha": "alpha",
17+
"data/subdir-file.txt": "subdir",
1618
"data/subdir/1": "1",
1719
"data/subdir/2": "2",
1820
"data/subdir/3": "3",
@@ -60,6 +62,7 @@ def test_exists(remote):
6062
(True, "data/subdir/1"),
6163
(False, "data/al"),
6264
(False, "foo/"),
65+
(True, "data1.txt"),
6366
]
6467

6568
for expected, path in test_cases:
@@ -71,6 +74,7 @@ def test_walk_files(remote):
7174
files = [
7275
remote.path_info / "data/alice",
7376
remote.path_info / "data/alpha",
77+
remote.path_info / "data/subdir-file.txt",
7478
remote.path_info / "data/subdir/1",
7579
remote.path_info / "data/subdir/2",
7680
remote.path_info / "data/subdir/3",
@@ -108,7 +112,7 @@ def test_makedirs(remote):
108112
assert remote.isdir(empty_dir)
109113

110114

111-
@pytest.mark.parametrize("remote", [GCP], indirect=True)
115+
@pytest.mark.parametrize("remote", [GCP, S3Mocked], indirect=True)
112116
def test_isfile(remote):
113117
test_cases = [
114118
(False, "empty_dir/"),

0 commit comments

Comments
 (0)