fix(scripts): preserve paths when formatting C++ sources

This commit is contained in:
zhifangli 2026-10-20 23:16:08 +08:00
parent 774ce244b8
commit 0d927ca8a0
2 changed files with 39 additions and 1 deletions

View File

@ -68,6 +68,8 @@ if [ "${1:-}" = "--check" ]; then
fi
else
echo "Formatting $FILE_COUNT files in csrc/..."
echo "$FILES" | xargs "$CLANG_FORMAT_BIN" -i
while IFS= read -r file; do
"$CLANG_FORMAT_BIN" -i "$file"
done <<< "$FILES"
echo "Done."
fi

View File

@ -0,0 +1,36 @@
from __future__ import annotations
import shutil
import subprocess
from pathlib import Path
import pytest
@pytest.mark.ci
@pytest.mark.ci_npu
def test_format_csrc_with_spaces(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
formatter = shutil.which("clang-format")
if formatter is None:
pytest.skip("clang-format is required (see requirements-dev.txt)")
monkeypatch.setenv("CLANG_FORMAT", formatter)
repo = Path(__file__).resolve().parents[2]
checkout = tmp_path / "checkout with spaces"
(checkout / "scripts").mkdir(parents=True)
(checkout / "csrc").mkdir()
script = checkout / "scripts/format_csrc.sh"
shutil.copyfile(repo / "scripts/format_csrc.sh", script)
shutil.copyfile(repo / ".clang-format", checkout / ".clang-format")
source = checkout / "csrc/source with spaces.cpp"
source.write_text("int value=1;\n")
for args in ([], ["--check"]):
subprocess.run(
["bash", str(script), *args],
check=True,
capture_output=True,
text=True,
timeout=15,
)
assert source.read_text() == "int value = 1;\n"