##// END OF EJS Templates
fix failing tests without X in IPython.lib...
Paul Ivanov -
Show More
@@ -1,119 +1,121
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. (For example, 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:
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.skip_if_no_x11
69 @decorators.skipif_not_matplotlib
70 @decorators.skipif_not_matplotlib
70 @decorators.skipif(pylab_not_importable, "Likely a run without X.")
71 @decorators.skipif(pylab_not_importable, "Likely a run without X.")
71 def test_pylab_import_all_enabled(self):
72 def test_pylab_import_all_enabled(self):
72 "Verify that plot is available when pylab_import_all = True"
73 "Verify that plot is available when pylab_import_all = True"
73 source = """
74 source = """
74 from IPython.config.application import Application
75 from IPython.config.application import Application
75 app = Application.instance()
76 app = Application.instance()
76 app.pylab_import_all = True
77 app.pylab_import_all = True
77 pylab
78 pylab
78 ip=get_ipython()
79 ip=get_ipython()
79 'plot' in ip.user_ns
80 'plot' in ip.user_ns
80 """
81 """
81 output = """
82 output = """
82 In \[1\]: from IPython\.config\.application import Application
83 In \[1\]: from IPython\.config\.application import Application
83 In \[2\]: app = Application\.instance\(\)
84 In \[2\]: app = Application\.instance\(\)
84 In \[3\]: app\.pylab_import_all = True
85 In \[3\]: app\.pylab_import_all = True
85 In \[4\]: pylab
86 In \[4\]: pylab
86 ^Using matplotlib backend:
87 ^Using matplotlib backend:
87 Populating the interactive namespace from numpy and matplotlib
88 Populating the interactive namespace from numpy and matplotlib
88 In \[5\]: ip=get_ipython\(\)
89 In \[5\]: ip=get_ipython\(\)
89 In \[6\]: \'plot\' in ip\.user_ns
90 In \[6\]: \'plot\' in ip\.user_ns
90 Out\[6\]: True
91 Out\[6\]: True
91 """
92 """
92 runner = irunner.IPythonRunner(out=self.out)
93 runner = irunner.IPythonRunner(out=self.out)
93 self._test_runner(runner,source,output)
94 self._test_runner(runner,source,output)
94
95
96 @decorators.skip_if_no_x11
95 @decorators.skipif_not_matplotlib
97 @decorators.skipif_not_matplotlib
96 @decorators.skipif(pylab_not_importable, "Likely a run without X.")
98 @decorators.skipif(pylab_not_importable, "Likely a run without X.")
97 def test_pylab_import_all_disabled(self):
99 def test_pylab_import_all_disabled(self):
98 "Verify that plot is not available when pylab_import_all = False"
100 "Verify that plot is not available when pylab_import_all = False"
99 source = """
101 source = """
100 from IPython.config.application import Application
102 from IPython.config.application import Application
101 app = Application.instance()
103 app = Application.instance()
102 app.pylab_import_all = False
104 app.pylab_import_all = False
103 pylab
105 pylab
104 ip=get_ipython()
106 ip=get_ipython()
105 'plot' in ip.user_ns
107 'plot' in ip.user_ns
106 """
108 """
107 output = """
109 output = """
108 In \[1\]: from IPython\.config\.application import Application
110 In \[1\]: from IPython\.config\.application import Application
109 In \[2\]: app = Application\.instance\(\)
111 In \[2\]: app = Application\.instance\(\)
110 In \[3\]: app\.pylab_import_all = False
112 In \[3\]: app\.pylab_import_all = False
111 In \[4\]: pylab
113 In \[4\]: pylab
112 ^Using matplotlib backend:
114 ^Using matplotlib backend:
113 Populating the interactive namespace from numpy and matplotlib
115 Populating the interactive namespace from numpy and matplotlib
114 In \[5\]: ip=get_ipython\(\)
116 In \[5\]: ip=get_ipython\(\)
115 In \[6\]: \'plot\' in ip\.user_ns
117 In \[6\]: \'plot\' in ip\.user_ns
116 Out\[6\]: False
118 Out\[6\]: False
117 """
119 """
118 runner = irunner.IPythonRunner(out=self.out)
120 runner = irunner.IPythonRunner(out=self.out)
119 self._test_runner(runner,source,output)
121 self._test_runner(runner,source,output)
General Comments 0
You need to be logged in to leave comments. Login now