Skip to content

Commit d3ff86f

Browse files
committed
test.py: only run *.gen.sh scripts actually generated during that run
Otherwise, a previously generated `*.gen.sh` file will still be run even if it's no longer supposed to be generated (and isn't anymore).
1 parent 7afd5be commit d3ff86f

File tree

3 files changed

+27
-21
lines changed

3 files changed

+27
-21
lines changed

test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,5 @@ def print_requirements(args):
4242
elif not conf.project_dirs and len(args.projects) > 0:
4343
util.die(f"no such project: {args.project}")
4444
else:
45-
templates.autogen(conf)
46-
tests.run_tests(conf)
45+
generated_scripts = set(templates.autogen(conf))
46+
tests.run_tests(conf, generated_scripts)

tests/__init__.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22
import os
3+
from pathlib import Path
34
import sys
45
import subprocess
56
from typing import List # , Set, Dict, Tuple, Optional
@@ -21,9 +22,11 @@ class Test(object):
2122
"check": ["check.sh", "test.sh"]
2223
}
2324

24-
def __init__(self, directory: str):
25-
ff = next(os.walk(directory))[2]
26-
self.scripts = set(filter(lambda f: f.endswith(".sh"), ff))
25+
def __init__(self, directory: str, generated_scripts: set[Path]):
26+
self.scripts = {
27+
f.name for f in Path(directory).iterdir() if f.suffix == ".sh" and
28+
(".gen" not in f.suffixes or any(f.samefile(script) for script in generated_scripts))
29+
}
2730
self.dir = directory
2831
self.conf_file = os.path.join(directory, CONF_YML)
2932
self.name = os.path.basename(directory)
@@ -206,11 +209,11 @@ def __call__(self, conf: Config):
206209
return True
207210

208211

209-
def run_tests(conf):
212+
def run_tests(conf: Config, generated_scripts: set[Path]):
210213
if not conf.ignore_requirements:
211214
check(conf)
212215

213-
tests = [Test(td) for td in conf.project_dirs]
216+
tests = [Test(td, generated_scripts) for td in conf.project_dirs]
214217

215218
failure = False
216219
for tt in tests:

tests/templates.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import os
2+
from pathlib import Path
23
import stat
34
from collections.abc import Mapping
4-
from typing import Any, Dict, List
5+
from typing import Any, Dict, Generator, List
56

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

9697

97-
def autogen_cargo(conf_file, yaml: Dict):
98-
def render_stage(stage_conf: Mapping[str, Any] | None, filename: str) -> bool:
98+
def autogen_cargo(conf_file, yaml: Dict) -> Generator[Path]:
99+
def render_stage(stage_conf: Mapping[str, Any] | None, filename: str) -> Generator[Path]:
99100
if not isinstance(stage_conf, Mapping):
100-
return False
101+
return
101102
if not stage_conf:
102-
return False
103+
return
103104

104105
ag = stage_conf.get("autogen")
105106
if not (ag and isinstance(ag, bool)):
106-
return False
107+
return
107108

108109
params: Dict[str, str] = {}
109110
rustflags = stage_conf.get("rustflags")
@@ -115,16 +116,16 @@ def render_stage(stage_conf: Mapping[str, Any] | None, filename: str) -> bool:
115116
filename
116117
)
117118
render_script(CARGO_SH, out_path, params)
118-
return True
119+
yield Path(out_path)
119120

120121
for key, fname in (
121122
("cargo.transpile", "cargo.transpile.gen.sh"),
122123
("cargo.refactor", "cargo.refactor.gen.sh"),
123124
):
124-
render_stage(yaml.get(key), fname)
125+
yield from render_stage(yaml.get(key), fname)
125126

126127

127-
def autogen_refactor(conf_file, yaml: Dict):
128+
def autogen_refactor(conf_file, yaml: Dict) -> Generator[str]:
128129
refactor = yaml.get("refactor")
129130
if refactor and isinstance(refactor, Dict):
130131
ag = refactor.get("autogen")
@@ -149,9 +150,10 @@ def autogen_refactor(conf_file, yaml: Dict):
149150
"refactor.gen.sh"
150151
)
151152
render_script(REFACTOR_SH, out_path, params)
153+
yield Path(out_path)
152154

153155

154-
def autogen_transpile(conf_file, yaml: Dict):
156+
def autogen_transpile(conf_file, yaml: Dict) -> Generator[Path]:
155157
transpile = yaml.get("transpile")
156158
if transpile and isinstance(transpile, Dict):
157159
ag = transpile.get("autogen")
@@ -180,10 +182,11 @@ def autogen_transpile(conf_file, yaml: Dict):
180182
"transpile.gen.sh"
181183
)
182184
render_script(TRANSPILE_SH, out_path, params)
185+
yield Path(out_path)
183186

184187

185-
def autogen(conf: Config):
188+
def autogen(conf: Config) -> Generator[Path]:
186189
for (cf, yaml) in conf.project_conf.items():
187-
autogen_transpile(cf, yaml)
188-
autogen_refactor(cf, yaml)
189-
autogen_cargo(cf, yaml)
190+
yield from autogen_transpile(cf, yaml)
191+
yield from autogen_refactor(cf, yaml)
192+
yield from autogen_cargo(cf, yaml)

0 commit comments

Comments
 (0)