-
-
Notifications
You must be signed in to change notification settings - Fork 632
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix PytestRun to honor user
conftest.py
plugins. (#7784)
Previously codebases that leveraged pytest's `conftest.py` support could fail when tested with `./pants test` in various situations. This was ultimately because we injected our own `conftest.py` to register our custom pytest plugins and plugin discovery of `conftest.py` plugins is brittle. We avoid the brittleness by moving our plugin into a `sys.path` module activated with `-p` fixing a previously failing test added in `test_conftests_discovery_with_coverage`.
- Loading branch information
Showing
7 changed files
with
232 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Copyright 2019 Pants project contributors (see CONTRIBUTORS.md). | ||
# Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
resources( | ||
name='plugin', | ||
sources=['plugin.py'] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
# coding=utf-8 | ||
# Copyright 2019 Pants project contributors (see CONTRIBUTORS.md). | ||
# Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
from __future__ import absolute_import, division, print_function, unicode_literals | ||
|
||
import json | ||
import os | ||
|
||
import pytest | ||
|
||
|
||
class NodeRenamerPlugin(object): | ||
"""A pytest plugin to modify the console output of test names. | ||
Replaces the chroot-based source paths with the source-tree based ones, which are more readable | ||
to the end user. | ||
""" | ||
|
||
def __init__(self, rootdir, sources_map): | ||
def rootdir_relative(path): | ||
return os.path.relpath(path, rootdir) | ||
|
||
self._sources_map = {rootdir_relative(k): rootdir_relative(v) for k, v in sources_map.items()} | ||
|
||
# We'd prefer to hook into pytest_runtest_logstart(), which actually prints the line we want to | ||
# fix, but we can't because we won't have access to any of its state, so we can't actually change | ||
# what it prints. | ||
# | ||
# Alternatively, we could hook into pytest_collect_file() and just set a custom nodeid for the | ||
# entire pytest run. However this interferes with pytest internals, including fixture | ||
# registration, leading to fixtures not running when they should. | ||
@pytest.hookimpl(hookwrapper=True) | ||
def pytest_runtest_protocol(self, item, nextitem): | ||
# Temporarily change the nodeid, which pytest uses for display. | ||
real_nodeid = item.nodeid | ||
real_path = real_nodeid.split('::', 1)[0] | ||
fixed_path = self._sources_map.get(real_path, real_path) | ||
fixed_nodeid = fixed_path + real_nodeid[len(real_path):] | ||
try: | ||
item._nodeid = fixed_nodeid | ||
yield | ||
finally: | ||
item._nodeid = real_nodeid | ||
|
||
|
||
class ShardingPlugin(object): | ||
def __init__(self, shard, num_shards): | ||
self._shard = shard | ||
self._num_shards = num_shards | ||
|
||
def pytest_report_header(self, config): | ||
return ('shard: {shard} of {num_shards} (0-based shard numbering)' | ||
.format(shard=self._shard, num_shards=self._num_shards)) | ||
|
||
def pytest_collection_modifyitems(self, session, config, items): | ||
total_count = len(items) | ||
removed = 0 | ||
def is_conftest(itm): | ||
return itm.fspath and itm.fspath.basename == 'conftest.py' | ||
for i, item in enumerate(list(x for x in items if not is_conftest(x))): | ||
if i % self._num_shards != self._shard: | ||
del items[i - removed] | ||
removed += 1 | ||
reporter = config.pluginmanager.getplugin('terminalreporter') | ||
reporter.write_line('Only executing {count} of {total} total tests in shard {shard} of ' | ||
'{num_shards}'.format(count=total_count - removed, | ||
total=total_count, | ||
shard=self._shard, | ||
num_shards=self._num_shards), | ||
bold=True, invert=True, yellow=True) | ||
|
||
|
||
def pytest_addoption(parser): | ||
group = parser.getgroup('pants', 'Pants testing support') | ||
group.addoption('--pants-rootdir-comm-path', | ||
dest='rootdir_comm_path', | ||
action='store', | ||
metavar='PATH', | ||
help='Path to a file to write the pytest rootdir to.') | ||
group.addoption('--pants-sources-map-path', | ||
dest='sources_map_path', | ||
action='store', | ||
metavar='PATH', | ||
help='Path to a source mapping file that should contain JSON object mapping from' | ||
'absolute source chroot path keys to the path of the original source ' | ||
'relative to the buildroot.') | ||
group.addoption('--pants-shard', | ||
dest='shard', | ||
action='store', | ||
default=0, | ||
type=int, | ||
help='The shard of tests to select for this run.') | ||
group.addoption('--pants-num-shards', | ||
dest='num_shards', | ||
action='store', | ||
default=1, | ||
type=int, | ||
help='The total number of shards being used to complete the run.') | ||
|
||
|
||
def pytest_configure(config): | ||
if config.getoption('help'): | ||
# Don't configure our plugins when the user is just asking for help. | ||
return | ||
|
||
rootdir = str(config.rootdir) | ||
rootdir_comm_path = config.getoption('rootdir_comm_path') | ||
with open(rootdir_comm_path, 'w') as fp: | ||
fp.write(rootdir) | ||
|
||
sources_map_path = config.getoption('sources_map_path') | ||
with open(sources_map_path) as fp: | ||
sources_map = json.load(fp) | ||
|
||
config.pluginmanager.register(NodeRenamerPlugin(rootdir, sources_map), 'pants_test_renamer') | ||
|
||
num_shards = config.getoption('num_shards') | ||
if num_shards > 1: | ||
shard = config.getoption('shard') | ||
config.pluginmanager.register(ShardingPlugin(shard, num_shards)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.