diff --git a/scripts/format_csrc.sh b/scripts/format_csrc.sh index f695afc..742009c 100755 --- a/scripts/format_csrc.sh +++ b/scripts/format_csrc.sh @@ -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 diff --git a/tests/scripts/test_format_csrc.py b/tests/scripts/test_format_csrc.py new file mode 100644 index 0000000..1514dd9 --- /dev/null +++ b/tests/scripts/test_format_csrc.py @@ -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"