diff --git a/py/test/config.py b/py/test/config.py index 4718a24b4..efc65604b 100644 --- a/py/test/config.py +++ b/py/test/config.py @@ -4,16 +4,11 @@ from conftesthandle import Conftest from py.__.test import parseopt from py.__.misc.warn import APIWARN -# XXX move to Config class -basetemp = None def ensuretemp(string, dir=1): """ return temporary directory path with the given string as the trailing part. """ - global basetemp - if basetemp is None: - basetemp = py.path.local.make_numbered_dir(prefix='pytest-') - return basetemp.ensure(string, dir=dir) + return py.test.config.ensuretemp(string, dir=dir) class CmdOptions(object): """ pure container instance for holding cmdline options @@ -29,6 +24,7 @@ class Config(object): """ central bus for dealing with configuration/initialization data. """ Option = py.compat.optparse.Option # deprecated Error = Error + basetemp = None _sessionclass = None def __init__(self, pytestplugins=None, topdir=None): @@ -123,6 +119,18 @@ class Config(object): self._preparse(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): return [self.getfsnode(arg) for arg in self.args] diff --git a/py/test/dsession/testing/test_functional_dsession.py b/py/test/dsession/testing/test_functional_dsession.py index 22988500f..82c5bd59f 100644 --- a/py/test/dsession/testing/test_functional_dsession.py +++ b/py/test/dsession/testing/test_functional_dsession.py @@ -63,38 +63,21 @@ class TestAsyncFunctional: assert ev.host.address == "popen" ev, = eq.geteventargs("testrunfinish") - def test_distribution_rsync_roots_example(self, testdir): - py.test.skip("testing for root rsync needs rework") - destdir = py.test.ensuretemp("example_dist_destdir") - subdir = "sub_example_dist" - sourcedir = self.tmpdir.mkdir("source") - sourcedir.ensure(subdir, "conftest.py").write(py.code.Source(""" - hosts = ["popen:%s"] - rsyncdirs = ["%s", "../py"] - """ % (destdir, tmpdir.join(subdir), ))) - tmpdir.ensure(subdir, "__init__.py") - tmpdir.ensure(subdir, "test_one.py").write(py.code.Source(""" - def test_1(): - pass - def test_2(): - assert 0 - 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 + @py.test.mark.xfail("XXX") + def test_distribution_rsyncdirs_example(self, testdir): + source = testdir.mkdir("source") + dest = testdir.mkdir("dest") + subdir = source.mkdir("example_pkg") + subdir.ensure("__init__.py") + p = subdir.join("test_one.py") + p.write("def test_5(): assert not __file__.startswith(%r)" % str(p)) + result = testdir.runpytest("-d", "--rsyncdirs=%(subdir)s" % locals(), + "--hosts=popen:%(dest)s" % locals()) + assert result.ret == 0 + result.stdout.fnmatch_lines([ + "*1 passed*" + ]) + assert dest.join(subdir.basename).check(dir=1) def test_nice_level(self, testdir): """ Tests if nice level behaviour is ok """ diff --git a/py/test/plugin/pytest_default.py b/py/test/plugin/pytest_default.py index bfd24116a..afd69334f 100644 --- a/py/test/plugin/pytest_default.py +++ b/py/test/plugin/pytest_default.py @@ -61,7 +61,9 @@ class DefaultPlugin: group._addoption('-s', '--nocapture', action="store_true", dest="nocapture", default=False, 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, help="box each test run in a separate process"), group._addoption('-f', '--looponfailing', diff --git a/py/test/plugin/pytest_pytester.py b/py/test/plugin/pytest_pytester.py index 953a5d7e3..2d1fa6f50 100644 --- a/py/test/plugin/pytest_pytester.py +++ b/py/test/plugin/pytest_pytester.py @@ -234,6 +234,9 @@ class TmpTestdir: return self.run(script, *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) class Event: diff --git a/py/test/testing/acceptance_test.py b/py/test/testing/acceptance_test.py index 7b5f9018d..d315a27b6 100644 --- a/py/test/testing/acceptance_test.py +++ b/py/test/testing/acceptance_test.py @@ -16,6 +16,17 @@ class TestPyTest: assert result.stderr.fnmatch_lines([ '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): p = testdir.makepyfile(""" diff --git a/py/test/testing/test_config.py b/py/test/testing/test_config.py index a9004f2f2..ad3ea6207 100644 --- a/py/test/testing/test_config.py +++ b/py/test/testing/test_config.py @@ -85,8 +85,9 @@ class TestConfigCmdlineParsing: yield check_conflict_option, opts class TestConfigAPI: + @py.test.mark.issue("ensuretemp should call config.maketemp(basename)") - def test_tmpdir(self): + def test_ensuretemp(self): d1 = py.test.ensuretemp('hello') d2 = py.test.ensuretemp('hello') assert d1 == d2 diff --git a/py/test/testing/test_session.py b/py/test/testing/test_session.py index c2e207355..e2f5c599c 100644 --- a/py/test/testing/test_session.py +++ b/py/test/testing/test_session.py @@ -214,6 +214,7 @@ class TestNewSession(SessionTests): assert len(colskipped) == 1 def test_minus_x_import_error(self, testdir): + testdir.makepyfile(__init__="") testdir.makepyfile(test_one="xxxx", test_two="yyyy") sorter = testdir.inline_run("-x", testdir.tmpdir) finished = sorter.getnamed("collectionreport")