2018-06-15 14:29:42 +00:00
|
|
|
#!/usr/bin/env python2
|
2016-09-30 11:09:45 +00:00
|
|
|
#
|
2017-07-10 21:52:47 +00:00
|
|
|
# This script reads C++ or RST source files and writes all
|
2016-09-30 11:09:45 +00:00
|
|
|
# multi-line strings into individual files.
|
|
|
|
# This can be used to extract the Solidity test cases
|
2016-10-10 20:04:11 +00:00
|
|
|
# into files for e.g. fuzz testing as
|
2016-12-06 22:21:38 +00:00
|
|
|
# scripts/isolate_tests.py test/libsolidity/*
|
2016-09-30 11:09:45 +00:00
|
|
|
|
|
|
|
import sys
|
2017-03-15 11:07:59 +00:00
|
|
|
import re
|
2017-03-22 19:19:20 +00:00
|
|
|
import os
|
|
|
|
import hashlib
|
2018-07-05 00:20:17 +00:00
|
|
|
from os.path import join, isfile
|
2016-12-06 22:21:38 +00:00
|
|
|
|
2017-07-10 21:52:47 +00:00
|
|
|
def extract_test_cases(path):
|
2017-03-22 19:19:20 +00:00
|
|
|
lines = open(path, 'rb').read().splitlines()
|
2016-12-06 22:21:38 +00:00
|
|
|
|
|
|
|
inside = False
|
2017-03-15 11:07:59 +00:00
|
|
|
delimiter = ''
|
2016-12-06 22:21:38 +00:00
|
|
|
tests = []
|
|
|
|
|
|
|
|
for l in lines:
|
|
|
|
if inside:
|
2017-03-15 11:07:59 +00:00
|
|
|
if l.strip().endswith(')' + delimiter + '";'):
|
2016-12-06 22:21:38 +00:00
|
|
|
inside = False
|
|
|
|
else:
|
|
|
|
tests[-1] += l + '\n'
|
|
|
|
else:
|
2017-03-15 11:07:59 +00:00
|
|
|
m = re.search(r'R"([^(]*)\($', l.strip())
|
|
|
|
if m:
|
2016-12-06 22:21:38 +00:00
|
|
|
inside = True
|
2017-03-15 11:07:59 +00:00
|
|
|
delimiter = m.group(1)
|
2016-12-06 22:21:38 +00:00
|
|
|
tests += ['']
|
|
|
|
|
|
|
|
return tests
|
|
|
|
|
2017-07-10 21:52:47 +00:00
|
|
|
# Contract sources are indented by 4 spaces.
|
2018-08-09 18:48:41 +00:00
|
|
|
# Look for `pragma solidity`, `contract`, `library` or `interface`
|
|
|
|
# and abort a line not indented properly.
|
2017-07-10 21:52:47 +00:00
|
|
|
def extract_docs_cases(path):
|
|
|
|
inside = False
|
|
|
|
tests = []
|
|
|
|
|
2018-08-09 18:48:41 +00:00
|
|
|
# Collect all snippets of indented blocks
|
|
|
|
for l in open(path, 'rb').read().splitlines():
|
|
|
|
if l != '':
|
|
|
|
if not inside and l.startswith(' '):
|
|
|
|
# start new test
|
|
|
|
tests += ['']
|
|
|
|
inside = l.startswith(' ')
|
|
|
|
if inside:
|
|
|
|
tests[-1] += l + '\n'
|
|
|
|
# Filter all tests that do not contain Solidity
|
|
|
|
return [
|
|
|
|
test for test in tests
|
|
|
|
if re.search(r'^ [ ]*(pragma solidity|contract |library |interface )', test, re.MULTILINE)
|
|
|
|
]
|
2016-12-06 22:21:38 +00:00
|
|
|
|
2018-09-06 09:37:44 +00:00
|
|
|
def write_cases(f, tests):
|
|
|
|
cleaned_filename = f.replace(".","_").replace("-","_").replace(" ","_").lower()
|
2017-03-22 19:19:20 +00:00
|
|
|
for test in tests:
|
2019-08-05 11:00:30 +00:00
|
|
|
# When code examples are extracted they indented by 8 spaces, which violates the style guide,
|
|
|
|
# so before checking remove 4 spaces from each line.
|
|
|
|
remainder = re.sub(r'^ {4}', '', test, 0, re.MULTILINE)
|
|
|
|
open('test_%s_%s.sol' % (hashlib.sha256(test).hexdigest(), cleaned_filename), 'wb').write(remainder)
|
2018-07-05 00:20:17 +00:00
|
|
|
|
|
|
|
def extract_and_write(f, path):
|
|
|
|
if docs:
|
|
|
|
cases = extract_docs_cases(path)
|
|
|
|
else:
|
2018-07-05 00:31:08 +00:00
|
|
|
if f.endswith('.sol'):
|
|
|
|
cases = [open(path, 'r').read()]
|
2018-07-05 00:20:17 +00:00
|
|
|
else:
|
|
|
|
cases = extract_test_cases(path)
|
2018-09-06 09:37:44 +00:00
|
|
|
write_cases(f, cases)
|
2018-07-05 00:20:17 +00:00
|
|
|
|
2016-12-06 22:21:38 +00:00
|
|
|
if __name__ == '__main__':
|
2017-03-22 19:19:20 +00:00
|
|
|
path = sys.argv[1]
|
2017-07-10 21:52:47 +00:00
|
|
|
docs = False
|
|
|
|
if len(sys.argv) > 2 and sys.argv[2] == 'docs':
|
|
|
|
docs = True
|
2016-12-06 22:21:38 +00:00
|
|
|
|
2018-07-05 00:20:17 +00:00
|
|
|
if isfile(path):
|
|
|
|
extract_and_write(path, path)
|
2018-09-03 13:01:15 +00:00
|
|
|
else:
|
2018-07-05 00:20:17 +00:00
|
|
|
for root, subdirs, files in os.walk(path):
|
|
|
|
if '_build' in subdirs:
|
|
|
|
subdirs.remove('_build')
|
|
|
|
if 'compilationTests' in subdirs:
|
|
|
|
subdirs.remove('compilationTests')
|
|
|
|
for f in files:
|
|
|
|
path = join(root, f)
|
|
|
|
extract_and_write(f, path)
|