23
23
from mypy .server .update import FineGrainedBuildManager
24
24
from mypy .strconv import StrConv , indent
25
25
from mypy .test .config import test_temp_dir , test_data_prefix
26
- from mypy .test .data import parse_test_cases , DataDrivenTestCase , DataSuite , UpdateFile
27
- from mypy .test .helpers import assert_string_arrays_equal
26
+ from mypy .test .data import (
27
+ parse_test_cases , DataDrivenTestCase , DataSuite , UpdateFile , module_from_path
28
+ )
29
+ from mypy .test .helpers import assert_string_arrays_equal , parse_options
28
30
from mypy .test .testtypegen import ignore_node
29
31
from mypy .types import TypeStrVisitor , Type
30
32
from mypy .util import short_type
@@ -42,7 +44,8 @@ class FineGrainedSuite(DataSuite):
42
44
43
45
def run_case (self , testcase : DataDrivenTestCase ) -> None :
44
46
main_src = '\n ' .join (testcase .input )
45
- messages , manager , graph = self .build (main_src )
47
+ sources_override = self .parse_sources (main_src )
48
+ messages , manager , graph = self .build (main_src , testcase , sources_override )
46
49
47
50
a = []
48
51
if messages :
@@ -63,6 +66,10 @@ def run_case(self, testcase: DataDrivenTestCase) -> None:
63
66
# Delete file
64
67
os .remove (op .path )
65
68
modules .append ((op .module , op .path ))
69
+ if sources_override is not None :
70
+ modules = [(module , path )
71
+ for module , path in sources_override
72
+ if any (m == module for m , _ in modules )]
66
73
new_messages = fine_grained_manager .update (modules )
67
74
all_triggered .append (fine_grained_manager .triggered )
68
75
new_messages = normalize_messages (new_messages )
@@ -85,16 +92,28 @@ def run_case(self, testcase: DataDrivenTestCase) -> None:
85
92
'Invalid active triggers ({}, line {})' .format (testcase .file ,
86
93
testcase .line ))
87
94
88
- def build (self , source : str ) -> Tuple [List [str ], BuildManager , Graph ]:
89
- options = Options ()
95
+ def build (self ,
96
+ source : str ,
97
+ testcase : DataDrivenTestCase ,
98
+ sources_override : Optional [List [Tuple [str , str ]]]) -> Tuple [List [str ],
99
+ BuildManager ,
100
+ Graph ]:
101
+ # This handles things like '# flags: --foo'.
102
+ options = parse_options (source , testcase , incremental_step = 1 )
90
103
options .incremental = True
91
104
options .use_builtins_fixtures = True
92
105
options .show_traceback = True
93
106
main_path = os .path .join (test_temp_dir , 'main' )
94
107
with open (main_path , 'w' ) as f :
95
108
f .write (source )
109
+ if sources_override is not None :
110
+ sources = [BuildSource (path , module , None )
111
+ for module , path in sources_override ]
112
+ else :
113
+ sources = [BuildSource (main_path , None , None )]
114
+ print (sources )
96
115
try :
97
- result = build .build (sources = [ BuildSource ( main_path , None , None )] ,
116
+ result = build .build (sources = sources ,
98
117
options = options ,
99
118
alt_lib_path = test_temp_dir )
100
119
except CompileError as e :
@@ -112,6 +131,27 @@ def format_triggered(self, triggered: List[List[str]]) -> List[str]:
112
131
result .append (('%d: %s' % (n + 2 , ', ' .join (filtered ))).strip ())
113
132
return result
114
133
134
+ def parse_sources (self , program_text : str ) -> Optional [List [Tuple [str , str ]]]:
135
+ """Return target (module, path) tuples for a test case, if not using the defaults.
136
+
137
+ These are defined through a comment like '# cmd: main a.py' in the test case
138
+ description.
139
+ """
140
+ # TODO: Support defining separately for each incremental step.
141
+ m = re .search ('# cmd: mypy ([a-zA-Z0-9_. ]+)$' , program_text , flags = re .MULTILINE )
142
+ if m :
143
+ # The test case wants to use a non-default set of files.
144
+ paths = m .group (1 ).strip ().split ()
145
+ result = []
146
+ for path in paths :
147
+ path = os .path .join (test_temp_dir , path )
148
+ module = module_from_path (path )
149
+ if module == 'main' :
150
+ module = '__main__'
151
+ result .append ((module , path ))
152
+ return result
153
+ return None
154
+
115
155
116
156
def normalize_messages (messages : List [str ]) -> List [str ]:
117
157
return [re .sub ('^tmp' + re .escape (os .sep ), '' , message )
0 commit comments