##// END OF EJS Templates
pathlib on test_module_paths.py
dswij -
Show More
@@ -12,13 +12,12 b''
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
@@ -27,9 +26,9 b' import IPython.utils.module_paths as mp'
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 #
@@ -46,13 +45,13 b' def setup_module():'
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:
@@ -70,7 +69,7 b' 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():
@@ -78,8 +77,8 b' def test_find_mod_1():'
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 """
@@ -87,24 +86,24 b' def test_find_mod_2():'
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 """
General Comments 0
You need to be logged in to leave comments. Login now