diff --git a/py/test/collect.py b/py/test/collect.py index ae6af6fd2..bf20d071b 100644 --- a/py/test/collect.py +++ b/py/test/collect.py @@ -429,35 +429,27 @@ class Directory(FSCollector): def consider(self, path): if path.check(file=1): - return self.consider_file(path) + res = self.consider_file(path) 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): - res = self._config.pytestplugins.call_each( + return self._config.pytestplugins.call_each( '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): if usefilters is not None: APIWARN("0.99", "usefilters argument not needed") - if not self.recfilter(path): - # check if cmdline specified this dir or a subdir - 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) + return self._config.pytestplugins.call_each( + 'pytest_collect_directory', path=path, parent=self) from py.__.test.runner import basic_run_report, forked_run_report class Item(Node): diff --git a/py/test/plugin/pytest_default.py b/py/test/plugin/pytest_default.py index aa6e899a4..209fa3527 100644 --- a/py/test/plugin/pytest_default.py +++ b/py/test/plugin/pytest_default.py @@ -14,6 +14,20 @@ class DefaultPlugin: path in parent._config.args: if ext == ".py": 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): group = parser.addgroup("general", "general options") diff --git a/py/test/testing/test_collect.py b/py/test/testing/test_collect.py index 9498a1fa3..048de632d 100644 --- a/py/test/testing/test_collect.py +++ b/py/test/testing/test_collect.py @@ -153,6 +153,24 @@ class TestCollectPluginHooks: assert len(wascalled) == 1 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: def test_non_python_files(self, testdir): testdir.makepyfile(conftest="""