Skip to content

Commit 2bac855

Browse files
authored
Merge pull request #728 from bluetech/hookimpl-decorators
Add explicit pytest.hookspec/hookimpl annotations, avoid legacy tryfirst/trylast marks
2 parents b2586c3 + b85b71c commit 2bac855

File tree

7 files changed

+40
-6
lines changed

7 files changed

+40
-6
lines changed

src/xdist/dsession.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def report_line(self, line):
6767
if self.terminal and self.config.option.verbose >= 0:
6868
self.terminal.write_line(line)
6969

70-
@pytest.mark.trylast
70+
@pytest.hookimpl(trylast=True)
7171
def pytest_sessionstart(self, session):
7272
"""Creates and starts the nodes.
7373
@@ -79,18 +79,20 @@ def pytest_sessionstart(self, session):
7979
self._active_nodes.update(nodes)
8080
self._session = session
8181

82+
@pytest.hookimpl
8283
def pytest_sessionfinish(self, session):
8384
"""Shutdown all nodes."""
8485
nm = getattr(self, "nodemanager", None) # if not fully initialized
8586
if nm is not None:
8687
nm.teardown_nodes()
8788
self._session = None
8889

90+
@pytest.hookimpl
8991
def pytest_collection(self):
9092
# prohibit collection of test items in controller process
9193
return True
9294

93-
@pytest.mark.trylast
95+
@pytest.hookimpl(trylast=True)
9496
def pytest_xdist_make_scheduler(self, config, log):
9597
dist = config.getvalue("dist")
9698
schedulers = {
@@ -101,6 +103,7 @@ def pytest_xdist_make_scheduler(self, config, log):
101103
}
102104
return schedulers[dist](config, log)
103105

106+
@pytest.hookimpl
104107
def pytest_runtestloop(self):
105108
self.sched = self.config.hook.pytest_xdist_make_scheduler(
106109
config=self.config, log=self.log
@@ -223,6 +226,7 @@ def worker_errordown(self, node, error):
223226
self._clone_node(node)
224227
self._active_nodes.remove(node)
225228

229+
@pytest.hookimpl
226230
def pytest_terminal_summary(self, terminalreporter):
227231
if self.config.option.verbose >= 0 and self._summary_report:
228232
terminalreporter.write_sep("=", "xdist: {}".format(self._summary_report))
@@ -390,13 +394,15 @@ def rewrite(self, line, newline=False):
390394
self._lastlen = len(line)
391395
self.tr.rewrite(pline, bold=True)
392396

397+
@pytest.hookimpl
393398
def pytest_xdist_setupnodes(self, specs):
394399
self._specs = specs
395400
for spec in specs:
396401
self.setstatus(spec, "I", show=False)
397402
self.setstatus(spec, "I", show=True)
398403
self.ensure_show_status()
399404

405+
@pytest.hookimpl
400406
def pytest_xdist_newgateway(self, gateway):
401407
if self.config.option.verbose > 0:
402408
rinfo = gateway._rinfo()
@@ -408,6 +414,7 @@ def pytest_xdist_newgateway(self, gateway):
408414
)
409415
self.setstatus(gateway.spec, "C")
410416

417+
@pytest.hookimpl
411418
def pytest_testnodeready(self, node):
412419
if self.config.option.verbose > 0:
413420
d = node.workerinfo
@@ -417,6 +424,7 @@ def pytest_testnodeready(self, node):
417424
self.rewrite(infoline, newline=True)
418425
self.setstatus(node.gateway.spec, "ok")
419426

427+
@pytest.hookimpl
420428
def pytest_testnodedown(self, node, error):
421429
if not error:
422430
return

src/xdist/looponfail.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import execnet
1414

1515

16+
@pytest.hookimpl
1617
def pytest_addoption(parser):
1718
group = parser.getgroup("xdist", "distributed and subprocess testing")
1819
group._addoption(
@@ -26,6 +27,7 @@ def pytest_addoption(parser):
2627
)
2728

2829

30+
@pytest.hookimpl
2931
def pytest_cmdline_main(config):
3032

3133
if config.getoption("looponfail"):
@@ -178,6 +180,7 @@ def DEBUG(self, *args):
178180
if self.config.option.debug:
179181
print(" ".join(map(str, args)))
180182

183+
@pytest.hookimpl
181184
def pytest_collection(self, session):
182185
self.session = session
183186
self.trails = self.current_command
@@ -192,10 +195,12 @@ def pytest_collection(self, session):
192195
hook.pytest_collection_finish(session=session)
193196
return True
194197

198+
@pytest.hookimpl
195199
def pytest_runtest_logreport(self, report):
196200
if report.failed:
197201
self.recorded_failures.append(report)
198202

203+
@pytest.hookimpl
199204
def pytest_collectreport(self, report):
200205
if report.failed:
201206
self.recorded_failures.append(report)

src/xdist/newhooks.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,22 @@
1414
import pytest
1515

1616

17+
@pytest.hookspec()
1718
def pytest_xdist_setupnodes(config, specs):
1819
"""called before any remote node is set up."""
1920

2021

22+
@pytest.hookspec()
2123
def pytest_xdist_newgateway(gateway):
2224
"""called on new raw gateway creation."""
2325

2426

27+
@pytest.hookspec()
2528
def pytest_xdist_rsyncstart(source, gateways):
2629
"""called before rsyncing a directory to remote gateways takes place."""
2730

2831

32+
@pytest.hookspec()
2933
def pytest_xdist_rsyncfinish(source, gateways):
3034
"""called after rsyncing a directory to remote gateways takes place."""
3135

@@ -35,18 +39,22 @@ def pytest_xdist_getremotemodule():
3539
"""called when creating remote node"""
3640

3741

42+
@pytest.hookspec()
3843
def pytest_configure_node(node):
3944
"""configure node information before it gets instantiated."""
4045

4146

47+
@pytest.hookspec()
4248
def pytest_testnodeready(node):
4349
"""Test Node is ready to operate."""
4450

4551

52+
@pytest.hookspec()
4653
def pytest_testnodedown(node, error):
4754
"""Test Node is down."""
4855

4956

57+
@pytest.hookspec()
5058
def pytest_xdist_node_collection_finished(node, ids):
5159
"""called by the controller node when a worker node finishes collecting."""
5260

src/xdist/plugin.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
_sys_path = list(sys.path) # freeze a copy of sys.path at interpreter startup
1313

1414

15+
@pytest.hookimpl
1516
def pytest_xdist_auto_num_workers(config):
1617
try:
1718
import psutil
@@ -50,6 +51,7 @@ def parse_numprocesses(s):
5051
return int(s)
5152

5253

54+
@pytest.hookimpl
5355
def pytest_addoption(parser):
5456
group = parser.getgroup("xdist", "distributed and subprocess testing")
5557
group._addoption(
@@ -171,6 +173,7 @@ def pytest_addoption(parser):
171173
# -------------------------------------------------------------------------
172174

173175

176+
@pytest.hookimpl
174177
def pytest_addhooks(pluginmanager):
175178
from xdist import newhooks
176179

@@ -182,7 +185,7 @@ def pytest_addhooks(pluginmanager):
182185
# -------------------------------------------------------------------------
183186

184187

185-
@pytest.mark.trylast
188+
@pytest.hookimpl(trylast=True)
186189
def pytest_configure(config):
187190
if config.getoption("dist") != "no" and not config.getvalue("collectonly"):
188191
from xdist.dsession import DSession
@@ -202,7 +205,7 @@ def pytest_configure(config):
202205
config.option.forked = True
203206

204207

205-
@pytest.mark.tryfirst
208+
@pytest.hookimpl(tryfirst=True)
206209
def pytest_cmdline_main(config):
207210
usepdb = config.getoption("usepdb", False) # a core option
208211
if config.option.numprocesses in ("auto", "logical"):

src/xdist/remote.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,14 @@ def sendevent(self, name, **kwargs):
4747
self.log("sending", name, kwargs)
4848
self.channel.send((name, kwargs))
4949

50+
@pytest.hookimpl
5051
def pytest_internalerror(self, excrepr):
5152
formatted_error = str(excrepr)
5253
for line in formatted_error.split("\n"):
5354
self.log("IERROR>", line)
5455
interactor.sendevent("internal_error", formatted_error=formatted_error)
5556

57+
@pytest.hookimpl
5658
def pytest_sessionstart(self, session):
5759
self.session = session
5860
workerinfo = getinfodict()
@@ -65,9 +67,11 @@ def pytest_sessionfinish(self, exitstatus):
6567
yield
6668
self.sendevent("workerfinished", workeroutput=self.config.workeroutput)
6769

70+
@pytest.hookimpl
6871
def pytest_collection(self, session):
6972
self.sendevent("collectionstart")
7073

74+
@pytest.hookimpl
7175
def pytest_runtestloop(self, session):
7276
self.log("entering main loop")
7377
torun = []
@@ -112,6 +116,7 @@ def run_one_test(self, torun):
112116
"runtest_protocol_complete", item_index=self.item_index, duration=duration
113117
)
114118

119+
@pytest.hookimpl
115120
def pytest_collection_finish(self, session):
116121
try:
117122
topdir = str(self.config.rootpath)
@@ -124,12 +129,15 @@ def pytest_collection_finish(self, session):
124129
ids=[item.nodeid for item in session.items],
125130
)
126131

132+
@pytest.hookimpl
127133
def pytest_runtest_logstart(self, nodeid, location):
128134
self.sendevent("logstart", nodeid=nodeid, location=location)
129135

136+
@pytest.hookimpl
130137
def pytest_runtest_logfinish(self, nodeid, location):
131138
self.sendevent("logfinish", nodeid=nodeid, location=location)
132139

140+
@pytest.hookimpl
133141
def pytest_runtest_logreport(self, report):
134142
data = self.config.hook.pytest_report_to_serializable(
135143
config=self.config, report=report
@@ -140,6 +148,7 @@ def pytest_runtest_logreport(self, report):
140148
assert self.session.items[self.item_index].nodeid == report.nodeid
141149
self.sendevent("testreport", data=data)
142150

151+
@pytest.hookimpl
143152
def pytest_collectreport(self, report):
144153
# send only reports that have not passed to controller as optimization (#330)
145154
if not report.passed:
@@ -148,6 +157,7 @@ def pytest_collectreport(self, report):
148157
)
149158
self.sendevent("collectreport", data=data)
150159

160+
@pytest.hookimpl
151161
def pytest_warning_recorded(self, warning_message, when, nodeid, location):
152162
self.sendevent(
153163
"warning_recorded",

src/xdist/workermanage.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ class WorkerController:
212212
ENDMARK = -1
213213

214214
class RemoteHook:
215-
@pytest.mark.trylast
215+
@pytest.hookimpl(trylast=True)
216216
def pytest_xdist_getremotemodule(self):
217217
return xdist.remote
218218

testing/acceptance_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ def test_distribution_rsyncdirs_example(
205205
def pytest_addoption(parser):
206206
parser.addoption("--foobar", action="store", dest="foobar_opt")
207207
208-
@pytest.mark.tryfirst
208+
@pytest.hookimpl(tryfirst=True)
209209
def pytest_load_initial_conftests(early_config):
210210
opt = early_config.known_args_namespace.foobar_opt
211211
print("--foobar=%s active! [%s]" % (opt, os.getpid()), file=sys.stderr)

0 commit comments

Comments
 (0)