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