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