Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,5 @@ def print_requirements(args):
elif not conf.project_dirs and len(args.projects) > 0:
util.die(f"no such project: {args.project}")
else:
templates.autogen(conf)
tests.run_tests(conf)
generated_scripts = set(templates.autogen(conf))
tests.run_tests(conf, generated_scripts)
20 changes: 12 additions & 8 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from dataclasses import dataclass
from datetime import timedelta
import os
from pathlib import Path
import sys
import subprocess
from time import perf_counter
Expand All @@ -25,12 +26,15 @@ class Test(object):
"check": ["check.sh", "test.sh"]
}

def __init__(self, directory: str):
ff = next(os.walk(directory))[2]
self.scripts = set(filter(lambda f: f.endswith(".sh"), ff))
self.dir = directory
self.conf_file = os.path.join(directory, CONF_YML)
self.name = os.path.basename(directory)
def __init__(self, directory: Path, generated_scripts: set[Path]):
# We only want scripts that have been generated by us now,
# but non `*.gen*` scripts we can search for.
non_gen_script_paths = { f for f in directory.iterdir() if f.suffix == ".sh" and ".gen" not in f.suffixes }
script_paths = non_gen_script_paths | generated_scripts
self.scripts = { str(script.relative_to(directory)) for script in script_paths }
self.dir = str(directory)
self.conf_file = str(directory / CONF_YML)
self.name = directory.name

def run_script(self, stage, script, verbose=False, xfail=False) -> bool:
"""
Expand Down Expand Up @@ -210,11 +214,11 @@ class TestResult:
time: timedelta


def run_tests(conf: Config):
def run_tests(conf: Config, generated_scripts: set[Path]):
if not conf.ignore_requirements:
check(conf)

tests = [Test(td) for td in conf.project_dirs]
tests = [Test(Path(td), generated_scripts) for td in conf.project_dirs]

def run(test: Test) -> TestResult:
start = perf_counter()
Expand Down
51 changes: 37 additions & 14 deletions tests/templates.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import os
from pathlib import Path
import stat
from collections.abc import Mapping
from typing import Any, Dict, List
from typing import Any, Dict, Generator, List

from tests.util import *
from jinja2 import Template
Expand Down Expand Up @@ -94,16 +95,24 @@ def render_script(template: str, out_path: str, params: Dict):
os.chmod(out_path, stat.S_IREAD | stat.S_IWRITE | stat.S_IEXEC)


def autogen_cargo(conf_file, yaml: Dict):
def render_stage(stage_conf: Mapping[str, Any] | None, filename: str) -> bool:
def autogen_cargo(conf_file, yaml: Dict) -> Generator[Path]:
"""
Yield generated paths.
"""

def render_stage(stage_conf: Mapping[str, Any] | None, filename: str) -> Generator[Path]:
"""
Yield generated paths.
"""

if not isinstance(stage_conf, Mapping):
return False
return
if not stage_conf:
return False
return

ag = stage_conf.get("autogen")
if not (ag and isinstance(ag, bool)):
return False
return

params: Dict[str, str] = {}
rustflags = stage_conf.get("rustflags")
Expand All @@ -115,16 +124,20 @@ def render_stage(stage_conf: Mapping[str, Any] | None, filename: str) -> bool:
filename
)
render_script(CARGO_SH, out_path, params)
return True
yield Path(out_path)

for key, fname in (
("cargo.transpile", "cargo.transpile.gen.sh"),
("cargo.refactor", "cargo.refactor.gen.sh"),
):
render_stage(yaml.get(key), fname)
yield from render_stage(yaml.get(key), fname)


def autogen_refactor(conf_file, yaml: Dict):
def autogen_refactor(conf_file, yaml: Dict) -> Generator[str]:
"""
Yield generated paths.
"""

refactor = yaml.get("refactor")
if refactor and isinstance(refactor, Dict):
ag = refactor.get("autogen")
Expand All @@ -149,9 +162,14 @@ def autogen_refactor(conf_file, yaml: Dict):
"refactor.gen.sh"
)
render_script(REFACTOR_SH, out_path, params)
yield Path(out_path)


def autogen_transpile(conf_file, yaml: Dict):
def autogen_transpile(conf_file, yaml: Dict) -> Generator[Path]:
"""
Yield generated paths.
"""

transpile = yaml.get("transpile")
if transpile and isinstance(transpile, Dict):
ag = transpile.get("autogen")
Expand Down Expand Up @@ -180,10 +198,15 @@ def autogen_transpile(conf_file, yaml: Dict):
"transpile.gen.sh"
)
render_script(TRANSPILE_SH, out_path, params)
yield Path(out_path)


def autogen(conf: Config):
def autogen(conf: Config) -> Generator[Path]:
"""
Yield generated paths.
"""

for (cf, yaml) in conf.project_conf.items():
autogen_transpile(cf, yaml)
autogen_refactor(cf, yaml)
autogen_cargo(cf, yaml)
yield from autogen_transpile(cf, yaml)
yield from autogen_refactor(cf, yaml)
yield from autogen_cargo(cf, yaml)