2019-03-27 16:28:20 +00:00
|
|
|
import argparse
|
2019-04-07 07:50:39 +00:00
|
|
|
from pathlib import Path
|
2019-03-27 16:28:20 +00:00
|
|
|
import sys
|
|
|
|
from typing import List
|
|
|
|
|
|
|
|
from ruamel.yaml import (
|
|
|
|
YAML,
|
|
|
|
)
|
|
|
|
|
|
|
|
from gen_base.gen_typing import TestSuiteCreator
|
|
|
|
|
|
|
|
|
|
|
|
def validate_output_dir(path_str):
|
2019-04-07 07:50:39 +00:00
|
|
|
path = Path(path_str)
|
2019-03-27 16:28:20 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
2019-04-07 07:50:39 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2019-03-27 16:28:20 +00:00
|
|
|
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",
|
|
|
|
)
|
2019-04-07 06:32:48 +00:00
|
|
|
parser.add_argument(
|
|
|
|
"-c",
|
|
|
|
"--configs-path",
|
|
|
|
dest="configs_path",
|
2019-04-07 07:50:39 +00:00
|
|
|
required=True,
|
|
|
|
type=validate_configs_dir,
|
2019-04-07 06:32:48 +00:00
|
|
|
help="specify the path of the configs directory (containing constants_presets and fork_timelines)",
|
|
|
|
)
|
2019-03-27 16:28:20 +00:00
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
output_dir = args.output_dir
|
|
|
|
if not args.force:
|
|
|
|
file_mode = "x"
|
|
|
|
else:
|
|
|
|
file_mode = "w"
|
|
|
|
|
|
|
|
yaml = YAML(pure=True)
|
2019-04-07 13:36:05 +00:00
|
|
|
yaml.default_flow_style = None
|
2019-03-27 16:28:20 +00:00
|
|
|
|
|
|
|
print(f"Generating tests for {generator_name}, creating {len(suite_creators)} test suite files...")
|
2019-04-07 06:32:48 +00:00
|
|
|
print(f"Reading config presets and fork timelines from {args.configs_path}")
|
2019-03-27 16:28:20 +00:00
|
|
|
for suite_creator in suite_creators:
|
2019-04-07 07:50:39 +00:00
|
|
|
(output_name, handler, suite) = suite_creator(args.configs_path)
|
|
|
|
|
|
|
|
handler_output_dir = Path(output_dir) / Path(handler)
|
|
|
|
try:
|
2019-04-07 13:36:05 +00:00
|
|
|
if not handler_output_dir.exists():
|
|
|
|
handler_output_dir.mkdir()
|
2019-04-07 07:50:39 +00:00
|
|
|
except FileNotFoundError as e:
|
|
|
|
sys.exit(f'Error when creating handler dir {handler} for test "{suite["title"]}" ({e})')
|
2019-03-27 16:28:20 +00:00
|
|
|
|
2019-04-07 07:50:39 +00:00
|
|
|
out_path = handler_output_dir / Path(output_name + '.yaml')
|
2019-03-27 16:28:20 +00:00
|
|
|
|
|
|
|
try:
|
2019-04-07 07:50:39 +00:00
|
|
|
with out_path.open(file_mode) as f:
|
2019-03-27 16:28:20 +00:00
|
|
|
yaml.dump(suite, f)
|
|
|
|
except IOError as e:
|
|
|
|
sys.exit(f'Error when dumping test "{suite["title"]}" ({e})')
|
|
|
|
|
|
|
|
print("done.")
|