2007-01-24 22:24:01 +08:00
|
|
|
import py
|
2009-08-28 00:46:42 +08:00
|
|
|
import sys, os
|
2007-01-24 22:24:01 +08:00
|
|
|
|
2010-04-24 01:05:22 +08:00
|
|
|
def fass():
|
|
|
|
assert 1 == 2
|
2009-08-28 00:46:42 +08:00
|
|
|
def check_assertion():
|
2010-04-24 01:05:22 +08:00
|
|
|
excinfo = py.test.raises(AssertionError, fass)
|
2009-08-28 00:46:42 +08:00
|
|
|
s = excinfo.exconly(tryshort=True)
|
|
|
|
if not s == "assert 1 == 2":
|
|
|
|
raise ValueError("assertion not enabled: got %s" % s)
|
|
|
|
|
|
|
|
def test_invoke_assertion(recwarn, monkeypatch):
|
2009-09-05 00:15:41 +08:00
|
|
|
monkeypatch.setattr(py.builtin.builtins, 'AssertionError', None)
|
2009-08-28 00:46:42 +08:00
|
|
|
py.magic.invoke(assertion=True)
|
|
|
|
try:
|
|
|
|
check_assertion()
|
|
|
|
finally:
|
|
|
|
py.magic.revoke(assertion=True)
|
|
|
|
recwarn.pop(DeprecationWarning)
|
|
|
|
|
2010-04-24 01:05:22 +08:00
|
|
|
@py.test.mark.skipif("sys.platform.startswith('java')")
|
2009-08-28 00:46:42 +08:00
|
|
|
def test_invoke_compile(recwarn, monkeypatch):
|
2009-09-05 00:15:41 +08:00
|
|
|
monkeypatch.setattr(py.builtin.builtins, 'compile', None)
|
2009-08-28 00:46:42 +08:00
|
|
|
py.magic.invoke(compile=True)
|
|
|
|
try:
|
2010-07-05 04:13:12 +08:00
|
|
|
co = compile("def f(): return 1\n", '', 'exec')
|
2009-08-28 00:46:42 +08:00
|
|
|
d = {}
|
2009-08-30 02:04:48 +08:00
|
|
|
py.builtin.exec_(co, d)
|
2010-07-27 03:15:15 +08:00
|
|
|
assert py.code.Source(d['f'])
|
2009-08-28 00:46:42 +08:00
|
|
|
finally:
|
|
|
|
py.magic.revoke(compile=True)
|
|
|
|
recwarn.pop(DeprecationWarning)
|
|
|
|
|
|
|
|
def test_patch_revert(recwarn):
|
|
|
|
class a:
|
|
|
|
pass
|
|
|
|
py.test.raises(AttributeError, "py.magic.patch(a, 'i', 42)")
|
|
|
|
|
|
|
|
a.i = 42
|
|
|
|
py.magic.patch(a, 'i', 23)
|
|
|
|
assert a.i == 23
|
|
|
|
recwarn.pop(DeprecationWarning)
|
|
|
|
py.magic.revert(a, 'i')
|
|
|
|
assert a.i == 42
|
|
|
|
recwarn.pop(DeprecationWarning)
|
|
|
|
|
|
|
|
def test_double_patch(recwarn):
|
|
|
|
class a:
|
|
|
|
i = 42
|
|
|
|
assert py.magic.patch(a, 'i', 2) == 42
|
|
|
|
recwarn.pop(DeprecationWarning)
|
|
|
|
assert py.magic.patch(a, 'i', 3) == 2
|
|
|
|
assert a.i == 3
|
|
|
|
assert py.magic.revert(a, 'i') == 3
|
|
|
|
recwarn.pop(DeprecationWarning)
|
|
|
|
assert a.i == 2
|
|
|
|
assert py.magic.revert(a, 'i') == 2
|
|
|
|
assert a.i == 42
|
|
|
|
|
|
|
|
def test_valueerror(recwarn):
|
|
|
|
class a:
|
|
|
|
i = 2
|
|
|
|
pass
|
|
|
|
py.test.raises(ValueError, "py.magic.revert(a, 'i')")
|
|
|
|
recwarn.pop(DeprecationWarning)
|
|
|
|
|
|
|
|
def test_AssertionError(testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import py
|
|
|
|
def test_hello(recwarn):
|
|
|
|
err = py.magic.AssertionError
|
|
|
|
recwarn.pop(DeprecationWarning)
|
|
|
|
assert err is py.code._AssertionError
|
|
|
|
""")
|
2010-07-27 03:15:15 +08:00
|
|
|
result = testdir.runpytest()
|
2009-08-28 00:46:42 +08:00
|
|
|
assert "1 passed" in result.stdout.str()
|
|
|
|
|
|
|
|
def test_autopath_deprecation(testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import py
|
|
|
|
def test_hello(recwarn):
|
|
|
|
p = py.magic.autopath()
|
|
|
|
recwarn.pop(DeprecationWarning)
|
|
|
|
assert py.path.local(__file__).dirpath() == p.dirpath()
|
|
|
|
""")
|
2010-07-27 03:15:15 +08:00
|
|
|
result = testdir.runpytest()
|
2009-08-28 00:46:42 +08:00
|
|
|
assert "1 passed" in result.stdout.str()
|
|
|
|
|
|
|
|
class Testautopath:
|
2007-01-24 22:24:01 +08:00
|
|
|
getauto = "from py.magic import autopath ; autopath = autopath()"
|
2010-07-27 03:15:15 +08:00
|
|
|
def setup_class(cls):
|
|
|
|
cls.root = py.test.ensuretemp(cls.__name__)
|
2007-01-24 22:24:01 +08:00
|
|
|
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:
|
2009-08-30 03:54:15 +08:00
|
|
|
py.builtin.exec_(self.getauto, d)
|
2007-01-24 22:24:01 +08:00
|
|
|
conf = d['autopath']
|
|
|
|
assert conf.dirpath() == self.initdir2
|
|
|
|
assert conf.pkgdir == self.initdir
|
|
|
|
assert str(self.root) in sys.path
|
2009-08-30 03:54:15 +08:00
|
|
|
py.builtin.exec_(self.getauto, d)
|
2007-01-24 22:24:01 +08:00
|
|
|
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:
|
2009-08-30 03:54:15 +08:00
|
|
|
py.builtin.exec_(self.getauto, d)
|
2007-01-24 22:24:01 +08:00
|
|
|
conf = d['autopath']
|
|
|
|
assert conf == self.initdir2.join('autoconfiguretest.py')
|
|
|
|
assert conf.pkgdir == self.initdir
|
|
|
|
assert str(self.root) in sys.path
|
2009-08-30 03:54:15 +08:00
|
|
|
py.builtin.exec_(self.getauto, d)
|
2007-01-24 22:24:01 +08:00
|
|
|
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:
|
2009-08-30 03:54:15 +08:00
|
|
|
py.builtin.exec_(self.getauto, d)
|
2007-01-24 22:24:01 +08:00
|
|
|
conf = d['autopath']
|
|
|
|
assert conf.dirpath() == self.root
|
|
|
|
assert conf.pkgdir == self.root
|
|
|
|
syspath = sys.path[:]
|
|
|
|
assert str(self.root) in syspath
|
2009-08-30 03:54:15 +08:00
|
|
|
py.builtin.exec_(self.getauto, d)
|
2007-01-24 22:24:01 +08:00
|
|
|
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 = {}
|
2009-08-30 03:54:15 +08:00
|
|
|
py.builtin.exec_(self.getauto, d)
|
2007-01-24 22:24:01 +08:00
|
|
|
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 = {}
|
2009-08-30 03:54:15 +08:00
|
|
|
py.builtin.exec_(self.getauto, d)
|
2007-01-24 22:24:01 +08:00
|
|
|
''')
|
|
|
|
finally:
|
|
|
|
sys.path[:] = oldsyspath
|
|
|
|
sys.argv = sys.argv
|