Skip to content

Commit

Permalink
fix: exception of no include guard (#39)
Browse files Browse the repository at this point in the history
* fix: exception of no include guard

Signed-off-by: Takagi, Isamu <isamu.takagi@tier4.jp>

* fix: add test
  • Loading branch information
isamu-takagi authored Feb 25, 2022
1 parent 8e53e9a commit 8aa88e8
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 6 deletions.
7 changes: 4 additions & 3 deletions pre_commit_hooks/ros_include_guard.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
20 changes: 17 additions & 3 deletions tests/test_ros_include_guard.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#pragma once
5 changes: 5 additions & 0 deletions tests/test_ros_include_guard/include/rospkg/pragma.right.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

#ifndef ROSPKG__PRAGMA_HPP_
#define ROSPKG__PRAGMA_HPP_
#endif // ROSPKG__PRAGMA_HPP_
5 changes: 5 additions & 0 deletions tests/test_ros_include_guard/include/rospkg/pragma.wrong.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

#ifndef DUMMY
#define DUMMY
#endif // DUMMY

0 comments on commit 8aa88e8

Please sign in to comment.