Skip to content

Commit 6621c53

Browse files
committed
add unittest
1 parent 608a052 commit 6621c53

File tree

8 files changed

+119
-15
lines changed

8 files changed

+119
-15
lines changed

.github/workflows/add-tag.yml renamed to .github/workflows/test.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: "Tag"
1+
name: "Test"
22

33
on:
44
push:
@@ -15,7 +15,7 @@ jobs:
1515
steps:
1616
- name: Checkout
1717
uses: actions/checkout@v3
18-
- name: retag latest commit for testing
18+
- name: Add tag to latest commit for testing
1919
run: |
2020
git config user.name 'github-actions'
2121
git config user.email '41898282+github-actions[bot]@users.noreply.github.com'

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
build
22
dist
3-
cpp_linter_hooks.egg-info
3+
cpp_linter_hooks.egg-info
4+
tests/.coverage
5+
tests/__pycache__

.pre-commit-config.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
repos:
2+
# For development test
23
- repo: https://github.com/shenxianpeng/cpp-linter-hooks
34
rev: latest
45
hooks:
56
- id: clang-format
67
verbose: true
78
args: [--style=Google, --version=13]
9+
- repo: https://github.com/pycqa/flake8
10+
rev: '4.0.1'
11+
hooks:
12+
- id: flake8
13+
args: [--max-line-length=120]

README.md

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,33 @@
1-
# C/C++ linter hooks for pre-commit
1+
# cpp-linter-hooks
22

