Skip to content

Commit

Permalink
Fix python tests
Browse files Browse the repository at this point in the history
  • Loading branch information
galaunay committed Jun 30, 2021
1 parent d974e00 commit eb78b16
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 83 deletions.
1 change: 1 addition & 0 deletions elpy/jedibackend.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def parse_version(*arg, **kwargs):
"please reinstall Elpy RPC virtualenv with"
" `M-x elpy-rpc-reinstall-virtualenv`", code=400)
JEDISUP17 = parse_version(jedi.__version__) >= parse_version("0.17.0")
JEDISUP18 = parse_version(jedi.__version__) >= parse_version("0.18.0")


class JediBackend(object):
Expand Down
13 changes: 8 additions & 5 deletions elpy/json_encoder.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import json
import pathlib

from elpy.jedibackend import JEDISUP18
if JEDISUP18:
import pathlib


class JSONEncoder(json.JSONEncoder):
def default(self, o):
if isinstance(o, pathlib.Path):
return str(o)
else:
return super().default(o)
if JEDISUP18:
if isinstance(o, pathlib.Path):
return str(o)
return super().default(o)
2 changes: 1 addition & 1 deletion elpy/rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import json
import sys
import traceback
from .json_encoder import JSONEncoder


class JSONRPCServer(object):
Expand Down Expand Up @@ -75,6 +74,7 @@ def write_json(self, **kwargs):
It's not possible with this method to write non-objects.
"""
from elpy.json_encoder import JSONEncoder
serialized_value = JSONEncoder().encode(kwargs)
self.stdout.write(serialized_value + "\n")
self.stdout.flush()
Expand Down
82 changes: 8 additions & 74 deletions elpy/tests/support.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
"""

import os
import pathlib
import re
import shutil
import sys
Expand All @@ -24,6 +23,9 @@
from elpy.rpc import Fault
from elpy.tests import compat

if jedibackend.JEDISUP18:
import pathlib


