2020-01-13 15:14:18 +00:00
|
|
|
#!/usr/bin/env python3
|
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
|
2020-08-20 00:35:15 +00:00
|
|
|
from os.path import join, isfile, split
|
2016-12-06 22:21:38 +00:00
|
|
|
|
2017-07-10 21:52:47 +00:00
|
|
|
def extract_test_cases(path):
|
2021-01-19 15:56:27 +00:00
|
|
|
lines = open(path, encoding="utf8", errors='ignore', mode='r', newline='').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:
|
2020-04-17 12:32:38 +00:00
|
|
|
if inside:
|
|
|
|
if l.strip().endswith(')' + delimiter + '";'):
|
|
|
|
inside = False
|
|
|
|
else:
|
|
|
|
tests[-1] += l + '\n'
|
2016-12-06 22:21:38 +00:00
|
|
|
else:
|
2020-04-17 12:32:38 +00:00
|
|
|
m = re.search(r'R"([^(]*)\($', l.strip())
|
|
|
|
if m:
|
|
|
|
inside = True
|
|
|
|
delimiter = m.group(1)
|
|
|
|
tests += ['']
|
2016-12-06 22:21:38 +00:00
|
|
|
|
|
|
|
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
|
2019-10-23 20:13:17 +00:00
|
|
|
extractedLines = []
|
2017-07-10 21:52:47 +00:00
|
|
|
tests = []
|
|
|
|
|
2018-08-09 18:48:41 +00:00
|
|
|
# Collect all snippets of indented blocks
|
2021-01-19 15:56:27 +00:00
|
|
|
for l in open(path, mode='r', errors='ignore', encoding='utf8', newline='').read().splitlines():
|
2018-08-09 18:48:41 +00:00
|
|
|
if l != '':
|
|
|
|
if not inside and l.startswith(' '):
|
|
|
|
# start new test
|
2019-10-23 20:13:17 +00:00
|
|
|
extractedLines += ['']
|
2018-08-09 18:48:41 +00:00
|
|
|
inside = l.startswith(' ')
|
|
|
|
if inside:
|
2019-10-23 20:13:17 +00:00
|
|
|
extractedLines[-1] += l + '\n'
|
|
|
|
|
2020-05-13 15:41:56 +00:00
|
|
|
codeStart = "(// SPDX-License-Identifier:|pragma solidity|contract.*{|library.*{|interface.*{)"
|
2019-10-23 20:13:17 +00:00
|
|
|
|
2021-03-10 19:40:18 +00:00
|
|
|
# Filter all tests that do not contain Solidity or are indented incorrectly.
|
2019-10-23 20:13:17 +00:00
|
|
|
for lines in extractedLines:
|
|
|
|
if re.search(r'^\s{0,3}' + codeStart, lines, re.MULTILINE):
|
2021-03-10 19:40:18 +00:00
|
|
|
print("Indentation error in " + path + ":")
|
2019-10-23 20:13:17 +00:00
|
|
|
print(lines)
|
|
|
|
exit(1)
|
|
|
|
if re.search(r'^\s{4}' + codeStart, lines, re.MULTILINE):
|
|
|
|
tests.append(lines)
|
|
|
|
|
|
|
|
return tests
|
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:
|
2021-03-10 19:40:18 +00:00
|
|
|
# When code examples are extracted they are indented by 8 spaces, which violates the style guide,
|
2019-08-05 11:00:30 +00:00
|
|
|
# so before checking remove 4 spaces from each line.
|
|
|
|
remainder = re.sub(r'^ {4}', '', test, 0, re.MULTILINE)
|
2020-01-13 15:14:18 +00:00
|
|
|
sol_filename = 'test_%s_%s.sol' % (hashlib.sha256(test.encode("utf-8")).hexdigest(), cleaned_filename)
|
2021-01-19 15:56:27 +00:00
|
|
|
open(sol_filename, mode='w', encoding='utf8', newline='').write(remainder)
|
2018-07-05 00:20:17 +00:00
|
|
|
|
|
|
|
def extract_and_write(f, path):
|
2020-04-17 12:32:38 +00:00
|
|
|
if docs:
|
|
|
|
cases = extract_docs_cases(path)
|
|
|
|
else:
|
|
|
|
if f.endswith('.sol'):
|
2021-01-19 15:56:27 +00:00
|
|
|
cases = [open(path, mode='r', encoding='utf8', newline='').read()]
|
2018-07-05 00:20:17 +00:00
|
|
|
else:
|
2020-04-17 12:32:38 +00:00
|
|
|
cases = extract_test_cases(path)
|
|
|
|
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':
|
2020-04-17 12:32:38 +00:00
|
|
|
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:
|
2020-08-20 00:35:15 +00:00
|
|
|
_, tail = split(f)
|
|
|
|
if tail == "invalid_utf8_sequence.sol":
|
|
|
|
continue # ignore the test with broken utf-8 encoding
|
2018-07-05 00:20:17 +00:00
|
|
|
path = join(root, f)
|
|
|
|
extract_and_write(f, path)
|