Skip to content

Commit

Permalink
fix: disable pytest-speed when installed and codspeed is enabled
Browse files Browse the repository at this point in the history
Fix COD-138
Fixes #16
  • Loading branch information
art049 committed Sep 12, 2024
1 parent 048a190 commit 597748a
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions src/pytest_codspeed/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import functools
import gc
import importlib.util
import os
import pkgutil
from dataclasses import dataclass, field
from typing import TYPE_CHECKING

Expand All @@ -26,7 +26,8 @@
T = TypeVar("T")
P = ParamSpec("P")

IS_PYTEST_BENCHMARK_INSTALLED = pkgutil.find_loader("pytest_benchmark") is not None
IS_PYTEST_BENCHMARK_INSTALLED = importlib.util.find_spec("pytest_benchmark") is not None
IS_PYTEST_SPEED_INSTALLED = importlib.util.find_spec("pytest_speed") is not None
BEFORE_PYTEST_8_1_1 = pytest.version_tuple < (8, 1, 1)


Expand Down Expand Up @@ -71,11 +72,17 @@ def pytest_configure(config: pytest.Config):
mode = CodSpeedMeasurementMode.Instrumentation
instrument = get_instrument_from_mode(mode)
disabled_plugins: list[str] = []
# Disable pytest-benchmark if codspeed is enabled
if is_codspeed_enabled and IS_PYTEST_BENCHMARK_INSTALLED:
object.__setattr__(config.option, "benchmark_disable", True)
config.pluginmanager.set_blocked("pytest-benchmark")
disabled_plugins.append("pytest-benchmark")
if is_codspeed_enabled:
if IS_PYTEST_BENCHMARK_INSTALLED:
# Disable pytest-benchmark
object.__setattr__(config.option, "benchmark_disable", True)
config.pluginmanager.set_blocked("pytest_benchmark")
config.pluginmanager.set_blocked("pytest-benchmark")
disabled_plugins.append("pytest-benchmark")
if IS_PYTEST_SPEED_INSTALLED:
# Disable pytest-speed
config.pluginmanager.set_blocked("speed")
disabled_plugins.append("pytest-speed")

plugin = CodSpeedPlugin(
disabled_plugins=tuple(disabled_plugins),
Expand All @@ -89,7 +96,9 @@ def pytest_configure(config: pytest.Config):
@pytest.hookimpl()
def pytest_plugin_registered(plugin, manager: pytest.PytestPluginManager):
"""Patch the benchmark fixture to use the codspeed one if codspeed is enabled"""
if IS_PYTEST_BENCHMARK_INSTALLED and isinstance(plugin, FixtureManager):
if (IS_PYTEST_BENCHMARK_INSTALLED or IS_PYTEST_SPEED_INSTALLED) and isinstance(
plugin, FixtureManager
):
fixture_manager = plugin
codspeed_plugin: CodSpeedPlugin = manager.get_plugin(PLUGIN_NAME)
if codspeed_plugin.is_codspeed_enabled:
Expand All @@ -100,11 +109,11 @@ def pytest_plugin_registered(plugin, manager: pytest.PytestPluginManager):
else fixture_manager.session,
)
assert codspeed_benchmark_fixtures is not None
# Archive the pytest-benchmark fixture
# Archive the alternative benchmark fixture
fixture_manager._arg2fixturedefs["__benchmark"] = (
fixture_manager._arg2fixturedefs["benchmark"]
)
# Replace the pytest-benchmark fixture with the codspeed one
# Replace the alternative fixture with the codspeed one
fixture_manager._arg2fixturedefs["benchmark"] = codspeed_benchmark_fixtures


Expand Down

0 comments on commit 597748a

Please sign in to comment.