3-
Linter your C/C++ code with `clang-format` and `clang-tidy` for [pre-commit](https://pre-commit.com/).
3+
Using `clang-format` and `clang-tidy` hooks with [pre-commit](https://pre-commit.com/) to lint your C/C++ code.
44

5-
**Automaticlly install `clang-format` and `clang-tidy`** when they do not exist.
5+
✨Highlight✨: automatically install `clang-format` and `clang-tidy` when they do not exist.
66

77
## Usage
88

9-
For example
9+
Add this to your `.pre-commit-config.yaml`
1010

1111
```yaml
1212
repos:
1313
- repo: https://github.com/shenxianpeng/cpp-linter-hooks
14+
rev: v0.1.0 # Use the ref you want to point at
1415
hooks:
1516
- id: clang-format
16-
args: ["--style=Google", "--version=14"]
17-
- id: clang-tidy
17+
args: [--style=Google]
18+
# - id: clang-tidy ## Work in progress
19+
# args: [--config-file=file]
1820
```
21+
22+
## Support hooks
23+
24+
### `clang-format`
25+
26+
Prevent committing unformatted C/C++ code.
27+
28+
* Set coding style: LLVM, GNU, Google, Chromium, Microsoft, Mozilla, WebKit with `args: [--style=LLVM]`
29+
* Load coding style configuration file `.clang-format` with `args: [--style=file]`
30+
31+
### `clang-tidy`
32+
33+
Prevent committing typical programming errors, like style violations, interface misuse, or bugs that can be deduced.

cpp_linter_hooks/clang_format.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ def run_clang_format(args) -> int:
1818
return 1
1919

2020

21-
def main() -> int:
21+
def main():
2222
run_clang_format(args)
2323

2424

2525
if __name__ == "__main__":
26-
raise SystemExit(main())
26+
main()

cpp_linter_hooks/util.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11
import subprocess
22

33

4-
def check_installed(tool, version='13'):
4+
def check_installed(tool, version='13') -> int:
55
command = [f'{tool}-{version} ', '--version']
66
try:
77
subprocess.run(command, stdout=subprocess.PIPE)
8+
retval = 0
89
except FileNotFoundError:
910
# clang-tools exist because install_requires=['clang-tools'] in setup.py
10-
subprocess.run(['clang-tools', '-i', version], stdout=subprocess.PIPE)
11+
retval = subprocess.run(['clang-tools', '-i', version], stdout=subprocess.PIPE).returncode
12+
return retval
1113

1214

13-
def get_expect_version(args):
15+
def get_expect_version(args) -> str:
1416
for arg in args:
15-
if arg.startswith("--version") : # expect specific clang-tools version.
17+
if arg.startswith("--version"): # expect specific clang-tools version.
1618
# If --version is passed in as 2 arguments, the second is version
1719
if arg == "--version" and args.index(arg) != len(args) - 1:
1820
expect_version = args[args.index(arg) + 1]
1921
# Expected split of --version=14 or --version 14
2022
else:
2123
expect_version = arg.replace(" ", "").replace("=", "").replace("--version", "")
2224
return expect_version
25+
return None

tests/test_clang_format.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import pytest
2+
from unittest.mock import patch
3+
from cpp_linter_hooks.clang_format import run_clang_format
4+
5+
6+
@pytest.mark.parametrize(
7+
('args', 'expected_retval'), (
8+
(['clang-format', '-i', '--style=Google', 'tests/main.c'], 0),
9+
(['clang-format', '-i', '--style=Google', '--version=13', 'tests/main.c'], 0),
10+
),
11+
)
12+
@patch('cpp_linter_hooks.clang_format.subprocess.run')
13+
def test_run_clang_format_valid(mock_subprocess_run, args, expected_retval):
14+
mock_subprocess_run.return_value = expected_retval
15+
ret = run_clang_format(args)
16+
assert ret == expected_retval
17+
18+
19+
@pytest.mark.parametrize(
20+
('args', 'expected_retval'), (
21+
(['clang-format', '-i', '--style=Google', 'abc/def.c'], 1),
22+
(['clang-format', '-i', '--style=Google', '--version=13', 'abc/def.c'], 1),
23+
),
24+
)
25+
@patch('cpp_linter_hooks.clang_format.subprocess.run', side_effect=FileNotFoundError)
26+
def test_run_clang_format_invalid(mock_subprocess_run, args, expected_retval):
27+
mock_subprocess_run.return_value = expected_retval
28+
try:
29+
ret = run_clang_format(args)
30+
except FileNotFoundError:
31+
assert ret == expected_retval

tests/test_util.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import pytest
2+
from unittest.mock import patch
3+
from cpp_linter_hooks.util import check_installed, get_expect_version
4+
5+
6+
@pytest.mark.parametrize(
7+
('tool', 'expected_retval'), (
8+
('clang-format', 0),
9+
('clang-tidy', 0),
10+
),
11+
)
12+
@patch('cpp_linter_hooks.clang_format.subprocess.run')
13+
def test_check_installed(mock_subprocess_run, tool, expected_retval):
14+
mock_subprocess_run.return_value = expected_retval
15+
ret = check_installed(tool)
16+
assert ret == expected_retval
17+
18+
19+
@pytest.mark.parametrize(
20+
('tool', 'version', 'expected_retval'), (
21+
('clang-format', '14', 0),
22+
('clang-tidy', '14', 0),
23+
),
24+
)
25+
@patch('cpp_linter_hooks.clang_format.subprocess.run')
26+
def test_check_installed_with_version(mock_subprocess_run, tool, version, expected_retval):
27+
mock_subprocess_run.return_value = expected_retval
28+
ret = check_installed(tool, version=version)
29+
assert ret == expected_retval
30+
31+
32+
def test_get_expect_version():
33+
args = ['clang-format', '-i', '--style=Google', '--version=13']
34+
version = get_expect_version(args)
35+
assert version == '13'
36+
37+
args = ['clang-format', '-i', '--style=Google', '--version = 13']
38+
version = get_expect_version(args)
39+
assert version == '13'
40+
41+
args = ['clang-format', '-i', '--style=Google']
42+
version = get_expect_version(args)
43+
assert version is None
44+
45+
args = ['clang-format', '-i' '--install=13']
46+
version = get_expect_version(args)
47+
assert version is None

0 commit comments

Comments
 (0)