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
|
|
|
|
2017-03-15 11:07:59 +00:00
|
|
|
import re
|
2017-03-22 19:19:20 +00:00
|
|
|
import os
|
|
|
|
import hashlib
|
2021-07-05 17:38:41 +00:00
|
|
|
from os.path import join, isfile, basename
|
2021-07-01 13:19:38 +00:00
|
|
|
from argparse import ArgumentParser
|
2021-07-05 17:38:41 +00:00
|
|
|
from textwrap import indent, dedent
|
2016-12-06 22:21:38 +00:00
|
|
|
|
2017-07-10 21:52:47 +00:00
|
|
|
def extract_test_cases(path):
|
2021-06-30 08:21:41 +00:00
|
|
|
with open(path, encoding="utf8", errors='ignore', mode='r', newline='') as file:
|
|
|
|
lines = file.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
|
|
|
|
|
2021-07-05 17:38:41 +00:00
|
|
|
def extract_solidity_docs_cases(path):
|
|
|
|
tests = extract_docs_cases(path, [".. code-block:: solidity", '::'])
|
|
|
|
|
|
|
|
codeStart = "(// SPDX-License-Identifier:|pragma solidity|contract.*{|library.*{|interface.*{)"
|
|
|
|
|
|
|
|
# Filter out tests that are not supposed to be compilable.
|
|
|
|
return [
|
|
|
|
test.lstrip("\n")
|
|
|
|
for test in tests
|
|
|
|
if re.search(r'^\s{4}' + codeStart, test, re.MULTILINE) is not None
|
|
|
|
]
|
|
|
|
|
|
|
|
def extract_yul_docs_cases(path):
|
|
|
|
tests = extract_docs_cases(path, [".. code-block:: yul"])
|
|
|
|
|
|
|
|
def wrap_in_object(code):
|
|
|
|
for line in code.splitlines():
|
|
|
|
line = line.lstrip()
|
|
|
|
if line.startswith("//"):
|
|
|
|
continue
|
|
|
|
if not line.startswith("object") and not line.startswith("{"):
|
2021-09-16 17:22:23 +00:00
|
|
|
return indent(f"{{\n{code.rstrip()}\n}}\n\n", " ")
|
2021-07-05 17:38:41 +00:00
|
|
|
break
|
|
|
|
|
|
|
|
return code
|
|
|
|
|
|
|
|
return [
|
|
|
|
wrap_in_object(test)
|
|
|
|
for test in tests
|
|
|
|
if test.strip() != ""
|
|
|
|
]
|
|
|
|
|
|
|
|
# Extract code examples based on the 'beginMarker' parameter
|
2021-07-01 13:19:38 +00:00
|
|
|
# up until we reach EOF or a line that is not empty and doesn't start with 4
|
|
|
|
# spaces.
|
2021-07-05 17:38:41 +00:00
|
|
|
def extract_docs_cases(path, beginMarkers):
|
2021-07-01 13:19:38 +00:00
|
|
|
immediatelyAfterMarker = False
|
2021-06-21 18:53:21 +00:00
|
|
|
insideBlock = False
|
2017-07-10 21:52:47 +00:00
|
|
|
tests = []
|
|
|
|
|
2018-08-09 18:48:41 +00:00
|
|
|
# Collect all snippets of indented blocks
|
2021-06-30 08:21:41 +00:00
|
|
|
with open(path, mode='r', errors='ignore', encoding='utf8', newline='') as f:
|
|
|
|
lines = f.read().splitlines()
|
2021-07-01 13:19:38 +00:00
|
|
|
|
|
|
|
for line in lines:
|
2021-06-21 18:53:21 +00:00
|
|
|
if insideBlock:
|
2021-07-01 13:19:38 +00:00
|
|
|
if immediatelyAfterMarker:
|
|
|
|
# Skip Sphinx instructions and empty lines between them
|
|
|
|
if line == '' or line.lstrip().startswith(":"):
|
|
|
|
continue
|
|
|
|
|
|
|
|
if line == '' or line.startswith(" "):
|
|
|
|
tests[-1] += line + "\n"
|
|
|
|
immediatelyAfterMarker = False
|
2021-07-05 17:38:41 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
insideBlock = False
|
|
|
|
if any(map(line.lower().startswith, beginMarkers)):
|
2021-07-01 13:19:38 +00:00
|
|
|
insideBlock = True
|
|
|
|
immediatelyAfterMarker = True
|
|
|
|
tests += ['']
|
2019-10-23 20:13:17 +00:00
|
|
|
|
2021-07-05 17:38:41 +00:00
|
|
|
return tests
|
2016-12-06 22:21:38 +00:00
|
|
|
|
2021-07-05 17:38:41 +00:00
|
|
|
def write_cases(f, solidityTests, yulTests):
|
2018-09-06 09:37:44 +00:00
|
|
|
cleaned_filename = f.replace(".","_").replace("-","_").replace(" ","_").lower()
|
2021-07-05 17:38:41 +00:00
|
|
|
for language, test in [("sol", t) for t in solidityTests] + [("yul", t) for t in yulTests]:
|
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.
|
2021-07-05 17:38:41 +00:00
|
|
|
remainder = dedent(test)
|
2021-09-16 17:22:23 +00:00
|
|
|
hash = hashlib.sha256(test.encode("utf-8")).hexdigest()
|
|
|
|
sol_filename = f'test_{hash}_{cleaned_filename}.{language}'
|
2021-06-30 08:21:41 +00:00
|
|
|
with open(sol_filename, mode='w', encoding='utf8', newline='') as fi:
|
|
|
|
fi.write(remainder)
|
2018-07-05 00:20:17 +00:00
|
|
|
|
2021-07-05 17:38:41 +00:00
|
|
|
def extract_and_write(path, language):
|
|
|
|
assert language in ["solidity", "yul", ""]
|
|
|
|
yulCases = []
|
|
|
|
cases = []
|
|
|
|
|
2021-07-01 13:19:38 +00:00
|
|
|
if path.lower().endswith('.rst'):
|
2021-07-05 17:38:41 +00:00
|
|
|
if language in ("solidity", ""):
|
|
|
|
cases = extract_solidity_docs_cases(path)
|
|
|
|
|
|
|
|
if language in ("yul", ""):
|
|
|
|
yulCases = extract_yul_docs_cases(path)
|
2021-07-01 13:19:38 +00:00
|
|
|
elif path.endswith('.sol'):
|
2021-07-05 17:38:41 +00:00
|
|
|
if language in ("solidity", ""):
|
|
|
|
with open(path, mode='r', encoding='utf8', newline='') as f:
|
|
|
|
cases = [f.read()]
|
2020-04-17 12:32:38 +00:00
|
|
|
else:
|
2021-07-01 13:19:38 +00:00
|
|
|
cases = extract_test_cases(path)
|
|
|
|
|
2021-07-05 17:38:41 +00:00
|
|
|
write_cases(basename(path), cases, yulCases)
|
2018-07-05 00:20:17 +00:00
|
|
|
|
2016-12-06 22:21:38 +00:00
|
|
|
if __name__ == '__main__':
|
2021-07-01 13:19:38 +00:00
|
|
|
script_description = (
|
|
|
|
"Reads Solidity, C++ or RST source files and extracts compilable solidity and yul code blocks from them. "
|
|
|
|
"Can be used to generate test cases to validade code examples. "
|
|
|
|
)
|
2021-07-01 13:18:32 +00:00
|
|
|
|
2021-07-01 13:19:38 +00:00
|
|
|
parser = ArgumentParser(description=script_description)
|
|
|
|
parser.add_argument(dest='path', help='Path to file or directory to look for code in.')
|
2021-07-05 17:38:41 +00:00
|
|
|
parser.add_argument(
|
|
|
|
'-l', '--language',
|
|
|
|
dest='language',
|
|
|
|
choices=["yul", "solidity"],
|
|
|
|
default="",
|
|
|
|
action='store',
|
|
|
|
help="Extract only code blocks in the given language"
|
|
|
|
)
|
2021-07-01 13:19:38 +00:00
|
|
|
options = parser.parse_args()
|
|
|
|
path = options.path
|
2016-12-06 22:21:38 +00:00
|
|
|
|
2018-07-05 00:20:17 +00:00
|
|
|
if isfile(path):
|
2021-07-05 17:38:41 +00:00
|
|
|
extract_and_write(path, options.language)
|
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:
|
2021-07-01 13:19:38 +00:00
|
|
|
if basename(f) == "invalid_utf8_sequence.sol":
|
2020-08-20 00:35:15 +00:00
|
|
|
continue # ignore the test with broken utf-8 encoding
|
2018-07-05 00:20:17 +00:00
|
|
|
path = join(root, f)
|
2021-07-05 17:38:41 +00:00
|
|
|
extract_and_write(path, options.language)
|