Skip to content

Commit

Permalink
Some test refactorings.
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhalter committed Dec 3, 2016
1 parent 69c23ac commit ee1f077
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 49 deletions.
27 changes: 14 additions & 13 deletions jedi/evaluate/sys_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -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::
Expand Down Expand Up @@ -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):
Expand All @@ -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.
"""
Expand All @@ -162,7 +162,7 @@ def get_sys_path_powers(names):
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

Expand All @@ -182,7 +182,7 @@ def get_sys_path_powers(names):
)
)
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


Expand All @@ -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):
Expand All @@ -225,11 +225,12 @@ def load(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


Expand Down
39 changes: 23 additions & 16 deletions test/test_evaluate/test_buildout_detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,19 @@
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(
Expand All @@ -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
Expand All @@ -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
11 changes: 4 additions & 7 deletions test/test_evaluate/test_precedence.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -12,9 +11,7 @@
'... == ...'
])
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
8 changes: 4 additions & 4 deletions test/test_evaluate/test_representation.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

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():
Expand All @@ -22,8 +22,8 @@ def x():
# 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():
Expand All @@ -33,4 +33,4 @@ class X(object):
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']
11 changes: 5 additions & 6 deletions test/test_evaluate/test_sys_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@

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'])
Expand Down
4 changes: 2 additions & 2 deletions test/test_parser/test_old_fast_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ def foo():
""")

script = jedi.Script(dedent(source))
script._get_module().end_pos == (6, 0)
script._get_module().module_node.end_pos == (6, 0)
assert script.completions()


Expand All @@ -275,7 +275,7 @@ def bla():

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():
Expand Down
2 changes: 1 addition & 1 deletion test/test_parser/test_tokenize.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def test_quoted_strings(self):
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():
Expand Down

0 comments on commit ee1f077

Please sign in to comment.