##// END OF EJS Templates
pylab_not_importable: Catch all exceptions, not just RuntimeErrors....
Bradley M. Froehle -
Show More
@@ -1,119 +1,119 b''
1 1 """Test suite for pylab_import_all magic
2 2 Modified from the irunner module but using regex.
3 3 """
4 4 from __future__ import print_function
5 5
6 6 # Global to make tests extra verbose and help debugging
7 7 VERBOSE = True
8 8
9 9 # stdlib imports
10 10 import StringIO
11 11 import sys
12 12 import unittest
13 13 import re
14 14
15 15 # IPython imports
16 16 from IPython.lib import irunner
17 17 from IPython.testing import decorators
18 18
19 19 def pylab_not_importable():
20 """Test if importing pylab fails with RuntimeError (true when having no display)"""
20 """Test if importing pylab fails. (For example, when having no display)"""
21 21 try:
22 22 import pylab
23 23 return False
24 except RuntimeError:
24 except:
25 25 return True
26 26
27 27 # Testing code begins
28 28 class RunnerTestCase(unittest.TestCase):
29 29
30 30 def setUp(self):
31 31 self.out = StringIO.StringIO()
32 32 #self.out = sys.stdout
33 33
34 34 def _test_runner(self,runner,source,output):
35 35 """Test that a given runner's input/output match."""
36 36
37 37 runner.run_source(source)
38 38 out = self.out.getvalue()
39 39 #out = ''
40 40 # this output contains nasty \r\n lineends, and the initial ipython
41 41 # banner. clean it up for comparison, removing lines of whitespace
42 42 output_l = [l for l in output.splitlines() if l and not l.isspace()]
43 43 out_l = [l for l in out.splitlines() if l and not l.isspace()]
44 44 mismatch = 0
45 45 if len(output_l) != len(out_l):
46 46 message = ("Mismatch in number of lines\n\n"
47 47 "Expected:\n"
48 48 "~~~~~~~~~\n"
49 49 "%s\n\n"
50 50 "Got:\n"
51 51 "~~~~~~~~~\n"
52 52 "%s"
53 53 ) % ("\n".join(output_l), "\n".join(out_l))
54 54 self.fail(message)
55 55 for n in range(len(output_l)):
56 56 # Do a line-by-line comparison
57 57 ol1 = output_l[n].strip()
58 58 ol2 = out_l[n].strip()
59 59 if not re.match(ol1,ol2):
60 60 mismatch += 1
61 61 if VERBOSE:
62 62 print('<<< line %s does not match:' % n)
63 63 print(repr(ol1))
64 64 print(repr(ol2))
65 65 print('>>>')
66 66 self.assertTrue(mismatch==0,'Number of mismatched lines: %s' %
67 67 mismatch)
68 68
69 69 @decorators.skipif_not_matplotlib
70 70 @decorators.skipif(pylab_not_importable, "Likely a run without X.")
71 71 def test_pylab_import_all_enabled(self):
72 72 "Verify that plot is available when pylab_import_all = True"
73 73 source = """
74 74 from IPython.config.application import Application
75 75 app = Application.instance()
76 76 app.pylab_import_all = True
77 77 pylab
78 78 ip=get_ipython()
79 79 'plot' in ip.user_ns
80 80 """
81 81 output = """
82 82 In \[1\]: from IPython\.config\.application import Application
83 83 In \[2\]: app = Application\.instance\(\)
84 84 In \[3\]: app\.pylab_import_all = True
85 85 In \[4\]: pylab
86 86 ^Welcome to pylab, a matplotlib-based Python environment
87 87 For more information, type 'help\(pylab\)'\.
88 88 In \[5\]: ip=get_ipython\(\)
89 89 In \[6\]: \'plot\' in ip\.user_ns
90 90 Out\[6\]: True
91 91 """
92 92 runner = irunner.IPythonRunner(out=self.out)
93 93 self._test_runner(runner,source,output)
94 94
95 95 @decorators.skipif_not_matplotlib
96 96 @decorators.skipif(pylab_not_importable, "Likely a run without X.")
97 97 def test_pylab_import_all_disabled(self):
98 98 "Verify that plot is not available when pylab_import_all = False"
99 99 source = """
100 100 from IPython.config.application import Application
101 101 app = Application.instance()
102 102 app.pylab_import_all = False
103 103 pylab
104 104 ip=get_ipython()
105 105 'plot' in ip.user_ns
106 106 """
107 107 output = """
108 108 In \[1\]: from IPython\.config\.application import Application
109 109 In \[2\]: app = Application\.instance\(\)
110 110 In \[3\]: app\.pylab_import_all = False
111 111 In \[4\]: pylab
112 112 ^Welcome to pylab, a matplotlib-based Python environment
113 113 For more information, type 'help\(pylab\)'\.
114 114 In \[5\]: ip=get_ipython\(\)
115 115 In \[6\]: \'plot\' in ip\.user_ns
116 116 Out\[6\]: False
117 117 """
118 118 runner = irunner.IPythonRunner(out=self.out)
119 119 self._test_runner(runner,source,output)
General Comments 0
You need to be logged in to leave comments. Login now