Some test refactorings.

This commit is contained in:
Dave Halter
2016-12-03 14:32:00 +01:00
parent 69c23ac113
commit ee1f077014
7 changed files with 53 additions and 49 deletions

View File

@@ -86,7 +86,7 @@ def _execute_code(module_path, code):
return []
def _paths_from_assignment(evaluator, expr_stmt):
def _paths_from_assignment(module_context, expr_stmt):
"""
Extracts the assigned strings from an assignment that looks as follows::
@@ -122,11 +122,11 @@ def _paths_from_assignment(evaluator, expr_stmt):
from jedi.evaluate.iterable import py__iter__
from jedi.evaluate.precedence import is_string
types = evaluator.eval_element(expr_stmt)
for types in py__iter__(evaluator, types, expr_stmt):
for typ in types:
if is_string(typ):
yield typ.obj
types = module_context.create_context(expr_stmt).eval_node(expr_stmt)
for lazy_context in py__iter__(module_context.evaluator, types, expr_stmt):
for context in lazy_context.infer():
if is_string(context):
yield context.obj
def _paths_from_list_modifications(module_path, trailer1, trailer2):
@@ -147,7 +147,7 @@ def _paths_from_list_modifications(module_path, trailer1, trailer2):
return _execute_code(module_path, arg.get_code())
def _check_module(evaluator, module_context):
def _check_module(module_context):
"""
Detect sys.path modifications within module.
"""
@@ -162,7 +162,7 @@ def _check_module(evaluator, module_context):
if isinstance(n, tree.Name) and n.value == 'path':
yield name, power
sys_path = list(evaluator.sys_path) # copy
sys_path = list(module_context.evaluator.sys_path) # copy
if isinstance(module_context, CompiledObject):
return sys_path
@@ -182,7 +182,7 @@ def _check_module(evaluator, module_context):
)
)
elif name.get_definition().type == 'expr_stmt':
sys_path.extend(_paths_from_assignment(evaluator, stmt))
sys_path.extend(_paths_from_assignment(module_context, stmt))
return sys_path
@@ -201,7 +201,7 @@ def sys_path_with_modifications(evaluator, module_context):
buildout_script_paths = set()
result = _check_module(evaluator, module_context)
result = _check_module(module_context)
result += _detect_django_path(path)
for buildout_script in _get_buildout_scripts(path):
for path in _get_paths_from_buildout_script(evaluator, buildout_script):
@@ -225,11 +225,12 @@ def _get_paths_from_buildout_script(evaluator, buildout_script):
return p.module
cached = load_parser(buildout_script)
module = cached and cached.module or load(buildout_script)
if not module:
module_node = cached and cached.module or load(buildout_script)
if module_node is None:
return
for path in _check_module(evaluator, module):
from jedi.evaluate.representation import ModuleContext
for path in _check_module(ModuleContext(evaluator, module_node)):
yield path

View File

@@ -7,11 +7,19 @@ from jedi.evaluate.sys_path import (_get_parent_dir_with_file,
sys_path_with_modifications,
_check_module)
from jedi.evaluate import Evaluator
from jedi.evaluate.representation import ModuleContext
from jedi.parser import ParserWithRecovery, load_grammar
from ..helpers import cwd_at
def check_module_test(code):
grammar = load_grammar()
p = ParserWithRecovery(grammar, code)
module_context = ModuleContext(Evaluator(grammar), p.module)
return _check_module(module_context)
@cwd_at('test/test_evaluate/buildout_project/src/proj_name')
def test_parent_dir_with_file():
parent = _get_parent_dir_with_file(
@@ -30,44 +38,44 @@ def test_buildout_detection():
def test_append_on_non_sys_path():
SRC = dedent(u("""
code = dedent(u("""
class Dummy(object):
path = []
d = Dummy()
d.path.append('foo')"""))
grammar = load_grammar()
p = ParserWithRecovery(grammar, SRC)
paths = _check_module(Evaluator(grammar), p.module)
paths = check_module_test(code)
assert len(paths) > 0
assert 'foo' not in paths
def test_path_from_invalid_sys_path_assignment():
SRC = dedent(u("""
code = dedent(u("""
import sys
sys.path = 'invalid'"""))
grammar = load_grammar()
p = ParserWithRecovery(grammar, SRC)
paths = _check_module(Evaluator(grammar), p.module)
paths = check_module_test(code)
assert len(paths) > 0
assert 'invalid' not in paths
@cwd_at('test/test_evaluate/buildout_project/src/proj_name/')
def test_sys_path_with_modifications():
SRC = dedent(u("""
code = dedent(u("""
import os
"""))
path = os.path.abspath(os.path.join(os.curdir, 'module_name.py'))
grammar = load_grammar()
p = ParserWithRecovery(grammar, SRC)
p.module.path = os.path.abspath(os.path.join(os.curdir, 'module_name.py'))
paths = sys_path_with_modifications(Evaluator(grammar), p.module)
p = ParserWithRecovery(grammar, code, module_path=path)
module_context = ModuleContext(Evaluator(grammar), p.module)
paths = sys_path_with_modifications(module_context.evaluator, module_context)
assert '/tmp/.buildout/eggs/important_package.egg' in paths
def test_path_from_sys_path_assignment():
SRC = dedent(u("""
code = dedent(u("""
#!/usr/bin/python
import sys
@@ -82,8 +90,7 @@ def test_path_from_sys_path_assignment():
if __name__ == '__main__':
sys.exit(important_package.main())"""))
grammar = load_grammar()
p = ParserWithRecovery(grammar, SRC)
paths = _check_module(Evaluator(grammar), p.module)
paths = check_module_test(code)
assert 1 not in paths
assert '/home/test/.buildout/eggs/important_package.egg' in paths

View File

@@ -1,6 +1,5 @@
from jedi.parser import load_grammar, Parser
from jedi.evaluate import Evaluator
from jedi.evaluate.compiled import CompiledObject
from jedi import Script
import pytest
@@ -12,9 +11,7 @@ import pytest
'... == ...'
])
def test_equals(source):
evaluator = Evaluator(load_grammar())
node = Parser(load_grammar(), source, 'eval_input').get_parsed_node()
results = evaluator.eval_element(node)
assert len(results) == 1
first = results.pop()
script = Script(source)
node = script._get_module_node().children[0].children[0]
first, = script._get_module().eval_node(node)
assert isinstance(first, CompiledObject) and first.obj is True

View File

@@ -5,7 +5,7 @@ from jedi import Script
def get_definition_and_evaluator(source):
d = Script(dedent(source)).goto_definitions()[0]
return d._name.parent, d._evaluator
return d._name.parent_context, d._evaluator
def test_function_execution():
@@ -22,8 +22,8 @@ def test_function_execution():
# Now just use the internals of the result (easiest way to get a fully
# usable function).
# Should return the same result both times.
assert len(evaluator.execute(func)) == 1
assert len(evaluator.execute(func)) == 1
assert len(func.execute_evaluated()) == 1
assert len(func.execute_evaluated()) == 1
def test_class_mro():
@@ -33,4 +33,4 @@ def test_class_mro():
X"""
cls, evaluator = get_definition_and_evaluator(s)
mro = cls.py__mro__()
assert [str(c.name) for c in mro] == ['X', 'object']
assert [c.name.string_name for c in mro] == ['X', 'object']

View File

@@ -4,16 +4,15 @@ import sys
import pytest
from jedi._compatibility import unicode
from jedi.parser import ParserWithRecovery, load_grammar
from jedi.evaluate import sys_path, Evaluator
from jedi.evaluate import sys_path
from jedi import Script
def test_paths_from_assignment():
def paths(src):
grammar = load_grammar()
stmt = ParserWithRecovery(grammar, unicode(src)).module.statements[0]
return set(sys_path._paths_from_assignment(Evaluator(grammar), stmt))
script = Script(src)
stmt = script._get_module_node().statements[0]
return set(sys_path._paths_from_assignment(script._get_module(), stmt))
assert paths('sys.path[0:0] = ["a"]') == set(['a'])
assert paths('sys.path = ["b", 1, x + 3, y, "c"]') == set(['b', 'c'])

View File

@@ -257,7 +257,7 @@ def test_string_literals():
""")
script = jedi.Script(dedent(source))
script._get_module().end_pos == (6, 0)
script._get_module().module_node.end_pos == (6, 0)
assert script.completions()
@@ -275,7 +275,7 @@ def test_decorator_string_issue():
s = jedi.Script(source)
assert s.completions()
assert s._get_module().get_code() == source
assert s._get_module().module_node.get_code() == source
def test_round_trip():

View File

@@ -121,7 +121,7 @@ class TokenTest(unittest.TestCase):
def test_tokenizer_with_string_literal_backslash():
import jedi
c = jedi.Script("statement = u'foo\\\n'; statement").goto_definitions()
assert c[0]._name.parent.obj == 'foo'
assert c[0]._name.parent_context.obj == 'foo'
def test_ur_literals():