config/tests/unit-mozunit.py

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 # This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 # License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
michael@0 4
michael@0 5 import sys
michael@0 6 import os
michael@0 7 from mozunit import main, MockedOpen
michael@0 8 import unittest
michael@0 9 from tempfile import mkstemp
michael@0 10
michael@0 11 class TestMozUnit(unittest.TestCase):
michael@0 12 def test_mocked_open(self):
michael@0 13 # Create a temporary file on the file system.
michael@0 14 (fd, path) = mkstemp()
michael@0 15 with os.fdopen(fd, 'w') as file:
michael@0 16 file.write('foobar');
michael@0 17
michael@0 18 self.assertFalse(os.path.exists('file1'))
michael@0 19 self.assertFalse(os.path.exists('file2'))
michael@0 20
michael@0 21 with MockedOpen({'file1': 'content1',
michael@0 22 'file2': 'content2'}):
michael@0 23 self.assertTrue(os.path.exists('file1'))
michael@0 24 self.assertTrue(os.path.exists('file2'))
michael@0 25 self.assertFalse(os.path.exists('foo/file1'))
michael@0 26
michael@0 27 # Check the contents of the files given at MockedOpen creation.
michael@0 28 self.assertEqual(open('file1', 'r').read(), 'content1')
michael@0 29 self.assertEqual(open('file2', 'r').read(), 'content2')
michael@0 30
michael@0 31 # Check that overwriting these files alters their content.
michael@0 32 with open('file1', 'w') as file:
michael@0 33 file.write('foo')
michael@0 34 self.assertTrue(os.path.exists('file1'))
michael@0 35 self.assertEqual(open('file1', 'r').read(), 'foo')
michael@0 36
michael@0 37 # ... but not until the file is closed.
michael@0 38 file = open('file2', 'w')
michael@0 39 file.write('bar')
michael@0 40 self.assertEqual(open('file2', 'r').read(), 'content2')
michael@0 41 file.close()
michael@0 42 self.assertEqual(open('file2', 'r').read(), 'bar')
michael@0 43
michael@0 44 # Check that appending to a file does append
michael@0 45 with open('file1', 'a') as file:
michael@0 46 file.write('bar')
michael@0 47 self.assertEqual(open('file1', 'r').read(), 'foobar')
michael@0 48
michael@0 49 self.assertFalse(os.path.exists('file3'))
michael@0 50
michael@0 51 # Opening a non-existing file ought to fail.
michael@0 52 self.assertRaises(IOError, open, 'file3', 'r')
michael@0 53 self.assertFalse(os.path.exists('file3'))
michael@0 54
michael@0 55 # Check that writing a new file does create the file.
michael@0 56 with open('file3', 'w') as file:
michael@0 57 file.write('baz')
michael@0 58 self.assertEqual(open('file3', 'r').read(), 'baz')
michael@0 59 self.assertTrue(os.path.exists('file3'))
michael@0 60
michael@0 61 # Check the content of the file created outside MockedOpen.
michael@0 62 self.assertEqual(open(path, 'r').read(), 'foobar')
michael@0 63
michael@0 64 # Check that overwriting a file existing on the file system
michael@0 65 # does modify its content.
michael@0 66 with open(path, 'w') as file:
michael@0 67 file.write('bazqux')
michael@0 68 self.assertEqual(open(path, 'r').read(), 'bazqux')
michael@0 69
michael@0 70 with MockedOpen():
michael@0 71 # Check that appending to a file existing on the file system
michael@0 72 # does modify its content.
michael@0 73 with open(path, 'a') as file:
michael@0 74 file.write('bazqux')
michael@0 75 self.assertEqual(open(path, 'r').read(), 'foobarbazqux')
michael@0 76
michael@0 77 # Check that the file was not actually modified on the file system.
michael@0 78 self.assertEqual(open(path, 'r').read(), 'foobar')
michael@0 79 os.remove(path)
michael@0 80
michael@0 81 # Check that the file created inside MockedOpen wasn't actually
michael@0 82 # created.
michael@0 83 self.assertRaises(IOError, open, 'file3', 'r')
michael@0 84
michael@0 85 if __name__ == "__main__":
michael@0 86 main()

mercurial