1
1
import posixpath
2
2
from pathlib import Path
3
+ from unittest .mock import patch
3
4
4
5
import fsspec
5
6
import pytest
@@ -40,10 +41,6 @@ def info(self, path, *args, **kwargs):
40
41
out ["name" ] = out ["name" ][len (self .local_root_dir ) :]
41
42
return out
42
43
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
-
47
44
def cp_file (self , path1 , path2 , * args , ** kwargs ):
48
45
path1 = posixpath .join (self .local_root_dir , self ._strip_protocol (path1 ))
49
46
path2 = posixpath .join (self .local_root_dir , self ._strip_protocol (path2 ))
@@ -77,10 +74,27 @@ def _strip_protocol(cls, path):
77
74
return path
78
75
79
76
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
+
80
93
@pytest .fixture
81
94
def mock_fsspec ():
82
95
original_registry = fsspec .registry .copy ()
83
96
fsspec .register_implementation ("mock" , MockFileSystem )
97
+ fsspec .register_implementation ("tmp" , TmpDirFileSystem )
84
98
yield
85
99
fsspec .registry = original_registry
86
100
@@ -89,3 +103,10 @@ def mock_fsspec():
89
103
def mockfs (tmp_path_factory , mock_fsspec ):
90
104
local_fs_dir = tmp_path_factory .mktemp ("mockfs" )
91
105
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