Skip to content

Commit d561507

Browse files
committed
support verbose output
1 parent 0339700 commit d561507

File tree

5 files changed

+57
-1
lines changed

5 files changed

+57
-1
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,21 @@ Use -header-filter=.* to display errors from all non-system headers. Use -system
151151
152152
```
153153

154+
## Troubleshooting
155+
156+
### Debugging `clang-format` hook
157+
158+
If you encounter issues with the clang-format hook (such as exit code 247 or other errors), you can enable verbose output to show the list of processed files.
159+
160+
```yaml
161+
repos:
162+
- repo: https://github.com/cpp-linter/cpp-linter-hooks
163+
rev: v0.8.1
164+
hooks:
165+
- id: clang-format
166+
args: [--style=file, --version=18, --verbose] # Add --verbose for detailed output
167+
```
168+
154169
## Contributing
155170

156171
We welcome contributions! Whether it's fixing issues, suggesting improvements, or submitting pull requests, your support is greatly appreciated.

cpp_linter_hooks/clang_format.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

88
parser = ArgumentParser()
99
parser.add_argument("--version", default=DEFAULT_CLANG_VERSION)
10+
parser.add_argument(
11+
"-v", "--verbose", action="store_true", help="Enable verbose output"
12+
)
1013

1114

1215
def run_clang_format(args=None) -> Tuple[int, str]:
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
repos:
2+
- repo: .
3+
rev: HEAD
4+
hooks:
5+
- id: clang-format
6+
args: [--style=file, --version=16, --verbose] # test with verbose output

testing/run.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
rm -f result.txt
22
git restore testing/main.c
33

4-
for config in testing/pre-commit-config.yaml testing/pre-commit-config-version.yaml; do
4+
for config in testing/pre-commit-config.yaml testing/pre-commit-config-version.yaml testing/pre-commit-config-verbose.yaml; do
55
pre-commit clean
66
pre-commit run -c $config --files testing/main.c | tee -a result.txt || true
77
git restore testing/main.c

tests/test_clang_format.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,35 @@ def test_run_clang_format_dry_run(args, expected_retval, tmp_path):
6464
test_file = tmp_path / "main.c"
6565
ret, _ = run_clang_format(["--dry-run", str(test_file)])
6666
assert ret == -1 # Dry run should not fail
67+
68+
69+
def test_run_clang_format_verbose(tmp_path):
70+
"""Test that verbose option works and provides detailed output."""
71+
# copy test file to tmp_path to prevent modifying repo data
72+
test_file = tmp_path / "main.c"
73+
test_file.write_bytes(Path("testing/main.c").read_bytes())
74+
75+
# Test with verbose flag
76+
ret, output = run_clang_format(["--verbose", "--style=Google", str(test_file)])
77+
78+
# Should succeed
79+
assert ret == 0
80+
# Should have verbose output (will be printed to stderr, not returned)
81+
# The function should still return successfully
82+
assert test_file.read_text() == Path("testing/good.c").read_text()
83+
84+
85+
def test_run_clang_format_verbose_error(tmp_path):
86+
"""Test that verbose option provides useful error information."""
87+
test_file = tmp_path / "main.c"
88+
test_file.write_bytes(Path("testing/main.c").read_bytes())
89+
90+
# Test with verbose flag and invalid style
91+
ret, output = run_clang_format(
92+
["--verbose", "--style=InvalidStyle", str(test_file)]
93+
)
94+
95+
# Should fail
96+
assert ret != 0
97+
# Should have error message in output
98+
assert "Invalid value for -style" in output

0 commit comments

Comments
 (0)