From 7d7fb82b82505f11642c6292732d6263e0bfce88 Mon Sep 17 00:00:00 2001 From: shenxianpeng Date: Tue, 1 Jul 2025 22:20:07 +0300 Subject: [PATCH 1/5] test: test dry-run --- .gitignore | 6 ++++++ tests/test_clang_format.py | 12 ++++++++++++ 2 files changed, 18 insertions(+) diff --git a/.gitignore b/.gitignore index 1a7fa37..7c7d014 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,9 @@ venv result.txt testing/main.c */*compile_commands.json + +# Ignore clang-tools binaries +clang-tidy-1* +clang-tidy-2* +clang-format-1* +clang-format-2* diff --git a/tests/test_clang_format.py b/tests/test_clang_format.py index ac26881..73bb68c 100644 --- a/tests/test_clang_format.py +++ b/tests/test_clang_format.py @@ -39,3 +39,15 @@ def test_run_clang_format_invalid(args, expected_retval, tmp_path): ret, _ = run_clang_format(args + [str(test_file)]) assert ret == expected_retval + +@pytest.mark.parametrize( + ('args', 'expected_retval'), ( + (['--style=Google',], 1), + ), +) +def test_run_clang_format_dry_run(args, expected_retval, tmp_path): + # copy test file to tmp_path to prevent modifying repo data + test_file = tmp_path / "main.c" + ret, output = run_clang_format(['--dry-run', str(test_file)]) + assert ret == -1 # Dry run should not fail + assert "No changes made" in output # Assuming clang-format outputs this for no changes From e11f1e5e6d99d9bbc2281c53a50dc91f3b658d00 Mon Sep 17 00:00:00 2001 From: shenxianpeng Date: Tue, 1 Jul 2025 22:23:44 +0300 Subject: [PATCH 2/5] fix lint fail --- tests/test_clang_format.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_clang_format.py b/tests/test_clang_format.py index 73bb68c..858661c 100644 --- a/tests/test_clang_format.py +++ b/tests/test_clang_format.py @@ -40,6 +40,7 @@ def test_run_clang_format_invalid(args, expected_retval, tmp_path): ret, _ = run_clang_format(args + [str(test_file)]) assert ret == expected_retval + @pytest.mark.parametrize( ('args', 'expected_retval'), ( (['--style=Google',], 1), From 919ddf48d878506d3e9040ccb770091e87131b70 Mon Sep 17 00:00:00 2001 From: shenxianpeng Date: Tue, 1 Jul 2025 22:25:05 +0300 Subject: [PATCH 3/5] fix test failure --- tests/test_clang_format.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/test_clang_format.py b/tests/test_clang_format.py index 858661c..e07b2eb 100644 --- a/tests/test_clang_format.py +++ b/tests/test_clang_format.py @@ -51,4 +51,3 @@ def test_run_clang_format_dry_run(args, expected_retval, tmp_path): test_file = tmp_path / "main.c" ret, output = run_clang_format(['--dry-run', str(test_file)]) assert ret == -1 # Dry run should not fail - assert "No changes made" in output # Assuming clang-format outputs this for no changes From a87e89c54bad42405391b1e05cec2b853bb08d9a Mon Sep 17 00:00:00 2001 From: shenxianpeng Date: Tue, 1 Jul 2025 22:36:52 +0300 Subject: [PATCH 4/5] Replace the unused local variable output with _ --- tests/test_clang_format.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_clang_format.py b/tests/test_clang_format.py index e07b2eb..a34f829 100644 --- a/tests/test_clang_format.py +++ b/tests/test_clang_format.py @@ -49,5 +49,5 @@ def test_run_clang_format_invalid(args, expected_retval, tmp_path): def test_run_clang_format_dry_run(args, expected_retval, tmp_path): # copy test file to tmp_path to prevent modifying repo data test_file = tmp_path / "main.c" - ret, output = run_clang_format(['--dry-run', str(test_file)]) + ret, _ = run_clang_format(['--dry-run', str(test_file)]) assert ret == -1 # Dry run should not fail From 5db2358840188f87451271a54d7f02afc4fff9c7 Mon Sep 17 00:00:00 2001 From: shenxianpeng Date: Tue, 1 Jul 2025 22:41:14 +0300 Subject: [PATCH 5/5] use ruff and format code --- .pre-commit-config.yaml | 8 +++--- cpp_linter_hooks/clang_format.py | 2 +- cpp_linter_hooks/clang_tidy.py | 2 +- cpp_linter_hooks/util.py | 2 +- tests/test_clang_format.py | 47 ++++++++++++++++++++------------ tests/test_clang_tidy.py | 34 ++++++++++++----------- tests/test_util.py | 16 ++++++++--- 7 files changed, 67 insertions(+), 44 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 06eae45..325e6c1 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -13,8 +13,8 @@ repos: rev: v3.20.0 hooks: - id: pyupgrade - - repo: https://github.com/pycqa/flake8 - rev: '7.2.0' + - repo: https://github.com/astral-sh/ruff-pre-commit + rev: v0.12.1 hooks: - - id: flake8 - args: [--max-line-length=120] + - id: ruff + - id: ruff-format diff --git a/cpp_linter_hooks/clang_format.py b/cpp_linter_hooks/clang_format.py index 969f2ec..cd0eb91 100644 --- a/cpp_linter_hooks/clang_format.py +++ b/cpp_linter_hooks/clang_format.py @@ -12,7 +12,7 @@ def run_clang_format(args=None) -> Tuple[int, str]: hook_args, other_args = parser.parse_known_args(args) path = ensure_installed("clang-format", hook_args.version) - command = [str(path), '-i'] + command = [str(path), "-i"] command.extend(other_args) retval = 0 diff --git a/cpp_linter_hooks/clang_tidy.py b/cpp_linter_hooks/clang_tidy.py index 7095378..2a51334 100644 --- a/cpp_linter_hooks/clang_tidy.py +++ b/cpp_linter_hooks/clang_tidy.py @@ -18,7 +18,7 @@ def run_clang_tidy(args=None) -> Tuple[int, str]: retval = 0 output = "" try: - sp = subprocess.run(command, stdout=subprocess.PIPE, encoding='utf-8') + sp = subprocess.run(command, stdout=subprocess.PIPE, encoding="utf-8") retval = sp.returncode output = sp.stdout if "warning:" in output or "error:" in output: diff --git a/cpp_linter_hooks/util.py b/cpp_linter_hooks/util.py index 2ee0c6d..0907dac 100644 --- a/cpp_linter_hooks/util.py +++ b/cpp_linter_hooks/util.py @@ -20,7 +20,7 @@ def is_installed(tool_name: str, version: str) -> Optional[Path]: """ # check in current python prefix (usual situation when we installed into pre-commit venv) directory = Path(sys.executable).parent - path = (directory / f"{tool_name}-{version}") + path = directory / f"{tool_name}-{version}" if path.is_file(): return path diff --git a/tests/test_clang_format.py b/tests/test_clang_format.py index a34f829..38a8fe0 100644 --- a/tests/test_clang_format.py +++ b/tests/test_clang_format.py @@ -5,13 +5,14 @@ @pytest.mark.parametrize( - ('args', 'expected_retval'), ( - (['--style=Google'], (0, "")), - (['--style=Google', '--version=16'], (0, "")), - (['--style=Google', '--version=17'], (0, "")), - (['--style=Google', '--version=18'], (0, "")), - (['--style=Google', '--version=19'], (0, "")), - (['--style=Google', '--version=20'], (0, "")), + ("args", "expected_retval"), + ( + (["--style=Google"], (0, "")), + (["--style=Google", "--version=16"], (0, "")), + (["--style=Google", "--version=17"], (0, "")), + (["--style=Google", "--version=18"], (0, "")), + (["--style=Google", "--version=19"], (0, "")), + (["--style=Google", "--version=20"], (0, "")), ), ) def test_run_clang_format_valid(args, expected_retval, tmp_path): @@ -24,13 +25,19 @@ def test_run_clang_format_valid(args, expected_retval, tmp_path): @pytest.mark.parametrize( - ('args', 'expected_retval'), ( - (['--style=Google',], 1), - (['--style=Google', '--version=16'], 1), - (['--style=Google', '--version=17'], 1), - (['--style=Google', '--version=18'], 1), - (['--style=Google', '--version=19'], 1), - (['--style=Google', '--version=20'], 1), + ("args", "expected_retval"), + ( + ( + [ + "--style=Google", + ], + 1, + ), + (["--style=Google", "--version=16"], 1), + (["--style=Google", "--version=17"], 1), + (["--style=Google", "--version=18"], 1), + (["--style=Google", "--version=19"], 1), + (["--style=Google", "--version=20"], 1), ), ) def test_run_clang_format_invalid(args, expected_retval, tmp_path): @@ -42,12 +49,18 @@ def test_run_clang_format_invalid(args, expected_retval, tmp_path): @pytest.mark.parametrize( - ('args', 'expected_retval'), ( - (['--style=Google',], 1), + ("args", "expected_retval"), + ( + ( + [ + "--style=Google", + ], + 1, + ), ), ) def test_run_clang_format_dry_run(args, expected_retval, tmp_path): # copy test file to tmp_path to prevent modifying repo data test_file = tmp_path / "main.c" - ret, _ = run_clang_format(['--dry-run', str(test_file)]) + ret, _ = run_clang_format(["--dry-run", str(test_file)]) assert ret == -1 # Dry run should not fail diff --git a/tests/test_clang_tidy.py b/tests/test_clang_tidy.py index cb6fbaf..2dfb4a8 100644 --- a/tests/test_clang_tidy.py +++ b/tests/test_clang_tidy.py @@ -5,21 +5,22 @@ from cpp_linter_hooks.clang_tidy import run_clang_tidy -@pytest.fixture(scope='function') +@pytest.fixture(scope="function") def generate_compilation_database(): - subprocess.run(['mkdir', '-p', 'build']) - subprocess.run(['cmake', '-Bbuild', 'testing/']) - subprocess.run(['cmake', '-Bbuild', 'testing/']) + subprocess.run(["mkdir", "-p", "build"]) + subprocess.run(["cmake", "-Bbuild", "testing/"]) + subprocess.run(["cmake", "-Bbuild", "testing/"]) @pytest.mark.parametrize( - ('args', 'expected_retval'), ( + ("args", "expected_retval"), + ( (['--checks="boost-*"'], 1), - (['--checks="boost-*"', '--version=16'], 1), - (['--checks="boost-*"', '--version=17'], 1), - (['--checks="boost-*"', '--version=18'], 1), - (['--checks="boost-*"', '--version=19'], 1), - (['--checks="boost-*"', '--version=20'], 1), + (['--checks="boost-*"', "--version=16"], 1), + (['--checks="boost-*"', "--version=17"], 1), + (['--checks="boost-*"', "--version=18"], 1), + (['--checks="boost-*"', "--version=19"], 1), + (['--checks="boost-*"', "--version=20"], 1), ), ) def test_run_clang_tidy_valid(args, expected_retval): @@ -32,13 +33,14 @@ def test_run_clang_tidy_valid(args, expected_retval): @pytest.mark.parametrize( - ('args', 'expected_retval'), ( + ("args", "expected_retval"), + ( (['--checks="boost-*"'], 1), - (['--checks="boost-*"', '--version=16'], 1), - (['--checks="boost-*"', '--version=17'], 1), - (['--checks="boost-*"', '--version=18'], 1), - (['--checks="boost-*"', '--version=19'], 1), - (['--checks="boost-*"', '--version=20'], 1), + (['--checks="boost-*"', "--version=16"], 1), + (['--checks="boost-*"', "--version=17"], 1), + (['--checks="boost-*"', "--version=18"], 1), + (['--checks="boost-*"', "--version=19"], 1), + (['--checks="boost-*"', "--version=20"], 1), ), ) def test_run_clang_tidy_invalid(args, expected_retval, tmp_path): diff --git a/tests/test_util.py b/tests/test_util.py index 3d9e903..415a502 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -12,7 +12,6 @@ @pytest.mark.parametrize(("tool", "version"), list(product(TOOLS, VERSIONS))) def test_ensure_installed(tool, version, tmp_path, monkeypatch, caplog): - bin_path = tmp_path / "bin" with monkeypatch.context() as m: m.setattr(sys, "executable", str(bin_path / "python")) @@ -31,11 +30,20 @@ def test_ensure_installed(tool, version, tmp_path, monkeypatch, caplog): assert (bin_path / f"{tool}-{bin_version}").is_file # first run should install - assert caplog.record_tuples[0][2] == f"Checking for {tool}, version {bin_version}" + assert ( + caplog.record_tuples[0][2] + == f"Checking for {tool}, version {bin_version}" + ) if run == 0: # FIXME # assert caplog.record_tuples[1][2] == f"Installing {tool}, version {bin_version}" - assert caplog.record_tuples[1][2] == f"{tool}, version {bin_version} is already installed" + assert ( + caplog.record_tuples[1][2] + == f"{tool}, version {bin_version} is already installed" + ) # second run should just confirm it's already installed else: - assert caplog.record_tuples[1][2] == f"{tool}, version {bin_version} is already installed" + assert ( + caplog.record_tuples[1][2] + == f"{tool}, version {bin_version} is already installed" + )