~ubuntu-branches/ubuntu/utopic/mutagen/utopic-proposed

« back to all changes in this revision

Viewing changes to tests/__init__.py

  • Committer: Package Import Robot
  • Author(s): Daniel T Chen
  • Date: 2013-11-27 22:10:48 UTC
  • mfrom: (8.1.17 sid)
  • Revision ID: package-import@ubuntu.com-20131127221048-ae2f5j42ak2ox3kw
Tags: 1.22-1ubuntu1
* Merge from Debian unstable.  Remaining changes:
  - debian/control: Drop faad and oggz-tools build dependencies (in
    universe).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
from __future__ import division
2
2
 
 
3
import re
3
4
import glob
4
5
import os
5
6
import sys
6
7
import unittest
7
8
 
8
 
from unittest import TestCase
9
 
TestCase.uses_mmap = True
 
9
from unittest import TestCase as BaseTestCase
10
10
suites = []
11
11
add = suites.append
12
12
 
 
13
 
 
14
class TestCase(BaseTestCase):
 
15
 
 
16
    def failUnlessRaisesRegexp(self, exc, re_, fun, *args, **kwargs):
 
17
        def wrapped(*args, **kwargs):
 
18
            try:
 
19
                fun(*args, **kwargs)
 
20
            except Exception, e:
 
21
                self.failUnless(re.search(re_, str(e)))
 
22
                raise
 
23
        self.failUnlessRaises(exc, wrapped, *args, **kwargs)
 
24
 
 
25
    # silence deprec warnings about useless renames
 
26
    failUnless = BaseTestCase.assertTrue
 
27
    failIf = BaseTestCase.assertFalse
 
28
    failUnlessEqual = BaseTestCase.assertEqual
 
29
    failUnlessRaises = BaseTestCase.assertRaises
 
30
    failUnlessAlmostEqual = BaseTestCase.assertAlmostEqual
 
31
    failIfEqual = BaseTestCase.assertNotEqual
 
32
    failIfAlmostEqual = BaseTestCase.assertNotAlmostEqual
 
33
 
 
34
    def assertReallyEqual(self, a, b):
 
35
        self.assertEqual(a, b)
 
36
        self.assertEqual(b, a)
 
37
        self.assertTrue(a == b)
 
38
        self.assertTrue(b == a)
 
39
        self.assertFalse(a != b)
 
40
        self.assertFalse(b != a)
 
41
        self.assertEqual(0, cmp(a, b))
 
42
        self.assertEqual(0, cmp(b, a))
 
43
 
 
44
    def assertReallyNotEqual(self, a, b):
 
45
        self.assertNotEqual(a, b)
 
46
        self.assertNotEqual(b, a)
 
47
        self.assertFalse(a == b)
 
48
        self.assertFalse(b == a)
 
49
        self.assertTrue(a != b)
 
50
        self.assertTrue(b != a)
 
51
        self.assertNotEqual(0, cmp(a, b))
 
52
        self.assertNotEqual(0, cmp(b, a))
 
53
 
 
54
 
13
55
for name in glob.glob(os.path.join(os.path.dirname(__file__), "test_*.py")):
14
56
    module = "tests." + os.path.basename(name)
15
57
    __import__(module[:-3], {}, {}, [])
56
98
        result.printErrors()
57
99
        return bool(result.failures + result.errors)
58
100
 
59
 
def unit(run=[], filter_func=None):
 
101
 
 
102
def unit(run=[], quick=False):
 
103
    import mmap
 
104
 
60
105
    runner = Runner()
61
106
    failures = False
62
 
    use_suites = filter(filter_func, suites)
63
 
    for test in use_suites:
64
 
        if not run or test.__name__ in run:
 
107
    tests = [t for t in suites if not run or t.__name__ in run]
 
108
 
 
109
    # normal run, trace mmap calls
 
110
    orig_mmap = mmap.mmap
 
111
    uses_mmap = []
 
112
    print "Running tests with real mmap."
 
113
    for test in tests:
 
114
        def new_mmap(*args, **kwargs):
 
115
            if test not in uses_mmap:
 
116
                uses_mmap.append(test)
 
117
            return orig_mmap(*args, **kwargs)
 
118
        mmap.mmap = new_mmap
 
119
        failures |= runner.run(test)
 
120
    mmap.mmap = orig_mmap
 
121
 
 
122
    # make sure the above works
 
123
    if not run:
 
124
        assert len(uses_mmap) > 1
 
125
 
 
126
    if quick:
 
127
        return failures
 
128
 
 
129
    # run mmap using tests with mocked lockf
 
130
    try:
 
131
        import fcntl
 
132
    except ImportError:
 
133
        print "Unable to run mocked fcntl.lockf tests."
 
134
    else:
 
135
        def MockLockF(*args, **kwargs):
 
136
            raise IOError
 
137
        lockf = fcntl.lockf
 
138
        fcntl.lockf = MockLockF
 
139
        print "Running tests with mocked failing fcntl.lockf."
 
140
        for test in uses_mmap:
65
141
            failures |= runner.run(test)
 
142
        fcntl.lockf = lockf
 
143
 
 
144
    # failing mmap.move
 
145
    class MockMMap(object):
 
146
        def __init__(self, *args, **kwargs):
 
147
            pass
 
148
 
 
149
        def move(self, dest, src, count):
 
150
            raise ValueError
 
151
 
 
152
        def close(self):
 
153
            pass
 
154
 
 
155
    print "Running tests with mocked failing mmap.move."
 
156
    mmap.mmap = MockMMap
 
157
    for test in uses_mmap:
 
158
        failures |= runner.run(test)
 
159
 
 
160
    # failing mmap.mmap
 
161
    def MockMMap2(*args, **kwargs):
 
162
        raise EnvironmentError
 
163
 
 
164
    mmap.mmap = MockMMap2
 
165
    print "Running tests with mocked failing mmap.mmap."
 
166
    for test in uses_mmap:
 
167
        failures |= runner.run(test)
 
168
 
66
169
    return failures