work in progress: setup test libs, pyspec, pytests

This commit is contained in:
protolambda 2019-03-28 00:28:20 +08:00
parent c978bb4a67
commit 53e528e56c
No known key found for this signature in database
GPG Key ID: EC89FDBB2B4C7623
32 changed files with 143 additions and 2 deletions

6
.gitignore vendored
View File

@ -1,7 +1,11 @@
*.pyc
/__pycache__
/venv
venv
.venvs
.venv
/.pytest_cache
build/
output/
yaml_tests/

View File

@ -0,0 +1,4 @@
ETH 2.0 test generator helpers
`eth2_test_gen_base`: A util to quickly write new test suite generators with.

View File

@ -0,0 +1,81 @@
import argparse
import pathlib
import sys
from typing import List
from ruamel.yaml import (
YAML,
)
from gen_base.gen_typing import TestSuiteCreator
def make_filename_for_test(test):
title = test["title"]
filename = title.lower().replace(" ", "_") + ".yaml"
return pathlib.Path(filename)
def validate_output_dir(path_str):
path = pathlib.Path(path_str)
if not path.exists():
raise argparse.ArgumentTypeError("Output directory must exist")
if not path.is_dir():
raise argparse.ArgumentTypeError("Output path must lead to a directory")
return path
def run_generator(generator_name, suite_creators: List[TestSuiteCreator]):
"""
Implementation for a general test generator.
:param generator_name: The name of the generator. (lowercase snake_case)
:param suite_creators: A list of suite creators, each of these builds a list of test cases.
:return:
"""
parser = argparse.ArgumentParser(
prog="gen-" + generator_name,
description=f"Generate YAML test suite files for {generator_name}",
)
parser.add_argument(
"-o",
"--output-dir",
dest="output_dir",
required=True,
type=validate_output_dir,
help="directory into which the generated YAML files will be dumped"
)
parser.add_argument(
"-f",
"--force",
action="store_true",
default=False,
help="if set overwrite test files if they exist",
)
args = parser.parse_args()
output_dir = args.output_dir
if not args.force:
file_mode = "x"
else:
file_mode = "w"
yaml = YAML(pure=True)
print(f"Generating tests for {generator_name}, creating {len(suite_creators)} test suite files...")
for suite_creator in suite_creators:
suite = suite_creator()
filename = make_filename_for_test(suite)
path = output_dir / filename
try:
with path.open(file_mode) as f:
yaml.dump(suite, f)
except IOError as e:
sys.exit(f'Error when dumping test "{suite["title"]}" ({e})')
print("done.")

View File

@ -0,0 +1,17 @@
from typing import Iterable
from eth_utils import (
to_dict,
)
from gen_base.gen_typing import TestCase
@to_dict
def render_suite(*, title: str, summary: str, fork: str, config: str, test_cases: Iterable[TestCase]):
yield "title", title
if summary is not None:
yield "summary", summary
yield "fork", fork
yield "config", config
yield "test_cases", test_cases

View File

@ -0,0 +1,5 @@
from typing import Callable, Dict, Any
TestCase = Dict[str, Any]
TestSuite = Dict[str, Any]
TestSuiteCreator = Callable[[], TestSuite]

View File

@ -0,0 +1,2 @@
ruamel.yaml==0.15.87
eth-utils==1.4.1

View File

@ -0,0 +1,8 @@
from distutils.core import setup
setup(
name='gen_helpers',
version='1.0',
packages=['gen_base'],
install_requires=['ruamel.yaml', 'eth-utils']
)

View File

@ -0,0 +1,3 @@
# ETH 2.0 PySpec
The py

View File

@ -1,4 +1,4 @@
from .minimal_ssz import hash_tree_root
from shared_eth2.minimal_ssz import hash_tree_root
def jsonize(value, typ, include_hash_tree_roots=False):

View File

@ -0,0 +1,4 @@
eth-utils>=1.3.0,<2
eth-typing>=2.1.0,<3.0.0
pycryptodome==3.7.3
py_ecc>=1.6.0

13
test_libs/pyspec/setup.py Normal file
View File

@ -0,0 +1,13 @@
from distutils.core import setup
setup(
name='pyspec',
version='1.0',
packages=['debug', 'utils', 'phase0'],
install_requires=[
"eth-utils>=1.3.0,<2",
"eth-typing>=2.1.0,<3.0.0",
"pycryptodome==3.7.3",
"py_ecc>=1.6.0",
]
)

View File