##// END OF EJS Templates
Unset PYTHONWARNINGS envvar before running subprocess tests....
Thomas Kluyver -
Show More
@@ -1,456 +1,458 b''
1 """Generic testing tools.
1 """Generic testing tools.
2
2
3 Authors
3 Authors
4 -------
4 -------
5 - Fernando Perez <Fernando.Perez@berkeley.edu>
5 - Fernando Perez <Fernando.Perez@berkeley.edu>
6 """
6 """
7
7
8 from __future__ import absolute_import
8 from __future__ import absolute_import
9
9
10 #-----------------------------------------------------------------------------
10 #-----------------------------------------------------------------------------
11 # Copyright (C) 2009 The IPython Development Team
11 # Copyright (C) 2009 The IPython Development Team
12 #
12 #
13 # Distributed under the terms of the BSD License. The full license is in
13 # Distributed under the terms of the BSD License. The full license is in
14 # the file COPYING, distributed as part of this software.
14 # the file COPYING, distributed as part of this software.
15 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
16
16
17 #-----------------------------------------------------------------------------
17 #-----------------------------------------------------------------------------
18 # Imports
18 # Imports
19 #-----------------------------------------------------------------------------
19 #-----------------------------------------------------------------------------
20
20
21 import os
21 import os
22 import re
22 import re
23 import sys
23 import sys
24 import tempfile
24 import tempfile
25
25
26 from contextlib import contextmanager
26 from contextlib import contextmanager
27 from io import StringIO
27 from io import StringIO
28 from subprocess import Popen, PIPE
28 from subprocess import Popen, PIPE
29
29
30 try:
30 try:
31 # These tools are used by parts of the runtime, so we make the nose
31 # These tools are used by parts of the runtime, so we make the nose
32 # dependency optional at this point. Nose is a hard dependency to run the
32 # dependency optional at this point. Nose is a hard dependency to run the
33 # test suite, but NOT to use ipython itself.
33 # test suite, but NOT to use ipython itself.
34 import nose.tools as nt
34 import nose.tools as nt
35 has_nose = True
35 has_nose = True
36 except ImportError:
36 except ImportError:
37 has_nose = False
37 has_nose = False
38
38
39 from IPython.config.loader import Config
39 from IPython.config.loader import Config
40 from IPython.utils.process import get_output_error_code
40 from IPython.utils.process import get_output_error_code
41 from IPython.utils.text import list_strings
41 from IPython.utils.text import list_strings
42 from IPython.utils.io import temp_pyfile, Tee
42 from IPython.utils.io import temp_pyfile, Tee
43 from IPython.utils import py3compat
43 from IPython.utils import py3compat
44 from IPython.utils.encoding import DEFAULT_ENCODING
44 from IPython.utils.encoding import DEFAULT_ENCODING
45
45
46 from . import decorators as dec
46 from . import decorators as dec
47 from . import skipdoctest
47 from . import skipdoctest
48
48
49 #-----------------------------------------------------------------------------
49 #-----------------------------------------------------------------------------
50 # Functions and classes
50 # Functions and classes
51 #-----------------------------------------------------------------------------
51 #-----------------------------------------------------------------------------
52
52
53 # The docstring for full_path doctests differently on win32 (different path
53 # The docstring for full_path doctests differently on win32 (different path
54 # separator) so just skip the doctest there. The example remains informative.
54 # separator) so just skip the doctest there. The example remains informative.
55 doctest_deco = skipdoctest.skip_doctest if sys.platform == 'win32' else dec.null_deco
55 doctest_deco = skipdoctest.skip_doctest if sys.platform == 'win32' else dec.null_deco
56
56
57 @doctest_deco
57 @doctest_deco
58 def full_path(startPath,files):
58 def full_path(startPath,files):
59 """Make full paths for all the listed files, based on startPath.
59 """Make full paths for all the listed files, based on startPath.
60
60
61 Only the base part of startPath is kept, since this routine is typically
61 Only the base part of startPath is kept, since this routine is typically
62 used with a script's ``__file__`` variable as startPath. The base of startPath
62 used with a script's ``__file__`` variable as startPath. The base of startPath
63 is then prepended to all the listed files, forming the output list.
63 is then prepended to all the listed files, forming the output list.
64
64
65 Parameters
65 Parameters
66 ----------
66 ----------
67 startPath : string
67 startPath : string
68 Initial path to use as the base for the results. This path is split
68 Initial path to use as the base for the results. This path is split
69 using os.path.split() and only its first component is kept.
69 using os.path.split() and only its first component is kept.
70
70
71 files : string or list
71 files : string or list
72 One or more files.
72 One or more files.
73
73
74 Examples
74 Examples
75 --------
75 --------
76
76
77 >>> full_path('/foo/bar.py',['a.txt','b.txt'])
77 >>> full_path('/foo/bar.py',['a.txt','b.txt'])
78 ['/foo/a.txt', '/foo/b.txt']
78 ['/foo/a.txt', '/foo/b.txt']
79
79
80 >>> full_path('/foo',['a.txt','b.txt'])
80 >>> full_path('/foo',['a.txt','b.txt'])
81 ['/a.txt', '/b.txt']
81 ['/a.txt', '/b.txt']
82
82
83 If a single file is given, the output is still a list::
83 If a single file is given, the output is still a list::
84
84
85 >>> full_path('/foo','a.txt')
85 >>> full_path('/foo','a.txt')
86 ['/a.txt']
86 ['/a.txt']
87 """
87 """
88
88
89 files = list_strings(files)
89 files = list_strings(files)
90 base = os.path.split(startPath)[0]
90 base = os.path.split(startPath)[0]
91 return [ os.path.join(base,f) for f in files ]
91 return [ os.path.join(base,f) for f in files ]
92
92
93
93
94 def parse_test_output(txt):
94 def parse_test_output(txt):
95 """Parse the output of a test run and return errors, failures.
95 """Parse the output of a test run and return errors, failures.
96
96
97 Parameters
97 Parameters
98 ----------
98 ----------
99 txt : str
99 txt : str
100 Text output of a test run, assumed to contain a line of one of the
100 Text output of a test run, assumed to contain a line of one of the
101 following forms::
101 following forms::
102
102
103 'FAILED (errors=1)'
103 'FAILED (errors=1)'
104 'FAILED (failures=1)'
104 'FAILED (failures=1)'
105 'FAILED (errors=1, failures=1)'
105 'FAILED (errors=1, failures=1)'
106
106
107 Returns
107 Returns
108 -------
108 -------
109 nerr, nfail
109 nerr, nfail
110 number of errors and failures.
110 number of errors and failures.
111 """
111 """
112
112
113 err_m = re.search(r'^FAILED \(errors=(\d+)\)', txt, re.MULTILINE)
113 err_m = re.search(r'^FAILED \(errors=(\d+)\)', txt, re.MULTILINE)
114 if err_m:
114 if err_m:
115 nerr = int(err_m.group(1))
115 nerr = int(err_m.group(1))
116 nfail = 0
116 nfail = 0
117 return nerr, nfail
117 return nerr, nfail
118
118
119 fail_m = re.search(r'^FAILED \(failures=(\d+)\)', txt, re.MULTILINE)
119 fail_m = re.search(r'^FAILED \(failures=(\d+)\)', txt, re.MULTILINE)
120 if fail_m:
120 if fail_m:
121 nerr = 0
121 nerr = 0
122 nfail = int(fail_m.group(1))
122 nfail = int(fail_m.group(1))
123 return nerr, nfail
123 return nerr, nfail
124
124
125 both_m = re.search(r'^FAILED \(errors=(\d+), failures=(\d+)\)', txt,
125 both_m = re.search(r'^FAILED \(errors=(\d+), failures=(\d+)\)', txt,
126 re.MULTILINE)
126 re.MULTILINE)
127 if both_m:
127 if both_m:
128 nerr = int(both_m.group(1))
128 nerr = int(both_m.group(1))
129 nfail = int(both_m.group(2))
129 nfail = int(both_m.group(2))
130 return nerr, nfail
130 return nerr, nfail
131
131
132 # If the input didn't match any of these forms, assume no error/failures
132 # If the input didn't match any of these forms, assume no error/failures
133 return 0, 0
133 return 0, 0
134
134
135
135
136 # So nose doesn't think this is a test
136 # So nose doesn't think this is a test
137 parse_test_output.__test__ = False
137 parse_test_output.__test__ = False
138
138
139
139
140 def default_argv():
140 def default_argv():
141 """Return a valid default argv for creating testing instances of ipython"""
141 """Return a valid default argv for creating testing instances of ipython"""
142
142
143 return ['--quick', # so no config file is loaded
143 return ['--quick', # so no config file is loaded
144 # Other defaults to minimize side effects on stdout
144 # Other defaults to minimize side effects on stdout
145 '--colors=NoColor', '--no-term-title','--no-banner',
145 '--colors=NoColor', '--no-term-title','--no-banner',
146 '--autocall=0']
146 '--autocall=0']
147
147
148
148
149 def default_config():
149 def default_config():
150 """Return a config object with good defaults for testing."""
150 """Return a config object with good defaults for testing."""
151 config = Config()
151 config = Config()
152 config.TerminalInteractiveShell.colors = 'NoColor'
152 config.TerminalInteractiveShell.colors = 'NoColor'
153 config.TerminalTerminalInteractiveShell.term_title = False,
153 config.TerminalTerminalInteractiveShell.term_title = False,
154 config.TerminalInteractiveShell.autocall = 0
154 config.TerminalInteractiveShell.autocall = 0
155 f = tempfile.NamedTemporaryFile(suffix=u'test_hist.sqlite', delete=False)
155 f = tempfile.NamedTemporaryFile(suffix=u'test_hist.sqlite', delete=False)
156 config.HistoryManager.hist_file = f.name
156 config.HistoryManager.hist_file = f.name
157 f.close()
157 f.close()
158 config.HistoryManager.db_cache_size = 10000
158 config.HistoryManager.db_cache_size = 10000
159 return config
159 return config
160
160
161
161
162 def get_ipython_cmd(as_string=False):
162 def get_ipython_cmd(as_string=False):
163 """
163 """
164 Return appropriate IPython command line name. By default, this will return
164 Return appropriate IPython command line name. By default, this will return
165 a list that can be used with subprocess.Popen, for example, but passing
165 a list that can be used with subprocess.Popen, for example, but passing
166 `as_string=True` allows for returning the IPython command as a string.
166 `as_string=True` allows for returning the IPython command as a string.
167
167
168 Parameters
168 Parameters
169 ----------
169 ----------
170 as_string: bool
170 as_string: bool
171 Flag to allow to return the command as a string.
171 Flag to allow to return the command as a string.
172 """
172 """
173 ipython_cmd = [sys.executable, "-m", "IPython"]
173 ipython_cmd = [sys.executable, "-m", "IPython"]
174
174
175 if as_string:
175 if as_string:
176 ipython_cmd = " ".join(ipython_cmd)
176 ipython_cmd = " ".join(ipython_cmd)
177
177
178 return ipython_cmd
178 return ipython_cmd
179
179
180 def ipexec(fname, options=None):
180 def ipexec(fname, options=None):
181 """Utility to call 'ipython filename'.
181 """Utility to call 'ipython filename'.
182
182
183 Starts IPython with a minimal and safe configuration to make startup as fast
183 Starts IPython with a minimal and safe configuration to make startup as fast
184 as possible.
184 as possible.
185
185
186 Note that this starts IPython in a subprocess!
186 Note that this starts IPython in a subprocess!
187
187
188 Parameters
188 Parameters
189 ----------
189 ----------
190 fname : str
190 fname : str
191 Name of file to be executed (should have .py or .ipy extension).
191 Name of file to be executed (should have .py or .ipy extension).
192
192
193 options : optional, list
193 options : optional, list
194 Extra command-line flags to be passed to IPython.
194 Extra command-line flags to be passed to IPython.
195
195
196 Returns
196 Returns
197 -------
197 -------
198 (stdout, stderr) of ipython subprocess.
198 (stdout, stderr) of ipython subprocess.
199 """
199 """
200 if options is None: options = []
200 if options is None: options = []
201
201
202 # For these subprocess calls, eliminate all prompt printing so we only see
202 # For these subprocess calls, eliminate all prompt printing so we only see
203 # output from script execution
203 # output from script execution
204 prompt_opts = [ '--PromptManager.in_template=""',
204 prompt_opts = [ '--PromptManager.in_template=""',
205 '--PromptManager.in2_template=""',
205 '--PromptManager.in2_template=""',
206 '--PromptManager.out_template=""'
206 '--PromptManager.out_template=""'
207 ]
207 ]
208 cmdargs = default_argv() + prompt_opts + options
208 cmdargs = default_argv() + prompt_opts + options
209
209
210 test_dir = os.path.dirname(__file__)
210 test_dir = os.path.dirname(__file__)
211
211
212 ipython_cmd = get_ipython_cmd()
212 ipython_cmd = get_ipython_cmd()
213 # Absolute path for filename
213 # Absolute path for filename
214 full_fname = os.path.join(test_dir, fname)
214 full_fname = os.path.join(test_dir, fname)
215 full_cmd = ipython_cmd + cmdargs + [full_fname]
215 full_cmd = ipython_cmd + cmdargs + [full_fname]
216 p = Popen(full_cmd, stdout=PIPE, stderr=PIPE)
216 env = os.environ.copy()
217 env.pop('PYTHONWARNINGS') # Avoid extraneous warnings appearing on stderr
218 p = Popen(full_cmd, stdout=PIPE, stderr=PIPE, env=env)
217 out, err = p.communicate()
219 out, err = p.communicate()
218 out, err = py3compat.bytes_to_str(out), py3compat.bytes_to_str(err)
220 out, err = py3compat.bytes_to_str(out), py3compat.bytes_to_str(err)
219 # `import readline` causes 'ESC[?1034h' to be output sometimes,
221 # `import readline` causes 'ESC[?1034h' to be output sometimes,
220 # so strip that out before doing comparisons
222 # so strip that out before doing comparisons
221 if out:
223 if out:
222 out = re.sub(r'\x1b\[[^h]+h', '', out)
224 out = re.sub(r'\x1b\[[^h]+h', '', out)
223 return out, err
225 return out, err
224
226
225
227
226 def ipexec_validate(fname, expected_out, expected_err='',
228 def ipexec_validate(fname, expected_out, expected_err='',
227 options=None):
229 options=None):
228 """Utility to call 'ipython filename' and validate output/error.
230 """Utility to call 'ipython filename' and validate output/error.
229
231
230 This function raises an AssertionError if the validation fails.
232 This function raises an AssertionError if the validation fails.
231
233
232 Note that this starts IPython in a subprocess!
234 Note that this starts IPython in a subprocess!
233
235
234 Parameters
236 Parameters
235 ----------
237 ----------
236 fname : str
238 fname : str
237 Name of the file to be executed (should have .py or .ipy extension).
239 Name of the file to be executed (should have .py or .ipy extension).
238
240
239 expected_out : str
241 expected_out : str
240 Expected stdout of the process.
242 Expected stdout of the process.
241
243
242 expected_err : optional, str
244 expected_err : optional, str
243 Expected stderr of the process.
245 Expected stderr of the process.
244
246
245 options : optional, list
247 options : optional, list
246 Extra command-line flags to be passed to IPython.
248 Extra command-line flags to be passed to IPython.
247
249
248 Returns
250 Returns
249 -------
251 -------
250 None
252 None
251 """
253 """
252
254
253 import nose.tools as nt
255 import nose.tools as nt
254
256
255 out, err = ipexec(fname, options)
257 out, err = ipexec(fname, options)
256 #print 'OUT', out # dbg
258 #print 'OUT', out # dbg
257 #print 'ERR', err # dbg
259 #print 'ERR', err # dbg
258 # If there are any errors, we must check those befor stdout, as they may be
260 # If there are any errors, we must check those befor stdout, as they may be
259 # more informative than simply having an empty stdout.
261 # more informative than simply having an empty stdout.
260 if err:
262 if err:
261 if expected_err:
263 if expected_err:
262 nt.assert_equal("\n".join(err.strip().splitlines()), "\n".join(expected_err.strip().splitlines()))
264 nt.assert_equal("\n".join(err.strip().splitlines()), "\n".join(expected_err.strip().splitlines()))
263 else:
265 else:
264 raise ValueError('Running file %r produced error: %r' %
266 raise ValueError('Running file %r produced error: %r' %
265 (fname, err))
267 (fname, err))
266 # If no errors or output on stderr was expected, match stdout
268 # If no errors or output on stderr was expected, match stdout
267 nt.assert_equal("\n".join(out.strip().splitlines()), "\n".join(expected_out.strip().splitlines()))
269 nt.assert_equal("\n".join(out.strip().splitlines()), "\n".join(expected_out.strip().splitlines()))
268
270
269
271
270 class TempFileMixin(object):
272 class TempFileMixin(object):
271 """Utility class to create temporary Python/IPython files.
273 """Utility class to create temporary Python/IPython files.
272
274
273 Meant as a mixin class for test cases."""
275 Meant as a mixin class for test cases."""
274
276
275 def mktmp(self, src, ext='.py'):
277 def mktmp(self, src, ext='.py'):
276 """Make a valid python temp file."""
278 """Make a valid python temp file."""
277 fname, f = temp_pyfile(src, ext)
279 fname, f = temp_pyfile(src, ext)
278 self.tmpfile = f
280 self.tmpfile = f
279 self.fname = fname
281 self.fname = fname
280
282
281 def tearDown(self):
283 def tearDown(self):
282 if hasattr(self, 'tmpfile'):
284 if hasattr(self, 'tmpfile'):
283 # If the tmpfile wasn't made because of skipped tests, like in
285 # If the tmpfile wasn't made because of skipped tests, like in
284 # win32, there's nothing to cleanup.
286 # win32, there's nothing to cleanup.
285 self.tmpfile.close()
287 self.tmpfile.close()
286 try:
288 try:
287 os.unlink(self.fname)
289 os.unlink(self.fname)
288 except:
290 except:
289 # On Windows, even though we close the file, we still can't
291 # On Windows, even though we close the file, we still can't
290 # delete it. I have no clue why
292 # delete it. I have no clue why
291 pass
293 pass
292
294
293 pair_fail_msg = ("Testing {0}\n\n"
295 pair_fail_msg = ("Testing {0}\n\n"
294 "In:\n"
296 "In:\n"
295 " {1!r}\n"
297 " {1!r}\n"
296 "Expected:\n"
298 "Expected:\n"
297 " {2!r}\n"
299 " {2!r}\n"
298 "Got:\n"
300 "Got:\n"
299 " {3!r}\n")
301 " {3!r}\n")
300 def check_pairs(func, pairs):
302 def check_pairs(func, pairs):
301 """Utility function for the common case of checking a function with a
303 """Utility function for the common case of checking a function with a
302 sequence of input/output pairs.
304 sequence of input/output pairs.
303
305
304 Parameters
306 Parameters
305 ----------
307 ----------
306 func : callable
308 func : callable
307 The function to be tested. Should accept a single argument.
309 The function to be tested. Should accept a single argument.
308 pairs : iterable
310 pairs : iterable
309 A list of (input, expected_output) tuples.
311 A list of (input, expected_output) tuples.
310
312
311 Returns
313 Returns
312 -------
314 -------
313 None. Raises an AssertionError if any output does not match the expected
315 None. Raises an AssertionError if any output does not match the expected
314 value.
316 value.
315 """
317 """
316 name = getattr(func, "func_name", getattr(func, "__name__", "<unknown>"))
318 name = getattr(func, "func_name", getattr(func, "__name__", "<unknown>"))
317 for inp, expected in pairs:
319 for inp, expected in pairs:
318 out = func(inp)
320 out = func(inp)
319 assert out == expected, pair_fail_msg.format(name, inp, expected, out)
321 assert out == expected, pair_fail_msg.format(name, inp, expected, out)
320
322
321
323
322 if py3compat.PY3:
324 if py3compat.PY3:
323 MyStringIO = StringIO
325 MyStringIO = StringIO
324 else:
326 else:
325 # In Python 2, stdout/stderr can have either bytes or unicode written to them,
327 # In Python 2, stdout/stderr can have either bytes or unicode written to them,
326 # so we need a class that can handle both.
328 # so we need a class that can handle both.
327 class MyStringIO(StringIO):
329 class MyStringIO(StringIO):
328 def write(self, s):
330 def write(self, s):
329 s = py3compat.cast_unicode(s, encoding=DEFAULT_ENCODING)
331 s = py3compat.cast_unicode(s, encoding=DEFAULT_ENCODING)
330 super(MyStringIO, self).write(s)
332 super(MyStringIO, self).write(s)
331
333
332 _re_type = type(re.compile(r''))
334 _re_type = type(re.compile(r''))
333
335
334 notprinted_msg = """Did not find {0!r} in printed output (on {1}):
336 notprinted_msg = """Did not find {0!r} in printed output (on {1}):
335 -------
337 -------
336 {2!s}
338 {2!s}
337 -------
339 -------
338 """
340 """
339
341
340 class AssertPrints(object):
342 class AssertPrints(object):
341 """Context manager for testing that code prints certain text.
343 """Context manager for testing that code prints certain text.
342
344
343 Examples
345 Examples
344 --------
346 --------
345 >>> with AssertPrints("abc", suppress=False):
347 >>> with AssertPrints("abc", suppress=False):
346 ... print("abcd")
348 ... print("abcd")
347 ... print("def")
349 ... print("def")
348 ...
350 ...
349 abcd
351 abcd
350 def
352 def
351 """
353 """
352 def __init__(self, s, channel='stdout', suppress=True):
354 def __init__(self, s, channel='stdout', suppress=True):
353 self.s = s
355 self.s = s
354 if isinstance(self.s, (py3compat.string_types, _re_type)):
356 if isinstance(self.s, (py3compat.string_types, _re_type)):
355 self.s = [self.s]
357 self.s = [self.s]
356 self.channel = channel
358 self.channel = channel
357 self.suppress = suppress
359 self.suppress = suppress
358
360
359 def __enter__(self):
361 def __enter__(self):
360 self.orig_stream = getattr(sys, self.channel)
362 self.orig_stream = getattr(sys, self.channel)
361 self.buffer = MyStringIO()
363 self.buffer = MyStringIO()
362 self.tee = Tee(self.buffer, channel=self.channel)
364 self.tee = Tee(self.buffer, channel=self.channel)
363 setattr(sys, self.channel, self.buffer if self.suppress else self.tee)
365 setattr(sys, self.channel, self.buffer if self.suppress else self.tee)
364
366
365 def __exit__(self, etype, value, traceback):
367 def __exit__(self, etype, value, traceback):
366 if value is not None:
368 if value is not None:
367 # If an error was raised, don't check anything else
369 # If an error was raised, don't check anything else
368 return False
370 return False
369 self.tee.flush()
371 self.tee.flush()
370 setattr(sys, self.channel, self.orig_stream)
372 setattr(sys, self.channel, self.orig_stream)
371 printed = self.buffer.getvalue()
373 printed = self.buffer.getvalue()
372 for s in self.s:
374 for s in self.s:
373 if isinstance(s, _re_type):
375 if isinstance(s, _re_type):
374 assert s.search(printed), notprinted_msg.format(s.pattern, self.channel, printed)
376 assert s.search(printed), notprinted_msg.format(s.pattern, self.channel, printed)
375 else:
377 else:
376 assert s in printed, notprinted_msg.format(s, self.channel, printed)
378 assert s in printed, notprinted_msg.format(s, self.channel, printed)
377 return False
379 return False
378
380
379 printed_msg = """Found {0!r} in printed output (on {1}):
381 printed_msg = """Found {0!r} in printed output (on {1}):
380 -------
382 -------
381 {2!s}
383 {2!s}
382 -------
384 -------
383 """
385 """
384
386
385 class AssertNotPrints(AssertPrints):
387 class AssertNotPrints(AssertPrints):
386 """Context manager for checking that certain output *isn't* produced.
388 """Context manager for checking that certain output *isn't* produced.
387
389
388 Counterpart of AssertPrints"""
390 Counterpart of AssertPrints"""
389 def __exit__(self, etype, value, traceback):
391 def __exit__(self, etype, value, traceback):
390 if value is not None:
392 if value is not None:
391 # If an error was raised, don't check anything else
393 # If an error was raised, don't check anything else
392 return False
394 return False
393 self.tee.flush()
395 self.tee.flush()
394 setattr(sys, self.channel, self.orig_stream)
396 setattr(sys, self.channel, self.orig_stream)
395 printed = self.buffer.getvalue()
397 printed = self.buffer.getvalue()
396 for s in self.s:
398 for s in self.s:
397 if isinstance(s, _re_type):
399 if isinstance(s, _re_type):
398 assert not s.search(printed), printed_msg.format(s.pattern, self.channel, printed)
400 assert not s.search(printed), printed_msg.format(s.pattern, self.channel, printed)
399 else:
401 else:
400 assert s not in printed, printed_msg.format(s, self.channel, printed)
402 assert s not in printed, printed_msg.format(s, self.channel, printed)
401 return False
403 return False
402
404
403 @contextmanager
405 @contextmanager
404 def mute_warn():
406 def mute_warn():
405 from IPython.utils import warn
407 from IPython.utils import warn
406 save_warn = warn.warn
408 save_warn = warn.warn
407 warn.warn = lambda *a, **kw: None
409 warn.warn = lambda *a, **kw: None
408 try:
410 try:
409 yield
411 yield
410 finally:
412 finally:
411 warn.warn = save_warn
413 warn.warn = save_warn
412
414
413 @contextmanager
415 @contextmanager
414 def make_tempfile(name):
416 def make_tempfile(name):
415 """ Create an empty, named, temporary file for the duration of the context.
417 """ Create an empty, named, temporary file for the duration of the context.
416 """
418 """
417 f = open(name, 'w')
419 f = open(name, 'w')
418 f.close()
420 f.close()
419 try:
421 try:
420 yield
422 yield
421 finally:
423 finally:
422 os.unlink(name)
424 os.unlink(name)
423
425
424
426
425 @contextmanager
427 @contextmanager
426 def monkeypatch(obj, name, attr):
428 def monkeypatch(obj, name, attr):
427 """
429 """
428 Context manager to replace attribute named `name` in `obj` with `attr`.
430 Context manager to replace attribute named `name` in `obj` with `attr`.
429 """
431 """
430 orig = getattr(obj, name)
432 orig = getattr(obj, name)
431 setattr(obj, name, attr)
433 setattr(obj, name, attr)
432 yield
434 yield
433 setattr(obj, name, orig)
435 setattr(obj, name, orig)
434
436
435
437
436 def help_output_test(subcommand=''):
438 def help_output_test(subcommand=''):
437 """test that `ipython [subcommand] -h` works"""
439 """test that `ipython [subcommand] -h` works"""
438 cmd = get_ipython_cmd() + [subcommand, '-h']
440 cmd = get_ipython_cmd() + [subcommand, '-h']
439 out, err, rc = get_output_error_code(cmd)
441 out, err, rc = get_output_error_code(cmd)
440 nt.assert_equal(rc, 0, err)
442 nt.assert_equal(rc, 0, err)
441 nt.assert_not_in("Traceback", err)
443 nt.assert_not_in("Traceback", err)
442 nt.assert_in("Options", out)
444 nt.assert_in("Options", out)
443 nt.assert_in("--help-all", out)
445 nt.assert_in("--help-all", out)
444 return out, err
446 return out, err
445
447
446
448
447 def help_all_output_test(subcommand=''):
449 def help_all_output_test(subcommand=''):
448 """test that `ipython [subcommand] --help-all` works"""
450 """test that `ipython [subcommand] --help-all` works"""
449 cmd = get_ipython_cmd() + [subcommand, '--help-all']
451 cmd = get_ipython_cmd() + [subcommand, '--help-all']
450 out, err, rc = get_output_error_code(cmd)
452 out, err, rc = get_output_error_code(cmd)
451 nt.assert_equal(rc, 0, err)
453 nt.assert_equal(rc, 0, err)
452 nt.assert_not_in("Traceback", err)
454 nt.assert_not_in("Traceback", err)
453 nt.assert_in("Options", out)
455 nt.assert_in("Options", out)
454 nt.assert_in("Class parameters", out)
456 nt.assert_in("Class parameters", out)
455 return out, err
457 return out, err
456
458
General Comments 0
You need to be logged in to leave comments. Login now