[svn r62993] * moving ensuretemp to config object

* adding --basetemp option
* added/rewrote some tests

--HG--
branch : trunk
This commit is contained in:
hpk 2009-03-17 11:29:45 +01:00
parent 6f1eca5e4a
commit 7ed26c2929
7 changed files with 49 additions and 40 deletions

View File

@ -4,16 +4,11 @@ from conftesthandle import Conftest
from py.__.test import parseopt from py.__.test import parseopt
from py.__.misc.warn import APIWARN from py.__.misc.warn import APIWARN
# XXX move to Config class
basetemp = None
def ensuretemp(string, dir=1): def ensuretemp(string, dir=1):
""" return temporary directory path with """ return temporary directory path with
the given string as the trailing part. the given string as the trailing part.
""" """
global basetemp return py.test.config.ensuretemp(string, dir=dir)
if basetemp is None:
basetemp = py.path.local.make_numbered_dir(prefix='pytest-')
return basetemp.ensure(string, dir=dir)
class CmdOptions(object): class CmdOptions(object):
""" pure container instance for holding cmdline options """ pure container instance for holding cmdline options
@ -29,6 +24,7 @@ class Config(object):
""" central bus for dealing with configuration/initialization data. """ """ central bus for dealing with configuration/initialization data. """
Option = py.compat.optparse.Option # deprecated Option = py.compat.optparse.Option # deprecated
Error = Error Error = Error
basetemp = None
_sessionclass = None _sessionclass = None
def __init__(self, pytestplugins=None, topdir=None): def __init__(self, pytestplugins=None, topdir=None):
@ -123,6 +119,18 @@ class Config(object):
self._preparse(args) self._preparse(args)
self.args = args self.args = args
def ensuretemp(self, string, dir=True):
if self.basetemp is None:
basetemp = self.option.basetemp
if basetemp:
basetemp = py.path.local(basetemp)
if not basetemp.check(dir=1):
basetemp.mkdir()
else:
basetemp = py.path.local.make_numbered_dir(prefix='pytest-')
self.basetemp = basetemp
return self.basetemp.ensure(string, dir=dir)
def getcolitems(self): def getcolitems(self):
return [self.getfsnode(arg) for arg in self.args] return [self.getfsnode(arg) for arg in self.args]

View File

@ -63,38 +63,21 @@ class TestAsyncFunctional:
assert ev.host.address == "popen" assert ev.host.address == "popen"
ev, = eq.geteventargs("testrunfinish") ev, = eq.geteventargs("testrunfinish")
def test_distribution_rsync_roots_example(self, testdir): @py.test.mark.xfail("XXX")
py.test.skip("testing for root rsync needs rework") def test_distribution_rsyncdirs_example(self, testdir):
destdir = py.test.ensuretemp("example_dist_destdir") source = testdir.mkdir("source")
subdir = "sub_example_dist" dest = testdir.mkdir("dest")
sourcedir = self.tmpdir.mkdir("source") subdir = source.mkdir("example_pkg")
sourcedir.ensure(subdir, "conftest.py").write(py.code.Source(""" subdir.ensure("__init__.py")
hosts = ["popen:%s"] p = subdir.join("test_one.py")
rsyncdirs = ["%s", "../py"] p.write("def test_5(): assert not __file__.startswith(%r)" % str(p))
""" % (destdir, tmpdir.join(subdir), ))) result = testdir.runpytest("-d", "--rsyncdirs=%(subdir)s" % locals(),
tmpdir.ensure(subdir, "__init__.py") "--hosts=popen:%(dest)s" % locals())
tmpdir.ensure(subdir, "test_one.py").write(py.code.Source(""" assert result.ret == 0
def test_1(): result.stdout.fnmatch_lines([
pass "*1 passed*"
def test_2(): ])
assert 0 assert dest.join(subdir.basename).check(dir=1)
def test_3():
raise ValueError(23)
def test_4(someargs):
pass
def test_5():
assert __file__ != '%s'
#def test_6():
# import py
# assert py.__file__ != '%s'
""" % (tmpdir.join(subdir), py.__file__)))
destdir.join("py").mksymlinkto(py.path.local(py.__file__).dirpath())
sorter = testdir.inline_run(tmpdir.join(subdir))
testevents = sorter.getnamed('itemtestreport')
assert len([x for x in testevents if x.passed]) == 2
assert len([x for x in testevents if x.failed]) == 3
assert len([x for x in testevents if x.skipped]) == 0
def test_nice_level(self, testdir): def test_nice_level(self, testdir):
""" Tests if nice level behaviour is ok """ """ Tests if nice level behaviour is ok """

