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