aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/isolate_tests.py
diff options
context:
space:
mode:
authorchriseth <chris@ethereum.org>2018-11-14 02:33:35 +0800
committerGitHub <noreply@github.com>2018-11-14 02:33:35 +0800
commit1d4f565a64988a3400847d2655ca24f73f234bc6 (patch)
treecaaa6c26e307513505349b50ca4f2a8a9506752b /scripts/isolate_tests.py
parent59dbf8f1085b8b92e8b7eb0ce380cbeb642e97eb (diff)
parent91b6b8a88e76016e0324036cb7a7f9300a1e2439 (diff)
downloaddexon-solidity-1d4f565a64988a3400847d2655ca24f73f234bc6.tar
dexon-solidity-1d4f565a64988a3400847d2655ca24f73f234bc6.tar.gz
dexon-solidity-1d4f565a64988a3400847d2655ca24f73f234bc6.tar.bz2
dexon-solidity-1d4f565a64988a3400847d2655ca24f73f234bc6.tar.lz
dexon-solidity-1d4f565a64988a3400847d2655ca24f73f234bc6.tar.xz
dexon-solidity-1d4f565a64988a3400847d2655ca24f73f234bc6.tar.zst
dexon-solidity-1d4f565a64988a3400847d2655ca24f73f234bc6.zip
Merge pull request #5416 from ethereum/develop
Merge develop into release for 0.5.0
Diffstat (limited to 'scripts/isolate_tests.py')
-rwxr-xr-xscripts/isolate_tests.py93
1 files changed, 42 insertions, 51 deletions
diff --git a/scripts/isolate_tests.py b/scripts/isolate_tests.py
index 5bf577d3..8a9aa0a7 100755
--- a/scripts/isolate_tests.py
+++ b/scripts/isolate_tests.py
@@ -1,4 +1,4 @@
-#!/usr/bin/python
+#!/usr/bin/env python2
#
# This script reads C++ or RST source files and writes all
# multi-line strings into individual files.
@@ -10,7 +10,7 @@ import sys
import re
import os
import hashlib
-from os.path import join
+from os.path import join, isfile
def extract_test_cases(path):
lines = open(path, 'rb').read().splitlines()
@@ -35,47 +35,42 @@ def extract_test_cases(path):
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.
+# Look for `pragma solidity`, `contract`, `library` or `interface`
+# and abort a line not indented properly.
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
+ # 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)
+ ]
- 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]
+def write_cases(f, tests):
+ cleaned_filename = f.replace(".","_").replace("-","_").replace(" ","_").lower()
+ for test in tests:
+ open('test_%s_%s.sol' % (hashlib.sha256(test).hexdigest(), cleaned_filename), 'wb').write(test)
- return tests
-def write_cases(tests):
- for test in tests:
- open('test_%s.sol' % hashlib.sha256(test).hexdigest(), 'wb').write(test)
+def extract_and_write(f, path):
+ if docs:
+ cases = extract_docs_cases(path)
+ else:
+ if f.endswith('.sol'):
+ cases = [open(path, 'r').read()]
+ else:
+ cases = extract_test_cases(path)
+ write_cases(f, cases)
if __name__ == '__main__':
path = sys.argv[1]
@@ -83,18 +78,14 @@ if __name__ == '__main__':
if len(sys.argv) > 2 and sys.argv[2] == 'docs':
docs = True
- 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)
- if docs:
- cases = extract_docs_cases(path)
- else:
- if f.endswith(".sol"):
- cases = [open(path, "r").read()]
- else:
- cases = extract_test_cases(path)
- write_cases(cases)
+ if isfile(path):
+ extract_and_write(path, path)
+ else:
+ 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)