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