##// END OF EJS Templates
[core][tests][paths] Remove nose
Samuel Gaist -
Show More
@@ -1,204 +1,205 b''
1 import errno
1 import errno
2 import os
2 import os
3 import shutil
3 import shutil
4 import sys
4 import sys
5 import tempfile
5 import tempfile
6 import warnings
6 import warnings
7 from unittest.mock import patch
7 from unittest.mock import patch
8
8
9 import nose.tools as nt
10 from testpath import modified_env, assert_isdir, assert_isfile
9 from testpath import modified_env, assert_isdir, assert_isfile
11
10
12 from IPython import paths
11 from IPython import paths
13 from IPython.testing.decorators import skip_win32
12 from IPython.testing.decorators import skip_win32
14 from IPython.utils.tempdir import TemporaryDirectory
13 from IPython.utils.tempdir import TemporaryDirectory
15
14
16 TMP_TEST_DIR = os.path.realpath(tempfile.mkdtemp())
15 TMP_TEST_DIR = os.path.realpath(tempfile.mkdtemp())
17 HOME_TEST_DIR = os.path.join(TMP_TEST_DIR, "home_test_dir")
16 HOME_TEST_DIR = os.path.join(TMP_TEST_DIR, "home_test_dir")
18 XDG_TEST_DIR = os.path.join(HOME_TEST_DIR, "xdg_test_dir")
17 XDG_TEST_DIR = os.path.join(HOME_TEST_DIR, "xdg_test_dir")
19 XDG_CACHE_DIR = os.path.join(HOME_TEST_DIR, "xdg_cache_dir")
18 XDG_CACHE_DIR = os.path.join(HOME_TEST_DIR, "xdg_cache_dir")
20 IP_TEST_DIR = os.path.join(HOME_TEST_DIR,'.ipython')
19 IP_TEST_DIR = os.path.join(HOME_TEST_DIR,'.ipython')
21
20
22 def setup_module():
21 def setup_module():
23 """Setup testenvironment for the module:
22 """Setup testenvironment for the module:
24
23
25 - Adds dummy home dir tree
24 - Adds dummy home dir tree
26 """
25 """
27 # Do not mask exceptions here. In particular, catching WindowsError is a
26 # Do not mask exceptions here. In particular, catching WindowsError is a
28 # problem because that exception is only defined on Windows...
27 # problem because that exception is only defined on Windows...
29 os.makedirs(IP_TEST_DIR)
28 os.makedirs(IP_TEST_DIR)
30 os.makedirs(os.path.join(XDG_TEST_DIR, 'ipython'))
29 os.makedirs(os.path.join(XDG_TEST_DIR, 'ipython'))
31 os.makedirs(os.path.join(XDG_CACHE_DIR, 'ipython'))
30 os.makedirs(os.path.join(XDG_CACHE_DIR, 'ipython'))
32
31
33
32
34 def teardown_module():
33 def teardown_module():
35 """Teardown testenvironment for the module:
34 """Teardown testenvironment for the module:
36
35
37 - Remove dummy home dir tree
36 - Remove dummy home dir tree
38 """
37 """
39 # Note: we remove the parent test dir, which is the root of all test
38 # Note: we remove the parent test dir, which is the root of all test
40 # subdirs we may have created. Use shutil instead of os.removedirs, so
39 # subdirs we may have created. Use shutil instead of os.removedirs, so
41 # that non-empty directories are all recursively removed.
40 # that non-empty directories are all recursively removed.
42 shutil.rmtree(TMP_TEST_DIR)
41 shutil.rmtree(TMP_TEST_DIR)
43
42
44 def patch_get_home_dir(dirpath):
43 def patch_get_home_dir(dirpath):
45 return patch.object(paths, 'get_home_dir', return_value=dirpath)
44 return patch.object(paths, 'get_home_dir', return_value=dirpath)
46
45
47
46
48 def test_get_ipython_dir_1():
47 def test_get_ipython_dir_1():
49 """test_get_ipython_dir_1, Testcase to see if we can call get_ipython_dir without Exceptions."""
48 """test_get_ipython_dir_1, Testcase to see if we can call get_ipython_dir without Exceptions."""
50 env_ipdir = os.path.join("someplace", ".ipython")
49 env_ipdir = os.path.join("someplace", ".ipython")
51 with patch.object(paths, '_writable_dir', return_value=True), \
50 with patch.object(paths, '_writable_dir', return_value=True), \
52 modified_env({'IPYTHONDIR': env_ipdir}):
51 modified_env({'IPYTHONDIR': env_ipdir}):
53 ipdir = paths.get_ipython_dir()
52 ipdir = paths.get_ipython_dir()
54
53
55 nt.assert_equal(ipdir, env_ipdir)
54 assert ipdir == env_ipdir
56
55
57 def test_get_ipython_dir_2():
56 def test_get_ipython_dir_2():
58 """test_get_ipython_dir_2, Testcase to see if we can call get_ipython_dir without Exceptions."""
57 """test_get_ipython_dir_2, Testcase to see if we can call get_ipython_dir without Exceptions."""
59 with patch_get_home_dir('someplace'), \
58 with patch_get_home_dir('someplace'), \
60 patch.object(paths, 'get_xdg_dir', return_value=None), \
59 patch.object(paths, 'get_xdg_dir', return_value=None), \
61 patch.object(paths, '_writable_dir', return_value=True), \
60 patch.object(paths, '_writable_dir', return_value=True), \
62 patch('os.name', "posix"), \
61 patch('os.name', "posix"), \
63 modified_env({'IPYTHON_DIR': None,
62 modified_env({'IPYTHON_DIR': None,
64 'IPYTHONDIR': None,
63 'IPYTHONDIR': None,
65 'XDG_CONFIG_HOME': None
64 'XDG_CONFIG_HOME': None
66 }):
65 }):
67 ipdir = paths.get_ipython_dir()
66 ipdir = paths.get_ipython_dir()
68
67
69 nt.assert_equal(ipdir, os.path.join("someplace", ".ipython"))
68 assert ipdir == os.path.join("someplace", ".ipython")
70
69
71 def test_get_ipython_dir_3():
70 def test_get_ipython_dir_3():
72 """test_get_ipython_dir_3, move XDG if defined, and .ipython doesn't exist."""
71 """test_get_ipython_dir_3, move XDG if defined, and .ipython doesn't exist."""
73 tmphome = TemporaryDirectory()
72 tmphome = TemporaryDirectory()
74 try:
73 try:
75 with patch_get_home_dir(tmphome.name), \
74 with patch_get_home_dir(tmphome.name), \
76 patch('os.name', 'posix'), \
75 patch('os.name', 'posix'), \
77 modified_env({
76 modified_env({
78 'IPYTHON_DIR': None,
77 'IPYTHON_DIR': None,
79 'IPYTHONDIR': None,
78 'IPYTHONDIR': None,
80 'XDG_CONFIG_HOME': XDG_TEST_DIR,
79 'XDG_CONFIG_HOME': XDG_TEST_DIR,
81 }), warnings.catch_warnings(record=True) as w:
80 }), warnings.catch_warnings(record=True) as w:
82 ipdir = paths.get_ipython_dir()
81 ipdir = paths.get_ipython_dir()
83
82
84 nt.assert_equal(ipdir, os.path.join(tmphome.name, ".ipython"))
83 assert ipdir == os.path.join(tmphome.name, ".ipython")
85 if sys.platform != 'darwin':
84 if sys.platform != 'darwin':
86 nt.assert_equal(len(w), 1)
85 assert len(w) == 1
87 nt.assert_in('Moving', str(w[0]))
86 assert "Moving" in str(w[0])
88 finally:
87 finally:
89 tmphome.cleanup()
88 tmphome.cleanup()
90
89
91 def test_get_ipython_dir_4():
90 def test_get_ipython_dir_4():
92 """test_get_ipython_dir_4, warn if XDG and home both exist."""
91 """test_get_ipython_dir_4, warn if XDG and home both exist."""
93 with patch_get_home_dir(HOME_TEST_DIR), \
92 with patch_get_home_dir(HOME_TEST_DIR), \
94 patch('os.name', 'posix'):
93 patch('os.name', 'posix'):
95 try:
94 try:
96 os.mkdir(os.path.join(XDG_TEST_DIR, 'ipython'))
95 os.mkdir(os.path.join(XDG_TEST_DIR, 'ipython'))
97 except OSError as e:
96 except OSError as e:
98 if e.errno != errno.EEXIST:
97 if e.errno != errno.EEXIST:
99 raise
98 raise
100
99
101
100
102 with modified_env({
101 with modified_env({
103 'IPYTHON_DIR': None,
102 'IPYTHON_DIR': None,
104 'IPYTHONDIR': None,
103 'IPYTHONDIR': None,
105 'XDG_CONFIG_HOME': XDG_TEST_DIR,
104 'XDG_CONFIG_HOME': XDG_TEST_DIR,
106 }), warnings.catch_warnings(record=True) as w:
105 }), warnings.catch_warnings(record=True) as w:
107 ipdir = paths.get_ipython_dir()
106 ipdir = paths.get_ipython_dir()
108
107
109 nt.assert_equal(ipdir, os.path.join(HOME_TEST_DIR, ".ipython"))
108 assert ipdir == os.path.join(HOME_TEST_DIR, ".ipython")
110 if sys.platform != 'darwin':
109 if sys.platform != 'darwin':
111 nt.assert_equal(len(w), 1)
110 assert len(w) == 1
112 nt.assert_in('Ignoring', str(w[0]))
111 assert "Ignoring" in str(w[0])
112
113
113
114 def test_get_ipython_dir_5():
114 def test_get_ipython_dir_5():
115 """test_get_ipython_dir_5, use .ipython if exists and XDG defined, but doesn't exist."""
115 """test_get_ipython_dir_5, use .ipython if exists and XDG defined, but doesn't exist."""
116 with patch_get_home_dir(HOME_TEST_DIR), \
116 with patch_get_home_dir(HOME_TEST_DIR), \
117 patch('os.name', 'posix'):
117 patch('os.name', 'posix'):
118 try:
118 try:
119 os.rmdir(os.path.join(XDG_TEST_DIR, 'ipython'))
119 os.rmdir(os.path.join(XDG_TEST_DIR, 'ipython'))
120 except OSError as e:
120 except OSError as e:
121 if e.errno != errno.ENOENT:
121 if e.errno != errno.ENOENT:
122 raise
122 raise
123
123
124 with modified_env({
124 with modified_env({
125 'IPYTHON_DIR': None,
125 'IPYTHON_DIR': None,
126 'IPYTHONDIR': None,
126 'IPYTHONDIR': None,
127 'XDG_CONFIG_HOME': XDG_TEST_DIR,
127 'XDG_CONFIG_HOME': XDG_TEST_DIR,
128 }):
128 }):
129 ipdir = paths.get_ipython_dir()
129 ipdir = paths.get_ipython_dir()
130
130
131 nt.assert_equal(ipdir, IP_TEST_DIR)
131 assert ipdir == IP_TEST_DIR
132
132
133 def test_get_ipython_dir_6():
133 def test_get_ipython_dir_6():
134 """test_get_ipython_dir_6, use home over XDG if defined and neither exist."""
134 """test_get_ipython_dir_6, use home over XDG if defined and neither exist."""
135 xdg = os.path.join(HOME_TEST_DIR, 'somexdg')
135 xdg = os.path.join(HOME_TEST_DIR, 'somexdg')
136 os.mkdir(xdg)
136 os.mkdir(xdg)
137 shutil.rmtree(os.path.join(HOME_TEST_DIR, '.ipython'))
137 shutil.rmtree(os.path.join(HOME_TEST_DIR, '.ipython'))
138 print(paths._writable_dir)
138 print(paths._writable_dir)
139 with patch_get_home_dir(HOME_TEST_DIR), \
139 with patch_get_home_dir(HOME_TEST_DIR), \
140 patch.object(paths, 'get_xdg_dir', return_value=xdg), \
140 patch.object(paths, 'get_xdg_dir', return_value=xdg), \
141 patch('os.name', 'posix'), \
141 patch('os.name', 'posix'), \
142 modified_env({
142 modified_env({
143 'IPYTHON_DIR': None,
143 'IPYTHON_DIR': None,
144 'IPYTHONDIR': None,
144 'IPYTHONDIR': None,
145 'XDG_CONFIG_HOME': None,
145 'XDG_CONFIG_HOME': None,
146 }), warnings.catch_warnings(record=True) as w:
146 }), warnings.catch_warnings(record=True) as w:
147 ipdir = paths.get_ipython_dir()
147 ipdir = paths.get_ipython_dir()
148
148
149 nt.assert_equal(ipdir, os.path.join(HOME_TEST_DIR, '.ipython'))
149 assert ipdir == os.path.join(HOME_TEST_DIR, ".ipython")
150 nt.assert_equal(len(w), 0)
150 assert len(w) == 0
151
151
152 def test_get_ipython_dir_7():
152 def test_get_ipython_dir_7():
153 """test_get_ipython_dir_7, test home directory expansion on IPYTHONDIR"""
153 """test_get_ipython_dir_7, test home directory expansion on IPYTHONDIR"""
154 home_dir = os.path.normpath(os.path.expanduser('~'))
154 home_dir = os.path.normpath(os.path.expanduser('~'))
155 with modified_env({'IPYTHONDIR': os.path.join('~', 'somewhere')}), \
155 with modified_env({'IPYTHONDIR': os.path.join('~', 'somewhere')}), \
156 patch.object(paths, '_writable_dir', return_value=True):
156 patch.object(paths, '_writable_dir', return_value=True):
157 ipdir = paths.get_ipython_dir()
157 ipdir = paths.get_ipython_dir()
158 nt.assert_equal(ipdir, os.path.join(home_dir, 'somewhere'))
158 assert ipdir == os.path.join(home_dir, "somewhere")
159
159
160
160 @skip_win32
161 @skip_win32
161 def test_get_ipython_dir_8():
162 def test_get_ipython_dir_8():
162 """test_get_ipython_dir_8, test / home directory"""
163 """test_get_ipython_dir_8, test / home directory"""
163 if not os.access("/", os.W_OK):
164 if not os.access("/", os.W_OK):
164 # test only when HOME directory actually writable
165 # test only when HOME directory actually writable
165 return
166 return
166
167
167 with patch.object(paths, '_writable_dir', lambda path: bool(path)), \
168 with patch.object(paths, "_writable_dir", lambda path: bool(path)), patch.object(
168 patch.object(paths, 'get_xdg_dir', return_value=None), \
169 paths, "get_xdg_dir", return_value=None
169 modified_env({
170 ), modified_env(
170 'IPYTHON_DIR': None,
171 {
171 'IPYTHONDIR': None,
172 "IPYTHON_DIR": None,
172 'HOME': '/',
173 "IPYTHONDIR": None,
173 }):
174 "HOME": "/",
174 nt.assert_equal(paths.get_ipython_dir(), '/.ipython')
175 }
176 ):
177 assert paths.get_ipython_dir() == "/.ipython"
175
178
176
179
177 def test_get_ipython_cache_dir():
180 def test_get_ipython_cache_dir():
178 with modified_env({'HOME': HOME_TEST_DIR}):
181 with modified_env({'HOME': HOME_TEST_DIR}):
179 if os.name == 'posix' and sys.platform != 'darwin':
182 if os.name == 'posix' and sys.platform != 'darwin':
180 # test default
183 # test default
181 os.makedirs(os.path.join(HOME_TEST_DIR, ".cache"))
184 os.makedirs(os.path.join(HOME_TEST_DIR, ".cache"))
182 with modified_env({'XDG_CACHE_HOME': None}):
185 with modified_env({'XDG_CACHE_HOME': None}):
183 ipdir = paths.get_ipython_cache_dir()
186 ipdir = paths.get_ipython_cache_dir()
184 nt.assert_equal(os.path.join(HOME_TEST_DIR, ".cache", "ipython"),
187 assert os.path.join(HOME_TEST_DIR, ".cache", "ipython") == ipdir
185 ipdir)
186 assert_isdir(ipdir)
188 assert_isdir(ipdir)
187
189
188 # test env override
190 # test env override
189 with modified_env({"XDG_CACHE_HOME": XDG_CACHE_DIR}):
191 with modified_env({"XDG_CACHE_HOME": XDG_CACHE_DIR}):
190 ipdir = paths.get_ipython_cache_dir()
192 ipdir = paths.get_ipython_cache_dir()
191 assert_isdir(ipdir)
193 assert_isdir(ipdir)
192 nt.assert_equal(ipdir, os.path.join(XDG_CACHE_DIR, "ipython"))
194 assert ipdir == os.path.join(XDG_CACHE_DIR, "ipython")
193 else:
195 else:
194 nt.assert_equal(paths.get_ipython_cache_dir(),
196 assert paths.get_ipython_cache_dir() == paths.get_ipython_dir()
195 paths.get_ipython_dir())
196
197
197 def test_get_ipython_package_dir():
198 def test_get_ipython_package_dir():
198 ipdir = paths.get_ipython_package_dir()
199 ipdir = paths.get_ipython_package_dir()
199 assert_isdir(ipdir)
200 assert_isdir(ipdir)
200
201
201
202
202 def test_get_ipython_module_path():
203 def test_get_ipython_module_path():
203 ipapp_path = paths.get_ipython_module_path('IPython.terminal.ipapp')
204 ipapp_path = paths.get_ipython_module_path('IPython.terminal.ipapp')
204 assert_isfile(ipapp_path)
205 assert_isfile(ipapp_path)
General Comments 0
You need to be logged in to leave comments. Login now