From d59f2627a2bbb3bba3da08299f556787e15ed81e Mon Sep 17 00:00:00 2001 From: shenxianpeng Date: Thu, 21 Dec 2023 00:54:27 -0500 Subject: [PATCH 1/6] chore: update debug infor --- cpp_linter_hooks/__init__.py | 1 + cpp_linter_hooks/clang_format.py | 4 ++-- cpp_linter_hooks/clang_tidy.py | 4 ++-- cpp_linter_hooks/util.py | 3 ++- pyproject.toml | 2 +- testing/.pre-commit-config.yaml | 6 +++--- testing/README.md | 8 ++++++++ 7 files changed, 19 insertions(+), 9 deletions(-) create mode 100644 testing/README.md diff --git a/cpp_linter_hooks/__init__.py b/cpp_linter_hooks/__init__.py index 8769504..a5fb9af 100644 --- a/cpp_linter_hooks/__init__.py +++ b/cpp_linter_hooks/__init__.py @@ -6,6 +6,7 @@ clang_tools = ['clang-format', 'clang-tidy'] args = list(sys.argv[1:]) +print(args) expect_version = get_expect_version(args) diff --git a/cpp_linter_hooks/clang_format.py b/cpp_linter_hooks/clang_format.py index c635af8..438da87 100644 --- a/cpp_linter_hooks/clang_format.py +++ b/cpp_linter_hooks/clang_format.py @@ -24,9 +24,9 @@ def run_clang_format(args) -> int: else: retval = subprocess.run(command, stdout=subprocess.PIPE).returncode return retval, output - except FileNotFoundError as e: + except FileNotFoundError as stderr: retval = 1 - return retval, e + return retval, stderr def main() -> int: diff --git a/cpp_linter_hooks/clang_tidy.py b/cpp_linter_hooks/clang_tidy.py index 2ce4f7c..a029d7d 100644 --- a/cpp_linter_hooks/clang_tidy.py +++ b/cpp_linter_hooks/clang_tidy.py @@ -23,9 +23,9 @@ def run_clang_tidy(args) -> int: if "warning:" in output or "error:" in output: retval = 1 return retval, output - except FileNotFoundError as e: + except FileNotFoundError as stderr: retval = 1 - return retval, e + return retval, stderr def main() -> int: diff --git a/cpp_linter_hooks/util.py b/cpp_linter_hooks/util.py index 8a86a4c..76fb0bd 100644 --- a/cpp_linter_hooks/util.py +++ b/cpp_linter_hooks/util.py @@ -19,7 +19,7 @@ def install_clang_tools(version: str) -> int: # clang-tools exist because install_requires=['clang-tools'] in setup.py install_tool_cmd = ['clang-tools', '-i', version] else: - # install verison 13 by default if clang-tools not exist. + # install version 13 by default if clang-tools not exist. install_tool_cmd = ['clang-tools', '-i', '13'] try: subprocess.run(install_tool_cmd, stdout=subprocess.PIPE) @@ -40,4 +40,5 @@ def get_expect_version(args) -> str: # when --version=14 expect_version = arg.replace(" ", "").replace("=", "").replace("--version", "") return expect_version + print(arg) return "" diff --git a/pyproject.toml b/pyproject.toml index 7396fc6..b450ecb 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -13,7 +13,7 @@ authors = [ ] classifiers = [ # https://pypi.org/pypi?%3Aaction=list_classifiers - "Development Status :: 4 - Beta", + "Development Status :: 5 - Production/Stable", "Intended Audience :: Developers", "Intended Audience :: System Administrators", "Intended Audience :: Information Technology", diff --git a/testing/.pre-commit-config.yaml b/testing/.pre-commit-config.yaml index 0116dec..5c8a39b 100644 --- a/testing/.pre-commit-config.yaml +++ b/testing/.pre-commit-config.yaml @@ -1,8 +1,8 @@ repos: - repo: https://github.com/cpp-linter/cpp-linter-hooks - rev: + rev: 4d7a77444488b57c42065e4a14e891ae29761d0b hooks: - id: clang-format - args: [--style=file] # to load .clang-format + args: [--style=file, --version=15] # to load .clang-format - id: clang-tidy - args: [--checks=.clang-tidy] # path/to/.clang-tidy + args: [--checks=.clang-tidy, --version=15] # path/to/.clang-tidy diff --git a/testing/README.md b/testing/README.md new file mode 100644 index 0000000..7c339a3 --- /dev/null +++ b/testing/README.md @@ -0,0 +1,8 @@ +# Test cpp-linter-hooks + +## Test locally + +```bash +pre-commit try-repo ./.. clang-format --verbose --all-files +pre-commit try-repo ./.. clang-tidy --verbose --all-files +``` From 2a92e91720ca4bc79d67c3e4aea57642f598d534 Mon Sep 17 00:00:00 2001 From: shenxianpeng Date: Thu, 21 Dec 2023 01:19:30 -0500 Subject: [PATCH 2/6] chore: add debug info --- cpp_linter_hooks/__init__.py | 1 + testing/.pre-commit-config.yaml | 2 +- testing/main.c | 8 +++++++- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/cpp_linter_hooks/__init__.py b/cpp_linter_hooks/__init__.py index a5fb9af..dc3c608 100644 --- a/cpp_linter_hooks/__init__.py +++ b/cpp_linter_hooks/__init__.py @@ -9,6 +9,7 @@ print(args) expect_version = get_expect_version(args) +print(f"expect_version is {expect_version}") for tool in clang_tools: if expect_version: diff --git a/testing/.pre-commit-config.yaml b/testing/.pre-commit-config.yaml index 5c8a39b..0db7d70 100644 --- a/testing/.pre-commit-config.yaml +++ b/testing/.pre-commit-config.yaml @@ -1,6 +1,6 @@ repos: - repo: https://github.com/cpp-linter/cpp-linter-hooks - rev: 4d7a77444488b57c42065e4a14e891ae29761d0b + rev: d59f2627a2bbb3bba3da08299f556787e15ed81e hooks: - id: clang-format args: [--style=file, --version=15] # to load .clang-format diff --git a/testing/main.c b/testing/main.c index 6b1a169..67e9a33 100644 --- a/testing/main.c +++ b/testing/main.c @@ -1,2 +1,8 @@ #include -int main() {for (;;) break; printf("Hello world!\n");return 0;} +int main() +{ + for (;;) + break; + printf("Hello world!\n"); + return 0; +} From cae77b86810763dae937a46e784701f1cef8d5cd Mon Sep 17 00:00:00 2001 From: shenxianpeng Date: Thu, 21 Dec 2023 02:43:27 -0500 Subject: [PATCH 3/6] chore: update debug infor --- cpp_linter_hooks/util.py | 1 - testing/.pre-commit-config.yaml | 2 +- testing/main.c | 8 +------- 3 files changed, 2 insertions(+), 9 deletions(-) diff --git a/cpp_linter_hooks/util.py b/cpp_linter_hooks/util.py index 76fb0bd..101aa02 100644 --- a/cpp_linter_hooks/util.py +++ b/cpp_linter_hooks/util.py @@ -40,5 +40,4 @@ def get_expect_version(args) -> str: # when --version=14 expect_version = arg.replace(" ", "").replace("=", "").replace("--version", "") return expect_version - print(arg) return "" diff --git a/testing/.pre-commit-config.yaml b/testing/.pre-commit-config.yaml index 0db7d70..04e3fc7 100644 --- a/testing/.pre-commit-config.yaml +++ b/testing/.pre-commit-config.yaml @@ -1,6 +1,6 @@ repos: - repo: https://github.com/cpp-linter/cpp-linter-hooks - rev: d59f2627a2bbb3bba3da08299f556787e15ed81e + rev: 2a92e91720ca4bc79d67c3e4aea57642f598d534 hooks: - id: clang-format args: [--style=file, --version=15] # to load .clang-format diff --git a/testing/main.c b/testing/main.c index 67e9a33..6b1a169 100644 --- a/testing/main.c +++ b/testing/main.c @@ -1,8 +1,2 @@ #include -int main() -{ - for (;;) - break; - printf("Hello world!\n"); - return 0; -} +int main() {for (;;) break; printf("Hello world!\n");return 0;} From a457fe8b2c7caf4e73b4833ac83a508be0652fbf Mon Sep 17 00:00:00 2001 From: shenxianpeng Date: Thu, 21 Dec 2023 02:46:47 -0500 Subject: [PATCH 4/6] chore: update debug infor --- testing/.pre-commit-config.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/testing/.pre-commit-config.yaml b/testing/.pre-commit-config.yaml index 04e3fc7..14e7205 100644 --- a/testing/.pre-commit-config.yaml +++ b/testing/.pre-commit-config.yaml @@ -3,6 +3,6 @@ repos: rev: 2a92e91720ca4bc79d67c3e4aea57642f598d534 hooks: - id: clang-format - args: [--style=file, --version=15] # to load .clang-format + args: [--style=file, --version=16] # to load .clang-format - id: clang-tidy - args: [--checks=.clang-tidy, --version=15] # path/to/.clang-tidy + args: [--checks=.clang-tidy, --version=16] # path/to/.clang-tidy From ca74e223eaf9f09debc5f32d48cc80251507755d Mon Sep 17 00:00:00 2001 From: shenxianpeng Date: Thu, 21 Dec 2023 03:04:09 -0500 Subject: [PATCH 5/6] chore: update --- .github/workflows/test.yml | 9 +-------- .gitignore | 3 ++- cpp_linter_hooks/__init__.py | 2 -- requirements-dev.txt | 1 + testing/run.sh | 17 +++++++++++++++++ 5 files changed, 21 insertions(+), 11 deletions(-) create mode 100644 testing/run.sh diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 1ddcb47..77a20de 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -34,11 +34,4 @@ jobs: fail_ci_if_error: true # optional (default = false) verbose: true # optional (default = false) - name: Test cpp-linter-hooks - run: | - pip install pre-commit - pre-commit install - pre-commit try-repo . -c testing/.pre-commit-config.yaml --files testing/main.c | tee result.txt || true - grep -e "Failed" result.txt - if [ $? -ne 0 ]; then - exit 1 - fi + run: sh testing/run.sh diff --git a/.gitignore b/.gitignore index 9dc11d2..ab736da 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,5 @@ tests/__pycache__ .coverage __pycache__ venv -result.txt \ No newline at end of file +result.txt +testing/main.c diff --git a/cpp_linter_hooks/__init__.py b/cpp_linter_hooks/__init__.py index dc3c608..8769504 100644 --- a/cpp_linter_hooks/__init__.py +++ b/cpp_linter_hooks/__init__.py @@ -6,10 +6,8 @@ clang_tools = ['clang-format', 'clang-tidy'] args = list(sys.argv[1:]) -print(args) expect_version = get_expect_version(args) -print(f"expect_version is {expect_version}") for tool in clang_tools: if expect_version: diff --git a/requirements-dev.txt b/requirements-dev.txt index 7093b61..4dbfffa 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -1,2 +1,3 @@ coverage +pre-commit pytest diff --git a/testing/run.sh b/testing/run.sh new file mode 100644 index 0000000..5d96fac --- /dev/null +++ b/testing/run.sh @@ -0,0 +1,17 @@ +pre-commit install +pre-commit try-repo . -c testing/.pre-commit-config.yaml --files testing/main.c | tee result.txt || true + +failed_cases=`grep -c "Failed" result.txt` + +if [ $failed_cases -eq 2 ]; then + echo "==============================" + echo "Test cpp-linter-hooks success." + echo "==============================" + exit 0 + rm result.txt +else + echo "=============================" + echo "Test cpp-linter-hooks failed." + echo "=============================" + exit 1 +fi From 69da9ce98ca1837ff44119833603abed386363ad Mon Sep 17 00:00:00 2001 From: shenxianpeng Date: Thu, 21 Dec 2023 03:44:32 -0500 Subject: [PATCH 6/6] docs: update README.md --- README.md | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 2e9cf07..ea431a5 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ The example of using custom config: `.clang-format` and `.clang-tidy` ```yaml repos: - repo: https://github.com/cpp-linter/cpp-linter-hooks - rev: v0.2.1 + rev: v0.2.5 hooks: - id: clang-format args: [--style=file] # to load .clang-format @@ -44,12 +44,12 @@ The example of using any version of [clang-tools](https://github.com/cpp-linter/ ```yaml repos: - repo: https://github.com/cpp-linter/cpp-linter-hooks - rev: v0.2.1 + rev: v0.2.5 hooks: - id: clang-format - args: [--style=file, --version=13] + args: [--style=file, --version=16] - id: clang-tidy - args: [--checks=.clang-tidy, --version=12] + args: [--checks=.clang-tidy, --version=16] ``` ## Output @@ -107,25 +107,21 @@ int main() {for (;;) break; printf("Hello world!\n");return 0;} ^ ``` -### chang-tidy output +### clang-tidy output ```bash clang-tidy...............................................................Failed - hook id: clang-tidy - exit code: 1 -418 warnings and 1 error generated. -Error while processing /home/ubuntu/cpp-linter-hooks/testing/main.c. -Suppressed 417 warnings (417 in non-user code). +522 warnings generated. +Suppressed 521 warnings (521 in non-user code). Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well. -Found compiler error(s). -/home/ubuntu/cpp-linter-hooks/testing/main.c:3:11: warning: statement should be inside braces [readability-braces-around-statements] - for (;;) break; - ^ - { -/usr/include/stdio.h:33:10: error: 'stddef.h' file not found [clang-diagnostic-error] -#include - ^~~~~~~~~~ +/home/runner/work/cpp-linter-hooks/cpp-linter-hooks/testing/main.c:4:13: warning: statement should be inside braces [readability-braces-around-statements] + for (;;) + ^ + { + ``` ## Contributing