Skip to content

Commit 11c7dff

Browse files
oharboeclaude
andcommitted
flow: add 18 unit tests for unexpanded $(PLATFORM_DIR) and $(PLATFORM)
Verify that config.py output (both Make and JSON) uses symbolic $(PLATFORM_DIR) and $(PLATFORM) references instead of absolute paths. Tests cover: JSON output, Make output, glob results, and $(PLATFORM) in sky130hd/sky130hs filenames. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> Signed-off-by: Øyvind Harboe <oyvind.harboe@zylin.com>
1 parent ce1c3a9 commit 11c7dff

1 file changed

Lines changed: 158 additions & 0 deletions

File tree

‎flow/test/test_platform_config.py‎

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1194,6 +1194,164 @@ def test_make_metadata(self, platform, design):
11941194
)
11951195

11961196

1197+
ALL_PLATFORMS = ["nangate45", "sky130hd", "sky130hs", "gf180", "ihp-sg13g2", "asap7"]
1198+
1199+
1200+
class TestUnexpandedOutput:
1201+
"""Verify config.py output uses $(PLATFORM_DIR) and $(PLATFORM) instead of absolute paths.
1202+
1203+
bazel-orfs runs in a sandbox where platform files live at different paths.
1204+
config.py must output symbolic references so consumers can expand on their end:
1205+
- Make: $(eval) expands $(PLATFORM_DIR) and $(PLATFORM)
1206+
- bazel-orfs: Python substitution when the Bazel rule executes
1207+
"""
1208+
1209+
@pytest.fixture(autouse=True)
1210+
def _clean_env(self, monkeypatch):
1211+
for v in [
1212+
"ADDITIONAL_LIBS",
1213+
"ADDITIONAL_GDS",
1214+
"ADDITIONAL_LEFS",
1215+
"ADDITIONAL_SLOW_LIBS",
1216+
"ADDITIONAL_FAST_LIBS",
1217+
"ADDITIONAL_TYP_LIBS",
1218+
"BC_ADDITIONAL_LIBS",
1219+
"BLOCKS",
1220+
"CORNER",
1221+
"ASAP7_USE_VT",
1222+
"LIB_MODEL",
1223+
"CLUSTER_FLOPS",
1224+
"FOOTPRINT_TCL",
1225+
"LOAD_ADDITIONAL_FILES",
1226+
"TRACK_OPTION",
1227+
"METAL_OPTION",
1228+
"KVALUE",
1229+
"POWER_OPTION",
1230+
"SDC_FILE",
1231+
"ABC_CLOCK_PERIOD_IN_PS",
1232+
"DONT_USE_CELLS",
1233+
]:
1234+
monkeypatch.delenv(v, raising=False)
1235+
1236+
@pytest.mark.parametrize("platform", ALL_PLATFORMS)
1237+
def test_json_no_absolute_platform_dir(self, platform, monkeypatch, tmp_path):
1238+
"""JSON output must not contain the absolute platform_dir path."""
1239+
pd = os.path.abspath(os.path.join(FLOW_DIR, "platforms", platform))
1240+
monkeypatch.setenv("PLATFORM_DIR", pd)
1241+
monkeypatch.setenv("PLATFORM", platform)
1242+
1243+
mod = _load_config_module(platform)
1244+
cfg = mod.get_config()
1245+
1246+
# Capture JSON output
1247+
import io
1248+
1249+
buf = io.StringIO()
1250+
old_stdout = sys.stdout
1251+
sys.stdout = buf
1252+
cfg.output_json()
1253+
sys.stdout = old_stdout
1254+
data = json.loads(buf.getvalue())
1255+
1256+
for key, value in data.items():
1257+
assert pd not in value, (
1258+
f"{platform}: {key} contains absolute path {pd!r} "
1259+
f"instead of $(PLATFORM_DIR). Value: {value!r}"
1260+
)
1261+
1262+
@pytest.mark.parametrize("platform", ALL_PLATFORMS)
1263+
def test_make_no_absolute_platform_dir(self, platform, monkeypatch):
1264+
"""Make output must not contain the absolute platform_dir path."""
1265+
pd = os.path.abspath(os.path.join(FLOW_DIR, "platforms", platform))
1266+
monkeypatch.setenv("PLATFORM_DIR", pd)
1267+
monkeypatch.setenv("PLATFORM", platform)
1268+
1269+
mod = _load_config_module(platform)
1270+
cfg = mod.get_config()
1271+
1272+
import io
1273+
1274+
buf = io.StringIO()
1275+
old_stdout = sys.stdout
1276+
sys.stdout = buf
1277+
cfg.output_make()
1278+
sys.stdout = old_stdout
1279+
output = buf.getvalue()
1280+
1281+
assert pd not in output, (
1282+
f"{platform}: Make output contains absolute path {pd!r} "
1283+
f"instead of $(PLATFORM_DIR)"
1284+
)
1285+
1286+
@pytest.mark.parametrize("platform", ["nangate45", "sky130hd", "sky130hs", "gf180"])
1287+
def test_glob_results_unexpanded(self, platform, monkeypatch):
1288+
"""GDS_FILES from glob must use $(PLATFORM_DIR), not absolute paths."""
1289+
pd = os.path.abspath(os.path.join(FLOW_DIR, "platforms", platform))
1290+
monkeypatch.setenv("PLATFORM_DIR", pd)
1291+
monkeypatch.setenv("PLATFORM", platform)
1292+
1293+
mod = _load_config_module(platform)
1294+
cfg = mod.get_config()
1295+
1296+
import io
1297+
1298+
buf = io.StringIO()
1299+
old_stdout = sys.stdout
1300+
sys.stdout = buf
1301+
cfg.output_json()
1302+
sys.stdout = old_stdout
1303+
data = json.loads(buf.getvalue())
1304+
1305+
gds = data.get("GDS_FILES", "")
1306+
assert gds, f"{platform}: GDS_FILES is empty"
1307+
assert (
1308+
"$(PLATFORM_DIR)" in gds
1309+
), f"{platform}: GDS_FILES glob result missing $(PLATFORM_DIR): {gds!r}"
1310+
assert pd not in gds, f"{platform}: GDS_FILES contains absolute path: {gds!r}"
1311+
1312+
@pytest.mark.parametrize(
1313+
"platform,keys",
1314+
[
1315+
(
1316+
"sky130hd",
1317+
[
1318+
"KLAYOUT_TECH_FILE",
1319+
"KLAYOUT_DRC_FILE",
1320+
"CDL_FILE",
1321+
"KLAYOUT_LVS_FILE",
1322+
],
1323+
),
1324+
("sky130hs", ["KLAYOUT_TECH_FILE"]),
1325+
],
1326+
)
1327+
def test_platform_name_unexpanded(self, platform, keys, monkeypatch):
1328+
"""Variables using $(PLATFORM) in filenames must not expand it."""
1329+
pd = os.path.abspath(os.path.join(FLOW_DIR, "platforms", platform))
1330+
monkeypatch.setenv("PLATFORM_DIR", pd)
1331+
monkeypatch.setenv("PLATFORM", platform)
1332+
1333+
mod = _load_config_module(platform)
1334+
cfg = mod.get_config()
1335+
1336+
import io
1337+
1338+
buf = io.StringIO()
1339+
old_stdout = sys.stdout
1340+
sys.stdout = buf
1341+
cfg.output_json()
1342+
sys.stdout = old_stdout
1343+
data = json.loads(buf.getvalue())
1344+
1345+
for key in keys:
1346+
value = data.get(key, "")
1347+
assert (
1348+
"$(PLATFORM)" in value
1349+
), f"{platform}: {key} should contain $(PLATFORM), got: {value!r}"
1350+
assert (
1351+
f"/{platform}." not in value
1352+
), f"{platform}: {key} contains expanded platform name: {value!r}"
1353+
1354+
11971355
if __name__ == "__main__":
11981356
try:
11991357
import pytest

0 commit comments

Comments
 (0)