Skip to content

Commit ffa8b20

Browse files
committed
Optionally use threads in test_on_corpus.py.
1 parent ba7a0a6 commit ffa8b20

File tree

1 file changed

+27
-5
lines changed

1 file changed

+27
-5
lines changed

test_on_corpus.py

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,18 @@
77
import unittest
88
import astpretty
99
import subprocess
10+
import threading
11+
import multiprocessing
1012

1113
def test_file(path):
1214
with open(path) as f:
1315
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
1622
if expected_ast == received_ast:
1723
print('{}: ok'.format(path))
1824
return
@@ -38,23 +44,39 @@ def test_file(path):
3844
print('========================')
3945
exit(1)
4046

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+
4256
for (dirpath, dirname, filenames) in os.walk(path):
4357
if any(x.startswith('.') for x in dirpath.split(os.path.sep)):
4458
# dotfile
4559
continue
4660
for filename in filenames:
4761
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))
4966

5067

5168

5269
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
5375
for path in sys.argv[1:]:
5476
if os.path.isfile(path):
5577
test_file(path)
5678
elif os.path.isdir(path):
57-
test_dir(path)
79+
test_dir(path, with_threads=with_threads)
5880
else:
5981
print('Error: no such file or directory: {}'.format(path))
6082
exit(1)

0 commit comments

Comments
 (0)