mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-06 22:14:27 +08:00
Rewrite the pyc test
This commit is contained in:
@@ -141,7 +141,7 @@ def load_module(evaluator, dotted_name, sys_path):
|
|||||||
__import__(dotted_name)
|
__import__(dotted_name)
|
||||||
except ImportError:
|
except ImportError:
|
||||||
# If a module is "corrupt" or not really a Python module or whatever.
|
# If a module is "corrupt" or not really a Python module or whatever.
|
||||||
print_to_stderr('Module %s not importable.' % dotted_name)
|
print_to_stderr('Module %s not importable in path %s.' % (dotted_name, sys_path))
|
||||||
return None
|
return None
|
||||||
except Exception:
|
except Exception:
|
||||||
# Since __import__ pretty much makes code execution possible, just
|
# Since __import__ pretty much makes code execution possible, just
|
||||||
|
|||||||
@@ -10,8 +10,10 @@ import os
|
|||||||
import shutil
|
import shutil
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
import jedi
|
import jedi
|
||||||
from ..helpers import cwd_at
|
from jedi.api.environment import SameEnvironment
|
||||||
|
|
||||||
|
|
||||||
SRC = """class Foo:
|
SRC = """class Foo:
|
||||||
@@ -22,35 +24,44 @@ class Bar:
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
def generate_pyc():
|
@pytest.fixture
|
||||||
os.mkdir("dummy_package")
|
def pyc_project_path(tmpdir):
|
||||||
with open("dummy_package/__init__.py", 'w'):
|
path = tmpdir.strpath
|
||||||
|
dummy_package_path = os.path.join(path, "dummy_package")
|
||||||
|
os.mkdir(dummy_package_path)
|
||||||
|
with open(os.path.join(dummy_package_path, "__init__.py"), 'w'):
|
||||||
pass
|
pass
|
||||||
with open("dummy_package/dummy.py", 'w') as f:
|
|
||||||
|
dummy_path = os.path.join(dummy_package_path, 'dummy.py')
|
||||||
|
with open(dummy_path, 'w') as f:
|
||||||
f.write(SRC)
|
f.write(SRC)
|
||||||
import compileall
|
import compileall
|
||||||
compileall.compile_file("dummy_package/dummy.py")
|
compileall.compile_file(dummy_path)
|
||||||
os.remove("dummy_package/dummy.py")
|
os.remove(dummy_path)
|
||||||
|
|
||||||
if sys.version_info[0] == 3:
|
if sys.version_info.major == 3:
|
||||||
# Python3 specific:
|
# Python3 specific:
|
||||||
# To import pyc modules, we must move them out of the __pycache__
|
# To import pyc modules, we must move them out of the __pycache__
|
||||||
# directory and rename them to remove ".cpython-%s%d"
|
# directory and rename them to remove ".cpython-%s%d"
|
||||||
# see: http://stackoverflow.com/questions/11648440/python-does-not-detect-pyc-files
|
# see: http://stackoverflow.com/questions/11648440/python-does-not-detect-pyc-files
|
||||||
for f in os.listdir("dummy_package/__pycache__"):
|
pycache = os.path.join(dummy_package_path, "__pycache__")
|
||||||
|
for f in os.listdir(pycache):
|
||||||
dst = f.replace('.cpython-%s%s' % sys.version_info[:2], "")
|
dst = f.replace('.cpython-%s%s' % sys.version_info[:2], "")
|
||||||
dst = os.path.join("dummy_package", dst)
|
dst = os.path.join(dummy_package_path, dst)
|
||||||
shutil.copy(os.path.join("dummy_package/__pycache__", f), dst)
|
shutil.copy(os.path.join(pycache, f), dst)
|
||||||
|
try:
|
||||||
|
yield path
|
||||||
|
finally:
|
||||||
|
shutil.rmtree(path)
|
||||||
|
|
||||||
|
|
||||||
@cwd_at('test/test_evaluate')
|
def test_pyc(pyc_project_path):
|
||||||
def test_pyc(Script):
|
|
||||||
"""
|
"""
|
||||||
The list of completion must be greater than 2.
|
The list of completion must be greater than 2.
|
||||||
"""
|
"""
|
||||||
try:
|
path = os.path.join(pyc_project_path, 'blub.py')
|
||||||
generate_pyc()
|
s = jedi.Script(
|
||||||
s = jedi.Script("from dummy_package import dummy; dummy.", path='blub.py')
|
"from dummy_package import dummy; dummy.",
|
||||||
|
path=path,
|
||||||
|
environment=SameEnvironment())
|
||||||
assert len(s.completions()) >= 2
|
assert len(s.completions()) >= 2
|
||||||
finally:
|
|
||||||
shutil.rmtree("dummy_package")
|
|
||||||
|
|||||||
Reference in New Issue
Block a user