Skip to content

Commit 5c09250

Browse files
kkysenthedataking
authored andcommitted
tests/integration: add postprocess stage and enable postprocess for json-c
1 parent c6af520 commit 5c09250

3 files changed

Lines changed: 50 additions & 6 deletions

File tree

tests/integration/tests/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import sys
77
import subprocess
88
from time import perf_counter
9-
from typing import List # , Set, Dict, Tuple, Optional
109

1110
from tests.util import *
1211
from tests.requirements import *
@@ -23,6 +22,7 @@ class Test(object):
2322
"refactor": ["refactor.gen.sh", "refactor.sh"],
2423
"cargo.refactor": ["cargo.refactor.gen.sh", "cargo.refactor.sh"],
2524
"check.refactor": ["check.refactor.sh", "test.sh"],
25+
"postprocess": ["postprocess.gen.sh", "postprocess.sh"],
2626
"cargo.postprocess": ["cargo.postprocess.gen.sh", "cargo.postprocess.sh"],
2727
"check.postprocess": ["check.postprocess.sh", "test.sh"],
2828
}

tests/integration/tests/json-c/conf.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,8 @@ refactor:
2323
cargo.refactor:
2424
autogen: true
2525

26+
postprocess:
27+
autogen: true
28+
2629
cargo.postprocess:
2730
autogen: true

tests/integration/tests/templates.py

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
from pathlib import Path
3+
import shlex
34
import stat
45
from collections.abc import Mapping
56
from typing import Any, Generator
@@ -78,6 +79,18 @@
7879
C2RUST_TRANSFORMS
7980
"""
8081

82+
POSTPROCESS_SH: str = r"""#!/usr/bin/env bash
83+
# this file was autogenerated by templates.py
84+
set -e; set -o pipefail
85+
86+
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
87+
LOG_FILE="$SCRIPT_DIR/$(basename "$0")".log
88+
89+
cd "${SCRIPT_DIR}"
90+
91+
c2rust-postprocess --update-rust {{args}} repo/lib.rs > "$LOG_FILE"
92+
"""
93+
8194
CARGO_SH: str = r"""#!/usr/bin/env bash
8295
# this file was autogenerated by templates.py
8396
set -e; set -o pipefail
@@ -92,7 +105,7 @@
92105
"""
93106

94107

95-
def render_script(template: str, out_path: str, params: dict):
108+
def render_script(template: str, out_path: str | Path, params: dict):
96109
out = Template(template).render(**params)
97110

98111
with open(out_path, "w") as fh:
@@ -108,11 +121,11 @@ def autogen_transpile(conf: str, yaml: dict[str, Any]) -> Generator[Path, None,
108121
transpile = yaml.get("transpile")
109122
if not (transpile and isinstance(transpile, dict)):
110123
return
111-
124+
112125
ag = transpile.get("autogen")
113126
if not (ag and isinstance(ag, bool)):
114127
return
115-
128+
116129
params = {"binary": "--emit-build-files", "cflags": ""}
117130

118131
binary = transpile.get("binary")
@@ -144,11 +157,11 @@ def autogen_refactor(conf: str, yaml: dict[str, Any]) -> Generator[Path, None, N
144157
refactor = yaml.get("refactor")
145158
if not (refactor and isinstance(refactor, dict)):
146159
return
147-
160+
148161
ag = refactor.get("autogen")
149162
if not (ag and isinstance(ag, bool)):
150163
return
151-
164+
152165
params = {"transform_lines": ""}
153166

154167
# Get list of transformations from config
@@ -171,6 +184,33 @@ def autogen_refactor(conf: str, yaml: dict[str, Any]) -> Generator[Path, None, N
171184
yield Path(out_path)
172185

173186

187+
def autogen_postprocess(conf: str, yaml: dict[str, Any]) -> Generator[Path, None, None]:
188+
"""
189+
Yield generated paths.
190+
"""
191+
192+
conf = Path(conf)
193+
194+
postprocess = yaml.get("postprocess")
195+
if not (postprocess and isinstance(postprocess, dict)):
196+
return
197+
198+
ag = postprocess.get("autogen")
199+
if not (ag and isinstance(ag, bool)):
200+
return
201+
202+
params = {"args": ""}
203+
204+
exclude_file = conf.with_name("postprocess-exclude.yml")
205+
if exclude_file.exists():
206+
# args are relative to script dir
207+
params["args"] = shlex.join(["--exclude-file", str(exclude_file.relative_to(conf.parent))])
208+
209+
out_path = conf.with_name("postprocess.gen.sh")
210+
render_script(POSTPROCESS_SH, out_path, params)
211+
yield out_path
212+
213+
174214
def autogen_cargo(conf: str, yaml: dict[str, Any]) -> Generator[Path, None, None]:
175215
"""
176216
Yield generated paths.
@@ -217,4 +257,5 @@ def autogen(conf: Config) -> Generator[Path, None, None]:
217257
for cf, yaml in conf.project_conf.items():
218258
yield from autogen_transpile(cf, yaml)
219259
yield from autogen_refactor(cf, yaml)
260+
yield from autogen_postprocess(cf, yaml)
220261
yield from autogen_cargo(cf, yaml)

0 commit comments

Comments
 (0)