"Virtual" file system mock object - replacing stuff in __builtins__

Remy Blank remy.blank_asps at pobox.com
Tue Mar 9 18:26:14 CET 2004


John Roth wrote:
> "Remy Blank" <remy.blank_asps at pobox.com> wrote in message
> news:404844e3$1 at epflnews.epfl.ch...
>>Now my questions:
>>  - Am I doing something fundamentally forbidden (replacing stuff in
>>existing modules like __builtins__ or os)?
> 
> Yes. It's never a good idea to replace stuff on the system
> level, and some day it might become impossible.

That's too bad. When test-driving code in C++, you have to use an 
abstraction layer between your code and the file system accessors, to be 
able to replace it by a mock object for testing. I always thought it was 
an advantage of Python to be able to use the "native" calls and still 
replace them by stubs for testing purposes.

> Well, I don't know about "better" but what I've discovered
> is that file system scans are ideal usages of the Visitor pattern.
> I've got a single module that contains the guts of the file
> system scanner and a number of useful utility methods (such
> as loading a file into memory, writing it out and setting the
> stats).

The visitor pattern really is an elegant solution for scanning a file 
system. I think I'll give it a try.

If I understand you correctly, your scanner module is the abstraction 
layer I mentioned above, and you can replace it by stubs for test cases. 
Maybe that's the most reasonable way to go. But it would still be useful 
to have a generic scanner mock object that simulates a file system.

-- Remy

Remove underscore and anti-spam suffix in reply address for a timely 
response.



More information about the Python-list mailing list