Skip to content

Commit

Permalink
Bug: isdir API when not found should list it to check
Browse files Browse the repository at this point in the history
  • Loading branch information
yanghua committed Nov 1, 2024
1 parent b4fb038 commit 50b94a1
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
Binary file added tosfs/.DS_Store
Binary file not shown.
11 changes: 10 additions & 1 deletion tosfs/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -839,7 +839,16 @@ def isdir(self, path: str) -> bool:
raise e
except TosServerError as e:
if e.status_code == TOS_SERVER_STATUS_CODE_NOT_FOUND:
return False
out = retryable_func_executor(
lambda: self.tos_client.list_objects_type2(
bucket,
prefix=key,
delimiter="/",
max_keys=1,
),
max_retry_num=self.max_retry_num,
)
return out.key_count > 0
else:
raise e
except Exception as e:
Expand Down
21 changes: 21 additions & 0 deletions tosfs/tests/test_tosfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,27 @@ def test_ls_dir(tosfs: TosFileSystem, bucket: str, temporary_workspace: str) ->

assert tosfs.ls(f"{bucket}/{temporary_workspace}/nonexistent", detail=False) == []

path = f"{bucket}/{temporary_workspace}/a/b/c/d"
bucket, key, _ = tosfs._split_path(path)
tosfs.tos_client.put_object(bucket=bucket, key=key, content="")
assert tosfs.isdir(f"{bucket}/{temporary_workspace}/a")
assert not tosfs.isfile(f"{bucket}/{temporary_workspace}/a")
assert tosfs.info(f"{bucket}/{temporary_workspace}/a")["type"] == "directory"
assert tosfs.exists(f"{bucket}/{temporary_workspace}/a")
assert not tosfs.isdir(f"{bucket}/{temporary_workspace}/b")
assert not tosfs.isfile(f"{bucket}/{temporary_workspace}/b")
assert tosfs.isdir(f"{bucket}/{temporary_workspace}/a/b")
assert not tosfs.isfile(f"{bucket}/{temporary_workspace}/a/b")
assert tosfs.info(f"{bucket}/{temporary_workspace}/a/b")["type"] == "directory"
assert tosfs.exists(f"{bucket}/{temporary_workspace}/a/b")
assert tosfs.isdir(f"{bucket}/{temporary_workspace}/a/b/c")
assert not tosfs.isfile(f"{bucket}/{temporary_workspace}/a/b/c")
assert tosfs.info(f"{bucket}/{temporary_workspace}/a/b/c")["type"] == "directory"
assert tosfs.exists(f"{bucket}/{temporary_workspace}/a/b/c")
assert not tosfs.isdir(f"{bucket}/{temporary_workspace}/a/b/c/d")
assert tosfs.isfile(f"{bucket}/{temporary_workspace}/a/b/c/d")
assert tosfs.info(f"{bucket}/{temporary_workspace}/a/b/c/d")["type"] == "file"


def test_ls_iterate(
tosfs: TosFileSystem, bucket: str, temporary_workspace: str
Expand Down

0 comments on commit 50b94a1

Please sign in to comment.