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
40
41
import os
41
42
import subprocess
42
43
import sys
43
44
import test
44
45
45
46
46
- TAGS_FILE = os .path .join (os .path .dirname (__file__ ), "working_unittests.txt " )
47
+ TAGS_DIR = os .path .join (os .path .dirname (__file__ ), "unittest_tags " )
47
48
48
49
49
- def working_tests ():
50
- working_tests = []
50
+ def working_selectors (tagfile ):
51
+ if os .path .exists (tagfile ):
52
+ with open (tagfile ) as f :
53
+ return [line .strip () for line in f if line ]
54
+ else :
55
+ return []
51
56
52
- def parse_line (iterator , line ):
53
- line = line .strip ()
54
- if line .endswith (":" ):
55
- test_selectors = []
56
- for testline in iterator :
57
- if testline .startswith (" " ):
58
- test_selectors .append (testline .strip ())
59
- else :
60
- parse_line (iterator , testline )
61
- working_tests .append ((line [:- 1 ], test_selectors ))
62
- elif line :
63
- working_tests .append (line )
64
-
65
- with open (TAGS_FILE ) as f :
66
- fiter = iter (f )
67
- for line in fiter :
68
- parse_line (fiter , line )
69
57
58
+ def working_tests ():
59
+ working_tests = []
60
+ for tagfile in glob .glob (os .path .join (TAGS_DIR , "*.txt" )):
61
+ test = os .path .splitext (os .path .basename (tagfile ))[0 ]
62
+ working_tests .append ((test , working_selectors (tagfile )))
70
63
return working_tests
71
64
72
65
73
66
for working_test in working_tests ():
74
67
def make_test_func (working_test ):
75
68
def fun ():
76
- if isinstance (working_test , str ):
77
- subprocess .check_call ([sys .executable , "-m" , working_test ])
69
+ if not working_test [1 ]: # no selectors, run entire test module
70
+ # TODO: remove branch
71
+ subprocess .check_call ([sys .executable , "-m" , "test." + working_test [0 ]])
78
72
else :
79
73
cmd = [sys .executable , "-m" , "unittest" ]
80
74
for testpattern in working_test [1 ]:
@@ -83,7 +77,7 @@ def fun():
83
77
cmd .append (os .path .join (os .path .dirname (test .__file__ ), "%s.py" % testmod ))
84
78
subprocess .check_call (cmd )
85
79
86
- fun .__name__ = working_test if isinstance ( working_test , str ) else working_test [0 ]
80
+ fun .__name__ = working_test [0 ]
87
81
return fun
88
82
89
83
test_f = make_test_func (working_test )
@@ -93,36 +87,53 @@ def fun():
93
87
94
88
if __name__ == "__main__" :
95
89
# find working tests
96
- import glob
97
90
import re
98
91
99
92
executable = sys .executable .split (" " ) # HACK: our sys.executable on Java is a cmdline
100
- re_success = re .compile ("(test[^ ]+).* ... ok" )
101
- with open (TAGS_FILE , "w" ) as f :
102
- for testfile in glob .glob (os .path .join (os .path .dirname (test .__file__ ), "test_*.py" )):
103
- testmod = "test.%s" % os .path .splitext (os .path .basename (testfile ))[0 ]
93
+ re_success = re .compile ("^(test\S+)[^\r \n ]* \.\.\. ok$" , re .MULTILINE )
94
+ kwargs = {"stdout" : subprocess .PIPE , "stderr" : subprocess .PIPE , "text" : True , "check" : False }
95
+
96
+ if len (sys .argv ) > 1 :
97
+ glob_pattern = sys .argv [1 ]
98
+ else :
99
+ glob_pattern = os .path .join (os .path .dirname (test .__file__ ), "test_*.py" )
100
+
101
+ for testfile in glob .glob (glob_pattern ):
102
+ testfile_stem = os .path .splitext (os .path .basename (testfile ))[0 ]
103
+ testmod = "test." + testfile_stem
104
+ cmd = ["/usr/bin/timeout" , "-s" , "9" , "60" ] + executable + ["-m" ]
105
+ tagfile = os .path .join (TAGS_DIR , testfile_stem + ".txt" )
106
+ test_selectors = working_selectors (tagfile )
107
+
108
+ if not test_selectors :
109
+ # TODO: remove branch
104
110
print ("Testing" , testmod )
105
- try :
106
- subprocess .check_call (["/usr/bin/timeout" , "-s" , "9" , "60" ] + executable + ["-m" , testmod , "-f" ])
107
- f .write (testmod )
108
- f .write ("\n " )
109
- except BaseException as e :
110
- print (e )
111
- p = subprocess .run (["/usr/bin/timeout" , "-s" , "9" , "60" ] + executable + ["-m" , testmod , "-v" ], stdout = subprocess .PIPE , stderr = subprocess .PIPE , text = True , check = False )
112
- print ("***" )
113
- print (p .stdout )
114
- print ("***" )
115
- print (p .stderr )
116
- print ("***" )
117
- passing_tests = []
118
- for m in re_success .findall (p .stdout ):
119
- passing_tests .append (m )
120
- for m in re_success .findall (p .stderr ):
121
- passing_tests .append (m )
122
- if passing_tests :
123
- f .write (testmod )
124
- f .write (":\n " )
125
- for passing_test in passing_tests :
126
- f .write (" " )
127
- f .write (passing_test )
128
- f .write ("\n " )
111
+ cmd += [testmod , "-v" ]
112
+ else :
113
+ print ("Testing tagged subset of" , testmod )
114
+ cmd += ["unittest" , "-v" ]
115
+ for selector in test_selectors :
116
+ cmd += ["-k" , selector ]
117
+ cmd .append (testfile )
118
+
119
+ p = subprocess .run (cmd , ** kwargs )
120
+ print ("*stdout*" )
121
+ print (p .stdout )
122
+ print ("*stderr*" )
123
+ print (p .stderr )
124
+
125
+ if p .returncode == 0 :
126
+ with open (tagfile , "w" ) as f :
127
+ pass
128
+ else :
129
+ passing_tests = []
130
+ for m in re_success .findall (p .stdout ):
131
+ passing_tests .append (m )
132
+ for m in re_success .findall (p .stderr ):
133
+ passing_tests .append (m )
134
+ with open (tagfile , "w" ) as f :
135
+ for passing_test in passing_tests :
136
+ f .write (passing_test )
137
+ f .write ("\n " )
138
+ if not passing_tests :
139
+ os .unlink (tagfile )
0 commit comments