support test format file structure in base generator

This commit is contained in:
protolambda 2019-04-07 17:50:39 +10:00
parent c350aaecf7
commit 8fdae5bced
No known key found for this signature in database
GPG Key ID: EC89FDBB2B4C7623
2 changed files with 41 additions and 15 deletions

View File

@ -1,5 +1,5 @@
import argparse import argparse
import pathlib from pathlib import Path
import sys import sys
from typing import List from typing import List
@ -10,14 +10,8 @@ from ruamel.yaml import (
from gen_base.gen_typing import TestSuiteCreator 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): def validate_output_dir(path_str):
path = pathlib.Path(path_str) path = Path(path_str)
if not path.exists(): if not path.exists():
raise argparse.ArgumentTypeError("Output directory must exist") raise argparse.ArgumentTypeError("Output directory must exist")
@ -28,6 +22,30 @@ def validate_output_dir(path_str):
return path return path
def validate_configs_dir(path_str):
path = Path(path_str)
if not path.exists():
raise argparse.ArgumentTypeError("Configs directory must exist")
if not path.is_dir():
raise argparse.ArgumentTypeError("Config path must lead to a directory")
if not Path(path, "constant_presets").exists():
raise argparse.ArgumentTypeError("Constant Presets directory must exist")
if not Path(path, "constant_presets").is_dir():
raise argparse.ArgumentTypeError("Constant Presets path must lead to a directory")
if not Path(path, "fork_timelines").exists():
raise argparse.ArgumentTypeError("Fork Timelines directory must exist")
if not Path(path, "fork_timelines").is_dir():
raise argparse.ArgumentTypeError("Fork Timelines path must lead to a directory")
return path
def run_generator(generator_name, suite_creators: List[TestSuiteCreator]): def run_generator(generator_name, suite_creators: List[TestSuiteCreator]):
""" """
Implementation for a general test generator. Implementation for a general test generator.
@ -59,7 +77,8 @@ def run_generator(generator_name, suite_creators: List[TestSuiteCreator]):
"-c", "-c",
"--configs-path", "--configs-path",
dest="configs_path", dest="configs_path",
default=True, required=True,
type=validate_configs_dir,
help="specify the path of the configs directory (containing constants_presets and fork_timelines)", help="specify the path of the configs directory (containing constants_presets and fork_timelines)",
) )
@ -75,13 +94,18 @@ def run_generator(generator_name, suite_creators: List[TestSuiteCreator]):
print(f"Generating tests for {generator_name}, creating {len(suite_creators)} test suite files...") print(f"Generating tests for {generator_name}, creating {len(suite_creators)} test suite files...")
print(f"Reading config presets and fork timelines from {args.configs_path}") print(f"Reading config presets and fork timelines from {args.configs_path}")
for suite_creator in suite_creators: for suite_creator in suite_creators:
suite = suite_creator(args.configs_path) (output_name, handler, suite) = suite_creator(args.configs_path)
filename = make_filename_for_test(suite) handler_output_dir = Path(output_dir) / Path(handler)
path = output_dir / filename try:
handler_output_dir.mkdir()
except FileNotFoundError as e:
sys.exit(f'Error when creating handler dir {handler} for test "{suite["title"]}" ({e})')
out_path = handler_output_dir / Path(output_name + '.yaml')
try: try:
with path.open(file_mode) as f: with out_path.open(file_mode) as f:
yaml.dump(suite, f) yaml.dump(suite, f)
except IOError as e: except IOError as e:
sys.exit(f'Error when dumping test "{suite["title"]}" ({e})') sys.exit(f'Error when dumping test "{suite["title"]}" ({e})')

View File

@ -1,6 +1,8 @@
from typing import Callable, Dict, Any from typing import Callable, Dict, Tuple, Any
TestCase = Dict[str, Any] TestCase = Dict[str, Any]
TestSuite = Dict[str, Any] TestSuite = Dict[str, Any]
# Tuple: (output name, handler name, suite) -- output name excl. ".yaml"
TestSuiteOutput = Tuple[str, str, TestSuite]
# Args: <presets path> # Args: <presets path>
TestSuiteCreator = Callable[[str], TestSuite] TestSuiteCreator = Callable[[str], TestSuiteOutput]