Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pjbull committed Oct 8, 2023
1 parent 921f84e commit b87d95d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
3 changes: 3 additions & 0 deletions cloudpathlib/cloudpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,9 @@ def with_name(self, name: str) -> Self:
return self._dispatch_to_path("with_name", name)

def with_segments(self, *pathsegments) -> Self:
"""Create a new CloudPath with the same client out of the given segments.
The first segment will be interpreted as the bucket/container name.
"""
return self._new_cloudpath("/".join(pathsegments))

def with_suffix(self, suffix: str) -> Self:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_cloudpath_file_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ def _check_glob(pattern, glob_method, **kwargs):
)

# 3.12+ kwargs
if sys.version_info < (3, 12):
if sys.version_info >= (3, 12):
_check_glob("dir*/FILE*", "glob", case_sensitive=False)
_check_glob("dir*/file*", "glob", case_sensitive=True)
_check_glob("dir*/FILE*", "rglob", case_sensitive=False)
Expand Down
20 changes: 20 additions & 0 deletions tests/test_cloudpath_manipulation.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from pathlib import PurePosixPath
import sys

import pytest

Expand Down Expand Up @@ -50,6 +51,12 @@ def test_relative_to(rig, azure_rig, gs_rig):
assert rig.create_cloud_path("bucket/b/c/d.file").relative_to(
rig.create_cloud_path("bucket/z")
)

if sys.version_info >= (3, 12):
assert rig.create_cloud_path("bucket/path/to/file.txt").relative_to(
rig.create_cloud_path("other_bucket/path2"), walk_up=True
) == PurePosixPath("../../bucket/path/to/file.txt")

with pytest.raises(ValueError):
assert rig.create_cloud_path("a/b/c/d.file").relative_to(PurePosixPath("/a/b/c"))
other_rig = azure_rig if rig.cloud_prefix != azure_rig.cloud_prefix else gs_rig
Expand All @@ -73,6 +80,9 @@ def test_joins(rig):
assert not rig.create_cloud_path("a/b/c/d").match("**/c")
assert rig.create_cloud_path("a/b/c/d").match("a/*/c/d")

if sys.version_info >= (3, 12):
assert rig.create_cloud_path("a/b/c/d").match("A/*/C/D", case_sensitive=False)

assert rig.create_cloud_path("a/b/c/d").anchor == rig.cloud_prefix
assert rig.create_cloud_path("a/b/c/d").parent == rig.create_cloud_path("a/b/c")

Expand Down Expand Up @@ -107,6 +117,16 @@ def test_joins(rig):
)


def test_with_segments(rig):
assert rig.create_cloud_path("a/b/c/d").with_segments(
"x", "y", "z"
) == rig.client_class().CloudPath(f"{rig.cloud_prefix}x/y/z")


def test_is_junction(rig):
assert not rig.create_cloud_path("a/b/foo").is_junction()


def test_equality(rig):
assert rig.create_cloud_path("a/b/foo") == rig.create_cloud_path("a/b/foo")
assert hash(rig.create_cloud_path("a/b/foo")) == hash(rig.create_cloud_path("a/b/foo"))
Expand Down

0 comments on commit b87d95d

Please sign in to comment.