Bug 937803 - os.path.exists should work with MockedOpen; r=glandium
authorGregory Szorc <gps@mozilla.com>
Tue, 12 Nov 2013 12:30:34 -0800
changeset 156404 e4e085ffb2ddb84b49c07c31e872ebef22d9828f
parent 156403 95fe878b5db8b276ecdb9b8106792800ba0615f5
child 156405 d51357993d255cf02cd91918ff5bd7e1eacef072
push id25678
push userryanvm@gmail.com
push dateWed, 20 Nov 2013 03:26:13 +0000
treeherdermozilla-central@4f993fa378eb [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersglandium
bugs937803
milestone28.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 937803 - os.path.exists should work with MockedOpen; r=glandium
config/mozunit.py
config/tests/unit-mozunit.py
--- a/config/mozunit.py
+++ b/config/mozunit.py
@@ -124,16 +124,29 @@ class MockedOpen(object):
             file = self.open(name, mode)
         if 'a' in mode:
             file.seek(0, os.SEEK_END)
         return file
 
     def __enter__(self):
         import __builtin__
         self.open = __builtin__.open
+        self._orig_path_exists = os.path.exists
         __builtin__.open = self
+        os.path.exists = self._wrapped_exists
 
     def __exit__(self, type, value, traceback):
         import __builtin__
         __builtin__.open = self.open
+        os.path.exists = self._orig_path_exists
+
+    def _wrapped_exists(self, p):
+        if p in self.files:
+            return True
+
+        abspath = os.path.abspath(p)
+        if abspath in self.files:
+            return True
+
+        return self._orig_path_exists(p)
 
 def main(*args):
     unittest.main(testRunner=MozTestRunner(),*args)
--- a/config/tests/unit-mozunit.py
+++ b/config/tests/unit-mozunit.py
@@ -10,46 +10,58 @@ from tempfile import mkstemp
 
 class TestMozUnit(unittest.TestCase):
     def test_mocked_open(self):
         # Create a temporary file on the file system.
         (fd, path) = mkstemp()
         with os.fdopen(fd, 'w') as file:
             file.write('foobar');
 
+        self.assertFalse(os.path.exists('file1'))
+        self.assertFalse(os.path.exists('file2'))
+
         with MockedOpen({'file1': 'content1',
                          'file2': 'content2'}):
+            self.assertTrue(os.path.exists('file1'))
+            self.assertTrue(os.path.exists('file2'))
+            self.assertFalse(os.path.exists('foo/file1'))
+
             # Check the contents of the files given at MockedOpen creation.
             self.assertEqual(open('file1', 'r').read(), 'content1')
             self.assertEqual(open('file2', 'r').read(), 'content2')
 
             # Check that overwriting these files alters their content.
             with open('file1', 'w') as file:
                 file.write('foo')
+            self.assertTrue(os.path.exists('file1'))
             self.assertEqual(open('file1', 'r').read(), 'foo')
 
             # ... but not until the file is closed.
             file = open('file2', 'w')
             file.write('bar')
             self.assertEqual(open('file2', 'r').read(), 'content2')
             file.close()
             self.assertEqual(open('file2', 'r').read(), 'bar')
 
             # Check that appending to a file does append
             with open('file1', 'a') as file:
                 file.write('bar')
             self.assertEqual(open('file1', 'r').read(), 'foobar')
 
+            self.assertFalse(os.path.exists('file3'))
+
             # Opening a non-existing file ought to fail.
             self.assertRaises(IOError, open, 'file3', 'r')
+            self.assertFalse(os.path.exists('file3'))
 
             # Check that writing a new file does create the file.
             with open('file3', 'w') as file:
                 file.write('baz')
             self.assertEqual(open('file3', 'r').read(), 'baz')
+            self.assertTrue(os.path.exists('file3'))
 
             # Check the content of the file created outside MockedOpen.
             self.assertEqual(open(path, 'r').read(), 'foobar')
 
             # Check that overwriting a file existing on the file system
             # does modify its content.
             with open(path, 'w') as file:
                 file.write('bazqux')