7
7
import unittest
8
8
import astpretty
9
9
import subprocess
10
+ import threading
11
+ import multiprocessing
10
12
11
13
def test_file (path ):
12
14
with open (path ) as f :
13
15
expected_ast = astpretty .pformat (ast .parse (f .read ()), show_offsets = False )
14
- printer_output = subprocess .check_output (['cargo' , 'run' , path ])
15
- received_ast = astpretty .pformat (ast .parse (printer_output ), show_offsets = False )
16
+ printer_output = subprocess .check_output (['cargo' , '--quiet' , 'run' , path ])
17
+ try :
18
+ received_ast = astpretty .pformat (ast .parse (printer_output ), show_offsets = False )
19
+ except :
20
+ print ('Error while parsing the output from {}:' .format (path ))
21
+ raise
16
22
if expected_ast == received_ast :
17
23
print ('{}: ok' .format (path ))
18
24
return
@@ -38,23 +44,39 @@ def test_file(path):
38
44
print ('========================' )
39
45
exit (1 )
40
46
41
- def test_dir (path ):
47
+ def test_dir (path , with_threads = False ):
48
+ sem = threading .BoundedSemaphore (value = multiprocessing .cpu_count ())
49
+ def thread (path ):
50
+ sem .acquire ()
51
+ try :
52
+ test_file (path )
53
+ finally :
54
+ sem .release ()
55
+
42
56
for (dirpath , dirname , filenames ) in os .walk (path ):
43
57
if any (x .startswith ('.' ) for x in dirpath .split (os .path .sep )):
44
58
# dotfile
45
59
continue
46
60
for filename in filenames :
47
61
if os .path .splitext (filename )[1 ] == '.py' :
48
- test_file (os .path .join (dirpath , filename ))
62
+ if with_threads :
63
+ threading .Thread (target = thread , args = (os .path .join (dirpath , filename ),)).start ()
64
+ else :
65
+ test_file (os .path .join (dirpath , filename ))
49
66
50
67
51
68
52
69
def main ():
70
+ if '--with-threads' in sys .argv :
71
+ sys .argv .remove ('--with-threads' )
72
+ with_threads = True
73
+ else :
74
+ with_threads = False
53
75
for path in sys .argv [1 :]:
54
76
if os .path .isfile (path ):
55
77
test_file (path )
56
78
elif os .path .isdir (path ):
57
- test_dir (path )
79
+ test_dir (path , with_threads = with_threads )
58
80
else :
59
81
print ('Error: no such file or directory: {}' .format (path ))
60
82
exit (1 )
0 commit comments