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