From 4486a85d4c3e6ed3776dedd17004c5ab1bc777ee Mon Sep 17 00:00:00 2001 From: valeros Date: Fri, 25 Oct 2019 15:40:50 +0300 Subject: [PATCH] Introduce new flag --fail-on-defect to pio check --- platformio/commands/check/command.py | 15 +++++++++++---- tests/commands/test_check.py | 26 ++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/platformio/commands/check/command.py b/platformio/commands/check/command.py index e7ba650a..b6fc2bec 100644 --- a/platformio/commands/check/command.py +++ b/platformio/commands/check/command.py @@ -56,6 +56,7 @@ from platformio.project.helpers import find_project_dir_above, get_project_dir @click.option("-s", "--silent", is_flag=True) @click.option("-v", "--verbose", is_flag=True) @click.option("--json-output", is_flag=True) +@click.option("--fail-on-defect", is_flag=True) def cli( environment, project_dir, @@ -66,6 +67,7 @@ def cli( silent, verbose, json_output, + fail_on_defect ): app.set_session_var("custom_project_conf", project_conf) @@ -134,9 +136,11 @@ def cli( result["defects"] = ct.get_defects() result["duration"] = time() - result["duration"] - result["succeeded"] = rc == 0 and not any( - d.severity == DefectItem.SEVERITY_HIGH for d in result["defects"] - ) + + result["succeeded"] = rc == 0 + if fail_on_defect: + result["succeeded"] = rc == 0 and not any( + d.severity == DefectItem.SEVERITY_HIGH for d in result["defects"]) result["stats"] = collect_component_stats(result) results.append(result) @@ -144,7 +148,10 @@ def cli( click.echo("\n".join(repr(d) for d in result["defects"])) if not json_output and not silent: - if not result["defects"]: + if rc != 0: + click.echo("Error: %s failed to perform check! Please " + "examine tool output in verbose mode." % tool) + elif not result["defects"]: click.echo("No defects found") print_processing_footer(result) diff --git a/tests/commands/test_check.py b/tests/commands/test_check.py index fbb5b881..028798ef 100644 --- a/tests/commands/test_check.py +++ b/tests/commands/test_check.py @@ -280,3 +280,29 @@ R21.4 text. assert result.exit_code != 0 assert "R21.3 Found MISRA defect" in result.output assert not isfile(join(str(check_dir), "src", "main.cpp.dump")) + + +def test_check_fails_on_defects_only_with_flag(clirunner, tmpdir): + config = DEFAULT_CONFIG + "\ncheck_tool = cppcheck, clangtidy" + tmpdir.join("platformio.ini").write(config) + tmpdir.mkdir("src").join("main.cpp").write(TEST_CODE) + + default_result = clirunner.invoke( + cmd_check, ["--project-dir", str(tmpdir)]) + + result_with_flag = clirunner.invoke( + cmd_check, ["--project-dir", str(tmpdir), "--fail-on-defect"]) + + assert default_result.exit_code == 0 + assert result_with_flag.exit_code != 0 + + +def test_check_bad_tool_flag_fails_check(clirunner, tmpdir): + config = DEFAULT_CONFIG + "\ncheck_tool = cppcheck, clangtidy" + config += "\ncheck_flags = --unknown-flag" + tmpdir.join("platformio.ini").write(config) + tmpdir.mkdir("src").join("main.cpp").write(TEST_CODE) + + result = clirunner.invoke(cmd_check, ["--project-dir", str(tmpdir)]) + + assert result.exit_code != 0