11import posixpath
22from pathlib import Path
3+ from unittest .mock import patch
34
45import fsspec
56import pytest
@@ -40,10 +41,6 @@ def info(self, path, *args, **kwargs):
4041 out ["name" ] = out ["name" ][len (self .local_root_dir ) :]
4142 return out
4243
43- def get_file (self , rpath , lpath , * args , ** kwargs ):
44- rpath = posixpath .join (self .local_root_dir , self ._strip_protocol (rpath ))
45- return self ._fs .get_file (rpath , lpath , * args , ** kwargs )
46-
4744 def cp_file (self , path1 , path2 , * args , ** kwargs ):
4845 path1 = posixpath .join (self .local_root_dir , self ._strip_protocol (path1 ))
4946 path2 = posixpath .join (self .local_root_dir , self ._strip_protocol (path2 ))
@@ -72,15 +69,32 @@ def modified(self, path):
7269 @classmethod
7370 def _strip_protocol (cls , path ):
7471 path = stringify_path (path )
75- if path .startswith ("mock://" ):
72+ if path .startswith (f "mock://" ):
7673 path = path [7 :]
7774 return path
7875
7976
77+ class TmpDirFileSystem (MockFileSystem ):
78+ protocol = "tmp"
79+ tmp_dir = None
80+
81+ def __init__ (self , * args , ** kwargs ):
82+ assert self .tmp_dir is not None , "TmpDirFileSystem.tmp_dir is not set"
83+ super ().__init__ (* args , ** kwargs , local_root_dir = self .tmp_dir , auto_mkdir = True )
84+
85+ @classmethod
86+ def _strip_protocol (cls , path ):
87+ path = stringify_path (path )
88+ if path .startswith ("tmp://" ):
89+ path = path [6 :]
90+ return path
91+
92+
8093@pytest .fixture
8194def mock_fsspec ():
8295 original_registry = fsspec .registry .copy ()
8396 fsspec .register_implementation ("mock" , MockFileSystem )
97+ fsspec .register_implementation ("tmp" , TmpDirFileSystem )
8498 yield
8599 fsspec .registry = original_registry
86100
@@ -89,3 +103,10 @@ def mock_fsspec():
89103def mockfs (tmp_path_factory , mock_fsspec ):
90104 local_fs_dir = tmp_path_factory .mktemp ("mockfs" )
91105 return MockFileSystem (local_root_dir = local_fs_dir , auto_mkdir = True )
106+
107+
108+ @pytest .fixture
109+ def tmpfs (tmp_path_factory , mock_fsspec ):
110+ tmp_fs_dir = tmp_path_factory .mktemp ("tmpfs" )
111+ with patch .object (TmpDirFileSystem , "tmp_dir" , tmp_fs_dir ):
112+ yield TmpDirFileSystem ()
0 commit comments