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