Skip to content

Commit

Permalink
Feature: Use pytest.stash instead of protected member (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
BeyondEvil committed May 27, 2023
1 parent 81c00d9 commit 4194258
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
9 changes: 9 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
Release Notes
-------------

3.0.0 (unreleased)
------------------

* Use `pytest.stash` internally instead of `_metadata`

* Simplify code

* Thanks to `@LieYangGH <https://github.com/LeiYangGH>`_ for the PR.

2.0.4 (2022-10-30)
------------------

Expand Down
2 changes: 1 addition & 1 deletion src/pytest_metadata/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.


def pytest_metadata(metadata):
def pytest_metadata(metadata, config):
"""Called after collecting metadata"""
29 changes: 16 additions & 13 deletions src/pytest_metadata/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
travis_ci.ENVIRONMENT_VARIABLES,
]

metadata_key = pytest.StashKey[dict]()


def pytest_addhooks(pluginmanager):
from . import hooks
Expand All @@ -41,13 +43,13 @@ def pytest_addhooks(pluginmanager):
@pytest.fixture(scope="session")
def metadata(pytestconfig):
"""Provide test session metadata"""
return pytestconfig._metadata
return pytestconfig.stash[metadata_key]


@pytest.fixture(scope="session")
def include_metadata_in_junit_xml(metadata, pytestconfig, record_testsuite_property):
"""Provide test session metadata"""
metadata_ = pytestconfig._metadata
metadata_ = pytestconfig.stash[metadata_key]
for name, value in metadata_.items():
record_testsuite_property(name, value)

Expand Down Expand Up @@ -79,46 +81,47 @@ def pytest_addoption(parser):

@pytest.hookimpl(tryfirst=True)
def pytest_configure(config):
config._metadata = {
config.stash[metadata_key] = {
"Python": platform.python_version(),
"Platform": platform.platform(),
"Packages": {
"pytest": pytest.__version__,
"pluggy": pluggy.__version__,
},
}
config._metadata.update({k: v for k, v in config.getoption("metadata")})
config._metadata.update(json.loads(config.getoption("metadata_from_json")))
config.stash[metadata_key].update({k: v for k, v in config.getoption("metadata")})
config.stash[metadata_key].update(
json.loads(config.getoption("metadata_from_json"))
)
if config.getoption("metadata_from_json_file"):
with open(config.getoption("metadata_from_json_file"), "r") as json_file:
config._metadata.update(json.load(json_file))
config.stash[metadata_key].update(json.load(json_file))
plugins = dict()
for plugin, dist in config.pluginmanager.list_plugin_distinfo():
name, version = dist.project_name, dist.version
if name.startswith("pytest-"):
name = name[7:]
plugins[name] = version
config._metadata["Plugins"] = plugins
config.stash[metadata_key]["Plugins"] = plugins

for provider in CONTINUOUS_INTEGRATION:
for var in provider:
if os.environ.get(var):
config._metadata.update({var: os.environ.get(var)})
config.stash[metadata_key].update({var: os.environ.get(var)})

if hasattr(config, "workeroutput"):
config.workeroutput["metadata"] = config._metadata

config.hook.pytest_metadata(metadata=config._metadata)
config.workeroutput["metadata"] = config.stash[metadata_key]
config.hook.pytest_metadata(metadata=config.stash[metadata_key], config=config)


def pytest_report_header(config):
if config.getoption("verbose") > 0:
return "metadata: {0}".format(config._metadata)
return "metadata: {0}".format(config.stash[metadata_key])


@pytest.hookimpl(optionalhook=True)
def pytest_testnodedown(node):
# note that any metadata from remote workers will be replaced with the
# environment from the final worker to quit
if hasattr(node, "workeroutput"):
node.config._metadata.update(node.workeroutput["metadata"])
node.config.stash[metadata_key].update(node.workeroutput["metadata"])

0 comments on commit 4194258

Please sign in to comment.