class BackendTestCase(unittest.TestCase):
"""Base class for backend tests.
Expand Down Expand Up @@ -56,7 +58,11 @@ def project_file(self, relname, contents):
fobj = open(full_name, "w")
with fobj as f:
f.write(contents)
return pathlib.Path(full_name)
# return full_name
if jedibackend.JEDISUP18:
return pathlib.Path(full_name)
else:
return full_name


class GenericRPCTests(object):
Expand Down Expand Up @@ -452,12 +458,6 @@ def test_should_return_nothing_when_no_completion(self):
self.assertEqual([], self.backend.rpc_get_completions("test.py",
source, offset))

def test_should_handle_jedi16(self):
backup = self.backend.rpc_get_completions
self.backend.rpc_get_completions = self.backend.rpc_get_completions_jedi16
self.test_should_complete_builtin()
self.backend.rpc_get_completions = backup


class RPCGetCompletionDocstringTests(object):
def test_should_return_docstring(self):
Expand Down Expand Up @@ -489,12 +489,6 @@ def test_should_return_none_if_on_a_builtin(self):
offset)
self.assertIsNone(completions)

def test_should_handle_jedi16(self):
backup = self.backend.rpc_get_docstring
self.backend.rpc_get_docstring = self.backend.rpc_get_docstring_jedi16
self.test_should_return_docstring()
self.backend.rpc_get_docstring = backup


class RPCGetCompletionLocationTests(object):
def test_should_return_location(self):
Expand Down Expand Up @@ -625,12 +619,6 @@ def test_should_find_variable_definition(self):
offset),
(filename, 0))

def test_should_handle_jedi16(self):
backup = self.backend.rpc_get_definition
self.backend.rpc_get_definition = self.backend.rpc_get_definition_jedi16
self.test_should_return_definition_location_same_file()
self.backend.rpc_get_definition = backup


class RPCGetAssignmentTests():
METHOD = "rpc_get_assignment"
Expand All @@ -640,25 +628,6 @@ def test_should_raise_fault(self):
with self.assertRaises(Fault):
self.backend.rpc_get_assignment("test.py", "dummy code", 1)

def test_should_handle_jedi16(self):
backup = self.backend.rpc_get_assignment
self.backend.rpc_get_assignment = self.backend.rpc_get_assignment_jedi16
source, offset = source_and_offset("import threading\n"
"def test_function(a, b):\n"
" return a + b\n"
"\n"
"test_func_|_tion(\n")
filename = self.project_file("test.py", source)

location = self.backend.rpc_get_assignment(filename,
source,
offset)

self.assertEqual(location[0], filename)
# On def or on the function name
self.assertIn(location[1], (17, 21))
self.backend.rpc_get_assignment = backup


class RPCGetCalltipTests(GenericRPCTests):
METHOD = "rpc_get_calltip"
Expand Down Expand Up @@ -805,12 +774,6 @@ def test_should_return_oneline_docstring_if_no_calltip(self):
self.assertEqual(calltip['kind'], 'oneline_doc')
self.assertEqual(calltip['doc'], 'No documentation')

def test_should_handle_jedi16(self):
backup = self.backend.rpc_get_calltip
self.backend.rpc_get_calltip = self.backend.rpc_get_calltip_jedi16
self.test_should_get_calltip()
self.backend.rpc_get_calltip = backup


class RPCGetDocstringTests(GenericRPCTests):
METHOD = "rpc_get_docstring"
Expand Down Expand Up @@ -841,12 +804,6 @@ def test_should_return_none_for_bad_identifier(self):
offset)
self.assertIsNone(docstring)

def test_should_handle_jedi16(self):
backup = self.backend.rpc_get_docstring
self.backend.rpc_get_docstring = self.backend.rpc_get_docstring_jedi16
self.test_should_get_docstring()
self.backend.rpc_get_docstring = backup


class RPCGetOnelineDocstringTests(GenericRPCTests):
METHOD = "rpc_get_oneline_docstring"
Expand Down Expand Up @@ -902,14 +859,6 @@ def test_should_return_none_for_bad_identifier(self):
offset)
self.assertIsNone(docstring)

def test_should_handle_jedi16(self):
backup = self.backend.rpc_get_oneline_docstring
self.backend.rpc_get_oneline_docstring = self.backend.rpc_get_oneline_docstring_jedi16
self.test_should_get_oneline_docstring()
self.test_should_get_oneline_docstring_for_modules()
self.test_should_return_none_for_bad_identifier()
self.backend.rpc_get_oneline_docstring = backup


@unittest.skipIf(not jedibackend.JEDISUP17,
"Refactoring not available with jedi<17")
Expand Down Expand Up @@ -1061,13 +1010,6 @@ def test_should_not_fail_without_symbol(self):

self.assertEqual(names, [])

def test_should_handle_jedi16(self):
backup = self.backend.rpc_get_names
self.backend.rpc_get_names = self.backend.rpc_get_names_jedi16
self.test_shouldreturn_names_in_same_file()
self.test_should_not_fail_without_symbol()
self.backend.rpc_get_names = backup


class RPCGetUsagesTests(GenericRPCTests):
METHOD = "rpc_get_usages"
Expand Down Expand Up @@ -1121,14 +1063,6 @@ def test_should_not_fail_without_symbol(self):

self.assertEqual(usages, [])

def test_should_handle_jedi16(self):
backup = self.backend.rpc_get_usages
self.backend.rpc_get_usages = self.backend.rpc_get_usages_jedi16
self.test_should_return_uses_in_same_file()
self.test_should_return_uses_in_other_file()
self.test_should_not_fail_without_symbol()
self.backend.rpc_get_usages = backup


def source_and_offset(source):
"""Return a source and offset from a source description.
Expand Down
5 changes: 4 additions & 1 deletion elpy/tests/test_jedibackend.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
import unittest

import jedi
import mock
try:
from unittest import mock
except ImportError:
import mock
import re

from elpy import jedibackend
Expand Down
5 changes: 4 additions & 1 deletion elpy/tests/test_pydocutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
import sys
import tempfile

import mock
try:
from unittest import mock
except ImportError:
import mock

import elpy.pydocutils

Expand Down
5 changes: 4 additions & 1 deletion elpy/tests/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
import tempfile
import unittest

import mock
try:
from unittest import mock
except ImportError:
import mock

from elpy import rpc
from elpy import server
Expand Down

0 comments on commit eb78b16

Please sign in to comment.