55import pytest
66
77from upath import UPath
8+ from upath .implementations .s3 import S3Path
89from upath .tests .cases import BaseTests
910
1011
1112@pytest .mark .skipif (
12- sys .platform .startswith ("win" ), reason = "don't run test on Windows" ,
13+ sys .platform .startswith ("win" ),
14+ reason = "don't run test on Windows" ,
1315)
1416def test_posix_path (local_testdir ):
1517 assert isinstance (UPath (local_testdir ), pathlib .PosixPath )
@@ -75,10 +77,15 @@ class MyPath(UPath):
7577
7678
7779def test_instance_check (local_testdir ):
78- path = UPath (local_testdir )
79- assert isinstance (path , UPath )
80- path = UPath (f"file://{ local_testdir } " )
81- assert type (path ) == UPath
80+ path = pathlib .Path (local_testdir )
81+ upath = UPath (local_testdir )
82+ # test instance check passes
83+ assert isinstance (upath , UPath )
84+ # test type is same as pathlib
85+ assert type (upath ) is type (path )
86+ upath = UPath (f"file://{ local_testdir } " )
87+ # test default implementation is used
88+ assert type (upath ) is UPath
8289
8390
8491def test_new_method (local_testdir ):
@@ -88,7 +95,8 @@ def test_new_method(local_testdir):
8895
8996
9097@pytest .mark .skipif (
91- sys .platform .startswith ("win" ), reason = "don't run test on Windows" ,
98+ sys .platform .startswith ("win" ),
99+ reason = "don't run test on Windows" ,
92100) # need to fix windows tests here
93101class TestFSSpecLocal (BaseTests ):
94102 @pytest .fixture (autouse = True )
@@ -97,23 +105,35 @@ def path(self, local_testdir):
97105 self .path = UPath (path )
98106
99107
100- PATHS = {
101- "/tmp/abc" : None ,
102- "s3://bucket/folder" : "s3fs" ,
103- "gs://bucket/folder" : "gcsfs" ,
104- }
108+ PATHS = (
109+ ("path" , "storage_options" , "module" , "object_type" ),
110+ (
111+ ("/tmp/abc" , (), None , pathlib .Path ),
112+ ("s3://bucket/folder" , ({"anon" : True }), "s3fs" , S3Path ),
113+ ("gs://bucket/folder" , ({"token" : "anon" }), "gcsfs" , UPath ),
114+ ),
115+ )
105116
106117
107- @pytest .mark .parametrize (("path" , "module" ), PATHS .items ())
108- def test_create_from_type (path , module ):
118+ @pytest .mark .parametrize (* PATHS )
119+ def test_create_from_type (path , storage_options , module , object_type ):
120+ """Test that derived paths use same fs instance."""
109121 if module :
122+ # skip if module cannot be imported
110123 pytest .importorskip (module )
111- parts = path .split ("/" )
112- parent = "/" .join (parts [:- 1 ])
113124 try :
114- upath = UPath (path )
125+ upath = UPath (path , storage_options = storage_options )
126+ # test expected object type
127+ assert isinstance (upath , object_type )
115128 cast = type (upath )
116- new = cast (parent )
129+ parent = upath .parent
130+ # test derived object is same type
131+ assert isinstance (parent , cast )
132+ # test that created fs uses fsspec instance cache
133+ assert not hasattr (upath , "fs" ) or upath .fs is parent .fs
134+ new = cast (str (parent ))
135+ # test that object cast is same type
117136 assert isinstance (new , cast )
118137 except (ImportError , ModuleNotFoundError ):
138+ # fs failed to import
119139 pass
0 commit comments