~ubuntu-branches/ubuntu/quantal/enigmail/quantal-security

« back to all changes in this revision

Viewing changes to mozilla/config/tests/unit-mozunit.py

  • Committer: Package Import Robot
  • Author(s): Chris Coulson
  • Date: 2013-09-13 16:02:15 UTC
  • mfrom: (0.12.16)
  • Revision ID: package-import@ubuntu.com-20130913160215-u3g8nmwa0pdwagwc
Tags: 2:1.5.2-0ubuntu0.12.10.1
* New upstream release v1.5.2 for Thunderbird 24

* Build enigmail using a stripped down Thunderbird 17 build system, as it's
  now quite difficult to build the way we were doing previously, with the
  latest Firefox build system
* Add debian/patches/no_libxpcom.patch - Don't link against libxpcom, as it
  doesn't exist anymore (but exists in the build system)
* Add debian/patches/use_sdk.patch - Use the SDK version of xpt.py and
  friends
* Drop debian/patches/ipc-pipe_rename.diff (not needed anymore)
* Drop debian/patches/makefile_depth.diff (not needed anymore)

Show diffs side-by-side

added added

removed removed

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