test_path.py
560 lines
| 19.2 KiB
| text/x-python
|
PythonLexer
|
r1617 | # encoding: utf-8 | ||
|
r2498 | """Tests for IPython.utils.path.py""" | ||
|
r1617 | |||
#----------------------------------------------------------------------------- | ||||
|
r5390 | # Copyright (C) 2008-2011 The IPython Development Team | ||
|
r1617 | # | ||
# Distributed under the terms of the BSD License. The full license is in | ||||
# the file COPYING, distributed as part of this software. | ||||
#----------------------------------------------------------------------------- | ||||
#----------------------------------------------------------------------------- | ||||
# Imports | ||||
#----------------------------------------------------------------------------- | ||||
|
r4688 | from __future__ import with_statement | ||
|
r1843 | import os | ||
import shutil | ||||
import sys | ||||
import tempfile | ||||
|
r4891 | from io import StringIO | ||
|
r8638 | from contextlib import contextmanager | ||
|
r1751 | |||
|
r1801 | from os.path import join, abspath, split | ||
|
r1843 | |||
|
r1802 | import nose.tools as nt | ||
|
r1617 | |||
|
r1843 | from nose import with_setup | ||
|
r1617 | |||
|
r1843 | import IPython | ||
|
r2436 | from IPython.testing import decorators as dec | ||
|
r2513 | from IPython.testing.decorators import skip_if_not_win32, skip_win32 | ||
|
r4901 | from IPython.testing.tools import make_tempfile, AssertPrints | ||
|
r4475 | from IPython.utils import path, io | ||
|
r4764 | from IPython.utils import py3compat | ||
|
r8014 | from IPython.utils.tempdir import TemporaryDirectory | ||
|
r1843 | |||
# Platform-dependent imports | ||||
|
r1750 | try: | ||
import _winreg as wreg | ||||
except ImportError: | ||||
|
r1793 | #Fake _winreg module on none windows platforms | ||
|
r4764 | import types | ||
wr_name = "winreg" if py3compat.PY3 else "_winreg" | ||||
sys.modules[wr_name] = types.ModuleType(wr_name) | ||||
|
r1793 | import _winreg as wreg | ||
#Add entries that needs to be stubbed by the testing code | ||||
(wreg.OpenKey, wreg.QueryValueEx,) = (None, None) | ||||
|
r4872 | |||
|
r4764 | try: | ||
reload | ||||
except NameError: # Python 3 | ||||
from imp import reload | ||||
|
r1750 | |||
|
r1843 | #----------------------------------------------------------------------------- | ||
# Globals | ||||
#----------------------------------------------------------------------------- | ||||
env = os.environ | ||||
TEST_FILE_PATH = split(abspath(__file__))[0] | ||||
TMP_TEST_DIR = tempfile.mkdtemp() | ||||
HOME_TEST_DIR = join(TMP_TEST_DIR, "home_test_dir") | ||||
|
r3347 | XDG_TEST_DIR = join(HOME_TEST_DIR, "xdg_test_dir") | ||
|
r10230 | XDG_CACHE_DIR = join(HOME_TEST_DIR, "xdg_cache_dir") | ||
|
r2449 | IP_TEST_DIR = join(HOME_TEST_DIR,'.ipython') | ||
|
r1804 | # | ||
# Setup/teardown functions/decorators | ||||
|
r1803 | # | ||
def setup(): | ||||
|
r1805 | """Setup testenvironment for the module: | ||
|
r4872 | |||
|
r1805 | - Adds dummy home dir tree | ||
""" | ||||
|
r1843 | # Do not mask exceptions here. In particular, catching WindowsError is a | ||
# problem because that exception is only defined on Windows... | ||||
os.makedirs(IP_TEST_DIR) | ||||
|
r3347 | os.makedirs(os.path.join(XDG_TEST_DIR, 'ipython')) | ||
|
r10230 | os.makedirs(os.path.join(XDG_CACHE_DIR, 'ipython')) | ||
|
r1803 | |||
|
r2505 | |||
|
r1803 | def teardown(): | ||
|
r1805 | """Teardown testenvironment for the module: | ||
|
r4872 | |||
|
r1805 | - Remove dummy home dir tree | ||
""" | ||||
|
r1843 | # Note: we remove the parent test dir, which is the root of all test | ||
# subdirs we may have created. Use shutil instead of os.removedirs, so | ||||
# that non-empty directories are all recursively removed. | ||||
shutil.rmtree(TMP_TEST_DIR) | ||||
|
r1803 | |||
|
r2505 | |||
|
r1750 | def setup_environment(): | ||
|
r4872 | """Setup testenvironment for some functions that are tested | ||
|
r1805 | in this module. In particular this functions stores attributes | ||
and other things that we need to stub in some test functions. | ||||
|
r4872 | This needs to be done on a function level and not module level because | ||
|
r1805 | each testfunction needs a pristine environment. | ||
""" | ||||
|
r1750 | global oldstuff, platformstuff | ||
|
r7086 | oldstuff = (env.copy(), os.name, sys.platform, path.get_home_dir, IPython.__file__, os.getcwd()) | ||
|
r1750 | |||
|
r1796 | if os.name == 'nt': | ||
platformstuff = (wreg.OpenKey, wreg.QueryValueEx,) | ||||
|
r1750 | |||
|
r2505 | |||
|
r1750 | def teardown_environment(): | ||
|
r1805 | """Restore things that were remebered by the setup_environment function | ||
""" | ||||
|
r7086 | (oldenv, os.name, sys.platform, path.get_home_dir, IPython.__file__, old_wd) = oldstuff | ||
|
r4688 | os.chdir(old_wd) | ||
|
r4475 | reload(path) | ||
|
r4872 | |||
|
r1750 | for key in env.keys(): | ||
if key not in oldenv: | ||||
del env[key] | ||||
env.update(oldenv) | ||||
if hasattr(sys, 'frozen'): | ||||
del sys.frozen | ||||
|
r1796 | if os.name == 'nt': | ||
(wreg.OpenKey, wreg.QueryValueEx,) = platformstuff | ||||
|
r1750 | |||
|
r1805 | # Build decorator that uses the setup_environment/setup_environment | ||
|
r2436 | with_environment = with_setup(setup_environment, teardown_environment) | ||
|
r1750 | |||
|
r1820 | @skip_if_not_win32 | ||
|
r2436 | @with_environment | ||
|
r1745 | def test_get_home_dir_1(): | ||
|
r1746 | """Testcase for py2exe logic, un-compressed lib | ||
""" | ||||
|
r1796 | sys.frozen = True | ||
|
r4872 | |||
|
r1746 | #fake filename for IPython.__init__ | ||
|
r1855 | IPython.__file__ = abspath(join(HOME_TEST_DIR, "Lib/IPython/__init__.py")) | ||
|
r4872 | |||
|
r2513 | home_dir = path.get_home_dir() | ||
|
r1843 | nt.assert_equal(home_dir, abspath(HOME_TEST_DIR)) | ||
|
r2505 | |||
|
r1812 | @skip_if_not_win32 | ||
|
r2436 | @with_environment | ||
|
r1800 | def test_get_home_dir_2(): | ||
|
r1746 | """Testcase for py2exe logic, compressed lib | ||
""" | ||||
|
r1801 | sys.frozen = True | ||
|
r1746 | #fake filename for IPython.__init__ | ||
|
r1855 | IPython.__file__ = abspath(join(HOME_TEST_DIR, "Library.zip/IPython/__init__.py")).lower() | ||
|
r4872 | |||
|
r5384 | home_dir = path.get_home_dir(True) | ||
|
r1843 | nt.assert_equal(home_dir, abspath(HOME_TEST_DIR).lower()) | ||
|
r1746 | |||
|
r2505 | |||
|
r2436 | @with_environment | ||
|
r1800 | def test_get_home_dir_3(): | ||
|
r5383 | """get_home_dir() uses $HOME if set""" | ||
|
r1843 | env["HOME"] = HOME_TEST_DIR | ||
|
r5384 | home_dir = path.get_home_dir(True) | ||
|
r6123 | # get_home_dir expands symlinks | ||
nt.assert_equal(home_dir, os.path.realpath(env["HOME"])) | ||||
|
r1745 | |||
|
r2505 | |||
|
r2436 | @with_environment | ||
|
r1800 | def test_get_home_dir_4(): | ||
|
r5383 | """get_home_dir() still works if $HOME is not set""" | ||
|
r4872 | |||
|
r1812 | if 'HOME' in env: del env['HOME'] | ||
|
r7669 | # this should still succeed, but we don't care what the answer is | ||
home = path.get_home_dir(False) | ||||
|
r2505 | |||
|
r2436 | @with_environment | ||
|
r1800 | def test_get_home_dir_5(): | ||
|
r5383 | """raise HomeDirError if $HOME is specified, but not a writable dir""" | ||
env['HOME'] = abspath(HOME_TEST_DIR+'garbage') | ||||
|
r5385 | # set os.name = posix, to prevent My Documents fallback on Windows | ||
os.name = 'posix' | ||||
|
r5384 | nt.assert_raises(path.HomeDirError, path.get_home_dir, True) | ||
|
r3373 | |||
|
r4872 | |||
|
r2513 | # Should we stub wreg fully so we can run the test on all platforms? | ||
@skip_if_not_win32 | ||||
@with_environment | ||||
def test_get_home_dir_8(): | ||||
"""Using registry hack for 'My Documents', os=='nt' | ||||
|
r4872 | |||
|
r2513 | HOMESHARE, HOMEDRIVE, HOMEPATH, USERPROFILE and others are missing. | ||
|
r1746 | """ | ||
|
r1796 | os.name = 'nt' | ||
|
r2447 | # Remove from stub environment all keys that may be set | ||
for key in ['HOME', 'HOMESHARE', 'HOMEDRIVE', 'HOMEPATH', 'USERPROFILE']: | ||||
env.pop(key, None) | ||||
|
r1746 | |||
#Stub windows registry functions | ||||
|
r1796 | def OpenKey(x, y): | ||
|
r1746 | class key: | ||
def Close(self): | ||||
pass | ||||
return key() | ||||
|
r1796 | def QueryValueEx(x, y): | ||
|
r1843 | return [abspath(HOME_TEST_DIR)] | ||
|
r1746 | |||
|
r1796 | wreg.OpenKey = OpenKey | ||
wreg.QueryValueEx = QueryValueEx | ||||
|
r1746 | |||
|
r2505 | home_dir = path.get_home_dir() | ||
|
r1843 | nt.assert_equal(home_dir, abspath(HOME_TEST_DIR)) | ||
|
r1745 | |||
|
r1750 | |||
|
r2436 | @with_environment | ||
|
r1749 | def test_get_ipython_dir_1(): | ||
|
r1805 | """test_get_ipython_dir_1, Testcase to see if we can call get_ipython_dir without Exceptions.""" | ||
|
r4105 | env_ipdir = os.path.join("someplace", ".ipython") | ||
|
r4475 | path._writable_dir = lambda path: True | ||
|
r6700 | env['IPYTHONDIR'] = env_ipdir | ||
|
r2505 | ipdir = path.get_ipython_dir() | ||
|
r4105 | nt.assert_equal(ipdir, env_ipdir) | ||
|
r1749 | |||
|
r2436 | @with_environment | ||
|
r1800 | def test_get_ipython_dir_2(): | ||
|
r1805 | """test_get_ipython_dir_2, Testcase to see if we can call get_ipython_dir without Exceptions.""" | ||
|
r2505 | path.get_home_dir = lambda : "someplace" | ||
|
r4475 | path.get_xdg_dir = lambda : None | ||
path._writable_dir = lambda path: True | ||||
|
r1796 | os.name = "posix" | ||
|
r2449 | env.pop('IPYTHON_DIR', None) | ||
env.pop('IPYTHONDIR', None) | ||||
|
r3347 | env.pop('XDG_CONFIG_HOME', None) | ||
|
r2505 | ipdir = path.get_ipython_dir() | ||
|
r2392 | nt.assert_equal(ipdir, os.path.join("someplace", ".ipython")) | ||
|
r1749 | |||
|
r3347 | @with_environment | ||
def test_get_ipython_dir_3(): | ||||
"""test_get_ipython_dir_3, use XDG if defined, and .ipython doesn't exist.""" | ||||
path.get_home_dir = lambda : "someplace" | ||||
|
r4475 | path._writable_dir = lambda path: True | ||
|
r3347 | os.name = "posix" | ||
env.pop('IPYTHON_DIR', None) | ||||
env.pop('IPYTHONDIR', None) | ||||
env['XDG_CONFIG_HOME'] = XDG_TEST_DIR | ||||
ipdir = path.get_ipython_dir() | ||||
|
r7086 | if sys.platform == "darwin": | ||
expected = os.path.join("someplace", ".ipython") | ||||
else: | ||||
expected = os.path.join(XDG_TEST_DIR, "ipython") | ||||
nt.assert_equal(ipdir, expected) | ||||
|
r3347 | |||
@with_environment | ||||
def test_get_ipython_dir_4(): | ||||
"""test_get_ipython_dir_4, use XDG if both exist.""" | ||||
path.get_home_dir = lambda : HOME_TEST_DIR | ||||
os.name = "posix" | ||||
env.pop('IPYTHON_DIR', None) | ||||
env.pop('IPYTHONDIR', None) | ||||
env['XDG_CONFIG_HOME'] = XDG_TEST_DIR | ||||
ipdir = path.get_ipython_dir() | ||||
|
r7086 | if sys.platform == "darwin": | ||
expected = os.path.join(HOME_TEST_DIR, ".ipython") | ||||
else: | ||||
expected = os.path.join(XDG_TEST_DIR, "ipython") | ||||
nt.assert_equal(ipdir, expected) | ||||
|
r3347 | |||
@with_environment | ||||
def test_get_ipython_dir_5(): | ||||
"""test_get_ipython_dir_5, use .ipython if exists and XDG defined, but doesn't exist.""" | ||||
|
r3896 | path.get_home_dir = lambda : HOME_TEST_DIR | ||
|
r3347 | os.name = "posix" | ||
env.pop('IPYTHON_DIR', None) | ||||
env.pop('IPYTHONDIR', None) | ||||
env['XDG_CONFIG_HOME'] = XDG_TEST_DIR | ||||
os.rmdir(os.path.join(XDG_TEST_DIR, 'ipython')) | ||||
ipdir = path.get_ipython_dir() | ||||
nt.assert_equal(ipdir, IP_TEST_DIR) | ||||
@with_environment | ||||
def test_get_ipython_dir_6(): | ||||
"""test_get_ipython_dir_6, use XDG if defined and neither exist.""" | ||||
|
r4475 | xdg = os.path.join(HOME_TEST_DIR, 'somexdg') | ||
os.mkdir(xdg) | ||||
shutil.rmtree(os.path.join(HOME_TEST_DIR, '.ipython')) | ||||
path.get_home_dir = lambda : HOME_TEST_DIR | ||||
path.get_xdg_dir = lambda : xdg | ||||
|
r3347 | os.name = "posix" | ||
env.pop('IPYTHON_DIR', None) | ||||
env.pop('IPYTHONDIR', None) | ||||
|
r4475 | env.pop('XDG_CONFIG_HOME', None) | ||
xdg_ipdir = os.path.join(xdg, "ipython") | ||||
|
r3347 | ipdir = path.get_ipython_dir() | ||
nt.assert_equal(ipdir, xdg_ipdir) | ||||
@with_environment | ||||
|
r3896 | def test_get_ipython_dir_7(): | ||
|
r6700 | """test_get_ipython_dir_7, test home directory expansion on IPYTHONDIR""" | ||
|
r4475 | path._writable_dir = lambda path: True | ||
|
r7673 | home_dir = os.path.normpath(os.path.expanduser('~')) | ||
|
r6700 | env['IPYTHONDIR'] = os.path.join('~', 'somewhere') | ||
|
r3896 | ipdir = path.get_ipython_dir() | ||
nt.assert_equal(ipdir, os.path.join(home_dir, 'somewhere')) | ||||
|
r10868 | @skip_win32 | ||
|
r10662 | @with_environment | ||
def test_get_ipython_dir_8(): | ||||
"""test_get_ipython_dir_8, test / home directory""" | ||||
old = path._writable_dir, path.get_xdg_dir | ||||
try: | ||||
path._writable_dir = lambda path: bool(path) | ||||
path.get_xdg_dir = lambda: None | ||||
|
r10664 | env.pop('IPYTHON_DIR', None) | ||
env.pop('IPYTHONDIR', None) | ||||
|
r10662 | env['HOME'] = '/' | ||
nt.assert_equal(path.get_ipython_dir(), '/.ipython') | ||||
finally: | ||||
path._writable_dir, path.get_xdg_dir = old | ||||
|
r3896 | |||
@with_environment | ||||
|
r7086 | def test_get_xdg_dir_0(): | ||
"""test_get_xdg_dir_0, check xdg_dir""" | ||||
|
r3347 | reload(path) | ||
|
r4475 | path._writable_dir = lambda path: True | ||
|
r3347 | path.get_home_dir = lambda : 'somewhere' | ||
os.name = "posix" | ||||
|
r7086 | sys.platform = "linux2" | ||
|
r3347 | env.pop('IPYTHON_DIR', None) | ||
env.pop('IPYTHONDIR', None) | ||||
env.pop('XDG_CONFIG_HOME', None) | ||||
|
r4872 | |||
|
r3347 | nt.assert_equal(path.get_xdg_dir(), os.path.join('somewhere', '.config')) | ||
@with_environment | ||||
def test_get_xdg_dir_1(): | ||||
"""test_get_xdg_dir_1, check nonexistant xdg_dir""" | ||||
reload(path) | ||||
path.get_home_dir = lambda : HOME_TEST_DIR | ||||
os.name = "posix" | ||||
|
r7086 | sys.platform = "linux2" | ||
|
r3347 | env.pop('IPYTHON_DIR', None) | ||
env.pop('IPYTHONDIR', None) | ||||
env.pop('XDG_CONFIG_HOME', None) | ||||
nt.assert_equal(path.get_xdg_dir(), None) | ||||
@with_environment | ||||
def test_get_xdg_dir_2(): | ||||
"""test_get_xdg_dir_2, check xdg_dir default to ~/.config""" | ||||
reload(path) | ||||
path.get_home_dir = lambda : HOME_TEST_DIR | ||||
os.name = "posix" | ||||
|
r7086 | sys.platform = "linux2" | ||
|
r3347 | env.pop('IPYTHON_DIR', None) | ||
env.pop('IPYTHONDIR', None) | ||||
env.pop('XDG_CONFIG_HOME', None) | ||||
cfgdir=os.path.join(path.get_home_dir(), '.config') | ||||
|
r7086 | if not os.path.exists(cfgdir): | ||
os.makedirs(cfgdir) | ||||
|
r4872 | |||
|
r3347 | nt.assert_equal(path.get_xdg_dir(), cfgdir) | ||
|
r1969 | |||
|
r7086 | @with_environment | ||
def test_get_xdg_dir_3(): | ||||
"""test_get_xdg_dir_3, check xdg_dir not used on OS X""" | ||||
reload(path) | ||||
path.get_home_dir = lambda : HOME_TEST_DIR | ||||
os.name = "posix" | ||||
sys.platform = "darwin" | ||||
env.pop('IPYTHON_DIR', None) | ||||
env.pop('IPYTHONDIR', None) | ||||
env.pop('XDG_CONFIG_HOME', None) | ||||
cfgdir=os.path.join(path.get_home_dir(), '.config') | ||||
if not os.path.exists(cfgdir): | ||||
os.makedirs(cfgdir) | ||||
nt.assert_equal(path.get_xdg_dir(), None) | ||||
|
r1969 | def test_filefind(): | ||
"""Various tests for filefind""" | ||||
f = tempfile.NamedTemporaryFile() | ||||
|
r2505 | # print 'fname:',f.name | ||
alt_dirs = path.get_ipython_dir() | ||||
t = path.filefind(f.name, alt_dirs) | ||||
# print 'found:',t | ||||
|
r2357 | |||
|
r10230 | @with_environment | ||
def test_get_ipython_cache_dir(): | ||||
os.environ["HOME"] = HOME_TEST_DIR | ||||
if os.name == 'posix' and sys.platform != 'darwin': | ||||
# test default | ||||
os.makedirs(os.path.join(HOME_TEST_DIR, ".cache")) | ||||
os.environ.pop("XDG_CACHE_HOME", None) | ||||
ipdir = path.get_ipython_cache_dir() | ||||
nt.assert_equal(os.path.join(HOME_TEST_DIR, ".cache", "ipython"), | ||||
ipdir) | ||||
nt.assert_true(os.path.isdir(ipdir)) | ||||
# test env override | ||||
os.environ["XDG_CACHE_HOME"] = XDG_CACHE_DIR | ||||
ipdir = path.get_ipython_cache_dir() | ||||
nt.assert_true(os.path.isdir(ipdir)) | ||||
nt.assert_equal(ipdir, os.path.join(XDG_CACHE_DIR, "ipython")) | ||||
else: | ||||
nt.assert_equal(path.get_ipython_cache_dir(), | ||||
path.get_ipython_dir()) | ||||
|
r2357 | |||
def test_get_ipython_package_dir(): | ||||
|
r2505 | ipdir = path.get_ipython_package_dir() | ||
|
r2357 | nt.assert_true(os.path.isdir(ipdir)) | ||
|
r2436 | |||
|
r2505 | |||
|
r2498 | def test_get_ipython_module_path(): | ||
|
r2760 | ipapp_path = path.get_ipython_module_path('IPython.frontend.terminal.ipapp') | ||
|
r2498 | nt.assert_true(os.path.isfile(ipapp_path)) | ||
|
r2436 | |||
|
r2505 | |||
|
r2498 | @dec.skip_if_not_win32 | ||
def test_get_long_path_name_win32(): | ||||
|
r2505 | p = path.get_long_path_name('c:\\docume~1') | ||
|
r7875 | nt.assert_equal(p,u'c:\\Documents and Settings') | ||
|
r2436 | |||
|
r2505 | |||
|
r2498 | @dec.skip_win32 | ||
def test_get_long_path_name(): | ||||
|
r2505 | p = path.get_long_path_name('/usr/local') | ||
|
r7875 | nt.assert_equal(p,'/usr/local') | ||
|
r2436 | |||
|
r4483 | @dec.skip_win32 # can't create not-user-writable dir on win | ||
|
r4475 | @with_environment | ||
def test_not_writable_ipdir(): | ||||
tmpdir = tempfile.mkdtemp() | ||||
os.name = "posix" | ||||
env.pop('IPYTHON_DIR', None) | ||||
env.pop('IPYTHONDIR', None) | ||||
env.pop('XDG_CONFIG_HOME', None) | ||||
env['HOME'] = tmpdir | ||||
ipdir = os.path.join(tmpdir, '.ipython') | ||||
os.mkdir(ipdir) | ||||
os.chmod(ipdir, 600) | ||||
|
r4902 | with AssertPrints('is not a writable location', channel='stderr'): | ||
|
r4901 | ipdir = path.get_ipython_dir() | ||
|
r4475 | env.pop('IPYTHON_DIR', None) | ||
|
r4688 | |||
|
r4696 | def test_unquote_filename(): | ||
for win32 in (True, False): | ||||
|
r7875 | nt.assert_equal(path.unquote_filename('foo.py', win32=win32), 'foo.py') | ||
nt.assert_equal(path.unquote_filename('foo bar.py', win32=win32), 'foo bar.py') | ||||
nt.assert_equal(path.unquote_filename('"foo.py"', win32=True), 'foo.py') | ||||
nt.assert_equal(path.unquote_filename('"foo bar.py"', win32=True), 'foo bar.py') | ||||
nt.assert_equal(path.unquote_filename("'foo.py'", win32=True), 'foo.py') | ||||
nt.assert_equal(path.unquote_filename("'foo bar.py'", win32=True), 'foo bar.py') | ||||
nt.assert_equal(path.unquote_filename('"foo.py"', win32=False), '"foo.py"') | ||||
nt.assert_equal(path.unquote_filename('"foo bar.py"', win32=False), '"foo bar.py"') | ||||
nt.assert_equal(path.unquote_filename("'foo.py'", win32=False), "'foo.py'") | ||||
nt.assert_equal(path.unquote_filename("'foo bar.py'", win32=False), "'foo bar.py'") | ||||
|
r4696 | |||
|
r4688 | @with_environment | ||
def test_get_py_filename(): | ||||
os.chdir(TMP_TEST_DIR) | ||||
for win32 in (True, False): | ||||
with make_tempfile('foo.py'): | ||||
|
r7875 | nt.assert_equal(path.get_py_filename('foo.py', force_win32=win32), 'foo.py') | ||
nt.assert_equal(path.get_py_filename('foo', force_win32=win32), 'foo.py') | ||||
|
r4688 | with make_tempfile('foo'): | ||
|
r7875 | nt.assert_equal(path.get_py_filename('foo', force_win32=win32), 'foo') | ||
|
r4696 | nt.assert_raises(IOError, path.get_py_filename, 'foo.py', force_win32=win32) | ||
nt.assert_raises(IOError, path.get_py_filename, 'foo', force_win32=win32) | ||||
nt.assert_raises(IOError, path.get_py_filename, 'foo.py', force_win32=win32) | ||||
|
r4688 | true_fn = 'foo with spaces.py' | ||
with make_tempfile(true_fn): | ||||
|
r7875 | nt.assert_equal(path.get_py_filename('foo with spaces', force_win32=win32), true_fn) | ||
nt.assert_equal(path.get_py_filename('foo with spaces.py', force_win32=win32), true_fn) | ||||
|
r4688 | if win32: | ||
|
r7875 | nt.assert_equal(path.get_py_filename('"foo with spaces.py"', force_win32=True), true_fn) | ||
nt.assert_equal(path.get_py_filename("'foo with spaces.py'", force_win32=True), true_fn) | ||||
|
r4688 | else: | ||
|
r4696 | nt.assert_raises(IOError, path.get_py_filename, '"foo with spaces.py"', force_win32=False) | ||
nt.assert_raises(IOError, path.get_py_filename, "'foo with spaces.py'", force_win32=False) | ||||
r6152 | ||||
def test_unicode_in_filename(): | ||||
|
r6164 | """When a file doesn't exist, the exception raised should be safe to call | ||
str() on - i.e. in Python 2 it must only have ASCII characters. | ||||
https://github.com/ipython/ipython/issues/875 | ||||
""" | ||||
r6152 | try: | |||
# these calls should not throw unicode encode exceptions | ||||
path.get_py_filename(u'fooéè.py', force_win32=False) | ||||
except IOError as ex: | ||||
str(ex) | ||||
|
r8014 | |||
|
r8638 | class TestShellGlob(object): | ||
|
r8014 | |||
|
r8638 | @classmethod | ||
def setUpClass(cls): | ||||
cls.filenames_start_with_a = map('a{0}'.format, range(3)) | ||||
cls.filenames_end_with_b = map('{0}b'.format, range(3)) | ||||
cls.filenames = cls.filenames_start_with_a + cls.filenames_end_with_b | ||||
cls.tempdir = TemporaryDirectory() | ||||
td = cls.tempdir.name | ||||
|
r8014 | |||
|
r8638 | with cls.in_tempdir(): | ||
|
r8014 | # Create empty files | ||
|
r8638 | for fname in cls.filenames: | ||
|
r8014 | open(os.path.join(td, fname), 'w').close() | ||
|
r8638 | @classmethod | ||
def tearDownClass(cls): | ||||
cls.tempdir.cleanup() | ||||
@classmethod | ||||
@contextmanager | ||||
def in_tempdir(cls): | ||||
save = os.getcwdu() | ||||
|
r8640 | try: | ||
os.chdir(cls.tempdir.name) | ||||
yield | ||||
finally: | ||||
os.chdir(save) | ||||
|
r8638 | |||
def check_match(self, patterns, matches): | ||||
with self.in_tempdir(): | ||||
# glob returns unordered list. that's why sorted is required. | ||||
nt.assert_equals(sorted(path.shellglob(patterns)), | ||||
sorted(matches)) | ||||
def common_cases(self): | ||||
return [ | ||||
(['*'], self.filenames), | ||||
(['a*'], self.filenames_start_with_a), | ||||
(['*c'], ['*c']), | ||||
(['*', 'a*', '*b', '*c'], self.filenames | ||||
+ self.filenames_start_with_a | ||||
+ self.filenames_end_with_b | ||||
+ ['*c']), | ||||
(['a[012]'], self.filenames_start_with_a), | ||||
] | ||||
@skip_win32 | ||||
def test_match_posix(self): | ||||
for (patterns, matches) in self.common_cases() + [ | ||||
([r'\*'], ['*']), | ||||
([r'a\*', 'a*'], ['a*'] + self.filenames_start_with_a), | ||||
([r'a\[012]'], ['a[012]']), | ||||
]: | ||||
yield (self.check_match, patterns, matches) | ||||
@skip_if_not_win32 | ||||
def test_match_windows(self): | ||||
for (patterns, matches) in self.common_cases() + [ | ||||
# In windows, backslash is interpreted as path | ||||
# separator. Therefore, you can't escape glob | ||||
# using it. | ||||
([r'a\*', 'a*'], [r'a\*'] + self.filenames_start_with_a), | ||||
([r'a\[012]'], [r'a\[012]']), | ||||
]: | ||||
yield (self.check_match, patterns, matches) | ||||
|
r8119 | |||
def test_unescape_glob(): | ||||
nt.assert_equals(path.unescape_glob(r'\*\[\!\]\?'), '*[!]?') | ||||
|
r8122 | nt.assert_equals(path.unescape_glob(r'\\*'), r'\*') | ||
nt.assert_equals(path.unescape_glob(r'\\\*'), r'\*') | ||||
nt.assert_equals(path.unescape_glob(r'\\a'), r'\a') | ||||
nt.assert_equals(path.unescape_glob(r'\a'), r'\a') | ||||