| 1 |
import os |
| 2 |
import sys |
| 3 |
|
| 4 |
import pytest |
| 5 |
|
| 6 |
from . import helpers |
| 7 |
from jedi.common import indent_block |
| 8 |
from jedi import RefactoringError |
| 9 |
|
| 10 |
|
| 11 |
def assert_case_equal(case, actual, desired): |
| 12 |
""" |
| 13 |
Assert ``actual == desired`` with formatted message. |
| 14 |
|
| 15 |
This is not needed for typical pytest use case, but as we need |
| 16 |
``--assert=plain`` (see ../pytest.ini) to workaround some issue |
| 17 |
due to pytest magic, let's format the message by hand. |
| 18 |
""" |
| 19 |
assert actual == desired, """ |
| 20 |
Test %r failed. |
| 21 |
actual = |
| 22 |
%s |
| 23 |
desired = |
| 24 |
%s |
| 25 |
""" % (case, indent_block(str(actual)), indent_block(str(desired))) |
| 26 |
|
| 27 |
|
| 28 |
def assert_static_analysis(case, actual, desired): |
| 29 |
"""A nicer formatting for static analysis tests.""" |
| 30 |
a = set(actual) |
| 31 |
d = set(desired) |
| 32 |
assert actual == desired, """ |
| 33 |
Test %r failed. |
| 34 |
not raised = %s |
| 35 |
unspecified = %s |
| 36 |
""" % (case, sorted(d - a), sorted(a - d)) |
| 37 |
|
| 38 |
|
| 39 |
def test_completion(case, monkeypatch, environment, has_django): |
| 40 |
skip_reason = case.get_skip_reason(environment) |
| 41 |
if skip_reason is not None: |
| 42 |
pytest.skip(skip_reason) |
| 43 |
|
| 44 |
if (not has_django) and case.path.endswith('django.py'): |
| 45 |
pytest.skip('Needs django to be installed to run this test.') |
| 46 |
|
| 47 |
if case.path.endswith("pytest.py"): |
| 48 |
|
| 49 |
|
| 50 |
pytest_plugin_dir = str(helpers.get_example_dir("pytest_plugin_package")) |
| 51 |
case._project.added_sys_path = [pytest_plugin_dir] |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
def mock_entry_points(*, group=None): |
| 56 |
import importlib.metadata |
| 57 |
entries = [importlib.metadata.EntryPoint( |
| 58 |
name=None, |
| 59 |
value="pytest_plugin.plugin", |
| 60 |
group="pytest11", |
| 61 |
)] |
| 62 |
|
| 63 |
if sys.version_info >= (3, 10): |
| 64 |
assert group == "pytest11" |
| 65 |
return entries |
| 66 |
else: |
| 67 |
assert group is None |
| 68 |
return {"pytest11": entries} |
| 69 |
|
| 70 |
monkeypatch.setattr("importlib.metadata.entry_points", mock_entry_points) |
| 71 |
|
| 72 |
repo_root = helpers.root_dir |
| 73 |
monkeypatch.chdir(os.path.join(repo_root, 'jedi')) |
| 74 |
case.run(assert_case_equal, environment) |
| 75 |
|
| 76 |
|
| 77 |
def test_static_analysis(static_analysis_case, environment): |
| 78 |
skip_reason = static_analysis_case.get_skip_reason(environment) |
| 79 |
if skip_reason is not None: |
| 80 |
pytest.skip(skip_reason) |
| 81 |
else: |
| 82 |
static_analysis_case.run(assert_static_analysis, environment) |
| 83 |
|
| 84 |
|
| 85 |
def test_refactor(refactor_case, environment): |
| 86 |
""" |
| 87 |
Run refactoring test case. |
| 88 |
|
| 89 |
:type refactor_case: :class:`.refactor.RefactoringCase` |
| 90 |
""" |
| 91 |
desired_result = refactor_case.get_desired_result() |
| 92 |
if refactor_case.type == 'error': |
| 93 |
with pytest.raises(RefactoringError) as e: |
| 94 |
refactor_case.refactor(environment) |
| 95 |
assert e.value.args[0] == desired_result.strip() |
| 96 |
elif refactor_case.type == 'text': |
| 97 |
refactoring = refactor_case.refactor(environment) |
| 98 |
assert not refactoring.get_renames() |
| 99 |
text = ''.join(f.get_new_code() for f in refactoring.get_changed_files().values()) |
| 100 |
assert_case_equal(refactor_case, text, desired_result) |
| 101 |
else: |
| 102 |
diff = refactor_case.refactor(environment).get_diff() |
| 103 |
assert_case_equal(refactor_case, diff, desired_result) |