Debian Codegraph @

test/refactor.py

raw symbols SCIP
1 #!/usr/bin/env python
2 """
3 Refactoring tests work a little bit similar to integration tests. But the idea
4 is here to compare two versions of code. If you want to add a new test case,
5 just look at the existing ones in the ``test/refactor`` folder and copy them.
6 """
7 import os
8 import platform
9 import re
10
11 from parso import split_lines
12
13 from functools import reduce
14 import jedi
15 from .helpers import test_dir
16
17
18 class RefactoringCase(object):
19
20 def __init__(self, name, code, line_nr, index, path, kwargs, type_, desired_result):
21 self.name = name
22 self._code = code
23 self._line_nr = line_nr
24 self._index = index
25 self._path = path
26 self._kwargs = kwargs
27 self.type = type_
28 self._desired_result = desired_result
29
30 def get_desired_result(self):
31
32 if platform.system().lower() == 'windows' and self.type == 'diff':
33 # Windows uses backslashes to separate paths.
34 lines = split_lines(self._desired_result, keepends=True)
35 for i, line in enumerate(lines):
36 if re.search(' import_tree/', line):
37 lines[i] = line.replace('/', '\\')
38 return ''.join(lines)
39 return self._desired_result
40
41 @property
42 def refactor_type(self):
43 f_name = os.path.basename(self._path)
44 return f_name.replace('.py', '')
45
46 def refactor(self, environment):
47 project = jedi.Project(os.path.join(test_dir, 'refactor'))
48 script = jedi.Script(self._code, path=self._path, project=project, environment=environment)
49 refactor_func = getattr(script, self.refactor_type)
50 return refactor_func(self._line_nr, self._index, **self._kwargs)
51
52 def __repr__(self):
53 return '<%s: %s:%s>' % (self.__class__.__name__,
54 self.name, self._line_nr - 1)
55
56
57 def _collect_file_tests(code, path, lines_to_execute):
58 r = r'^# -{5,} ?([^\n]*)\n((?:(?!\n# \+{5,}).)*\n)' \
59 r'# \+{5,}\n((?:(?!\n# -{5,}).)*\n)'
60 match = None
61 for match in re.finditer(r, code, re.DOTALL | re.MULTILINE):
62 name = match.group(1).strip()
63 first = match.group(2)
64 second = match.group(3)
65
66 # get the line with the position of the operation
67 p = re.match(r'((?:(?!#\?).)*)#\? (\d*)( error| text|) ?([^\n]*)', first, re.DOTALL)
68 if p is None:
69 raise Exception("Please add a test start.")
70 continue
71 until = p.group(1)
72 index = int(p.group(2))
73 type_ = p.group(3).strip() or 'diff'
74 if p.group(4):
75 kwargs = eval(p.group(4))
76 else:
77 kwargs = {}
78
79 line_nr = until.count('\n') + 2
80 if lines_to_execute and line_nr - 1 not in lines_to_execute:
81 continue
82
83 yield RefactoringCase(name, first, line_nr, index, path, kwargs, type_, second)
84 if match is None:
85 raise Exception(f"Didn't match any test for {path}, {code!r}")
86 if match.end() != len(code):
87 raise Exception(f"Didn't match until the end of the file in {path}")
88
89
90 def collect_dir_tests(base_dir, test_files):
91 for f_name in os.listdir(base_dir):
92 files_to_execute = [a for a in test_files.items() if a[0] in f_name]
93 lines_to_execute = reduce(lambda x, y: x + y[1], files_to_execute, [])
94 if f_name.endswith(".py") and (not test_files or files_to_execute):
95 path = os.path.join(base_dir, f_name)
96 with open(path, newline='') as f:
97 code = f.read()
98 for case in _collect_file_tests(code, path, lines_to_execute):
99 yield case