##// END OF EJS Templates
Brian Granger -
Show More
@@ -1,76 +1,80 b''
1 """Tests for the key iplib module, where the main ipython class is defined.
1 """Tests for the key iplib module, where the main ipython class is defined.
2 """
2 """
3 #-----------------------------------------------------------------------------
3 #-----------------------------------------------------------------------------
4 # Module imports
4 # Module imports
5 #-----------------------------------------------------------------------------
5 #-----------------------------------------------------------------------------
6
6
7 # stdlib
7 # stdlib
8 import os
8 import os
9 import shutil
9 import shutil
10 import tempfile
10 import tempfile
11
11
12 # third party
12 # third party
13 import nose.tools as nt
13 import nose.tools as nt
14
14
15 # our own packages
15 # our own packages
16 from IPython import ipapi, iplib
16 from IPython import ipapi, iplib
17
17
18 #-----------------------------------------------------------------------------
18 #-----------------------------------------------------------------------------
19 # Globals
19 # Globals
20 #-----------------------------------------------------------------------------
20 #-----------------------------------------------------------------------------
21
21
22 # Useful global ipapi object and main IPython one. Unfortunately we have a
22 # Useful global ipapi object and main IPython one. Unfortunately we have a
23 # long precedent of carrying the 'ipapi' global object which is injected into
23 # long precedent of carrying the 'ipapi' global object which is injected into
24 # the system namespace as _ip, but that keeps a pointer to the actual IPython
24 # the system namespace as _ip, but that keeps a pointer to the actual IPython
25 # InteractiveShell instance, which is named IP. Since in testing we do need
25 # InteractiveShell instance, which is named IP. Since in testing we do need
26 # access to the real thing (we want to probe beyond what ipapi exposes), make
26 # access to the real thing (we want to probe beyond what ipapi exposes), make
27 # here a global reference to each. In general, things that are exposed by the
27 # here a global reference to each. In general, things that are exposed by the
28 # ipapi instance should be read from there, but we also will often need to use
28 # ipapi instance should be read from there, but we also will often need to use
29 # the actual IPython one.
29 # the actual IPython one.
30
30
31 # Get the public instance of IPython, and if it's None, make one so we can use
31 # Get the public instance of IPython, and if it's None, make one so we can use
32 # it for testing
32 # it for testing
33 ip = ipapi.get()
33 ip = ipapi.get()
34 if ip is None:
34 if ip is None:
35 # IPython not running yet, make one from the testing machinery for
35 # IPython not running yet, make one from the testing machinery for
36 # consistency when the test suite is being run via iptest
36 # consistency when the test suite is being run via iptest
37 from IPython.testing.plugin import ipdoctest
37 from IPython.testing.plugin import ipdoctest
38 ip = ipapi.get()
38 ip = ipapi.get()
39
39
40 IP = ip.IP # This is the actual IPython shell 'raw' object.
40 IP = ip.IP # This is the actual IPython shell 'raw' object.
41
41
42 #-----------------------------------------------------------------------------
42 #-----------------------------------------------------------------------------
43 # Test functions
43 # Test functions
44 #-----------------------------------------------------------------------------
44 #-----------------------------------------------------------------------------
45
45
46 def test_reset():
46 def test_reset():
47 """reset must clear most namespaces."""
47 """reset must clear most namespaces."""
48 IP.reset() # first, it should run without error
48 IP.reset() # first, it should run without error
49 # Then, check that most namespaces end up empty
49 # Then, check that most namespaces end up empty
50 for ns in IP.ns_refs_table:
50 for ns in IP.ns_refs_table:
51 if ns is IP.user_ns:
51 if ns is IP.user_ns:
52 # The user namespace is reset with some data, so we can't check for
52 # The user namespace is reset with some data, so we can't check for
53 # it being empty
53 # it being empty
54 continue
54 continue
55 nt.assert_equals(len(ns),0)
55 nt.assert_equals(len(ns),0)
56
56
57
57
58 # make sure that user_setup can be run re-entrantly in 'install' mode.
58 # make sure that user_setup can be run re-entrantly in 'install' mode.
59 def test_user_setup():
59 def test_user_setup():
60 # use a lambda to pass kwargs to the generator
60 # use a lambda to pass kwargs to the generator
61 user_setup = lambda a,k: iplib.user_setup(*a,**k)
61 user_setup = lambda a,k: iplib.user_setup(*a,**k)
62 kw = dict(mode='install', interactive=False)
62 kw = dict(mode='install', interactive=False)
63
63
64 # Call the user setup and verify that the directory exists
64 # Call the user setup and verify that the directory exists
65 yield user_setup, (ip.options.ipythondir,''), kw
65 yield user_setup, (ip.options.ipythondir,''), kw
66 yield os.path.isdir, ip.options.ipythondir
66 yield os.path.isdir, ip.options.ipythondir
67
67
68 # Now repeat the operation with a non-existent directory. Check both that
68 # Now repeat the operation with a non-existent directory. Check both that
69 # the call succeeds and that the directory is created.
69 # the call succeeds and that the directory is created.
70 tmpdir = tempfile.mktemp(prefix='ipython-test-')
70 tmpdir = tempfile.mktemp(prefix='ipython-test-')
71 # Use a try with an empty except because try/finally doesn't work with a
72 # yield in Python 2.4.
71 try:
73 try:
72 yield user_setup, (tmpdir,''), kw
74 yield user_setup, (tmpdir,''), kw
73 yield os.path.isdir, tmpdir
75 yield os.path.isdir, tmpdir
74 finally:
76 except:
75 # In this case, clean up the temp dir once done
77 pass
76 shutil.rmtree(tmpdir)
78 # Clean up the temp dir once done
79 shutil.rmtree(tmpdir)
80 No newline at end of file
General Comments 0
You need to be logged in to leave comments. Login now