##// END OF EJS Templates
teardown changes to sys.path
Thomas Kluyver -
Show More
@@ -1,133 +1,135 b''
1 # encoding: utf-8
1 # encoding: utf-8
2 """Tests for IPython.utils.path.py"""
2 """Tests for IPython.utils.path.py"""
3
3
4 #-----------------------------------------------------------------------------
4 #-----------------------------------------------------------------------------
5 # Copyright (C) 2008-2011 The IPython Development Team
5 # Copyright (C) 2008-2011 The IPython Development Team
6 #
6 #
7 # Distributed under the terms of the BSD License. The full license is in
7 # Distributed under the terms of the BSD License. The full license is in
8 # the file COPYING, distributed as part of this software.
8 # the file COPYING, distributed as part of this software.
9 #-----------------------------------------------------------------------------
9 #-----------------------------------------------------------------------------
10
10
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12 # Imports
12 # Imports
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14
14
15 from __future__ import with_statement
15 from __future__ import with_statement
16
16
17 import os
17 import os
18 import shutil
18 import shutil
19 import sys
19 import sys
20 import tempfile
20 import tempfile
21 import StringIO
21 import StringIO
22
22
23 from os.path import join, abspath, split
23 from os.path import join, abspath, split
24
24
25 import nose.tools as nt
25 import nose.tools as nt
26
26
27 from nose import with_setup
27 from nose import with_setup
28
28
29 import IPython
29 import IPython
30 from IPython.testing import decorators as dec
30 from IPython.testing import decorators as dec
31 from IPython.testing.decorators import skip_if_not_win32, skip_win32
31 from IPython.testing.decorators import skip_if_not_win32, skip_win32
32 from IPython.testing.tools import make_tempfile
32 from IPython.testing.tools import make_tempfile
33 from IPython.utils import path, io
33 from IPython.utils import path, io
34 from IPython.utils import py3compat
34 from IPython.utils import py3compat
35
35
36 import IPython.utils.module_paths as mp
36 import IPython.utils.module_paths as mp
37
37
38 env = os.environ
38 env = os.environ
39 TEST_FILE_PATH = split(abspath(__file__))[0]
39 TEST_FILE_PATH = split(abspath(__file__))[0]
40 TMP_TEST_DIR = tempfile.mkdtemp()
40 TMP_TEST_DIR = tempfile.mkdtemp()
41 #
41 #
42 # Setup/teardown functions/decorators
42 # Setup/teardown functions/decorators
43 #
43 #
44
44
45 old_syspath = sys.path
45 old_syspath = sys.path
46 sys.path = [TMP_TEST_DIR]
47
46
48 def make_empty_file(fname):
47 def make_empty_file(fname):
49 f = open(fname, 'w')
48 f = open(fname, 'w')
50 f.close()
49 f.close()
51
50
52
51
53 def setup():
52 def setup():
54 """Setup testenvironment for the module:
53 """Setup testenvironment for the module:
55
54
56 """
55 """
57 # Do not mask exceptions here. In particular, catching WindowsError is a
56 # Do not mask exceptions here. In particular, catching WindowsError is a
58 # problem because that exception is only defined on Windows...
57 # problem because that exception is only defined on Windows...
59 os.makedirs(join(TMP_TEST_DIR, "xmod"))
58 os.makedirs(join(TMP_TEST_DIR, "xmod"))
60 os.makedirs(join(TMP_TEST_DIR, "nomod"))
59 os.makedirs(join(TMP_TEST_DIR, "nomod"))
61 make_empty_file(join(TMP_TEST_DIR, "xmod/__init__.py"))
60 make_empty_file(join(TMP_TEST_DIR, "xmod/__init__.py"))
62 make_empty_file(join(TMP_TEST_DIR, "xmod/sub.py"))
61 make_empty_file(join(TMP_TEST_DIR, "xmod/sub.py"))
63 make_empty_file(join(TMP_TEST_DIR, "pack.py"))
62 make_empty_file(join(TMP_TEST_DIR, "pack.py"))
64 make_empty_file(join(TMP_TEST_DIR, "packpyc.pyc"))
63 make_empty_file(join(TMP_TEST_DIR, "packpyc.pyc"))
64 sys.path = [TMP_TEST_DIR]
65
65
66 def teardown():
66 def teardown():
67 """Teardown testenvironment for the module:
67 """Teardown testenvironment for the module:
68
68
69 - Remove tempdir
69 - Remove tempdir
70 - restore sys.path
70 """
71 """
71 # Note: we remove the parent test dir, which is the root of all test
72 # Note: we remove the parent test dir, which is the root of all test
72 # subdirs we may have created. Use shutil instead of os.removedirs, so
73 # subdirs we may have created. Use shutil instead of os.removedirs, so
73 # that non-empty directories are all recursively removed.
74 # that non-empty directories are all recursively removed.
74 shutil.rmtree(TMP_TEST_DIR)
75 shutil.rmtree(TMP_TEST_DIR)
76 sys.path = old_syspath
75
77
76
78
77 def test_get_init_1():
79 def test_get_init_1():
78 """See if get_init can find __init__.py in this testdir"""
80 """See if get_init can find __init__.py in this testdir"""
79 with make_tempfile(join(TMP_TEST_DIR, "__init__.py")):
81 with make_tempfile(join(TMP_TEST_DIR, "__init__.py")):
80 assert mp.get_init(TMP_TEST_DIR)
82 assert mp.get_init(TMP_TEST_DIR)
81
83
82 def test_get_init_2():
84 def test_get_init_2():
83 """See if get_init can find __init__.pyw in this testdir"""
85 """See if get_init can find __init__.pyw in this testdir"""
84 with make_tempfile(join(TMP_TEST_DIR, "__init__.pyw")):
86 with make_tempfile(join(TMP_TEST_DIR, "__init__.pyw")):
85 assert mp.get_init(TMP_TEST_DIR)
87 assert mp.get_init(TMP_TEST_DIR)
86
88
87 def test_get_init_3():
89 def test_get_init_3():
88 """get_init can't find __init__.pyc in this testdir"""
90 """get_init can't find __init__.pyc in this testdir"""
89 with make_tempfile(join(TMP_TEST_DIR, "__init__.pyc")):
91 with make_tempfile(join(TMP_TEST_DIR, "__init__.pyc")):
90 assert mp.get_init(TMP_TEST_DIR) is None
92 assert mp.get_init(TMP_TEST_DIR) is None
91
93
92 def test_get_init_3():
94 def test_get_init_3():
93 """get_init can't find __init__ in empty testdir"""
95 """get_init can't find __init__ in empty testdir"""
94 assert mp.get_init(TMP_TEST_DIR) is None
96 assert mp.get_init(TMP_TEST_DIR) is None
95
97
96
98
97 def test_find_mod_1():
99 def test_find_mod_1():
98 modpath = join(TMP_TEST_DIR, "xmod", "__init__.py")
100 modpath = join(TMP_TEST_DIR, "xmod", "__init__.py")
99 assert mp.find_mod("xmod") == modpath
101 assert mp.find_mod("xmod") == modpath
100
102
101 def test_find_mod_2():
103 def test_find_mod_2():
102 modpath = join(TMP_TEST_DIR, "xmod", "__init__.py")
104 modpath = join(TMP_TEST_DIR, "xmod", "__init__.py")
103 assert mp.find_mod("xmod") == modpath
105 assert mp.find_mod("xmod") == modpath
104
106
105 def test_find_mod_3():
107 def test_find_mod_3():
106 modpath = join(TMP_TEST_DIR, "xmod", "sub.py")
108 modpath = join(TMP_TEST_DIR, "xmod", "sub.py")
107 assert mp.find_mod("xmod.sub") == modpath
109 assert mp.find_mod("xmod.sub") == modpath
108
110
109 def test_find_mod_4():
111 def test_find_mod_4():
110 modpath = join(TMP_TEST_DIR, "pack.py")
112 modpath = join(TMP_TEST_DIR, "pack.py")
111 assert mp.find_mod("pack") == modpath
113 assert mp.find_mod("pack") == modpath
112
114
113 def test_find_mod_5():
115 def test_find_mod_5():
114 assert mp.find_mod("packpyc") is None
116 assert mp.find_mod("packpyc") is None
115
117
116 def test_find_module_1():
118 def test_find_module_1():
117 modpath = join(TMP_TEST_DIR, "xmod")
119 modpath = join(TMP_TEST_DIR, "xmod")
118 assert mp.find_module("xmod") == modpath
120 assert mp.find_module("xmod") == modpath
119
121
120 def test_find_module_2():
122 def test_find_module_2():
121 """Testing sys.path that is empty"""
123 """Testing sys.path that is empty"""
122 assert mp.find_module("xmod", []) is None
124 assert mp.find_module("xmod", []) is None
123
125
124 def test_find_module_3():
126 def test_find_module_3():
125 """Testing sys.path that is empty"""
127 """Testing sys.path that is empty"""
126 assert mp.find_module(None, None) is None
128 assert mp.find_module(None, None) is None
127
129
128 def test_find_module_4():
130 def test_find_module_4():
129 """Testing sys.path that is empty"""
131 """Testing sys.path that is empty"""
130 assert mp.find_module(None) is None
132 assert mp.find_module(None) is None
131
133
132 def test_find_module_5():
134 def test_find_module_5():
133 assert mp.find_module("xmod.nopack") is None
135 assert mp.find_module("xmod.nopack") is None
General Comments 0
You need to be logged in to leave comments. Login now