Upgrade isolate_tests.py to support extracting code from docs

This commit is contained in:
Alex Beregszaszi 2017-07-10 22:52:47 +01:00
parent b5da5f6e42
commit 32a76f15e0

View File

@ -1,6 +1,6 @@
#!/usr/bin/python #!/usr/bin/python
# #
# This script reads C++ source files and writes all # This script reads C++ or RST source files and writes all
# multi-line strings into individual files. # multi-line strings into individual files.
# This can be used to extract the Solidity test cases # This can be used to extract the Solidity test cases
# into files for e.g. fuzz testing as # into files for e.g. fuzz testing as
@ -12,7 +12,7 @@ import os
import hashlib import hashlib
from os.path import join from os.path import join
def extract_cases(path): def extract_test_cases(path):
lines = open(path, 'rb').read().splitlines() lines = open(path, 'rb').read().splitlines()
inside = False inside = False
@ -34,6 +34,44 @@ def extract_cases(path):
return tests return tests
# Contract sources are indented by 4 spaces.
# Look for `pragma solidity` and abort a line not indented properly.
# If the comment `// This will not compile` is above the pragma,
# the code is skipped.
def extract_docs_cases(path):
# Note: this code works, because splitlines() removes empty new lines
# and thus even if the empty new lines are missing indentation
lines = open(path, 'rb').read().splitlines()
ignore = False
inside = False
tests = []
for l in lines:
if inside:
# Abort if indentation is missing
m = re.search(r'^[^ ]+', l)
if m:
inside = False
else:
tests[-1] += l + '\n'
else:
m = re.search(r'^ // This will not compile', l)
if m:
ignore = True
if ignore:
# Abort if indentation is missing
m = re.search(r'^[^ ]+', l)
if m:
ignore = False
else:
m = re.search(r'^ pragma solidity .*[0-9]+\.[0-9]+\.[0-9]+;$', l)
if m:
inside = True
tests += [l]
return tests
def write_cases(tests): def write_cases(tests):
for test in tests: for test in tests:
@ -41,8 +79,15 @@ def write_cases(tests):
if __name__ == '__main__': if __name__ == '__main__':
path = sys.argv[1] path = sys.argv[1]
docs = False
if len(sys.argv) > 2 and sys.argv[2] == 'docs':
docs = True
for root, dir, files in os.walk(path): for root, dir, files in os.walk(path):
for f in files: for f in files:
cases = extract_cases(join(root, f)) path = join(root, f)
if docs:
cases = extract_docs_cases(path)
else:
cases = extract_test_cases(path)
write_cases(cases) write_cases(cases)