View File

@ -61,7 +61,9 @@ class DefaultPlugin:
group._addoption('-s', '--nocapture', group._addoption('-s', '--nocapture',
action="store_true", dest="nocapture", default=False, action="store_true", dest="nocapture", default=False,
help="disable catching of sys.stdout/stderr output."), help="disable catching of sys.stdout/stderr output."),
group._addoption('--boxed', group.addoption('--basetemp', dest="basetemp", default=None,
help="directory to use for this test run.")
group.addoption('--boxed',
action="store_true", dest="boxed", default=False, action="store_true", dest="boxed", default=False,
help="box each test run in a separate process"), help="box each test run in a separate process"),
group._addoption('-f', '--looponfailing', group._addoption('-f', '--looponfailing',

View File

@ -234,6 +234,9 @@ class TmpTestdir:
return self.run(script, *args) return self.run(script, *args)
def runpytest(self, *args): def runpytest(self, *args):
p = py.path.local.make_numbered_dir(prefix="runpytest-",
keep=None, rootdir=self.tmpdir)
args = ('--basetemp=%s' % p, ) + args
return self.runpybin("py.test", *args) return self.runpybin("py.test", *args)
class Event: class Event:

View File

@ -17,6 +17,17 @@ class TestPyTest:
'config ERROR: hello' 'config ERROR: hello'
]) ])
def test_basetemp(self, testdir):
mytemp = testdir.tmpdir.mkdir("mytemp")
p = testdir.makepyfile("""
import py
def test_1():
py.test.ensuretemp('xyz')
""")
result = testdir.runpytest(p, '--basetemp=%s' %mytemp)
assert result.ret == 0
assert mytemp.join('xyz').check(dir=1)
def test_assertion_magic(self, testdir): def test_assertion_magic(self, testdir):
p = testdir.makepyfile(""" p = testdir.makepyfile("""
def test_this(): def test_this():

View File

@ -85,8 +85,9 @@ class TestConfigCmdlineParsing:
yield check_conflict_option, opts yield check_conflict_option, opts
class TestConfigAPI: class TestConfigAPI:
@py.test.mark.issue("ensuretemp should call config.maketemp(basename)") @py.test.mark.issue("ensuretemp should call config.maketemp(basename)")
def test_tmpdir(self): def test_ensuretemp(self):
d1 = py.test.ensuretemp('hello') d1 = py.test.ensuretemp('hello')
d2 = py.test.ensuretemp('hello') d2 = py.test.ensuretemp('hello')
assert d1 == d2 assert d1 == d2

View File

@ -214,6 +214,7 @@ class TestNewSession(SessionTests):
assert len(colskipped) == 1 assert len(colskipped) == 1
def test_minus_x_import_error(self, testdir): def test_minus_x_import_error(self, testdir):
testdir.makepyfile(__init__="")
testdir.makepyfile(test_one="xxxx", test_two="yyyy") testdir.makepyfile(test_one="xxxx", test_two="yyyy")
sorter = testdir.inline_run("-x", testdir.tmpdir) sorter = testdir.inline_run("-x", testdir.tmpdir)
finished = sorter.getnamed("collectionreport") finished = sorter.getnamed("collectionreport")