From 80f003670e9ce2515b23813fec9dfdf7a6696fb3 Mon Sep 17 00:00:00 2001 From: Justin DuJardin Date: Thu, 12 Nov 2020 12:58:15 -0800 Subject: [PATCH] fix: path.scheme would error with schemeless paths (#37) - return "" from file paths as expected rather than error --- pathy/base.py | 4 ++++ pathy/clients.py | 1 + tests/test_base.py | 7 +++++++ tests/test_clients.py | 1 + 4 files changed, 13 insertions(+) diff --git a/pathy/base.py b/pathy/base.py index 6848bd5..0d5c2c5 100644 --- a/pathy/base.py +++ b/pathy/base.py @@ -235,8 +235,12 @@ def scheme(self) -> str: ```python assert Pathy("gs://foo/bar").scheme == "gs" assert Pathy("file:///tmp/foo/bar").scheme == "file" + assert Pathy("/dev/null").scheme == "" """ + # If there is no drive, return nothing + if self.drive == "": + return "" # This is an assumption of mine. I think it's fine, but let's # cause an error if it's not the case. assert self.drive[-1] == ":", "drive should end with :" diff --git a/pathy/clients.py b/pathy/clients.py index bd61141..f55f7ee 100644 --- a/pathy/clients.py +++ b/pathy/clients.py @@ -11,6 +11,7 @@ # hardcode it (atleast the base schemes) to get nice types flowing # in cases where they would otherwise be lost. _client_registry: Dict[str, Type[BucketClient]] = { + "": BucketClientFS, "file": BucketClientFS, "gs": BucketClientGCS, } diff --git a/tests/test_base.py b/tests/test_base.py index 22c3dc3..188d095 100644 --- a/tests/test_base.py +++ b/tests/test_base.py @@ -129,6 +129,13 @@ def test_base_repr(): assert bytes(PurePathy("fake_file.txt")) == b"fake_file.txt" +def test_base_scheme_extraction(): + assert PurePathy("gs://var/tests/fake").scheme == "gs" + assert PurePathy("s3://var/tests/fake").scheme == "s3" + assert PurePathy("file://var/tests/fake").scheme == "file" + assert PurePathy("/var/tests/fake").scheme == "" + + @pytest.mark.skipif(sys.version_info < (3, 6), reason="requires python3.6 or higher") def test_base_fspath(): assert os.fspath(PurePathy("/var/tests/fake")) == "/var/tests/fake" diff --git a/tests/test_clients.py b/tests/test_clients.py index 972b97b..4676473 100644 --- a/tests/test_clients.py +++ b/tests/test_clients.py @@ -14,6 +14,7 @@ def test_clients_get_client_works_with_builtin_schems(): assert isinstance(get_client("gs"), BucketClientGCS) assert isinstance(get_client("file"), BucketClientFS) + assert isinstance(get_client(""), BucketClientFS) def test_clients_get_client_errors_with_unknown_scheme():