michael@0: # This Source Code Form is subject to the terms of the Mozilla Public michael@0: # License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: # file, You can obtain one at http://mozilla.org/MPL/2.0/. michael@0: michael@0: import sys michael@0: import os michael@0: from mozunit import main, MockedOpen michael@0: import unittest michael@0: from tempfile import mkstemp michael@0: michael@0: class TestMozUnit(unittest.TestCase): michael@0: def test_mocked_open(self): michael@0: # Create a temporary file on the file system. michael@0: (fd, path) = mkstemp() michael@0: with os.fdopen(fd, 'w') as file: michael@0: file.write('foobar'); michael@0: michael@0: self.assertFalse(os.path.exists('file1')) michael@0: self.assertFalse(os.path.exists('file2')) michael@0: michael@0: with MockedOpen({'file1': 'content1', michael@0: 'file2': 'content2'}): michael@0: self.assertTrue(os.path.exists('file1')) michael@0: self.assertTrue(os.path.exists('file2')) michael@0: self.assertFalse(os.path.exists('foo/file1')) michael@0: michael@0: # Check the contents of the files given at MockedOpen creation. michael@0: self.assertEqual(open('file1', 'r').read(), 'content1') michael@0: self.assertEqual(open('file2', 'r').read(), 'content2') michael@0: michael@0: # Check that overwriting these files alters their content. michael@0: with open('file1', 'w') as file: michael@0: file.write('foo') michael@0: self.assertTrue(os.path.exists('file1')) michael@0: self.assertEqual(open('file1', 'r').read(), 'foo') michael@0: michael@0: # ... but not until the file is closed. michael@0: file = open('file2', 'w') michael@0: file.write('bar') michael@0: self.assertEqual(open('file2', 'r').read(), 'content2') michael@0: file.close() michael@0: self.assertEqual(open('file2', 'r').read(), 'bar') michael@0: michael@0: # Check that appending to a file does append michael@0: with open('file1', 'a') as file: michael@0: file.write('bar') michael@0: self.assertEqual(open('file1', 'r').read(), 'foobar') michael@0: michael@0: self.assertFalse(os.path.exists('file3')) michael@0: michael@0: # Opening a non-existing file ought to fail. michael@0: self.assertRaises(IOError, open, 'file3', 'r') michael@0: self.assertFalse(os.path.exists('file3')) michael@0: michael@0: # Check that writing a new file does create the file. michael@0: with open('file3', 'w') as file: michael@0: file.write('baz') michael@0: self.assertEqual(open('file3', 'r').read(), 'baz') michael@0: self.assertTrue(os.path.exists('file3')) michael@0: michael@0: # Check the content of the file created outside MockedOpen. michael@0: self.assertEqual(open(path, 'r').read(), 'foobar') michael@0: michael@0: # Check that overwriting a file existing on the file system michael@0: # does modify its content. michael@0: with open(path, 'w') as file: michael@0: file.write('bazqux') michael@0: self.assertEqual(open(path, 'r').read(), 'bazqux') michael@0: michael@0: with MockedOpen(): michael@0: # Check that appending to a file existing on the file system michael@0: # does modify its content. michael@0: with open(path, 'a') as file: michael@0: file.write('bazqux') michael@0: self.assertEqual(open(path, 'r').read(), 'foobarbazqux') michael@0: michael@0: # Check that the file was not actually modified on the file system. michael@0: self.assertEqual(open(path, 'r').read(), 'foobar') michael@0: os.remove(path) michael@0: michael@0: # Check that the file created inside MockedOpen wasn't actually michael@0: # created. michael@0: self.assertRaises(IOError, open, 'file3', 'r') michael@0: michael@0: if __name__ == "__main__": michael@0: main()