diff --git a/CHANGELOG.md b/CHANGELOG.md index 9af80eeb..5b65be65 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +## [1.15.2](https://github.com/getappmap/appmap-python/compare/v1.15.1...v1.15.2) (2023-05-23) + + +### Bug Fixes + +* Don't record requests in test cases ([114038c](https://github.com/getappmap/appmap-python/commit/114038c704fed31a72d58edfa959bf2f0c10eef3)), closes [#234](https://github.com/getappmap/appmap-python/issues/234) + ## [1.15.1](https://github.com/getappmap/appmap-python/compare/v1.15.0...v1.15.1) (2023-05-10) diff --git a/_appmap/env.py b/_appmap/env.py index 4022f884..73c9d3ce 100644 --- a/_appmap/env.py +++ b/_appmap/env.py @@ -1,5 +1,6 @@ """Initialize from the environment""" +from contextlib import contextmanager import logging import logging.config import os @@ -10,6 +11,10 @@ _bootenv = environ.copy() +def _recording_method_key(recording_method): + return f"APPMAP_RECORD_{recording_method.upper()}" + + class _EnvMeta(type): def __init__(cls, *args, **kwargs): type.__init__(cls, *args, **kwargs) @@ -85,9 +90,21 @@ def enables(self, recording_method, default="true"): if not self.enabled: return False - v = self.get(f"APPMAP_RECORD_{recording_method.upper()}", default).lower() + v = self.get(_recording_method_key(recording_method), default).lower() return v != "false" + @contextmanager + def disabled(self, recording_method: str): + key = _recording_method_key(recording_method) + value = self.get(key) + self.set(key, "false") + try: + yield + finally: + self.delete(key) + if value: + self.set(key, value) + @property def is_appmap_repo(self): return os.path.exists("appmap/__init__.py") and os.path.exists( diff --git a/_appmap/test/test_django.py b/_appmap/test/test_django.py index 6b4876fb..65fc2bec 100644 --- a/_appmap/test/test_django.py +++ b/_appmap/test/test_django.py @@ -2,6 +2,7 @@ # pylint: disable=missing-function-docstring,redefined-outer-name import json +import os import sys from pathlib import Path from threading import Thread @@ -186,6 +187,8 @@ def test_enabled(self, pytester): # Look for the http_server_request event in test_app's appmap. If # middleware reset is broken, it won't be there. appmap_file = pytester.path / "tmp" / "appmap" / "pytest" / "test_request.appmap.json" + assert not os.path.exists(pytester.path / "tmp" / "appmap" / "requests") + events = json.loads(appmap_file.read_text())["events"] assert "http_server_request" in events[0] diff --git a/_appmap/test/test_env.py b/_appmap/test/test_env.py new file mode 100644 index 00000000..2cfff6b8 --- /dev/null +++ b/_appmap/test/test_env.py @@ -0,0 +1,13 @@ +from _appmap.env import Env + + +def test_disable_temporarily(): + env = Env({"APPMAP": "true"}) + assert env.enables("requests") + try: + with env.disabled("requests"): + assert not env.enables("requests") + raise 'hell' + except: + ... + assert env.enables("requests") diff --git a/_appmap/test/test_flask.py b/_appmap/test/test_flask.py index f78dc550..7951557d 100644 --- a/_appmap/test/test_flask.py +++ b/_appmap/test/test_flask.py @@ -2,6 +2,7 @@ # pylint: disable=missing-function-docstring import importlib +import os from threading import Thread import flask @@ -145,6 +146,7 @@ def test_enabled(self, pytester): appmap_file = ( pytester.path / "tmp" / "appmap" / "pytest" / "test_request.appmap.json" ) + assert not os.path.exists(pytester.path / "tmp" / "appmap" / "requests") assert appmap_file.exists() def test_disabled(self, pytester, monkeypatch): diff --git a/_appmap/testing_framework.py b/_appmap/testing_framework.py index 5da503ff..217ec42b 100644 --- a/_appmap/testing_framework.py +++ b/_appmap/testing_framework.py @@ -116,11 +116,12 @@ def record(self, klass, method, **kwds): ) rec = Recording() + environ = env.Env.current try: - with rec: + with rec, environ.disabled("requests"): yield metadata finally: - basedir = env.Env.current.output_dir / self.name + basedir = environ.output_dir / self.name web_framework.write_appmap( basedir, item.filename, generation.dump(rec, metadata) ) diff --git a/pyproject.toml b/pyproject.toml index ddcca60e..4d6c87ed 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "appmap" -version = "1.15.1" +version = "1.15.2" description = "Create AppMap files by recording a Python application." readme = "README.md" authors = [