[TIP] virtual filesystem

Michael Foord michael at voidspace.org.uk
Tue Dec 6 06:19:49 PST 2011


On 06/12/2011 14:07, exarkun at twistedmatrix.com wrote:
> On 10:20 am, andrea.crotti.0 at gmail.com wrote:
>> I have a lot of code which has to manipulate and handle the 
>> filesystem, creating
>> directories and so on.
>>
>> The testing now is far from complete, because even if I tried to use 
>> a functional
>> paradigm, still many things are too bound to the "real world".
>> And it's really time for me to learn how to use mock objects ;)
>>
>> Before I waste too much time trying (and failing a few times), any 
>> suggestions of how
>> it could be possible?
>>
>>
>> I thought for example something like:
>> class FileSystemMock(object):
>>     """Create a filesystem like object
>>     """
>>     def __init__(self, initial_state):
>>         self.state = dict(initial_state) if initial_state else {}
>>
>>     # what are the various things
>>
>>
>> Where I have an initial state of the directory structure, and every 
>> operation
>> will manage this dictionary of directories, raising the right errors 
>> when needed.
>>
>> The problem is that I should rewrite the whole "os." if I want to do 
>> something
>> general, or is there a better way?
>
> A while ago I started working on something like this.  Since `os` is 
> such a fundamental part of I/O in Python, I think it's necessary to 
> fake it.  From there you can proceed to something more general.
>
> However, it's true that `os` is a sprawling mess, and it proved to be 
> more effort than I cared to invest.  Hence my work is incomplete.  You 
> can see it here:
>
>   http://twistedmatrix.com:12435/2931
>
> in case you're curious.  If you wanted to pick it up, that'd be fine 
> with me. ;)

A full mock filesystem that can patch and unpatch os / os.path functions 
sounds like a great tool.

I wrote a mock "open" that implements the full file api and uses a 
configurable backend, for "Try Python" [1].

     
http://code.google.com/p/trypython/source/browse/trunk/trypython/app/storage.py

It uses functions in a "storage_backend" module to do that actual 
reading or writing. (In the case of Try Python it was using browser 
storage). You can write a custom backend by implementing four functions:

     
http://code.google.com/p/trypython/source/browse/trunk/trypython/app/storage_backend.py

Tests here:

     
http://code.google.com/p/trypython/source/browse/trunk/trypython/app/tests/test_storage.py

I always intended to, but never got round to writing os / os.path 
functions...

All the best,

Michael Foord



[1] http://trypython.org (uses Silverlight)


>
> Jean-Paul
>
> _______________________________________________
> testing-in-python mailing list
> testing-in-python at lists.idyll.org
> http://lists.idyll.org/listinfo/testing-in-python
>


-- 
http://www.voidspace.org.uk/

May you do good and not evil
May you find forgiveness for yourself and forgive others
May you share freely, never taking more than you give.
-- the sqlite blessing http://www.sqlite.org/different.html




More information about the testing-in-python mailing list