##// END OF EJS Templates
Add AssertPrints context manager to check output from tests.
Thomas Kluyver -
Show More
@@ -1,73 +1,90 b''
1 1 # encoding: utf-8
2 2 """
3 3 Tests for testing.tools
4 4 """
5 5
6 6 #-----------------------------------------------------------------------------
7 7 # Copyright (C) 2008-2009 The IPython Development Team
8 8 #
9 9 # Distributed under the terms of the BSD License. The full license is in
10 10 # the file COPYING, distributed as part of this software.
11 11 #-----------------------------------------------------------------------------
12 12
13 13 #-----------------------------------------------------------------------------
14 14 # Imports
15 15 #-----------------------------------------------------------------------------
16 16 from __future__ import with_statement
17 17
18 18 import os
19 19 import sys
20 import unittest
20 21
21 22 import nose.tools as nt
22 23
23 24 from IPython.testing import decorators as dec
24 25 from IPython.testing import tools as tt
25 26
26 27 #-----------------------------------------------------------------------------
27 28 # Tests
28 29 #-----------------------------------------------------------------------------
29 30
30 31 @dec.skip_win32
31 32 def test_full_path_posix():
32 33 spath = '/foo/bar.py'
33 34 result = tt.full_path(spath,['a.txt','b.txt'])
34 35 nt.assert_equal(result, ['/foo/a.txt', '/foo/b.txt'])
35 36 spath = '/foo'
36 37 result = tt.full_path(spath,['a.txt','b.txt'])
37 38 nt.assert_equal(result, ['/a.txt', '/b.txt'])
38 39 result = tt.full_path(spath,'a.txt')
39 40 nt.assert_equal(result, ['/a.txt'])
40 41
41 42
42 43 @dec.skip_if_not_win32
43 44 def test_full_path_win32():
44 45 spath = 'c:\\foo\\bar.py'
45 46 result = tt.full_path(spath,['a.txt','b.txt'])
46 47 nt.assert_equal(result, ['c:\\foo\\a.txt', 'c:\\foo\\b.txt'])
47 48 spath = 'c:\\foo'
48 49 result = tt.full_path(spath,['a.txt','b.txt'])
49 50 nt.assert_equal(result, ['c:\\a.txt', 'c:\\b.txt'])
50 51 result = tt.full_path(spath,'a.txt')
51 52 nt.assert_equal(result, ['c:\\a.txt'])
52 53
53 54
54 55 @dec.parametric
55 56 def test_parser():
56 57 err = ("FAILED (errors=1)", 1, 0)
57 58 fail = ("FAILED (failures=1)", 0, 1)
58 59 both = ("FAILED (errors=1, failures=1)", 1, 1)
59 60 for txt, nerr, nfail in [err, fail, both]:
60 61 nerr1, nfail1 = tt.parse_test_output(txt)
61 62 yield nt.assert_equal(nerr, nerr1)
62 63 yield nt.assert_equal(nfail, nfail1)
63 64
64 65
65 66 @dec.parametric
66 67 def test_temp_pyfile():
67 68 src = 'pass\n'
68 69 fname, fh = tt.temp_pyfile(src)
69 70 yield nt.assert_true(os.path.isfile(fname))
70 71 fh.close()
71 72 with open(fname) as fh2:
72 73 src2 = fh2.read()
73 74 yield nt.assert_equal(src2, src)
75
76 class TestAssertPrints(unittest.TestCase):
77 def test_passing(self):
78 with tt.AssertPrints("abc"):
79 print "abcd"
80 print "def"
81 print b"ghi"
82
83 def test_failing(self):
84 def func():
85 with tt.AssertPrints("abc"):
86 print "acd"
87 print "def"
88 print b"ghi"
89
90 self.assertRaises(AssertionError, func)
@@ -1,346 +1,388 b''
1 1 """Generic testing tools that do NOT depend on Twisted.
2 2
3 3 In particular, this module exposes a set of top-level assert* functions that
4 4 can be used in place of nose.tools.assert* in method generators (the ones in
5 5 nose can not, at least as of nose 0.10.4).
6 6
7 7 Note: our testing package contains testing.util, which does depend on Twisted
8 8 and provides utilities for tests that manage Deferreds. All testing support
9 9 tools that only depend on nose, IPython or the standard library should go here
10 10 instead.
11 11
12 12
13 13 Authors
14 14 -------
15 15 - Fernando Perez <Fernando.Perez@berkeley.edu>
16 16 """
17 17
18 18 from __future__ import absolute_import
19 19
20 20 #-----------------------------------------------------------------------------
21 21 # Copyright (C) 2009 The IPython Development Team
22 22 #
23 23 # Distributed under the terms of the BSD License. The full license is in
24 24 # the file COPYING, distributed as part of this software.
25 25 #-----------------------------------------------------------------------------
26 26
27 27 #-----------------------------------------------------------------------------
28 28 # Imports
29 29 #-----------------------------------------------------------------------------
30 30
31 31 import os
32 32 import re
33 33 import sys
34 34 import tempfile
35 35
36 36 from contextlib import contextmanager
37 from io import StringIO
37 38
38 39 try:
39 40 # These tools are used by parts of the runtime, so we make the nose
40 41 # dependency optional at this point. Nose is a hard dependency to run the
41 42 # test suite, but NOT to use ipython itself.
42 43 import nose.tools as nt
43 44 has_nose = True
44 45 except ImportError:
45 46 has_nose = False
46 47
47 48 from IPython.config.loader import Config
48 49 from IPython.utils.process import find_cmd, getoutputerror
49 from IPython.utils.text import list_strings
50 from IPython.utils.io import temp_pyfile
51 from IPython.utils.py3compat import PY3
50 from IPython.utils.text import list_strings, getdefaultencoding
51 from IPython.utils.io import temp_pyfile, Tee
52 from IPython.utils import py3compat
52 53
53 54 from . import decorators as dec
54 55 from . import skipdoctest
55 56
56 57 #-----------------------------------------------------------------------------
57 58 # Globals
58 59 #-----------------------------------------------------------------------------
59 60
60 61 # Make a bunch of nose.tools assert wrappers that can be used in test
61 62 # generators. This will expose an assert* function for each one in nose.tools.
62 63
63 64 _tpl = """
64 65 def %(name)s(*a,**kw):
65 66 return nt.%(name)s(*a,**kw)
66 67 """
67 68
68 69 if has_nose:
69 70 for _x in [a for a in dir(nt) if a.startswith('assert')]:
70 71 exec _tpl % dict(name=_x)
71 72
72 73 #-----------------------------------------------------------------------------
73 74 # Functions and classes
74 75 #-----------------------------------------------------------------------------
75 76
76 77 # The docstring for full_path doctests differently on win32 (different path
77 78 # separator) so just skip the doctest there. The example remains informative.
78 79 doctest_deco = skipdoctest.skip_doctest if sys.platform == 'win32' else dec.null_deco
79 80
80 81 @doctest_deco
81 82 def full_path(startPath,files):
82 83 """Make full paths for all the listed files, based on startPath.
83 84
84 85 Only the base part of startPath is kept, since this routine is typically
85 86 used with a script's __file__ variable as startPath. The base of startPath
86 87 is then prepended to all the listed files, forming the output list.
87 88
88 89 Parameters
89 90 ----------
90 91 startPath : string
91 92 Initial path to use as the base for the results. This path is split
92 93 using os.path.split() and only its first component is kept.
93 94
94 95 files : string or list
95 96 One or more files.
96 97
97 98 Examples
98 99 --------
99 100
100 101 >>> full_path('/foo/bar.py',['a.txt','b.txt'])
101 102 ['/foo/a.txt', '/foo/b.txt']
102 103
103 104 >>> full_path('/foo',['a.txt','b.txt'])
104 105 ['/a.txt', '/b.txt']
105 106
106 107 If a single file is given, the output is still a list:
107 108 >>> full_path('/foo','a.txt')
108 109 ['/a.txt']
109 110 """
110 111
111 112 files = list_strings(files)
112 113 base = os.path.split(startPath)[0]
113 114 return [ os.path.join(base,f) for f in files ]
114 115
115 116
116 117 def parse_test_output(txt):
117 118 """Parse the output of a test run and return errors, failures.
118 119
119 120 Parameters
120 121 ----------
121 122 txt : str
122 123 Text output of a test run, assumed to contain a line of one of the
123 124 following forms::
124 125 'FAILED (errors=1)'
125 126 'FAILED (failures=1)'
126 127 'FAILED (errors=1, failures=1)'
127 128
128 129 Returns
129 130 -------
130 131 nerr, nfail: number of errors and failures.
131 132 """
132 133
133 134 err_m = re.search(r'^FAILED \(errors=(\d+)\)', txt, re.MULTILINE)
134 135 if err_m:
135 136 nerr = int(err_m.group(1))
136 137 nfail = 0
137 138 return nerr, nfail
138 139
139 140 fail_m = re.search(r'^FAILED \(failures=(\d+)\)', txt, re.MULTILINE)
140 141 if fail_m:
141 142 nerr = 0
142 143 nfail = int(fail_m.group(1))
143 144 return nerr, nfail
144 145
145 146 both_m = re.search(r'^FAILED \(errors=(\d+), failures=(\d+)\)', txt,
146 147 re.MULTILINE)
147 148 if both_m:
148 149 nerr = int(both_m.group(1))
149 150 nfail = int(both_m.group(2))
150 151 return nerr, nfail
151 152
152 153 # If the input didn't match any of these forms, assume no error/failures
153 154 return 0, 0
154 155
155 156
156 157 # So nose doesn't think this is a test
157 158 parse_test_output.__test__ = False
158 159
159 160
160 161 def default_argv():
161 162 """Return a valid default argv for creating testing instances of ipython"""
162 163
163 164 return ['--quick', # so no config file is loaded
164 165 # Other defaults to minimize side effects on stdout
165 166 '--colors=NoColor', '--no-term-title','--no-banner',
166 167 '--autocall=0']
167 168
168 169
169 170 def default_config():
170 171 """Return a config object with good defaults for testing."""
171 172 config = Config()
172 173 config.TerminalInteractiveShell.colors = 'NoColor'
173 174 config.TerminalTerminalInteractiveShell.term_title = False,
174 175 config.TerminalInteractiveShell.autocall = 0
175 176 config.HistoryManager.hist_file = tempfile.mktemp(u'test_hist.sqlite')
176 177 config.HistoryManager.db_cache_size = 10000
177 178 return config
178 179
179 180
180 181 def ipexec(fname, options=None):
181 182 """Utility to call 'ipython filename'.
182 183
183 184 Starts IPython witha minimal and safe configuration to make startup as fast
184 185 as possible.
185 186
186 187 Note that this starts IPython in a subprocess!
187 188
188 189 Parameters
189 190 ----------
190 191 fname : str
191 192 Name of file to be executed (should have .py or .ipy extension).
192 193
193 194 options : optional, list
194 195 Extra command-line flags to be passed to IPython.
195 196
196 197 Returns
197 198 -------
198 199 (stdout, stderr) of ipython subprocess.
199 200 """
200 201 if options is None: options = []
201 202
202 203 # For these subprocess calls, eliminate all prompt printing so we only see
203 204 # output from script execution
204 205 prompt_opts = [ '--InteractiveShell.prompt_in1=""',
205 206 '--InteractiveShell.prompt_in2=""',
206 207 '--InteractiveShell.prompt_out=""'
207 208 ]
208 209 cmdargs = ' '.join(default_argv() + prompt_opts + options)
209 210
210 211 _ip = get_ipython()
211 212 test_dir = os.path.dirname(__file__)
212 213
213 ipython_cmd = find_cmd('ipython3' if PY3 else 'ipython')
214 ipython_cmd = find_cmd('ipython3' if py3compat.PY3 else 'ipython')
214 215 # Absolute path for filename
215 216 full_fname = os.path.join(test_dir, fname)
216 217 full_cmd = '%s %s %s' % (ipython_cmd, cmdargs, full_fname)
217 218 #print >> sys.stderr, 'FULL CMD:', full_cmd # dbg
218 219 out = getoutputerror(full_cmd)
219 220 # `import readline` causes 'ESC[?1034h' to be the first output sometimes,
220 221 # so strip that off the front of the first line if it is found
221 222 if out:
222 223 first = out[0]
223 224 m = re.match(r'\x1b\[[^h]+h', first)
224 225 if m:
225 226 # strip initial readline escape
226 227 out = list(out)
227 228 out[0] = first[len(m.group()):]
228 229 out = tuple(out)
229 230 return out
230 231
231 232
232 233 def ipexec_validate(fname, expected_out, expected_err='',
233 234 options=None):
234 235 """Utility to call 'ipython filename' and validate output/error.
235 236
236 237 This function raises an AssertionError if the validation fails.
237 238
238 239 Note that this starts IPython in a subprocess!
239 240
240 241 Parameters
241 242 ----------
242 243 fname : str
243 244 Name of the file to be executed (should have .py or .ipy extension).
244 245
245 246 expected_out : str
246 247 Expected stdout of the process.
247 248
248 249 expected_err : optional, str
249 250 Expected stderr of the process.
250 251
251 252 options : optional, list
252 253 Extra command-line flags to be passed to IPython.
253 254
254 255 Returns
255 256 -------
256 257 None
257 258 """
258 259
259 260 import nose.tools as nt
260 261
261 262 out, err = ipexec(fname)
262 263 #print 'OUT', out # dbg
263 264 #print 'ERR', err # dbg
264 265 # If there are any errors, we must check those befor stdout, as they may be
265 266 # more informative than simply having an empty stdout.
266 267 if err:
267 268 if expected_err:
268 269 nt.assert_equals(err.strip(), expected_err.strip())
269 270 else:
270 271 raise ValueError('Running file %r produced error: %r' %
271 272 (fname, err))
272 273 # If no errors or output on stderr was expected, match stdout
273 274 nt.assert_equals(out.strip(), expected_out.strip())
274 275
275 276
276 277 class TempFileMixin(object):
277 278 """Utility class to create temporary Python/IPython files.
278 279
279 280 Meant as a mixin class for test cases."""
280 281
281 282 def mktmp(self, src, ext='.py'):
282 283 """Make a valid python temp file."""
283 284 fname, f = temp_pyfile(src, ext)
284 285 self.tmpfile = f
285 286 self.fname = fname
286 287
287 288 def tearDown(self):
288 289 if hasattr(self, 'tmpfile'):
289 290 # If the tmpfile wasn't made because of skipped tests, like in
290 291 # win32, there's nothing to cleanup.
291 292 self.tmpfile.close()
292 293 try:
293 294 os.unlink(self.fname)
294 295 except:
295 296 # On Windows, even though we close the file, we still can't
296 297 # delete it. I have no clue why
297 298 pass
298 299
299 300 pair_fail_msg = ("Testing {0}\n\n"
300 301 "In:\n"
301 302 " {1!r}\n"
302 303 "Expected:\n"
303 304 " {2!r}\n"
304 305 "Got:\n"
305 306 " {3!r}\n")
306 307 def check_pairs(func, pairs):
307 308 """Utility function for the common case of checking a function with a
308 309 sequence of input/output pairs.
309 310
310 311 Parameters
311 312 ----------
312 313 func : callable
313 314 The function to be tested. Should accept a single argument.
314 315 pairs : iterable
315 316 A list of (input, expected_output) tuples.
316 317
317 318 Returns
318 319 -------
319 320 None. Raises an AssertionError if any output does not match the expected
320 321 value.
321 322 """
322 323 name = getattr(func, "func_name", getattr(func, "__name__", "<unknown>"))
323 324 for inp, expected in pairs:
324 325 out = func(inp)
325 326 assert out == expected, pair_fail_msg.format(name, inp, expected, out)
326 327
328 if py3compat.PY3:
329 MyStringIO = StringIO
330 else:
331 # In Python 2, stdout/stderr can have either bytes or unicode written to them,
332 # so we need a class that can handle both.
333 class MyStringIO(StringIO):
334 def write(self, s):
335 s = py3compat.cast_unicode(s, encoding=getdefaultencoding())
336 super(MyStringIO, self).write(s)
337
338 notprinted_msg = """Did not find {0!r} in printed output (on {1}):
339 {2!r}"""
340 class AssertPrints(object):
341 """Context manager for testing that code prints certain text.
342
343 Examples
344 --------
345 >>> with AssertPrints("abc"):
346 ... print "abcd"
347 ... print "def"
348 ...
349 abcd
350 def
351 """
352 def __init__(self, s, channel='stdout'):
353 self.s = s
354 self.channel = channel
355
356 def __enter__(self):
357 self.orig_stream = getattr(sys, self.channel)
358 self.buffer = MyStringIO()
359 self.tee = Tee(self.buffer, channel=self.channel)
360 setattr(sys, self.channel, self.tee)
361
362 def __exit__(self, etype, value, traceback):
363 self.tee.flush()
364 setattr(sys, self.channel, self.orig_stream)
365 printed = self.buffer.getvalue()
366 assert self.s in printed, notprinted_msg.format(self.s, self.channel, printed)
367 return False
368
327 369 @contextmanager
328 370 def mute_warn():
329 371 from IPython.utils import warn
330 372 save_warn = warn.warn
331 373 warn.warn = lambda *a, **kw: None
332 374 try:
333 375 yield
334 376 finally:
335 377 warn.warn = save_warn
336 378
337 379 @contextmanager
338 380 def make_tempfile(name):
339 381 """ Create an empty, named, temporary file for the duration of the context.
340 382 """
341 383 f = open(name, 'w')
342 384 f.close()
343 385 try:
344 386 yield
345 387 finally:
346 388 os.unlink(name)
@@ -1,450 +1,445 b''
1 1 # encoding: utf-8
2 2 """Tests for IPython.utils.path.py"""
3 3
4 4 #-----------------------------------------------------------------------------
5 5 # Copyright (C) 2008 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 from __future__ import with_statement
16 16
17 17 import os
18 18 import shutil
19 19 import sys
20 20 import tempfile
21 21 from io import StringIO
22 22
23 23 from os.path import join, abspath, split
24 24
25 25 import nose.tools as nt
26 26
27 27 from nose import with_setup
28 28
29 29 import IPython
30 30 from IPython.testing import decorators as dec
31 31 from IPython.testing.decorators import skip_if_not_win32, skip_win32
32 from IPython.testing.tools import make_tempfile
32 from IPython.testing.tools import make_tempfile, AssertPrints
33 33 from IPython.utils import path, io
34 34 from IPython.utils import py3compat
35 35
36 36 # Platform-dependent imports
37 37 try:
38 38 import _winreg as wreg
39 39 except ImportError:
40 40 #Fake _winreg module on none windows platforms
41 41 import types
42 42 wr_name = "winreg" if py3compat.PY3 else "_winreg"
43 43 sys.modules[wr_name] = types.ModuleType(wr_name)
44 44 import _winreg as wreg
45 45 #Add entries that needs to be stubbed by the testing code
46 46 (wreg.OpenKey, wreg.QueryValueEx,) = (None, None)
47 47
48 48 try:
49 49 reload
50 50 except NameError: # Python 3
51 51 from imp import reload
52 52
53 53 #-----------------------------------------------------------------------------
54 54 # Globals
55 55 #-----------------------------------------------------------------------------
56 56 env = os.environ
57 57 TEST_FILE_PATH = split(abspath(__file__))[0]
58 58 TMP_TEST_DIR = tempfile.mkdtemp()
59 59 HOME_TEST_DIR = join(TMP_TEST_DIR, "home_test_dir")
60 60 XDG_TEST_DIR = join(HOME_TEST_DIR, "xdg_test_dir")
61 61 IP_TEST_DIR = join(HOME_TEST_DIR,'.ipython')
62 62 #
63 63 # Setup/teardown functions/decorators
64 64 #
65 65
66 66 def setup():
67 67 """Setup testenvironment for the module:
68 68
69 69 - Adds dummy home dir tree
70 70 """
71 71 # Do not mask exceptions here. In particular, catching WindowsError is a
72 72 # problem because that exception is only defined on Windows...
73 73 os.makedirs(IP_TEST_DIR)
74 74 os.makedirs(os.path.join(XDG_TEST_DIR, 'ipython'))
75 75
76 76
77 77 def teardown():
78 78 """Teardown testenvironment for the module:
79 79
80 80 - Remove dummy home dir tree
81 81 """
82 82 # Note: we remove the parent test dir, which is the root of all test
83 83 # subdirs we may have created. Use shutil instead of os.removedirs, so
84 84 # that non-empty directories are all recursively removed.
85 85 shutil.rmtree(TMP_TEST_DIR)
86 86
87 87
88 88 def setup_environment():
89 89 """Setup testenvironment for some functions that are tested
90 90 in this module. In particular this functions stores attributes
91 91 and other things that we need to stub in some test functions.
92 92 This needs to be done on a function level and not module level because
93 93 each testfunction needs a pristine environment.
94 94 """
95 95 global oldstuff, platformstuff
96 96 oldstuff = (env.copy(), os.name, path.get_home_dir, IPython.__file__, os.getcwd())
97 97
98 98 if os.name == 'nt':
99 99 platformstuff = (wreg.OpenKey, wreg.QueryValueEx,)
100 100
101 101
102 102 def teardown_environment():
103 103 """Restore things that were remebered by the setup_environment function
104 104 """
105 105 (oldenv, os.name, path.get_home_dir, IPython.__file__, old_wd) = oldstuff
106 106 os.chdir(old_wd)
107 107 reload(path)
108 108
109 109 for key in env.keys():
110 110 if key not in oldenv:
111 111 del env[key]
112 112 env.update(oldenv)
113 113 if hasattr(sys, 'frozen'):
114 114 del sys.frozen
115 115 if os.name == 'nt':
116 116 (wreg.OpenKey, wreg.QueryValueEx,) = platformstuff
117 117
118 118 # Build decorator that uses the setup_environment/setup_environment
119 119 with_environment = with_setup(setup_environment, teardown_environment)
120 120
121 121
122 122 @skip_if_not_win32
123 123 @with_environment
124 124 def test_get_home_dir_1():
125 125 """Testcase for py2exe logic, un-compressed lib
126 126 """
127 127 sys.frozen = True
128 128
129 129 #fake filename for IPython.__init__
130 130 IPython.__file__ = abspath(join(HOME_TEST_DIR, "Lib/IPython/__init__.py"))
131 131
132 132 home_dir = path.get_home_dir()
133 133 nt.assert_equal(home_dir, abspath(HOME_TEST_DIR))
134 134
135 135
136 136 @skip_if_not_win32
137 137 @with_environment
138 138 def test_get_home_dir_2():
139 139 """Testcase for py2exe logic, compressed lib
140 140 """
141 141 sys.frozen = True
142 142 #fake filename for IPython.__init__
143 143 IPython.__file__ = abspath(join(HOME_TEST_DIR, "Library.zip/IPython/__init__.py")).lower()
144 144
145 145 home_dir = path.get_home_dir()
146 146 nt.assert_equal(home_dir, abspath(HOME_TEST_DIR).lower())
147 147
148 148
149 149 @with_environment
150 150 @skip_win32
151 151 def test_get_home_dir_3():
152 152 """Testcase $HOME is set, then use its value as home directory."""
153 153 env["HOME"] = HOME_TEST_DIR
154 154 home_dir = path.get_home_dir()
155 155 nt.assert_equal(home_dir, env["HOME"])
156 156
157 157
158 158 @with_environment
159 159 @skip_win32
160 160 def test_get_home_dir_4():
161 161 """Testcase $HOME is not set, os=='posix'.
162 162 This should fail with HomeDirError"""
163 163
164 164 os.name = 'posix'
165 165 if 'HOME' in env: del env['HOME']
166 166 nt.assert_raises(path.HomeDirError, path.get_home_dir)
167 167
168 168
169 169 @skip_if_not_win32
170 170 @with_environment
171 171 def test_get_home_dir_5():
172 172 """Using HOMEDRIVE + HOMEPATH, os=='nt'.
173 173
174 174 HOMESHARE is missing.
175 175 """
176 176
177 177 os.name = 'nt'
178 178 env.pop('HOMESHARE', None)
179 179 env['HOMEDRIVE'], env['HOMEPATH'] = os.path.splitdrive(HOME_TEST_DIR)
180 180 home_dir = path.get_home_dir()
181 181 nt.assert_equal(home_dir, abspath(HOME_TEST_DIR))
182 182
183 183
184 184 @skip_if_not_win32
185 185 @with_environment
186 186 def test_get_home_dir_6():
187 187 """Using USERPROFILE, os=='nt'.
188 188
189 189 HOMESHARE, HOMEDRIVE, HOMEPATH are missing.
190 190 """
191 191
192 192 os.name = 'nt'
193 193 env.pop('HOMESHARE', None)
194 194 env.pop('HOMEDRIVE', None)
195 195 env.pop('HOMEPATH', None)
196 196 env["USERPROFILE"] = abspath(HOME_TEST_DIR)
197 197 home_dir = path.get_home_dir()
198 198 nt.assert_equal(home_dir, abspath(HOME_TEST_DIR))
199 199
200 200
201 201 @skip_if_not_win32
202 202 @with_environment
203 203 def test_get_home_dir_7():
204 204 """Using HOMESHARE, os=='nt'."""
205 205
206 206 os.name = 'nt'
207 207 env["HOMESHARE"] = abspath(HOME_TEST_DIR)
208 208 home_dir = path.get_home_dir()
209 209 nt.assert_equal(home_dir, abspath(HOME_TEST_DIR))
210 210
211 211
212 212 # Should we stub wreg fully so we can run the test on all platforms?
213 213 @skip_if_not_win32
214 214 @with_environment
215 215 def test_get_home_dir_8():
216 216 """Using registry hack for 'My Documents', os=='nt'
217 217
218 218 HOMESHARE, HOMEDRIVE, HOMEPATH, USERPROFILE and others are missing.
219 219 """
220 220 os.name = 'nt'
221 221 # Remove from stub environment all keys that may be set
222 222 for key in ['HOME', 'HOMESHARE', 'HOMEDRIVE', 'HOMEPATH', 'USERPROFILE']:
223 223 env.pop(key, None)
224 224
225 225 #Stub windows registry functions
226 226 def OpenKey(x, y):
227 227 class key:
228 228 def Close(self):
229 229 pass
230 230 return key()
231 231 def QueryValueEx(x, y):
232 232 return [abspath(HOME_TEST_DIR)]
233 233
234 234 wreg.OpenKey = OpenKey
235 235 wreg.QueryValueEx = QueryValueEx
236 236
237 237 home_dir = path.get_home_dir()
238 238 nt.assert_equal(home_dir, abspath(HOME_TEST_DIR))
239 239
240 240
241 241 @with_environment
242 242 def test_get_ipython_dir_1():
243 243 """test_get_ipython_dir_1, Testcase to see if we can call get_ipython_dir without Exceptions."""
244 244 env_ipdir = os.path.join("someplace", ".ipython")
245 245 path._writable_dir = lambda path: True
246 246 env['IPYTHON_DIR'] = env_ipdir
247 247 ipdir = path.get_ipython_dir()
248 248 nt.assert_equal(ipdir, env_ipdir)
249 249
250 250
251 251 @with_environment
252 252 def test_get_ipython_dir_2():
253 253 """test_get_ipython_dir_2, Testcase to see if we can call get_ipython_dir without Exceptions."""
254 254 path.get_home_dir = lambda : "someplace"
255 255 path.get_xdg_dir = lambda : None
256 256 path._writable_dir = lambda path: True
257 257 os.name = "posix"
258 258 env.pop('IPYTHON_DIR', None)
259 259 env.pop('IPYTHONDIR', None)
260 260 env.pop('XDG_CONFIG_HOME', None)
261 261 ipdir = path.get_ipython_dir()
262 262 nt.assert_equal(ipdir, os.path.join("someplace", ".ipython"))
263 263
264 264 @with_environment
265 265 def test_get_ipython_dir_3():
266 266 """test_get_ipython_dir_3, use XDG if defined, and .ipython doesn't exist."""
267 267 path.get_home_dir = lambda : "someplace"
268 268 path._writable_dir = lambda path: True
269 269 os.name = "posix"
270 270 env.pop('IPYTHON_DIR', None)
271 271 env.pop('IPYTHONDIR', None)
272 272 env['XDG_CONFIG_HOME'] = XDG_TEST_DIR
273 273 ipdir = path.get_ipython_dir()
274 274 nt.assert_equal(ipdir, os.path.join(XDG_TEST_DIR, "ipython"))
275 275
276 276 @with_environment
277 277 def test_get_ipython_dir_4():
278 278 """test_get_ipython_dir_4, use XDG if both exist."""
279 279 path.get_home_dir = lambda : HOME_TEST_DIR
280 280 os.name = "posix"
281 281 env.pop('IPYTHON_DIR', None)
282 282 env.pop('IPYTHONDIR', None)
283 283 env['XDG_CONFIG_HOME'] = XDG_TEST_DIR
284 284 xdg_ipdir = os.path.join(XDG_TEST_DIR, "ipython")
285 285 ipdir = path.get_ipython_dir()
286 286 nt.assert_equal(ipdir, xdg_ipdir)
287 287
288 288 @with_environment
289 289 def test_get_ipython_dir_5():
290 290 """test_get_ipython_dir_5, use .ipython if exists and XDG defined, but doesn't exist."""
291 291 path.get_home_dir = lambda : HOME_TEST_DIR
292 292 os.name = "posix"
293 293 env.pop('IPYTHON_DIR', None)
294 294 env.pop('IPYTHONDIR', None)
295 295 env['XDG_CONFIG_HOME'] = XDG_TEST_DIR
296 296 os.rmdir(os.path.join(XDG_TEST_DIR, 'ipython'))
297 297 ipdir = path.get_ipython_dir()
298 298 nt.assert_equal(ipdir, IP_TEST_DIR)
299 299
300 300 @with_environment
301 301 def test_get_ipython_dir_6():
302 302 """test_get_ipython_dir_6, use XDG if defined and neither exist."""
303 303 xdg = os.path.join(HOME_TEST_DIR, 'somexdg')
304 304 os.mkdir(xdg)
305 305 shutil.rmtree(os.path.join(HOME_TEST_DIR, '.ipython'))
306 306 path.get_home_dir = lambda : HOME_TEST_DIR
307 307 path.get_xdg_dir = lambda : xdg
308 308 os.name = "posix"
309 309 env.pop('IPYTHON_DIR', None)
310 310 env.pop('IPYTHONDIR', None)
311 311 env.pop('XDG_CONFIG_HOME', None)
312 312 xdg_ipdir = os.path.join(xdg, "ipython")
313 313 ipdir = path.get_ipython_dir()
314 314 nt.assert_equal(ipdir, xdg_ipdir)
315 315
316 316 @with_environment
317 317 def test_get_ipython_dir_7():
318 318 """test_get_ipython_dir_7, test home directory expansion on IPYTHON_DIR"""
319 319 path._writable_dir = lambda path: True
320 320 home_dir = os.path.expanduser('~')
321 321 env['IPYTHON_DIR'] = os.path.join('~', 'somewhere')
322 322 ipdir = path.get_ipython_dir()
323 323 nt.assert_equal(ipdir, os.path.join(home_dir, 'somewhere'))
324 324
325 325
326 326 @with_environment
327 327 def test_get_xdg_dir_1():
328 328 """test_get_xdg_dir_1, check xdg_dir"""
329 329 reload(path)
330 330 path._writable_dir = lambda path: True
331 331 path.get_home_dir = lambda : 'somewhere'
332 332 os.name = "posix"
333 333 env.pop('IPYTHON_DIR', None)
334 334 env.pop('IPYTHONDIR', None)
335 335 env.pop('XDG_CONFIG_HOME', None)
336 336
337 337 nt.assert_equal(path.get_xdg_dir(), os.path.join('somewhere', '.config'))
338 338
339 339
340 340 @with_environment
341 341 def test_get_xdg_dir_1():
342 342 """test_get_xdg_dir_1, check nonexistant xdg_dir"""
343 343 reload(path)
344 344 path.get_home_dir = lambda : HOME_TEST_DIR
345 345 os.name = "posix"
346 346 env.pop('IPYTHON_DIR', None)
347 347 env.pop('IPYTHONDIR', None)
348 348 env.pop('XDG_CONFIG_HOME', None)
349 349 nt.assert_equal(path.get_xdg_dir(), None)
350 350
351 351 @with_environment
352 352 def test_get_xdg_dir_2():
353 353 """test_get_xdg_dir_2, check xdg_dir default to ~/.config"""
354 354 reload(path)
355 355 path.get_home_dir = lambda : HOME_TEST_DIR
356 356 os.name = "posix"
357 357 env.pop('IPYTHON_DIR', None)
358 358 env.pop('IPYTHONDIR', None)
359 359 env.pop('XDG_CONFIG_HOME', None)
360 360 cfgdir=os.path.join(path.get_home_dir(), '.config')
361 361 os.makedirs(cfgdir)
362 362
363 363 nt.assert_equal(path.get_xdg_dir(), cfgdir)
364 364
365 365 def test_filefind():
366 366 """Various tests for filefind"""
367 367 f = tempfile.NamedTemporaryFile()
368 368 # print 'fname:',f.name
369 369 alt_dirs = path.get_ipython_dir()
370 370 t = path.filefind(f.name, alt_dirs)
371 371 # print 'found:',t
372 372
373 373
374 374 def test_get_ipython_package_dir():
375 375 ipdir = path.get_ipython_package_dir()
376 376 nt.assert_true(os.path.isdir(ipdir))
377 377
378 378
379 379 def test_get_ipython_module_path():
380 380 ipapp_path = path.get_ipython_module_path('IPython.frontend.terminal.ipapp')
381 381 nt.assert_true(os.path.isfile(ipapp_path))
382 382
383 383
384 384 @dec.skip_if_not_win32
385 385 def test_get_long_path_name_win32():
386 386 p = path.get_long_path_name('c:\\docume~1')
387 387 nt.assert_equals(p,u'c:\\Documents and Settings')
388 388
389 389
390 390 @dec.skip_win32
391 391 def test_get_long_path_name():
392 392 p = path.get_long_path_name('/usr/local')
393 393 nt.assert_equals(p,'/usr/local')
394 394
395 395 @dec.skip_win32 # can't create not-user-writable dir on win
396 396 @with_environment
397 397 def test_not_writable_ipdir():
398 398 tmpdir = tempfile.mkdtemp()
399 399 os.name = "posix"
400 400 env.pop('IPYTHON_DIR', None)
401 401 env.pop('IPYTHONDIR', None)
402 402 env.pop('XDG_CONFIG_HOME', None)
403 403 env['HOME'] = tmpdir
404 404 ipdir = os.path.join(tmpdir, '.ipython')
405 405 os.mkdir(ipdir)
406 406 os.chmod(ipdir, 600)
407 stderr = io.stderr
408 pipe = StringIO()
409 io.stderr = pipe
410 ipdir = path.get_ipython_dir()
411 io.stderr.flush()
412 io.stderr = stderr
413 nt.assert_true('WARNING' in pipe.getvalue())
407 with AssertPrints('WARNING', channel='stderr'):
408 ipdir = path.get_ipython_dir()
414 409 env.pop('IPYTHON_DIR', None)
415 410
416 411 def test_unquote_filename():
417 412 for win32 in (True, False):
418 413 nt.assert_equals(path.unquote_filename('foo.py', win32=win32), 'foo.py')
419 414 nt.assert_equals(path.unquote_filename('foo bar.py', win32=win32), 'foo bar.py')
420 415 nt.assert_equals(path.unquote_filename('"foo.py"', win32=True), 'foo.py')
421 416 nt.assert_equals(path.unquote_filename('"foo bar.py"', win32=True), 'foo bar.py')
422 417 nt.assert_equals(path.unquote_filename("'foo.py'", win32=True), 'foo.py')
423 418 nt.assert_equals(path.unquote_filename("'foo bar.py'", win32=True), 'foo bar.py')
424 419 nt.assert_equals(path.unquote_filename('"foo.py"', win32=False), '"foo.py"')
425 420 nt.assert_equals(path.unquote_filename('"foo bar.py"', win32=False), '"foo bar.py"')
426 421 nt.assert_equals(path.unquote_filename("'foo.py'", win32=False), "'foo.py'")
427 422 nt.assert_equals(path.unquote_filename("'foo bar.py'", win32=False), "'foo bar.py'")
428 423
429 424 @with_environment
430 425 def test_get_py_filename():
431 426 os.chdir(TMP_TEST_DIR)
432 427 for win32 in (True, False):
433 428 with make_tempfile('foo.py'):
434 429 nt.assert_equals(path.get_py_filename('foo.py', force_win32=win32), 'foo.py')
435 430 nt.assert_equals(path.get_py_filename('foo', force_win32=win32), 'foo.py')
436 431 with make_tempfile('foo'):
437 432 nt.assert_equals(path.get_py_filename('foo', force_win32=win32), 'foo')
438 433 nt.assert_raises(IOError, path.get_py_filename, 'foo.py', force_win32=win32)
439 434 nt.assert_raises(IOError, path.get_py_filename, 'foo', force_win32=win32)
440 435 nt.assert_raises(IOError, path.get_py_filename, 'foo.py', force_win32=win32)
441 436 true_fn = 'foo with spaces.py'
442 437 with make_tempfile(true_fn):
443 438 nt.assert_equals(path.get_py_filename('foo with spaces', force_win32=win32), true_fn)
444 439 nt.assert_equals(path.get_py_filename('foo with spaces.py', force_win32=win32), true_fn)
445 440 if win32:
446 441 nt.assert_equals(path.get_py_filename('"foo with spaces.py"', force_win32=True), true_fn)
447 442 nt.assert_equals(path.get_py_filename("'foo with spaces.py'", force_win32=True), true_fn)
448 443 else:
449 444 nt.assert_raises(IOError, path.get_py_filename, '"foo with spaces.py"', force_win32=False)
450 445 nt.assert_raises(IOError, path.get_py_filename, "'foo with spaces.py'", force_win32=False)
General Comments 0
You need to be logged in to leave comments. Login now