From 8aa88e8d3f96f26745daf4289dc0170571c31b41 Mon Sep 17 00:00:00 2001 From: "Takagi, Isamu" <43976882+isamu-takagi@users.noreply.github.com> Date: Fri, 25 Feb 2022 13:49:42 +0900 Subject: [PATCH] fix: exception of no include guard (#39) * fix: exception of no include guard Signed-off-by: Takagi, Isamu * fix: add test --- pre_commit_hooks/ros_include_guard.py | 7 ++++--- tests/test_ros_include_guard.py | 20 ++++++++++++++++--- .../include/rospkg/none.hpp | 0 .../include/rospkg/pragma.only.hpp | 1 + .../include/rospkg/pragma.right.hpp | 5 +++++ .../include/rospkg/pragma.wrong.hpp | 5 +++++ 6 files changed, 32 insertions(+), 6 deletions(-) create mode 100644 tests/test_ros_include_guard/include/rospkg/none.hpp create mode 100644 tests/test_ros_include_guard/include/rospkg/pragma.only.hpp create mode 100644 tests/test_ros_include_guard/include/rospkg/pragma.right.hpp create mode 100644 tests/test_ros_include_guard/include/rospkg/pragma.wrong.hpp diff --git a/pre_commit_hooks/ros_include_guard.py b/pre_commit_hooks/ros_include_guard.py index 78d5d77..1b14233 100644 --- a/pre_commit_hooks/ros_include_guard.py +++ b/pre_commit_hooks/ros_include_guard.py @@ -120,9 +120,10 @@ def main(argv=None): lines = filepath.read_text().split("\n") guard = get_include_guard_info(lines) # Error if the include guard is not found. - if guard.is_none() and not guard.has_pragma_once: - print("No include guard in {}".format(filepath)) - return_code = 1 + if guard.is_none(): + if not guard.has_pragma_once: + print("No include guard in {}".format(filepath)) + return_code = 1 continue # Error and auto fix if the macro name is not correct. macro_name = get_include_guard_macro_name(filepath) diff --git a/tests/test_ros_include_guard.py b/tests/test_ros_include_guard.py index f17da82..5555bc4 100644 --- a/tests/test_ros_include_guard.py +++ b/tests/test_ros_include_guard.py @@ -4,19 +4,25 @@ from pre_commit_hooks import ros_include_guard -cases = [ +cases_auto_fix = [ "include/rospkg/foobar.h", "include/rospkg/foobar.hpp", "include/rospkg/nolint.hpp", + "include/rospkg/pragma.hpp", "src/foo_bar_baz.hpp", "src/foo_bar/baz.hpp", "src/foo/bar_baz.hpp", "src/foo/bar/baz.hpp", ] +cases_no_fix = [ + ("include/rospkg/pragma.only.hpp", 0), + ("include/rospkg/none.hpp", 1), +] + -@pytest.mark.parametrize(("target_file"), cases) -def test(target_file, datadir): +@pytest.mark.parametrize(("target_file"), cases_auto_fix) +def test_auto_fix(target_file, datadir): target_path = datadir.joinpath(target_file) right_path = target_path.with_suffix(".right" + target_path.suffix) @@ -33,3 +39,11 @@ def test(target_file, datadir): return_code = ros_include_guard.main([str(target_path)]) assert return_code == 0 assert target_path.read_text() == right_path.read_text() + + +@pytest.mark.parametrize(("target_file", "answer_code"), cases_no_fix) +def test_no_fix(target_file, answer_code, datadir): + + target_path = datadir.joinpath(target_file) + return_code = ros_include_guard.main([str(target_path)]) + assert return_code == answer_code diff --git a/tests/test_ros_include_guard/include/rospkg/none.hpp b/tests/test_ros_include_guard/include/rospkg/none.hpp new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_ros_include_guard/include/rospkg/pragma.only.hpp b/tests/test_ros_include_guard/include/rospkg/pragma.only.hpp new file mode 100644 index 0000000..6f70f09 --- /dev/null +++ b/tests/test_ros_include_guard/include/rospkg/pragma.only.hpp @@ -0,0 +1 @@ +#pragma once diff --git a/tests/test_ros_include_guard/include/rospkg/pragma.right.hpp b/tests/test_ros_include_guard/include/rospkg/pragma.right.hpp new file mode 100644 index 0000000..c40d633 --- /dev/null +++ b/tests/test_ros_include_guard/include/rospkg/pragma.right.hpp @@ -0,0 +1,5 @@ +#pragma once + +#ifndef ROSPKG__PRAGMA_HPP_ +#define ROSPKG__PRAGMA_HPP_ +#endif // ROSPKG__PRAGMA_HPP_ diff --git a/tests/test_ros_include_guard/include/rospkg/pragma.wrong.hpp b/tests/test_ros_include_guard/include/rospkg/pragma.wrong.hpp new file mode 100644 index 0000000..5c2e3b1 --- /dev/null +++ b/tests/test_ros_include_guard/include/rospkg/pragma.wrong.hpp @@ -0,0 +1,5 @@ +#pragma once + +#ifndef DUMMY +#define DUMMY +#endif // DUMMY