Skip to content

Commit 4e54e3c

Browse files
fix: use absolute TESTING_DIR and reset file in benchmark tests
- Change TESTING_DIR to Path(__file__).parent.parent / "testing" so all testing/ paths are absolute and CWD-independent - Reset file to unformatted bytes at start of "reformats" test bodies and compare against MAIN_C.read_bytes() to handle pytest-codspeed calling benchmark bodies multiple times per test invocation Agent-Logs-Url: https://github.com/cpp-linter/cpp-linter-hooks/sessions/e6f1960a-f8fc-4507-8baa-2b659f052346 Co-authored-by: shenxianpeng <3353385+shenxianpeng@users.noreply.github.com>
1 parent 755b139 commit 4e54e3c

1 file changed

Lines changed: 7 additions & 5 deletions

File tree

tests/test_integration.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from cpp_linter_hooks.clang_format import run_clang_format
2222
from cpp_linter_hooks.clang_tidy import run_clang_tidy
2323

24-
TESTING_DIR = Path("testing")
24+
TESTING_DIR = Path(__file__).parent.parent / "testing"
2525
MAIN_C = TESTING_DIR / "main.c"
2626
GOOD_C = TESTING_DIR / "good.c"
2727

@@ -61,13 +61,14 @@ def clang_format_workspace(tmp_path, monkeypatch):
6161
def test_clang_format_style_from_file_reformats(clang_format_workspace):
6262
"""--style=file reads .clang-format and reformats the unformatted file."""
6363
test_file = clang_format_workspace
64-
original = test_file.read_text()
64+
# Reset to unformatted content each time (handles repeated benchmark calls)
65+
test_file.write_bytes(MAIN_C.read_bytes())
6566

6667
ret, output = run_clang_format(["--style=file", str(test_file)])
6768

6869
assert ret == 0
6970
assert isinstance(output, str)
70-
assert test_file.read_text() != original, "file should have been reformatted"
71+
assert test_file.read_bytes() != MAIN_C.read_bytes(), "file should have been reformatted"
7172

7273

7374
@pytest.mark.benchmark
@@ -124,13 +125,14 @@ def test_clang_format_style_from_file_with_version(tmp_path, monkeypatch, versio
124125
@pytest.mark.parametrize("style", STYLE_PRESETS)
125126
def test_clang_format_style_preset_reformats_unformatted_file(style, unformatted_file):
126127
"""Each style preset should reformat the unformatted main.c."""
127-
original = unformatted_file.read_text()
128+
# Reset to unformatted content each time (handles repeated benchmark calls)
129+
unformatted_file.write_bytes(MAIN_C.read_bytes())
128130

129131
ret, output = run_clang_format([f"--style={style}", str(unformatted_file)])
130132

131133
assert ret == 0
132134
assert isinstance(output, str)
133-
assert unformatted_file.read_text() != original, (
135+
assert unformatted_file.read_bytes() != MAIN_C.read_bytes(), (
134136
f"style={style}: file should have been reformatted"
135137
)
136138

0 commit comments

Comments
 (0)