Skip to content

Commit f05ed0a

Browse files
authored
Refactor hooks(#3)
* Remove comment out code from .pre-commit-hooks.yaml * Refactor utils * Update .pre-commit-hooks.yaml
1 parent d4a5de6 commit f05ed0a

File tree

4 files changed

+47
-56
lines changed

4 files changed

+47
-56
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ cpp_linter_hooks/__pycache__/
55
tests/.coverage
66
tests/__pycache__
77
.coverage
8+
__pycache__

.pre-commit-hooks.yaml

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,15 @@
1-
---
2-
# This file tells https://pre-commit.com/ which hooks
3-
# are provided by this repo for use by other git repos.
4-
51
- id: clang-format
62
name: clang-format
7-
description: Automatically run `clang-format` against C/C++ source code
3+
description: formatting C/C++ code with `clang-format`
84
entry: clang-format-hook
95
language: python
106
files: \.(h\+\+|h|hh|hxx|hpp|c|cc|cpp|c\+\+|cxx)$
117
types_or: [c, c++, c#, objective-c]
128

139
- id: clang-tidy
1410
name: clang-tidy
15-
description: Run `clang-tidy` against C/C++ code to find warnings/errors
11+
description: diagnosing and fixing typical programming errors with `clang-tidy`
1612
entry: clang-tidy-hook
1713
language: python
1814
files: \.(h\+\+|h|hh|hxx|hpp|c|cc|cpp|c\+\+|cxx)$
1915
types_or: [c, c++, c#, objective-c]
20-
21-
# - id: docker-clang-format
22-
# name: clang-format
23-
# description: Run `clang-format` against C/C++ source code in Docker container
24-
# language: docker
25-
# files: \.(h\+\+|h|hh|hxx|hpp|cuh|c|cc|cpp|cu|c\+\+|cxx|tpp|txx)$
26-
# entry: clang-format -i
27-
# args: ["-style=Google"]
28-
29-
# - id: docker-clang-tidy
30-
# name: clang-tidy
31-
# description: Run `clang-tidy` against C/C++ code to find warnings/errors in Docker container
32-
# language: docker
33-
# types_or: [c, c++, c#, objective-c]
34-
# entry: clang-tidy

cpp_linter_hooks/util.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,31 @@
11
import subprocess
22

33

4-
def check_installed(tool, version="") -> int:
4+
def check_installed(tool: str, version="") -> int:
55
if version:
66
check_version_cmd = [f'{tool}-{version} ', '--version']
7+
else:
8+
check_version_cmd = [tool, '--version']
9+
try:
10+
subprocess.run(check_version_cmd, stdout=subprocess.PIPE)
11+
retval = 0
12+
except FileNotFoundError:
13+
retval = install_clang_tools(version)
14+
return retval
15+
16+
17+
def install_clang_tools(version: str) -> int:
18+
if version:
719
# clang-tools exist because install_requires=['clang-tools'] in setup.py
820
install_tool_cmd = ['clang-tools', '-i', version]
921
else:
10-
check_version_cmd = [tool, '--version']
1122
# install verison 13 by default if clang-tools not exist.
1223
install_tool_cmd = ['clang-tools', '-i', '13']
1324
try:
14-
subprocess.run(check_version_cmd, stdout=subprocess.PIPE)
25+
subprocess.run(install_tool_cmd, stdout=subprocess.PIPE)
1526
retval = 0
16-
except FileNotFoundError:
17-
try:
18-
subprocess.run(install_tool_cmd, stdout=subprocess.PIPE)
19-
retval = 0
20-
except Exception:
21-
retval = 1
27+
except Exception:
28+
retval = 1
2229
return retval
2330

2431

@@ -30,7 +37,7 @@ def get_expect_version(args) -> str:
3037
# when --version 14
3138
expect_version = args[args.index(arg) + 1]
3239
else:
33-
# when --version=14, --version='14' or --version="14"
40+
# when --version=14
3441
expect_version = arg.replace(" ", "").replace("=", "").replace("--version", "")
3542
return expect_version
3643
return ""

tests/test_util.py

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,47 +4,49 @@
44

55
from cpp_linter_hooks.util import check_installed
66
from cpp_linter_hooks.util import get_expect_version
7+
from cpp_linter_hooks.util import install_clang_tools
78

89

9-
@pytest.mark.parametrize(
10-
('tool', 'expected_retval'), (
11-
('clang-format', 0),
12-
('clang-tidy', 0),
13-
),
14-
)
15-
@patch('cpp_linter_hooks.clang_format.subprocess.run')
10+
@pytest.mark.parametrize(('tool', 'expected_retval'), (('clang-format', 0), ('clang-tidy', 0),),)
11+
@patch('cpp_linter_hooks.util.subprocess.run')
1612
def test_check_installed(mock_subprocess_run, tool, expected_retval):
1713
mock_subprocess_run.return_value = expected_retval
1814
ret = check_installed(tool)
1915
assert ret == expected_retval
2016

2117

22-
@pytest.mark.parametrize(
23-
('tool', 'version', 'expected_retval'), (
24-
('clang-format', '14', 0),
25-
('clang-tidy', '14', 0),
26-
),
27-
)
28-
@patch('cpp_linter_hooks.clang_format.subprocess.run')
18+
@pytest.mark.parametrize(('tool', 'version', 'expected_retval'), (('clang-format', '14', 0), ('clang-tidy', '14', 0),),)
19+
@patch('cpp_linter_hooks.util.subprocess.run')
2920
def test_check_installed_with_version(mock_subprocess_run, tool, version, expected_retval):
3021
mock_subprocess_run.return_value = expected_retval
3122
ret = check_installed(tool, version=version)
3223
assert ret == expected_retval
3324

3425

35-
def test_get_expect_version():
36-
args = ['clang-format', '-i', '--style=Google', '--version=13']
37-
version = get_expect_version(args)
38-
assert version == '13'
26+
@pytest.mark.parametrize(('tool', 'version', 'expected_retval'), (('non-exist-cmd', '14', 0),),)
27+
@patch('cpp_linter_hooks.util.subprocess.run')
28+
def test_check_installed_with_except(mock_subprocess_run, tool, version, expected_retval):
29+
mock_subprocess_run.return_value = expected_retval
30+
ret = check_installed(tool, version=version)
31+
assert ret == expected_retval
3932

40-
args = ['clang-format', '-i', '--style=Google', '--version = 13']
41-
version = get_expect_version(args)
42-
assert version == '13'
4333

44-
args = ['clang-format', '-i', '--style=Google']
34+
@pytest.mark.parametrize(('version', 'expected_retval'), (('100', 1),),)
35+
@patch('cpp_linter_hooks.util.subprocess.run')
36+
def test_install_clang_tools(mock_subprocess_run, version, expected_retval):
37+
mock_subprocess_run.return_value = expected_retval
38+
try:
39+
ret = install_clang_tools(version)
40+
assert ret == expected_retval
41+
except Exception:
42+
pass
43+
44+
45+
def test_get_expect_version():
46+
args = ['clang-format', '--version 14']
4547
version = get_expect_version(args)
46-
assert version == ""
48+
assert version == '14'
4749

48-
args = ['clang-format', '-i', '--install=13']
50+
args = ['clang-format', '--version=14']
4951
version = get_expect_version(args)
50-
assert version == ""
52+
assert version == '14'

0 commit comments

Comments
 (0)