~ubuntu-branches/ubuntu/precise/enigmail/precise-security

« back to all changes in this revision

Viewing changes to config/mozunit.py

  • Committer: Package Import Robot
  • Author(s): Chris Coulson
  • Date: 2012-11-12 16:36:01 UTC
  • mfrom: (0.12.15)
  • Revision ID: package-import@ubuntu.com-20121112163601-t8e8skdfi3ni9iqp
Tags: 2:1.4.6-0ubuntu0.12.04.1
* New upstream release v1.4.6
  - see LP: #1080212 for USN information
* Drop unneeded patches
  - remove debian/patches/correct-version-number.diff
  - remove debian/patches/dont_register_cids_multiple_times.diff
  - update debian/patches/series
* Support building in an objdir
  - update debian/rules

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
 
1
5
from unittest import TextTestRunner as _TestRunner, TestResult as _TestResult
 
6
import unittest
2
7
import inspect
 
8
from StringIO import StringIO
 
9
import os
3
10
 
4
11
'''Helper to make python unit tests report the way that the Mozilla
5
12
unit test infrastructure expects tests to report.
7
14
Usage:
8
15
 
9
16
import unittest
10
 
from mozunit import MozTestRunner
 
17
import mozunit
11
18
 
12
19
if __name__ == '__main__':
13
 
    unittest.main(testRunner=MozTestRunner())
 
20
    mozunit.main()
14
21
'''
15
22
 
16
23
class _MozTestResult(_TestResult):
64
71
        test(result)
65
72
        result.printErrorList()
66
73
        return result
 
74
 
 
75
class MockedFile(StringIO):
 
76
    def __init__(self, context, filename, content = ''):
 
77
        self.context = context
 
78
        self.name = filename
 
79
        StringIO.__init__(self, content)
 
80
 
 
81
    def close(self):
 
82
        self.context.files[self.name] = self.getvalue()
 
83
        StringIO.close(self)
 
84
 
 
85
    def __enter__(self):
 
86
        return self
 
87
 
 
88
    def __exit__(self, type, value, traceback):
 
89
        self.close()
 
90
 
 
91
class MockedOpen(object):
 
92
    '''
 
93
    Context manager diverting the open builtin such that opening files
 
94
    can open "virtual" file instances given when creating a MockedOpen.
 
95
 
 
96
    with MockedOpen({'foo': 'foo', 'bar': 'bar'}):
 
97
        f = open('foo', 'r')
 
98
 
 
99
    will thus open the virtual file instance for the file 'foo' to f.
 
100
 
 
101
    MockedOpen also masks writes, so that creating or replacing files
 
102
    doesn't touch the file system, while subsequently opening the file
 
103
    will return the recorded content.
 
104
 
 
105
    with MockedOpen():
 
106
        f = open('foo', 'w')
 
107
        f.write('foo')
 
108
    self.assertRaises(Exception,f.open('foo', 'r'))
 
109
    '''
 
110
    def __init__(self, files = {}):
 
111
        self.files = {}
 
112
        for name, content in files.iteritems():
 
113
            self.files[os.path.abspath(name)] = content
 
114
 
 
115
    def __call__(self, name, mode = 'r'):
 
116
        absname = os.path.abspath(name)
 
117
        if 'w' in mode:
 
118
            file = MockedFile(self, absname)
 
119
        elif absname in self.files:
 
120
            file = MockedFile(self, absname, self.files[absname])
 
121
        elif 'a' in mode:
 
122
            file = MockedFile(self, absname, self.open(name, 'r').read())
 
123
        else:
 
124
            file = self.open(name, mode)
 
125
        if 'a' in mode:
 
126
            file.seek(0, os.SEEK_END)
 
127
        return file
 
128
 
 
129
    def __enter__(self):
 
130
        import __builtin__
 
131
        self.open = __builtin__.open
 
132
        __builtin__.open = self
 
133
 
 
134
    def __exit__(self, type, value, traceback):
 
135
        import __builtin__
 
136
        __builtin__.open = self.open
 
137
 
 
138
def main(*args):
 
139
    unittest.main(testRunner=MozTestRunner(),*args)