mirror of
https://github.com/hwchase17/langchain.git
synced 2026-07-11 18:18:51 +00:00
fix(f173c903-1467-4c00-aa7f-bcf48007d079): attach CI LangSmith metadata before pytest runs
This commit is contained in:
@@ -23,7 +23,6 @@ import sys
|
||||
import warnings
|
||||
from typing import TYPE_CHECKING, Any
|
||||
|
||||
import pytest
|
||||
from langsmith.run_helpers import tracing_context
|
||||
|
||||
if TYPE_CHECKING:
|
||||
@@ -84,8 +83,22 @@ def _langsmith_ci_cm() -> Iterator[None]:
|
||||
yield
|
||||
|
||||
|
||||
@pytest.fixture(scope="session", autouse=True)
|
||||
def _langsmith_ci_context() -> Iterator[None]:
|
||||
"""Apply `LANGSMITH_TAGS`/`LANGSMITH_METADATA` to traces in this session."""
|
||||
with _langsmith_ci_cm():
|
||||
yield
|
||||
_langsmith_ci_context: contextlib.ExitStack | None = None
|
||||
|
||||
|
||||
def pytest_configure() -> None:
|
||||
"""Apply `LANGSMITH_TAGS`/`LANGSMITH_METADATA` to traces."""
|
||||
global _langsmith_ci_context # noqa: PLW0603
|
||||
|
||||
if _langsmith_ci_context is None:
|
||||
_langsmith_ci_context = contextlib.ExitStack()
|
||||
_langsmith_ci_context.enter_context(_langsmith_ci_cm())
|
||||
|
||||
|
||||
def pytest_unconfigure() -> None:
|
||||
"""Restore the previous LangSmith tracing context."""
|
||||
global _langsmith_ci_context # noqa: PLW0603
|
||||
|
||||
if _langsmith_ci_context is not None:
|
||||
_langsmith_ci_context.close()
|
||||
_langsmith_ci_context = None
|
||||
|
||||
@@ -129,22 +129,23 @@ class TestContextManager:
|
||||
|
||||
|
||||
class TestPluginDiscovery:
|
||||
"""End-to-end: the `pytest11` entry point wires up the autouse fixture."""
|
||||
"""End-to-end: the `pytest11` entry point wires up the plugin hooks."""
|
||||
|
||||
def test_autouse_fixture_applies_env_in_subprocess(
|
||||
def test_plugin_hooks_apply_env_in_subprocess(
|
||||
self, pytester: pytest.Pytester, monkeypatch: pytest.MonkeyPatch
|
||||
) -> None:
|
||||
monkeypatch.setenv("GITHUB_ACTIONS", "true")
|
||||
monkeypatch.setenv("LANGSMITH_TAGS", "discovered,from-entrypoint")
|
||||
monkeypatch.delenv("LANGSMITH_METADATA", raising=False)
|
||||
pytester.makepyfile(
|
||||
pytester.makeconftest(
|
||||
dedent("""
|
||||
from langsmith.run_helpers import get_tracing_context
|
||||
|
||||
def test_tags_visible_via_autouse_fixture():
|
||||
def pytest_runtest_setup(item):
|
||||
ctx = get_tracing_context()
|
||||
assert ctx["tags"] == ["discovered", "from-entrypoint"]
|
||||
"""),
|
||||
)
|
||||
pytester.makepyfile("def test_tags_visible_before_test_setup(): pass")
|
||||
result = pytester.runpytest_subprocess("-q")
|
||||
result.assert_outcomes(passed=1)
|
||||
|
||||
Reference in New Issue
Block a user