Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reenable coverate instrumentation of test files #11819

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion ddtrace/internal/coverage/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from ddtrace.internal.coverage.util import collapse_ranges
from ddtrace.internal.logger import get_logger
from ddtrace.internal.module import ModuleWatchdog
from ddtrace.internal.packages import is_ddtrace_pytest_test
from ddtrace.internal.packages import is_user_code
from ddtrace.internal.packages import platlib_path
from ddtrace.internal.packages import platstdlib_path
Expand Down Expand Up @@ -327,7 +328,7 @@ def transform(self, code: CodeType, _module: ModuleType) -> CodeType:
# Don't instrument code from standard library/site packages/etc.
return code

if not is_user_code(code_path):
if not is_user_code(code_path) and not is_ddtrace_pytest_test(code_path):
return code

retval = self.instrument_code(code, _module.__package__ if _module is not None else "")
Expand Down
9 changes: 9 additions & 0 deletions ddtrace/internal/packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from functools import singledispatch
import inspect
import logging
from os import environ
from os import fspath # noqa:F401
import sys
import sysconfig
Expand Down Expand Up @@ -272,6 +273,14 @@ def _(path: str) -> bool:
return not (is_stdlib(_path) or is_third_party(_path))


def is_ddtrace_pytest_test(path: Path) -> bool:
"""Return an relatively confident guess about whether the file relates to a pytest test run"""
package = filename_to_package(str(path))
if package is None:
return False
return package.name == "ddtrace" and "PYTEST_CURRENT_TEST" in environ and "/dd-trace-py/tests/" in str(path)


@cached(maxsize=256)
def is_distribution_available(name: str) -> bool:
"""Determine if a distribution is available in the current environment."""
Expand Down
Loading