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

Remy Blank remy.blank_asps at pobox.com
Fri Mar 5 04:14:11 EST 2004


Hi everybody,

I find myself writing lots of small utilities these days that need to 
iterate over a file system tree and perform operations on every file, 
e.g. setting ID3 tags or generating playlists or <plug> archiving files 
(sync2cd: an incremental archiving tool to CD/DVD at 
http://www.calins.ch/software/sync2cd.html) </plug>.

As a big fan of Python, that's the language of choice for these tools. 
By the way, this is my first posting, and therefore also my first 
opportunity to thank all the people who help providing such a great 
language!

As a fan of TDD (test-driven development), I use the unittest module to 
write the test cases for these tools. However, to make the test cases 
independent of the file system environment, I simulate a file system 
tree with a mock object using the following technique (sorry for the 
long posting):

TestFiles = {
         "/lost": ((16832, 1227166L, 774L, 51, 500, 600, 4096L, 
1067342814, 1067336294, 1067336294), ),
         "/lost/file1.mp3": ((33188, 884738L, 774L, 1, 0, 0, 1463L, 
1054331223, 991209287, 1054333751), "File content"),
         "/lost/file2.mp3": ((33188, 884738L, 774L, 1, 0, 0, 1463L, 
1054331223, 991209287, 1054333751), "Other file content")
}

class VfsTestCase(unittest.TestCase):
         """Setup test case for 'virtual' filesystem."""
         def setUp(self):
                 self.OldOpen = __builtins__.open
                 self.OldStat = os.stat
                 __builtins__.open = self.open
                 os.stat = self.stat
		# Same technique for other calls

         def tearDown(self):
                 __builtins__.open = self.OldOpen
                 os.stat = self.OldStat

         def open(self, Path, Mode):
                 Path = os.path.abspath(Path)
                 try:
                         return StringIO.StringIO(TestFiles[Path][1])
                 except KeyError:
                         raise OSError, "[Errno 2] No such file or 
directory: '" + Path + "'"

         def stat(self, Path):
                 Path = os.path.abspath(Path)
                 try:
                         Data = TestFiles[Path]
                         Mode = Data[0][stat.ST_MODE]
                         RDev = 0
                         if stat.S_ISBLK(Mode) or stat.S_ISCHR(Mode):
                                 RDev = Data[1]
                         return os.stat_result(Data[0], {"st_rdev": RDev})

                 except KeyError:
                         raise OSError, "[Errno 2] No such file or 
directory: '" + Path + "'"


Now my questions:
  - Am I doing something fundamentally forbidden (replacing stuff in 
existing modules like __builtins__ or os)?
  - Is there a better way of achieving the same result?
  - If the technique is generally useful, would there be interest in 
having a full-featured "virtual" file system mock object? I couldn't 
find anything similar on Google.

Thanks.
-- Remy


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



More information about the Python-list mailing list