12
12
from test .libregrtest .cmdline import _parse_args , Namespace
13
13
from test .libregrtest .runtest import (
14
14
findtests , split_test_packages , runtest , abs_module_name ,
15
- PROGRESS_MIN_TIME , State , FilterDict , RunTests , TestResult , TestList )
15
+ PROGRESS_MIN_TIME , State , RunTests , TestResult ,
16
+ FilterTuple , FilterDict , TestList )
16
17
from test .libregrtest .setup import setup_tests
17
18
from test .libregrtest .pgo import setup_pgo_tests
18
19
from test .libregrtest .utils import (strip_py_suffix , count , format_duration ,
@@ -62,10 +63,35 @@ def __init__(self, ns: Namespace):
62
63
# Namespace of command line options
63
64
self .ns : Namespace = ns
64
65
66
+ # Actions
67
+ self .want_header = ns .header
68
+ self .want_list_tests = ns .list_tests
69
+ self .want_list_cases = ns .list_cases
70
+ self .want_wait = ns .wait
71
+ self .want_cleanup = ns .cleanup
72
+
73
+ # Select tests
74
+ if ns .match_tests :
75
+ self .match_tests : FilterTuple = tuple (ns .match_tests )
76
+ else :
77
+ self .match_tests = None
78
+ if ns .ignore_tests :
79
+ self .ignore_tests : FilterTuple = tuple (ns .ignore_tests )
80
+ else :
81
+ self .ignore_tests = None
82
+ self .exclude = ns .exclude
83
+ self .fromfile = ns .fromfile
84
+ self .starting_test = ns .start
85
+
86
+ # Options to run tests
87
+ self .forever = ns .forever
88
+ self .randomize = ns .randomize
89
+ self .random_seed = ns .random_seed
90
+
65
91
# tests
66
92
self .tests = []
67
93
self .selected = []
68
- self .all_runtests : list [ RunTests ] = []
94
+ self .first_runtests : RunTests | None = None
69
95
70
96
# test results
71
97
self .good : TestList = []
@@ -187,12 +213,8 @@ def display_progress(self, test_index, text):
187
213
def find_tests (self ):
188
214
ns = self .ns
189
215
single = ns .single
190
- fromfile = ns .fromfile
191
216
pgo = ns .pgo
192
- exclude = ns .exclude
193
217
test_dir = ns .testdir
194
- starting_test = ns .start
195
- randomize = ns .randomize
196
218
197
219
if single :
198
220
self .next_single_filename = os .path .join (self .tmp_dir , 'pynexttest' )
@@ -203,12 +225,12 @@ def find_tests(self):
203
225
except OSError :
204
226
pass
205
227
206
- if fromfile :
228
+ if self . fromfile :
207
229
self .tests = []
208
230
# regex to match 'test_builtin' in line:
209
231
# '0:00:00 [ 4/400] test_builtin -- test_dict took 1 sec'
210
232
regex = re .compile (r'\btest_[a-zA-Z0-9_]+\b' )
211
- with open (os .path .join (os_helper .SAVEDCWD , fromfile )) as fp :
233
+ with open (os .path .join (os_helper .SAVEDCWD , self . fromfile )) as fp :
212
234
for line in fp :
213
235
line = line .split ('#' , 1 )[0 ]
214
236
line = line .strip ()
@@ -223,14 +245,14 @@ def find_tests(self):
223
245
setup_pgo_tests (ns )
224
246
225
247
exclude_tests = set ()
226
- if exclude :
248
+ if self . exclude :
227
249
for arg in ns .args :
228
250
exclude_tests .add (arg )
229
251
ns .args = []
230
252
231
253
alltests = findtests (testdir = test_dir , exclude = exclude_tests )
232
254
233
- if not fromfile :
255
+ if not self . fromfile :
234
256
self .selected = self .tests or ns .args
235
257
if self .selected :
236
258
self .selected = split_test_packages (self .selected )
@@ -248,17 +270,17 @@ def find_tests(self):
248
270
pass
249
271
250
272
# Remove all the selected tests that precede start if it's set.
251
- if starting_test :
273
+ if self . starting_test :
252
274
try :
253
- del self .selected [:self .selected .index (starting_test )]
275
+ del self .selected [:self .selected .index (self . starting_test )]
254
276
except ValueError :
255
- print (f"Cannot find starting test: { starting_test } " )
277
+ print (f"Cannot find starting test: { self . starting_test } " )
256
278
sys .exit (1 )
257
279
258
- if randomize :
259
- if ns .random_seed is None :
260
- ns .random_seed = random .randrange (10000000 )
261
- random .seed (ns .random_seed )
280
+ if self . randomize :
281
+ if self .random_seed is None :
282
+ self .random_seed = random .randrange (100_000_000 )
283
+ random .seed (self .random_seed )
262
284
random .shuffle (self .selected )
263
285
264
286
def list_tests (self ):
@@ -279,7 +301,7 @@ def list_cases(self):
279
301
ns = self .ns
280
302
test_dir = ns .testdir
281
303
support .verbose = False
282
- support .set_match_tests (ns .match_tests , ns .ignore_tests )
304
+ support .set_match_tests (self .match_tests , self .ignore_tests )
283
305
284
306
skipped = []
285
307
for test_name in self .selected :
@@ -306,20 +328,18 @@ def get_rerun_match(self, rerun_list) -> FilterDict:
306
328
rerun_match_tests [result .test_name ] = match_tests
307
329
return rerun_match_tests
308
330
309
- def _rerun_failed_tests (self , need_rerun ):
331
+ def _rerun_failed_tests (self , need_rerun , runtests : RunTests ):
310
332
# Configure the runner to re-run tests
311
333
ns = self .ns
312
334
ns .verbose = True
313
335
ns .failfast = False
314
336
ns .verbose3 = False
315
- ns .forever = False
316
337
if ns .use_mp is None :
317
338
ns .use_mp = 1
318
339
319
340
# Get tests to re-run
320
341
tests = [result .test_name for result in need_rerun ]
321
342
match_tests = self .get_rerun_match (need_rerun )
322
- self .set_tests (tests )
323
343
324
344
# Clear previously failed tests
325
345
self .rerun_bad .extend (self .bad )
@@ -328,11 +348,14 @@ def _rerun_failed_tests(self, need_rerun):
328
348
329
349
# Re-run failed tests
330
350
self .log (f"Re-running { len (tests )} failed tests in verbose mode in subprocesses" )
331
- runtests = RunTests (tuple (tests ), match_tests = match_tests , rerun = True )
332
- self .all_runtests .append (runtests )
351
+ runtests = runtests .copy (tests = tuple (tests ),
352
+ match_tests = match_tests ,
353
+ rerun = True ,
354
+ forever = False )
355
+ self .set_tests (runtests )
333
356
self ._run_tests_mp (runtests )
334
357
335
- def rerun_failed_tests (self , need_rerun ):
358
+ def rerun_failed_tests (self , need_rerun , runtests : RunTests ):
336
359
if self .ns .python :
337
360
# Temp patch for https://github.com/python/cpython/issues/94052
338
361
self .log (
@@ -344,7 +367,7 @@ def rerun_failed_tests(self, need_rerun):
344
367
self .first_state = self .get_tests_state ()
345
368
346
369
print ()
347
- self ._rerun_failed_tests (need_rerun )
370
+ self ._rerun_failed_tests (need_rerun , runtests )
348
371
349
372
if self .bad :
350
373
print (count (len (self .bad ), 'test' ), "failed again:" )
@@ -572,9 +595,9 @@ def _run_tests_mp(self, runtests: RunTests) -> None:
572
595
self .win_load_tracker .close ()
573
596
self .win_load_tracker = None
574
597
575
- def set_tests (self , tests ):
576
- self .tests = tests
577
- if self . ns .forever :
598
+ def set_tests (self , runtests : RunTests ):
599
+ self .tests = runtests . tests
600
+ if runtests .forever :
578
601
self .test_count_text = ''
579
602
self .test_count_width = 3
580
603
else :
@@ -583,7 +606,7 @@ def set_tests(self, tests):
583
606
584
607
def run_tests (self ):
585
608
# For a partial run, we do not need to clutter the output.
586
- if (self .ns . header
609
+ if (self .want_header
587
610
or not (self .ns .pgo or self .ns .quiet or self .ns .single
588
611
or self .tests or self .ns .args )):
589
612
self .display_header ()
@@ -595,17 +618,18 @@ def run_tests(self):
595
618
"3 warmup repetitions can give false positives!" )
596
619
print (msg , file = sys .stdout , flush = True )
597
620
598
- if self .ns . randomize :
599
- print ("Using random seed" , self .ns . random_seed )
621
+ if self .randomize :
622
+ print ("Using random seed" , self .random_seed )
600
623
601
624
tests = self .selected
602
- self . set_tests ( tests )
603
- runtests = RunTests ( tuple ( tests ), forever = self . ns . forever )
604
- self .all_runtests . append (runtests )
625
+ runtests = RunTests ( tuple ( tests ), forever = self . forever )
626
+ self . first_runtests = runtests
627
+ self .set_tests (runtests )
605
628
if self .ns .use_mp :
606
629
self ._run_tests_mp (runtests )
607
630
else :
608
631
self .run_tests_sequentially (runtests )
632
+ return runtests
609
633
610
634
def finalize (self ):
611
635
if self .next_single_filename :
@@ -627,11 +651,7 @@ def finalize(self):
627
651
628
652
def display_summary (self ):
629
653
duration = time .perf_counter () - self .start_time
630
- first_runtests = self .all_runtests [0 ]
631
- # the second runtests (re-run failed tests) disables forever,
632
- # use the first runtests
633
- forever = first_runtests .forever
634
- filtered = bool (self .ns .match_tests ) or bool (self .ns .ignore_tests )
654
+ filtered = bool (self .match_tests ) or bool (self .ignore_tests )
635
655
636
656
# Total duration
637
657
print ()
@@ -655,8 +675,8 @@ def display_summary(self):
655
675
self .environment_changed , self .run_no_tests ]
656
676
run = sum (map (len , all_tests ))
657
677
text = f'run={ run } '
658
- if not forever :
659
- ntest = len (first_runtests .tests )
678
+ if not self . first_runtests . forever :
679
+ ntest = len (self . first_runtests .tests )
660
680
text = f"{ text } /{ ntest } "
661
681
if filtered :
662
682
text = f"{ text } (filtered)"
@@ -788,7 +808,7 @@ def main(self, tests: TestList | None = None):
788
808
789
809
self .fix_umask ()
790
810
791
- if ns . cleanup :
811
+ if self . want_cleanup :
792
812
self .cleanup ()
793
813
sys .exit (0 )
794
814
@@ -838,12 +858,12 @@ def get_exitcode(self):
838
858
return exitcode
839
859
840
860
def action_run_tests (self ):
841
- self .run_tests ()
861
+ runtests = self .run_tests ()
842
862
self .display_result ()
843
863
844
864
need_rerun = self .need_rerun
845
865
if self .ns .rerun and need_rerun :
846
- self .rerun_failed_tests (need_rerun )
866
+ self .rerun_failed_tests (need_rerun , runtests )
847
867
848
868
self .display_summary ()
849
869
self .finalize ()
@@ -854,16 +874,16 @@ def _main(self):
854
874
run_tests_worker (self .ns .worker_args )
855
875
return
856
876
857
- if self .ns . wait :
877
+ if self .want_wait :
858
878
input ("Press any key to continue..." )
859
879
860
880
setup_tests (self .ns )
861
881
self .find_tests ()
862
882
863
883
exitcode = 0
864
- if self .ns . list_tests :
884
+ if self .want_list_tests :
865
885
self .list_tests ()
866
- elif self .ns . list_cases :
886
+ elif self .want_list_cases :
867
887
self .list_cases ()
868
888
else :
869
889
self .action_run_tests ()
0 commit comments