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