37
37
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
38
38
# SOFTWARE.
39
39
40
- import glob
41
- import os
42
- import subprocess
43
40
import sys
41
+
42
+ import glob
44
43
import test
45
44
45
+ import os
46
+ import subprocess
46
47
47
48
if os .environ .get ("ENABLE_CPYTHON_TAGGED_UNITTESTS" ) == "true" or __name__ == "__main__" :
48
49
TAGS_DIR = os .path .join (os .path .dirname (__file__ ), "unittest_tags" )
49
50
else :
50
51
TAGS_DIR = "null"
51
52
52
-
53
53
RUNNER = os .path .join (os .path .dirname (__file__ ), "run_cpython_test.py" )
54
54
55
55
@@ -61,7 +61,7 @@ def working_selectors(tagfile):
61
61
return None
62
62
63
63
64
- def working_tests ():
64
+ def collect_working_tests ():
65
65
working_tests = []
66
66
glob_pattern = os .path .join (TAGS_DIR , "*.txt" )
67
67
for arg in sys .argv :
@@ -72,39 +72,60 @@ def working_tests():
72
72
for tagfile in glob .glob (glob_pattern ):
73
73
test = os .path .splitext (os .path .basename (tagfile ))[0 ]
74
74
working_tests .append ((test , working_selectors (tagfile )))
75
- return working_tests
76
-
77
-
78
- class TestAllWorkingTests ():
79
- pass
80
-
81
- WORKING_TESTS = working_tests ()
82
- for idx , working_test in enumerate (WORKING_TESTS ):
83
- def make_test_func (working_test ):
84
- def fun (self ):
85
- cmd = [sys .executable ]
86
- if "--inspect" in sys .argv :
87
- cmd .append ("--inspect" )
88
- if "-debug-java" in sys .argv :
89
- cmd .append ("-debug-java" )
90
- cmd += ["-S" , RUNNER ]
91
- for testpattern in working_test [1 ]:
92
- cmd .extend (["-k" , testpattern ])
93
- testmod = working_test [0 ].rpartition ("." )[2 ]
94
- print ("Running test:" , working_test [0 ])
95
- testfile = os .path .join (os .path .dirname (test .__file__ ), "%s.py" % testmod )
96
- if not os .path .isfile (testfile ):
97
- testfile = os .path .join (os .path .dirname (test .__file__ ), "%s/__init__.py" % testmod )
98
- cmd .append (testfile )
99
- subprocess .check_call (cmd )
100
- print (working_test [0 ], "was finished." )
75
+ return sorted (working_tests )
76
+
77
+
78
+ def make_test_function (working_test ):
79
+ testmod = working_test [0 ].rpartition ("." )[2 ]
80
+
81
+ def test_tagged ():
82
+ cmd = [sys .executable ]
83
+ if "--inspect" in sys .argv :
84
+ cmd .append ("--inspect" )
85
+ if "-debug-java" in sys .argv :
86
+ cmd .append ("-debug-java" )
87
+ cmd += ["-S" , RUNNER ]
88
+ for testpattern in working_test [1 ]:
89
+ cmd .extend (["-k" , testpattern ])
90
+ print ("Running test:" , working_test [0 ])
91
+ testfile = os .path .join (os .path .dirname (test .__file__ ), "%s.py" % testmod )
92
+ if not os .path .isfile (testfile ):
93
+ testfile = os .path .join (os .path .dirname (test .__file__ ), "%s/__init__.py" % testmod )
94
+ cmd .append (testfile )
95
+ subprocess .check_call (cmd )
96
+ print (working_test [0 ], "was finished." )
101
97
102
- fun .__name__ = "%s[%d/%d]" % (working_test [0 ], idx + 1 , len (WORKING_TESTS ))
103
- return fun
98
+ if testmod .startswith ('test_' ):
99
+ test_tagged .__name__ = testmod
100
+ else :
101
+ test_tagged .__name__ = 'test_' + testmod
102
+ return test_tagged
103
+
104
+
105
+ def make_tests_class ():
106
+ import unittest
107
+
108
+ global TestTaggedUnittests
109
+
110
+ class TestTaggedUnittests (unittest .TestCase ):
111
+ pass
112
+
113
+ partial = os .environ .get ('TAGGED_UNITTEST_PARTIAL' )
114
+ if partial :
115
+ selected_str , total_str = partial .split ('/' , 1 )
116
+ selected = int (selected_str ) - 1
117
+ total = int (total_str )
118
+ else :
119
+ selected = 0
120
+ total = 1
121
+ assert selected < total
104
122
105
- test_f = make_test_func (working_test )
106
- setattr (TestAllWorkingTests , test_f .__name__ , test_f )
107
- del test_f
123
+ working_tests = collect_working_tests ()[selected ::total ]
124
+ for idx , working_test in enumerate (working_tests ):
125
+ fn = make_test_function (working_test )
126
+ fn .__name__ = "%s[%d/%d]" % (fn .__name__ , idx + 1 , len (working_tests ))
127
+ fn .__qualname__ = "%s.%s" % (TestTaggedUnittests .__name__ , fn .__name__ )
128
+ setattr (TestTaggedUnittests , fn .__name__ , staticmethod (fn ))
108
129
109
130
110
131
# This function has a unittest in test_tagger
@@ -130,11 +151,8 @@ def parse_unittest_output(output):
130
151
return
131
152
132
153
133
- if __name__ == "__main__" :
134
- # find working tests
135
- import re
136
-
137
- executable = sys .executable .split (" " ) # HACK: our sys.executable on Java is a cmdline
154
+ def main ():
155
+ executable = sys .executable .split (" " ) # HACK: our sys.executable on Java is a cmdline
138
156
kwargs = {"stdout" : subprocess .PIPE , "stderr" : subprocess .PIPE , "text" : True , "check" : False }
139
157
140
158
glob_pattern = os .path .join (os .path .dirname (test .__file__ ), "test_*.py" )
@@ -195,7 +213,7 @@ def parse_unittest_output(output):
195
213
# shouldn't be tried).
196
214
continue
197
215
198
- print ("[%d/%d, Try %d] Testing %s" % (idx + 1 , len (testfiles ), repeat + 1 , testmod ))
216
+ print ("[%d/%d, Try %d] Testing %s" % (idx + 1 , len (testfiles ), repeat + 1 , testmod ))
199
217
for selector in test_selectors :
200
218
cmd += ["-k" , selector ]
201
219
cmd .append (testfile )
@@ -232,8 +250,9 @@ def parse_unittest_output(output):
232
250
continue
233
251
print (f"Suite succeeded with { len (passing_tests )} tests" )
234
252
break
235
- elif p .returncode == - 9 :
236
- print (f"\n Timeout (return code -9)\n you can try to increase the current timeout { tout } s by using --timeout=NNN" )
253
+ elif p .returncode == - 9 :
254
+ print (
255
+ f"\n Timeout (return code -9)\n you can try to increase the current timeout { tout } s by using --timeout=NNN" )
237
256
break
238
257
else :
239
258
print (f"Suite failed, retrying with { len (passing_tests )} tests" )
@@ -246,3 +265,9 @@ def parse_unittest_output(output):
246
265
os .unlink (tagfile )
247
266
except Exception :
248
267
pass
268
+
269
+
270
+ if __name__ == '__main__' :
271
+ main ()
272
+ else :
273
+ make_tests_class ()
0 commit comments