1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
|
import py
import sys
class TestAutoPath:
getauto = "from py.magic import autopath ; autopath = autopath()"
def setup_class(cls):
cls.root = py.test.ensuretemp(cls.__name__)
cls.initdir = cls.root.ensure('pkgdir', dir=1)
cls.initdir.ensure('__init__.py')
cls.initdir2 = cls.initdir.ensure('initdir2', dir=1)
cls.initdir2.ensure('__init__.py')
def test_import_autoconfigure__file__with_init(self):
testpath = self.initdir2 / 'autoconfiguretest.py'
d = {'__file__' : str(testpath)}
oldsyspath = sys.path[:]
try:
exec self.getauto in d
conf = d['autopath']
assert conf.dirpath() == self.initdir2
assert conf.pkgdir == self.initdir
assert str(self.root) in sys.path
exec self.getauto in d
assert conf is not d['autopath']
finally:
sys.path[:] = oldsyspath
def test_import_autoconfigure__file__with_py_exts(self):
for ext in '.pyc', '.pyo':
testpath = self.initdir2 / ('autoconfiguretest' + ext)
d = {'__file__' : str(testpath)}
oldsyspath = sys.path[:]
try:
exec self.getauto in d
conf = d['autopath']
assert conf == self.initdir2.join('autoconfiguretest.py')
assert conf.pkgdir == self.initdir
assert str(self.root) in sys.path
exec self.getauto in d
assert conf is not d['autopath']
finally:
sys.path[:] = oldsyspath
def test_import_autoconfigure___file__without_init(self):
testpath = self.root / 'autoconfiguretest.py'
d = {'__file__' : str(testpath)}
oldsyspath = sys.path[:]
try:
exec self.getauto in d
conf = d['autopath']
assert conf.dirpath() == self.root
assert conf.pkgdir == self.root
syspath = sys.path[:]
assert str(self.root) in syspath
exec self.getauto in d
assert conf is not d['autopath']
finally:
sys.path[:] = oldsyspath
def test_import_autoconfigure__nofile(self):
p = self.initdir2 / 'autoconfiguretest.py'
oldsysarg = sys.argv
sys.argv = [str(p)]
oldsyspath = sys.path[:]
try:
d = {}
exec self.getauto in d
conf = d['autopath']
assert conf.dirpath() == self.initdir2
assert conf.pkgdir == self.initdir
syspath = sys.path[:]
assert str(self.root) in syspath
finally:
sys.path[:] = oldsyspath
sys.argv = sys.argv
def test_import_autoconfigure__nofile_interactive(self):
oldsysarg = sys.argv
sys.argv = ['']
oldsyspath = sys.path[:]
try:
py.test.raises(ValueError,'''
d = {}
exec self.getauto in d
''')
finally:
sys.path[:] = oldsyspath
sys.argv = sys.argv
|