-
Notifications
You must be signed in to change notification settings - Fork 18
Support python 3.11 #241
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
Support python 3.11 #241
Changes from all commits
2776194
646d1bc
c62e64e
e00527a
bd9598b
15b0ddc
affdbda
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +0,0 @@ | ||
[submodule "extern/wrapt"] | ||
path = vendor/wrapt | ||
url = https://github.com/applandinc/wrapt.git | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ os: linux | |
dist: jammy | ||
language: python | ||
python: | ||
- "3.11" | ||
- "3.10" | ||
- "3.9.14" | ||
- "3.8" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import sys | ||
import unittest | ||
from contextlib import contextmanager | ||
|
||
from _appmap import testing_framework, wrapt | ||
from _appmap.utils import get_function_location | ||
|
||
_session = testing_framework.session("unittest", "tests") | ||
|
||
|
||
def _get_test_location(cls, method_name): | ||
fn = getattr(cls, method_name) | ||
return get_function_location(fn) | ||
|
||
|
||
if sys.version_info[1] < 8: | ||
# Prior to 3.8, unittest called the test case's test method directly, which left us without an | ||
# opportunity to hook it. So, instead, instrument unittest.case._Outcome.testPartExecutor, a | ||
# method used to run test cases. `isTest` will be True when the part is the actual test method, | ||
# False when it's setUp or teardown. | ||
@wrapt.patch_function_wrapper("unittest.case", "_Outcome.testPartExecutor") | ||
@contextmanager | ||
def testPartExecutor(wrapped, _, args, kwargs): | ||
def _args(test_case, *_, isTest=False, **__): | ||
return (test_case, isTest) | ||
|
||
test_case, is_test = _args(*args, **kwargs) | ||
already_recording = getattr(test_case, "_appmap_pytest_recording", None) | ||
# fmt: off | ||
if ( | ||
(not is_test) | ||
or isinstance(test_case, unittest.case._SubTest) # pylint: disable=protected-access | ||
or already_recording | ||
): | ||
# fmt: on | ||
with wrapped(*args, **kwargs): | ||
yield | ||
return | ||
|
||
method_name = test_case.id().split(".")[-1] | ||
location = _get_test_location(test_case.__class__, method_name) | ||
with _session.record( | ||
test_case.__class__, method_name, location=location | ||
) as metadata: | ||
if metadata: | ||
with wrapped( | ||
*args, **kwargs | ||
), testing_framework.collect_result_metadata(metadata): | ||
yield | ||
else: | ||
# session.record may return None | ||
yield | ||
|
||
else: | ||
# As of 3.8, unittest.case.TestCase now calls the test's method indirectly, through | ||
# TestCase._callTestMethod. Hook that to manage a recording session. | ||
@wrapt.patch_function_wrapper("unittest.case", "TestCase._callTestMethod") | ||
def callTestMethod(wrapped, test_case, args, kwargs): | ||
already_recording = getattr(test_case, "_appmap_pytest_recording", None) | ||
if already_recording: | ||
wrapped(*args, **kwargs) | ||
return | ||
|
||
method_name = test_case.id().split(".")[-1] | ||
location = _get_test_location(test_case.__class__, method_name) | ||
with _session.record(test_case.__class__, method_name, location=location) as metadata: | ||
if metadata: | ||
with testing_framework.collect_result_metadata(metadata): | ||
wrapped(*args, **kwargs) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../vendor/_appmap/wrapt | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is here to make development easier. It's exclude from the wheel when it gets built: https://github.com/getappmap/appmap-python/pull/241/files#diff-50c86b7ed8ac2cf95bd48334961bf0530cdc77b5a56f852c5c61b89d735fd711R25 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,7 @@ | ||
import unittest | ||
from contextlib import contextmanager | ||
|
||
from _appmap import testing_framework | ||
from _appmap.env import Env | ||
from _appmap.utils import get_function_location | ||
from appmap import wrapt | ||
|
||
logger = Env.current.getLogger(__name__) | ||
|
||
|
||
def setup_unittest(): | ||
session = testing_framework.session("unittest", "tests") | ||
|
||
def get_test_location(cls, method_name): | ||
|
||
fn = getattr(cls, method_name) | ||
return get_function_location(fn) | ||
|
||
# unittest.case._Outcome.testPartExecutor is used by all supported | ||
# versions of unittest to run test cases. `isTest` will be True when | ||
# the part is the actual test method, False when it's setUp or | ||
# teardown. | ||
@wrapt.patch_function_wrapper("unittest.case", "_Outcome.testPartExecutor") | ||
@contextmanager | ||
def testPartExecutor(wrapped, _, args, kwargs): | ||
def _args(test_case, *_, isTest=False, **__): | ||
return (test_case, isTest) | ||
|
||
test_case, is_test = _args(*args, **kwargs) | ||
already_recording = getattr(test_case, "_appmap_pytest_recording", None) | ||
# fmt: off | ||
if ( | ||
(not is_test) | ||
or isinstance(test_case, unittest.case._SubTest) # pylint: disable=protected-access | ||
or already_recording | ||
): | ||
# fmt: on | ||
with wrapped(*args, **kwargs): | ||
yield | ||
return | ||
|
||
method_name = test_case.id().split(".")[-1] | ||
location = get_test_location(test_case.__class__, method_name) | ||
with session.record( | ||
test_case.__class__, method_name, location=location | ||
) as metadata: | ||
if metadata: | ||
with wrapped( | ||
*args, **kwargs | ||
), testing_framework.collect_result_metadata(metadata): | ||
yield | ||
else: | ||
# session.record may return None | ||
yield | ||
|
||
|
||
if not Env.current.is_appmap_repo and Env.current.enables("unittest"): | ||
logger.debug("Test recording is enabled (unittest)") | ||
setup_unittest() | ||
import _appmap.unittest # pyright: ignore pylint: disable=unused-import |
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.