|
| 1 | +# Copyright (c) 2018 Markus Unterwaditzer and individual contributors. |
| 2 | +"""The Sentry client used to report tests.""" |
| 3 | + |
1 | 4 | import os |
| 5 | +from typing import Any, cast |
| 6 | + |
2 | 7 | import sentry_sdk |
3 | 8 |
|
4 | | -from .integration import PytestIntegration |
| 9 | +from pytest_sentry.integration import PytestIntegration |
| 10 | + |
| 11 | +_TRUTHY = frozenset({"1", "true", "yes"}) |
5 | 12 |
|
6 | 13 |
|
7 | 14 | class Client(sentry_sdk.Client): |
8 | | - """ |
9 | | - A client that is used to report errors from tests. |
10 | | - Configured via `PYTEST_SENTRY_*` environment variables. |
| 15 | + """A client that reports errors from tests. |
| 16 | +
|
| 17 | + Configured via `PYTEST_SENTRY_*` environment variables. Explicitly passed |
| 18 | + options take precedence over the environment. |
11 | 19 | """ |
12 | 20 |
|
13 | | - def __init__(self, *args, **kwargs): |
14 | | - kwargs.setdefault( |
15 | | - "dsn", |
16 | | - os.environ.get("PYTEST_SENTRY_DSN", None), |
17 | | - ) |
18 | | - kwargs.setdefault( |
19 | | - "traces_sample_rate", |
20 | | - float(os.environ.get("PYTEST_SENTRY_TRACES_SAMPLE_RATE", 1.0)), |
21 | | - ) |
22 | | - kwargs.setdefault( |
23 | | - "profiles_sample_rate", |
24 | | - float(os.environ.get("PYTEST_SENTRY_PROFILES_SAMPLE_RATE", 0.0)), |
25 | | - ) |
26 | | - kwargs.setdefault( |
27 | | - "_experiments", |
28 | | - {}, |
29 | | - ).setdefault( |
30 | | - "auto_enabling_integrations", |
31 | | - True, |
32 | | - ) |
33 | | - kwargs.setdefault( |
34 | | - "environment", |
35 | | - os.environ.get("SENTRY_ENVIRONMENT", "test"), |
36 | | - ) |
37 | | - kwargs.setdefault("integrations", []).append(PytestIntegration()) |
38 | | - |
39 | | - debug = os.environ.get("PYTEST_SENTRY_DEBUG", "").lower() in ("1", "true", "yes") |
40 | | - kwargs.setdefault("debug", debug) |
41 | | - |
42 | | - sentry_sdk.Client.__init__(self, *args, **kwargs) |
| 21 | + def __init__(self, dsn: str | None = None, **options: object) -> None: |
| 22 | + """Create a client, filling in options from the environment.""" |
| 23 | + integrations = options.pop("integrations", None) |
| 24 | + merged: dict[str, object] = { |
| 25 | + "dsn": dsn if dsn is not None else os.environ.get("PYTEST_SENTRY_DSN"), |
| 26 | + "traces_sample_rate": float(os.environ.get("PYTEST_SENTRY_TRACES_SAMPLE_RATE", "1.0")), |
| 27 | + "profiles_sample_rate": float( |
| 28 | + os.environ.get("PYTEST_SENTRY_PROFILES_SAMPLE_RATE", "0.0") |
| 29 | + ), |
| 30 | + "environment": os.environ.get("SENTRY_ENVIRONMENT", "test"), |
| 31 | + "debug": os.environ.get("PYTEST_SENTRY_DEBUG", "").lower() in _TRUTHY, |
| 32 | + } | options |
| 33 | + merged["integrations"] = [ |
| 34 | + *(integrations if isinstance(integrations, list) else []), |
| 35 | + PytestIntegration(), |
| 36 | + ] |
| 37 | + # Options come from users and are forwarded as-is; the SDK validates them. |
| 38 | + sentry_sdk.Client.__init__(self, **cast("dict[str, Any]", merged)) |
0 commit comments