Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 154 lines (137 sloc) 4.864 kB
ec742a2 Change proprietary license to MIT License
Jyrki Pulliainen authored
1 # Copyright (c) 2010 Inoi Oy
8f2e627 Add copyright notices
Petri Lehtinen authored
2 #
ec742a2 Change proprietary license to MIT License
Jyrki Pulliainen authored
3 # Permission is hereby granted, free of charge, to any person
4 # obtaining a copy of this software and associated documentation
5 # files (the "Software"), to deal in the Software without
6 # restriction, including without limitation the rights to use, copy,
7 # modify, merge, publish, distribute, sublicense, and/or sell copies
8 # of the Software, and to permit persons to whom the Software is
9 # furnished to do so, subject to the following conditions:
8f2e627 Add copyright notices
Petri Lehtinen authored
10 #
ec742a2 Change proprietary license to MIT License
Jyrki Pulliainen authored
11 # The above copyright notice and this permission notice shall be
12 # included in all copies or substantial portions of the Software.
13 #
14 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
18 # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19 # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 # SOFTWARE.
8f2e627 Add copyright notices
Petri Lehtinen authored
22
388b2aa Support custom JSON encoder
Jyrki Pulliainen authored
23 import json
400610f Initial commit
Jyrki Pulliainen authored
24 import os
25 import sys
26 import errno
27 import shutil
424b288 Support Python 3
Petri Lehtinen authored
28 import types
400610f Initial commit
Jyrki Pulliainen authored
29 import nose.tools
30
388b2aa Support custom JSON encoder
Jyrki Pulliainen authored
31 from datetime import datetime
400610f Initial commit
Jyrki Pulliainen authored
32 from tornado.ioloop import IOLoop
33
34 def unrandom(random=None):
35 # Make os.urandom not so random. If user wants, a custom random
36 # function can be used providing keyword argument `random`
37 if random is None:
38 random = lambda x: '1' * x
39
40 def outer(func):
41 @nose.tools.make_decorator(func)
42 def wrap_urandom(*a, **kw):
43 _old_urandom = os.urandom
44 try:
45 os.urandom = random
46 func(*a, **kw)
47 finally:
48 os.urandom = _old_urandom
49 return wrap_urandom
50 return outer
51
b60bb66 Drop dependenices on internal software
Jyrki Pulliainen authored
52 def with_ioloop(func):
53 @nose.tools.make_decorator(func)
54 def wrapper(*args, **kwargs):
55 ioloop = IOLoop()
400610f Initial commit
Jyrki Pulliainen authored
56
b60bb66 Drop dependenices on internal software
Jyrki Pulliainen authored
57 # Override ioloop's _run_callback to let all exceptions through
58 def run_callback(self, callback):
59 callback()
424b288 Support Python 3
Petri Lehtinen authored
60 ioloop._run_callback = types.MethodType(run_callback, ioloop)
400610f Initial commit
Jyrki Pulliainen authored
61
b60bb66 Drop dependenices on internal software
Jyrki Pulliainen authored
62 return func(ioloop, *args, **kwargs)
400610f Initial commit
Jyrki Pulliainen authored
63
b60bb66 Drop dependenices on internal software
Jyrki Pulliainen authored
64 return wrapper
400610f Initial commit
Jyrki Pulliainen authored
65
66 def mkdir(*a, **kw):
67 try:
68 os.mkdir(*a, **kw)
424b288 Support Python 3
Petri Lehtinen authored
69 except OSError:
70 # Python 3
71 e = sys.exc_info()[1]
400610f Initial commit
Jyrki Pulliainen authored
72 if e.errno == errno.EEXIST:
73 pass
74 else:
75 raise
76
77 def find_test_name():
78 try:
79 from nose.case import Test
80 from nose.suite import ContextSuite
81 import types
82 def get_nose_name(its_self):
83 if isinstance(its_self, Test):
84 file_, module, class_ = its_self.address()
85 name = '%s:%s' % (module, class_)
86 return name
87 elif isinstance(its_self, ContextSuite):
88 if isinstance(its_self.context, types.ModuleType):
89 return its_self.context.__name__
90 except ImportError:
91 # older nose
92 from nose.case import FunctionTestCase, MethodTestCase
93 from nose.suite import TestModule
94 from nose.util import test_address
95 def get_nose_name(its_self):
96 if isinstance(its_self, (FunctionTestCase, MethodTestCase)):
97 file_, module, class_ = test_address(its_self)
98 name = '%s:%s' % (module, class_)
99 return name
100 elif isinstance(its_self, TestModule):
101 return its_self.moduleName
102
103 i = 0
104 while True:
105 i += 1
106 frame = sys._getframe(i)
107 # kludge, hunt callers upwards until we find our nose
108 if (frame.f_code.co_varnames
109 and frame.f_code.co_varnames[0] == 'self'):
110 its_self = frame.f_locals['self']
111 name = get_nose_name(its_self)
112 if name is not None:
113 return name
114
115 def maketemp():
116 tmp = os.path.join(os.path.dirname(__file__), 'tmp')
117 mkdir(tmp)
118
119 name = find_test_name()
120 tmp = os.path.join(tmp, name)
121 try:
122 shutil.rmtree(tmp)
424b288 Support Python 3
Petri Lehtinen authored
123 except OSError:
124 # Python 3
125 e = sys.exc_info()[1]
400610f Initial commit
Jyrki Pulliainen authored
126 if e.errno == errno.ENOENT:
127 pass
128 else:
129 raise
130 os.mkdir(tmp)
131 return tmp
132
133
134 def assert_raises(excClass, callableObj, *args, **kwargs):
135 """
136 Like unittest.TestCase.assertRaises, but returns the exception.
137 """
138 try:
139 callableObj(*args, **kwargs)
424b288 Support Python 3
Petri Lehtinen authored
140 except excClass:
141 # Python 3
142 return sys.exc_info()[1]
400610f Initial commit
Jyrki Pulliainen authored
143 except:
144 if hasattr(excClass,'__name__'): excName = excClass.__name__
145 else: excName = str(excClass)
146 raise AssertionError("%s not raised" % excName)
388b2aa Support custom JSON encoder
Jyrki Pulliainen authored
147
148 class DatetimeEncoder(json.JSONEncoder):
149 def default(self, o):
150 if isinstance(o, datetime):
151 return o.isoformat()
152 else:
153 return super(DatetimeEncoder, self).default(o)
Something went wrong with that request. Please try again.