config/tests/unit-mozunit.py

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/config/tests/unit-mozunit.py	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,86 @@
     1.4 +# This Source Code Form is subject to the terms of the Mozilla Public
     1.5 +# License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 +# file, You can obtain one at http://mozilla.org/MPL/2.0/.
     1.7 +
     1.8 +import sys
     1.9 +import os
    1.10 +from mozunit import main, MockedOpen
    1.11 +import unittest
    1.12 +from tempfile import mkstemp
    1.13 +
    1.14 +class TestMozUnit(unittest.TestCase):
    1.15 +    def test_mocked_open(self):
    1.16 +        # Create a temporary file on the file system.
    1.17 +        (fd, path) = mkstemp()
    1.18 +        with os.fdopen(fd, 'w') as file:
    1.19 +            file.write('foobar');
    1.20 +
    1.21 +        self.assertFalse(os.path.exists('file1'))
    1.22 +        self.assertFalse(os.path.exists('file2'))
    1.23 +
    1.24 +        with MockedOpen({'file1': 'content1',
    1.25 +                         'file2': 'content2'}):
    1.26 +            self.assertTrue(os.path.exists('file1'))
    1.27 +            self.assertTrue(os.path.exists('file2'))
    1.28 +            self.assertFalse(os.path.exists('foo/file1'))
    1.29 +
    1.30 +            # Check the contents of the files given at MockedOpen creation.
    1.31 +            self.assertEqual(open('file1', 'r').read(), 'content1')
    1.32 +            self.assertEqual(open('file2', 'r').read(), 'content2')
    1.33 +
    1.34 +            # Check that overwriting these files alters their content.
    1.35 +            with open('file1', 'w') as file:
    1.36 +                file.write('foo')
    1.37 +            self.assertTrue(os.path.exists('file1'))
    1.38 +            self.assertEqual(open('file1', 'r').read(), 'foo')
    1.39 +
    1.40 +            # ... but not until the file is closed.
    1.41 +            file = open('file2', 'w')
    1.42 +            file.write('bar')
    1.43 +            self.assertEqual(open('file2', 'r').read(), 'content2')
    1.44 +            file.close()
    1.45 +            self.assertEqual(open('file2', 'r').read(), 'bar')
    1.46 +
    1.47 +            # Check that appending to a file does append
    1.48 +            with open('file1', 'a') as file:
    1.49 +                file.write('bar')
    1.50 +            self.assertEqual(open('file1', 'r').read(), 'foobar')
    1.51 +
    1.52 +            self.assertFalse(os.path.exists('file3'))
    1.53 +
    1.54 +            # Opening a non-existing file ought to fail.
    1.55 +            self.assertRaises(IOError, open, 'file3', 'r')
    1.56 +            self.assertFalse(os.path.exists('file3'))
    1.57 +
    1.58 +            # Check that writing a new file does create the file.
    1.59 +            with open('file3', 'w') as file:
    1.60 +                file.write('baz')
    1.61 +            self.assertEqual(open('file3', 'r').read(), 'baz')
    1.62 +            self.assertTrue(os.path.exists('file3'))
    1.63 +
    1.64 +            # Check the content of the file created outside MockedOpen.
    1.65 +            self.assertEqual(open(path, 'r').read(), 'foobar')
    1.66 +
    1.67 +            # Check that overwriting a file existing on the file system
    1.68 +            # does modify its content.
    1.69 +            with open(path, 'w') as file:
    1.70 +                file.write('bazqux')
    1.71 +            self.assertEqual(open(path, 'r').read(), 'bazqux')
    1.72 +
    1.73 +        with MockedOpen():
    1.74 +            # Check that appending to a file existing on the file system
    1.75 +            # does modify its content.
    1.76 +            with open(path, 'a') as file:
    1.77 +                file.write('bazqux')
    1.78 +            self.assertEqual(open(path, 'r').read(), 'foobarbazqux')
    1.79 +
    1.80 +        # Check that the file was not actually modified on the file system.
    1.81 +        self.assertEqual(open(path, 'r').read(), 'foobar')
    1.82 +        os.remove(path)
    1.83 +
    1.84 +        # Check that the file created inside MockedOpen wasn't actually
    1.85 +        # created.
    1.86 +        self.assertRaises(IOError, open, 'file3', 'r')
    1.87 +
    1.88 +if __name__ == "__main__":
    1.89 +    main()

mercurial