##// END OF EJS Templates
pathlib on test_module_paths.py
dswij -
Show More
@@ -1,114 +1,113 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
16 import os
15 import os
17 import shutil
16 import shutil
18 import sys
17 import sys
19 import tempfile
18 import tempfile
20
19
21 from os.path import join, abspath, split
20 from pathlib import Path
22
21
23 from IPython.testing.tools import make_tempfile
22 from IPython.testing.tools import make_tempfile
24
23
25 import IPython.utils.module_paths as mp
24 import IPython.utils.module_paths as mp
26
25
27 import nose.tools as nt
26 import nose.tools as nt
28
27
29 env = os.environ
28 env = os.environ
30 TEST_FILE_PATH = split(abspath(__file__))[0]
29 TEST_FILE_PATH = Path(__file__).resolve().parent
31
30
32 TMP_TEST_DIR = tempfile.mkdtemp(suffix='with.dot')
31 TMP_TEST_DIR = Path(tempfile.mkdtemp(suffix="with.dot"))
33 #
32 #
34 # Setup/teardown functions/decorators
33 # Setup/teardown functions/decorators
35 #
34 #
36
35
37 old_syspath = sys.path
36 old_syspath = sys.path
38
37
39 def make_empty_file(fname):
38 def make_empty_file(fname):
40 open(fname, 'w').close()
39 open(fname, 'w').close()
41
40
42
41
43 def setup_module():
42 def setup_module():
44 """Setup testenvironment for the module:
43 """Setup testenvironment for the module:
45
44
46 """
45 """
47 # Do not mask exceptions here. In particular, catching WindowsError is a
46 # Do not mask exceptions here. In particular, catching WindowsError is a
48 # problem because that exception is only defined on Windows...
47 # problem because that exception is only defined on Windows...
49 os.makedirs(join(TMP_TEST_DIR, "xmod"))
48 Path(TMP_TEST_DIR / "xmod").mkdir(parents=True)
50 os.makedirs(join(TMP_TEST_DIR, "nomod"))
49 Path(TMP_TEST_DIR / "nomod").mkdir(parents=True)
51 make_empty_file(join(TMP_TEST_DIR, "xmod/__init__.py"))
50 make_empty_file(TMP_TEST_DIR / "xmod/__init__.py")
52 make_empty_file(join(TMP_TEST_DIR, "xmod/sub.py"))
51 make_empty_file(TMP_TEST_DIR / "xmod/sub.py")
53 make_empty_file(join(TMP_TEST_DIR, "pack.py"))
52 make_empty_file(TMP_TEST_DIR / "pack.py")
54 make_empty_file(join(TMP_TEST_DIR, "packpyc.pyc"))
53 make_empty_file(TMP_TEST_DIR / "packpyc.pyc")
55 sys.path = [TMP_TEST_DIR]
54 sys.path = [str(TMP_TEST_DIR)]
56
55
57 def teardown_module():
56 def teardown_module():
58 """Teardown testenvironment for the module:
57 """Teardown testenvironment for the module:
59
58
60 - Remove tempdir
59 - Remove tempdir
61 - restore sys.path
60 - restore sys.path
62 """
61 """
63 # Note: we remove the parent test dir, which is the root of all test
62 # Note: we remove the parent test dir, which is the root of all test
64 # subdirs we may have created. Use shutil instead of os.removedirs, so
63 # subdirs we may have created. Use shutil instead of os.removedirs, so
65 # that non-empty directories are all recursively removed.
64 # that non-empty directories are all recursively removed.
66 shutil.rmtree(TMP_TEST_DIR)
65 shutil.rmtree(TMP_TEST_DIR)
67 sys.path = old_syspath
66 sys.path = old_syspath
68
67
69 def test_tempdir():
68 def test_tempdir():
70 """
69 """
71 Ensure the test are done with a temporary file that have a dot somewhere.
70 Ensure the test are done with a temporary file that have a dot somewhere.
72 """
71 """
73 nt.assert_in('.',TMP_TEST_DIR)
72 nt.assert_in(".", str(TMP_TEST_DIR))
74
73
75
74
76 def test_find_mod_1():
75 def test_find_mod_1():
77 """
76 """
78 Search for a directory's file path.
77 Search for a directory's file path.
79 Expected output: a path to that directory's __init__.py file.
78 Expected output: a path to that directory's __init__.py file.
80 """
79 """
81 modpath = join(TMP_TEST_DIR, "xmod", "__init__.py")
80 modpath = TMP_TEST_DIR / "xmod" / "__init__.py"
82 nt.assert_equal(mp.find_mod("xmod"), modpath)
81 nt.assert_equal(Path(mp.find_mod("xmod")), modpath)
83
82
84 def test_find_mod_2():
83 def test_find_mod_2():
85 """
84 """
86 Search for a directory's file path.
85 Search for a directory's file path.
87 Expected output: a path to that directory's __init__.py file.
86 Expected output: a path to that directory's __init__.py file.
88 TODO: Confirm why this is a duplicate test.
87 TODO: Confirm why this is a duplicate test.
89 """
88 """
90 modpath = join(TMP_TEST_DIR, "xmod", "__init__.py")
89 modpath = TMP_TEST_DIR / "xmod" / "__init__.py"
91 nt.assert_equal(mp.find_mod("xmod"), modpath)
90 nt.assert_equal(Path(mp.find_mod("xmod")), modpath)
92
91
93 def test_find_mod_3():
92 def test_find_mod_3():
94 """
93 """
95 Search for a directory + a filename without its .py extension
94 Search for a directory + a filename without its .py extension
96 Expected output: full path with .py extension.
95 Expected output: full path with .py extension.
97 """
96 """
98 modpath = join(TMP_TEST_DIR, "xmod", "sub.py")
97 modpath = TMP_TEST_DIR / "xmod" / "sub.py"
99 nt.assert_equal(mp.find_mod("xmod.sub"), modpath)
98 nt.assert_equal(Path(mp.find_mod("xmod.sub")), modpath)
100
99
101 def test_find_mod_4():
100 def test_find_mod_4():
102 """
101 """
103 Search for a filename without its .py extension
102 Search for a filename without its .py extension
104 Expected output: full path with .py extension
103 Expected output: full path with .py extension
105 """
104 """
106 modpath = join(TMP_TEST_DIR, "pack.py")
105 modpath = TMP_TEST_DIR / "pack.py"
107 nt.assert_equal(mp.find_mod("pack"), modpath)
106 nt.assert_equal(Path(mp.find_mod("pack")), modpath)
108
107
109 def test_find_mod_5():
108 def test_find_mod_5():
110 """
109 """
111 Search for a filename with a .pyc extension
110 Search for a filename with a .pyc extension
112 Expected output: TODO: do we exclude or include .pyc files?
111 Expected output: TODO: do we exclude or include .pyc files?
113 """
112 """
114 nt.assert_equal(mp.find_mod("packpyc"), None)
113 nt.assert_equal(mp.find_mod("packpyc"), None)
General Comments 0
You need to be logged in to leave comments. Login now