[svn r62613] introduced a pytest_collect_directory hook.

--HG--
branch : trunk
This commit is contained in:
hpk 2009-03-05 23:15:42 +01:00
parent 27a501d171
commit ad06cfdc9d
3 changed files with 45 additions and 21 deletions

View File

@ -429,35 +429,27 @@ class Directory(FSCollector):
def consider(self, path): def consider(self, path):
if path.check(file=1): if path.check(file=1):
return self.consider_file(path) res = self.consider_file(path)
elif path.check(dir=1): elif path.check(dir=1):
return self.consider_dir(path) res = self.consider_dir(path)
if isinstance(res, list):
# throw out identical modules
l = []
for x in res:
if x not in l:
l.append(x)
res = l
return res
def consider_file(self, path): def consider_file(self, path):
res = self._config.pytestplugins.call_each( return self._config.pytestplugins.call_each(
'pytest_collect_file', path=path, parent=self) 'pytest_collect_file', path=path, parent=self)
l = []
# throw out identical modules
for x in res:
if x not in l:
l.append(x)
return l
def consider_dir(self, path, usefilters=None): def consider_dir(self, path, usefilters=None):
if usefilters is not None: if usefilters is not None:
APIWARN("0.99", "usefilters argument not needed") APIWARN("0.99", "usefilters argument not needed")
if not self.recfilter(path): return self._config.pytestplugins.call_each(
# check if cmdline specified this dir or a subdir 'pytest_collect_directory', path=path, parent=self)
for arg in self._config.args:
if path == arg or arg.relto(path):
break
else:
return
# not use self.Directory here as
# dir/conftest.py shall be able to
# define Directory(dir) already
Directory = self._config.getvalue('Directory', path)
return Directory(path, parent=self)
from py.__.test.runner import basic_run_report, forked_run_report from py.__.test.runner import basic_run_report, forked_run_report
class Item(Node): class Item(Node):

View File

@ -15,6 +15,20 @@ class DefaultPlugin:
if ext == ".py": if ext == ".py":
return parent.Module(path, parent=parent) return parent.Module(path, parent=parent)
def pytest_collect_directory(self, path, parent):
if not parent.recfilter(path):
# check if cmdline specified this dir or a subdir
for arg in parent._config.args:
if path == arg or arg.relto(path):
break
else:
return
# not use parent.Directory here as we want
# dir/conftest.py to be able to
# define Directory(dir) already
Directory = parent._config.getvalue('Directory', path)
return Directory(path, parent=parent)
def pytest_addoption(self, parser): def pytest_addoption(self, parser):
group = parser.addgroup("general", "general options") group = parser.addgroup("general", "general options")
group._addoption('-v', '--verbose', action="count", group._addoption('-v', '--verbose', action="count",

View File

@ -153,6 +153,24 @@ class TestCollectPluginHooks:
assert len(wascalled) == 1 assert len(wascalled) == 1
assert wascalled[0].ext == '.abc' assert wascalled[0].ext == '.abc'
def test_pytest_collect_directory(self, testdir):
tmpdir = testdir.tmpdir
wascalled = []
class Plugin:
def pytest_collect_directory(self, path, parent):
wascalled.append(path.basename)
return parent.Directory(path, parent)
testdir.plugins.append(Plugin())
testdir.mkdir("hello")
testdir.mkdir("world")
evrec = testdir.inline_run()
assert "hello" in wascalled
assert "world" in wascalled
# make sure the directories do not get double-appended
colreports = evrec.getnamed("collectionreport")
names = [rep.colitem.name for rep in colreports]
assert names.count("hello") == 1
class TestCustomConftests: class TestCustomConftests:
def test_non_python_files(self, testdir): def test_non_python_files(self, testdir):
testdir.makepyfile(conftest=""" testdir.makepyfile(conftest="""