Skip to content

Commit

Permalink
extra-arg accumulates as a list
Browse files Browse the repository at this point in the history
  • Loading branch information
2bndy5 committed Sep 7, 2022
1 parent f87ef8d commit 55fee27
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 16 deletions.
28 changes: 18 additions & 10 deletions cpp_linter/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,10 +239,16 @@
)
cli_arg_parser.add_argument(
"-x",
"--extra-args",
default="",
"--extra-arg",
default=[],
action="append",
help="""A string of extra arguments passed to clang-tidy for use as
compiler arguments.
compiler arguments. This can be specified more than once for each
additional argument. Recommend using quotes around the value here:
.. code-block:: shell
cpp-linter --extra-arg="-std=c++17" --extra-arg="-Wall"
Defaults to ``'%(default)s'``.
""",
Expand Down Expand Up @@ -487,7 +493,7 @@ def run_clang_tidy(
lines_changed_only: int,
database: str,
repo_root: str,
extra_args: str,
extra_args: List[str],
) -> None:
"""Run clang-tidy on a certain file.
Expand All @@ -500,7 +506,7 @@ def run_clang_tidy(
diff info.
:param database: The path to the compilation database.
:param repo_root: The path to the repository root folder.
:param extra_args: A string of extra arguments used by clang-tidy as compiler
:param extra_args: A list of extra arguments used by clang-tidy as compiler
arguments.
"""
if checks == "-*": # if all checks are disabled, then clang-tidy is skipped
Expand All @@ -524,8 +530,10 @@ def run_clang_tidy(
line_ranges = dict(name=filename, lines=file_obj["line_filter"][ranges])
logger.info("line_filter = %s", json.dumps([line_ranges]))
cmds.append(f"--line-filter={json.dumps([line_ranges])}")
if extra_args:
cmds.append(f"--extra-arg={repr(extra_args)}")
if len(extra_args) == 1 and " " in extra_args[0]:
extra_args = extra_args[0].split()
for extra_arg in extra_args:
cmds.append(f"--extra-arg={extra_arg}")
cmds.append(filename)
# clear yml file's content before running clang-tidy
Path("clang_tidy_output.yml").write_bytes(b"")
Expand Down Expand Up @@ -638,7 +646,7 @@ def capture_clang_tools_output(
lines_changed_only: int,
database: str,
repo_root: str,
extra_args: str,
extra_args: List[str],
):
"""Execute and capture all output from clang-tidy and clang-format. This aggregates
results in the :attr:`~cpp_linter.Globals.OUTPUT`.
Expand All @@ -652,7 +660,7 @@ def capture_clang_tools_output(
diff info.
:param database: The path to the compilation database.
:param repo_root: The path to the repository root folder.
:param extra_args: A string of extra arguments used by clang-tidy as compiler
:param extra_args: A list of extra arguments used by clang-tidy as compiler
arguments.
"""
# temporary cache of parsed notifications for use in log commands
Expand Down Expand Up @@ -987,7 +995,7 @@ def main():
args.lines_changed_only,
args.database,
args.repo_root,
args.extra_args,
args.extra_arg,
)

start_log_group("Posting comment(s)")
Expand Down
2 changes: 1 addition & 1 deletion tests/capture_tools_output/test_database_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def test_db_detection(
lines_changed_only=0, # analyze complete file
database=database,
repo_root=rel_root,
extra_args="",
extra_args=[],
)
matched_args = []
for record in caplog.records:
Expand Down
6 changes: 3 additions & 3 deletions tests/capture_tools_output/test_tools_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def test_format_annotations(
lines_changed_only=lines_changed_only,
database="",
repo_root="",
extra_args="",
extra_args=[],
)
assert "Output from `clang-tidy`" not in cpp_linter.Globals.OUTPUT
caplog.set_level(logging.INFO, logger=log_commander.name)
Expand Down Expand Up @@ -163,7 +163,7 @@ def test_tidy_annotations(
lines_changed_only=lines_changed_only,
database="",
repo_root="",
extra_args="",
extra_args=[],
)
assert "Run `clang-format` on the following files" not in cpp_linter.Globals.OUTPUT
caplog.set_level(logging.INFO, logger=log_commander.name)
Expand Down Expand Up @@ -197,7 +197,7 @@ def test_diff_comment(lines_changed_only: int):
lines_changed_only=lines_changed_only,
database="",
repo_root="",
extra_args="",
extra_args=[],
)
diff_comments = list_diff_comments(lines_changed_only)
# output = Path(__file__).parent / "diff_comments.json"
Expand Down
5 changes: 3 additions & 2 deletions tests/test_cli_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Args:
files_changed_only: bool = False
thread_comments: bool = False
file_annotations: bool = True
extra_args: str = ""
extra_arg: List[str] = []


def test_defaults():
Expand All @@ -63,7 +63,8 @@ def test_defaults():
("files-changed-only", "True", "files_changed_only", True),
("thread-comments", "True", "thread_comments", True),
("file-annotations", "False", "file_annotations", False),
("extra-args", '"-std=c++17"', "extra_args", '"-std=c++17"'),
("extra-arg", "-std=c++17", "extra_arg", ["-std=c++17"]),
("extra-arg", '"-std=c++17 -Wall"', "extra_arg", ['"-std=c++17 -Wall"']),
],
)
def test_arg_parser(
Expand Down

0 comments on commit 55fee27

Please sign in to comment.