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