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