@@ -130,6 +130,69 @@ def write_files_without_closing():
130
130
except OSError :
131
131
print ("remove OSError" )
132
132
133
+ # construct new VfsPosix with absolute path argument
134
+ temp_dir_abs = os .getcwd () + os .sep + temp_dir
135
+ vfs = os .VfsPosix (temp_dir_abs )
136
+ # when VfsPosix is used the intended way via os.mount(), it can only be called
137
+ # with relative paths when the CWD is inside or at its root, so simulate that
138
+ os .chdir (temp_dir_abs )
139
+ vfs .mkdir ("subdir" )
140
+ vfs .mkdir ("subdir/one" )
141
+ print ('listdir("/"):' , sorted (i [0 ] for i in vfs .ilistdir ("/" )))
142
+ print ('listdir("."):' , sorted (i [0 ] for i in vfs .ilistdir ("." )))
143
+ print ('getcwd() in {"", "/"}:' , vfs .getcwd () in {"" , "/" })
144
+ print ('chdir("subdir"):' , vfs .chdir ("subdir" ))
145
+ print ("getcwd():" , vfs .getcwd ())
146
+ print ('mkdir("two"):' , vfs .mkdir ("two" ))
147
+ f = vfs .open ("file.py" , "w" )
148
+ f .write ("print('hello')" )
149
+ f .close ()
150
+ print ('listdir("/"):' , sorted (i [0 ] for i in vfs .ilistdir ("/" )))
151
+ print ('listdir("/subdir"):' , sorted (i [0 ] for i in vfs .ilistdir ("/subdir" )))
152
+ print ('listdir("."):' , sorted (i [0 ] for i in vfs .ilistdir ("." )))
153
+ try :
154
+ f = vfs .open ("/subdir/file.py" , "r" )
155
+ print (f .read ())
156
+ f .close ()
157
+ except Exception as e :
158
+ print (e )
159
+ import sys
160
+
161
+ sys .path .insert (0 , "" )
162
+ try :
163
+ import file
164
+
165
+ print (file )
166
+ except Exception as e :
167
+ print (e )
168
+ del sys .path [0 ]
169
+ vfs .remove ("file.py" )
170
+ vfs .rmdir ("two" )
171
+ vfs .rmdir ("/subdir/one" )
172
+ vfs .rmdir ("/subdir" )
173
+
174
+ # done with vfs, restore CWD
175
+ os .chdir (curdir )
176
+
177
+ # some integration tests with a mounted VFS
178
+ os .mount (os .VfsPosix (temp_dir_abs ), "/mnt" )
179
+ os .mkdir ("/mnt/dir" )
180
+ print ('chdir("/mnt/dir"):' , os .chdir ("/mnt/dir" ))
181
+ print ("getcwd():" , os .getcwd ())
182
+ print ('chdir("/mnt"):' , os .chdir ("/mnt" ))
183
+ print ("getcwd():" , os .getcwd ())
184
+ print ('chdir("/"):' , os .chdir ("/" ))
185
+ print ("getcwd():" , os .getcwd ())
186
+ print ('chdir("/mnt/dir"):' , os .chdir ("/mnt/dir" ))
187
+ print ("getcwd():" , os .getcwd ())
188
+ print ('chdir(".."):' , os .chdir (".." ))
189
+ print ("getcwd():" , os .getcwd ())
190
+ os .rmdir ("/mnt/dir" )
191
+ os .umount ("/mnt" )
192
+
193
+ # restore CWD
194
+ os .chdir (curdir )
195
+
133
196
# rmdir
134
197
os .rmdir (temp_dir )
135
198
print (temp_dir in os .listdir ())
0 commit comments