I'm using PyFilesystem. The code I am testing creates a default filesystem using open_fs(file_url)
. When I test this code I can now pass in mem://filepath/filename
to run the test using a memory filesystem.
However for testing I need to populate the memory filesystem with my test file. Is there a way for me to force a global singeton on filesystems? I would hope that when open_fs
is run in code that I don't control, it would open the same filesystem I previously created and seeded with my test file.
PyFilesystem author here. Short answer is no, in the case of memory filesystems, once you close the FS object, the memory is discarded. Filesystems will only persist if the underlying storage mechanism is persistent.
Slightly longer answer would be that you could use sub directories of a TempFS to create persistent filesystems.
# Create a temporary filesystem at module scope
temp_fs = open_fs('temp://')
# Acquire new persistent temporary directory
# (this is just a path you can pass to open_fs)
# If you open this multiple times, the data will persist
new_dir = temp_fs.makedir('foo').getsyspath()
# Close temporary filesystem, to delete all directories
temp_fs.close()
That should give you the semantics you describe.
If the project you are using is opening and closing filesystems using the FS url, and still expecting the data to be there, then it will break with filesystems that aren't backed by persistent storage. Maybe you can suggest to the authors that they keep the FS object for as long as it is needed, which is what I'd generally recommend.