From d815daed2977a389125d779619ff20074540a9e7 Mon Sep 17 00:00:00 2001 From: valeros Date: Wed, 30 Oct 2019 13:38:46 +0200 Subject: [PATCH] Allow specifying defect level that will cause failure --- platformio/commands/check/command.py | 8 ++++-- tests/commands/test_check.py | 43 ++++++++++++++++++++++++---- 2 files changed, 43 insertions(+), 8 deletions(-) diff --git a/platformio/commands/check/command.py b/platformio/commands/check/command.py index be270b3d..cb155e8a 100644 --- a/platformio/commands/check/command.py +++ b/platformio/commands/check/command.py @@ -56,7 +56,11 @@ 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) +@click.option( + "--fail-on-defect", + multiple=True, + type=click.Choice(DefectItem.SEVERITY_LABELS.values()), +) def cli( environment, project_dir, @@ -140,7 +144,7 @@ def cli( result["succeeded"] = rc == 0 if fail_on_defect: result["succeeded"] = rc == 0 and not any( - d.severity == DefectItem.SEVERITY_HIGH + DefectItem.SEVERITY_LABELS[d.severity] in fail_on_defect for d in result["defects"] ) result["stats"] = collect_component_stats(result) diff --git a/tests/commands/test_check.py b/tests/commands/test_check.py index c5a8a7ff..59bfff0f 100644 --- a/tests/commands/test_check.py +++ b/tests/commands/test_check.py @@ -32,25 +32,26 @@ void run_defects() { int* doubleFreePi = (int*)malloc(sizeof(int)); *doubleFreePi=2; free(doubleFreePi); - free(doubleFreePi); + free(doubleFreePi); /* High */ /* Reading uninitialized memory */ int* uninitializedPi = (int*)malloc(sizeof(int)); - *uninitializedPi++; + *uninitializedPi++; /* High + Medium*/ free(uninitializedPi); /* Delete instead of delete [] */ int* wrongDeletePi = new int[10]; wrongDeletePi++; - delete wrongDeletePi; + delete wrongDeletePi; /* High */ /* Index out of bounds */ int arr[10]; for(int i=0; i < 11; i++) { - arr[i] = 0; + arr[i] = 0; /* High */ } } +/* Low */ void unusedFuntion(){ } @@ -197,7 +198,7 @@ def test_check_success_if_no_errors(clirunner, tmpdir): """ #include -void unused_functin(){ +void unused_function(){ int unusedVar = 0; int* iP = &unusedVar; *iP++; @@ -290,8 +291,38 @@ def test_check_fails_on_defects_only_with_flag(clirunner, tmpdir): default_result = clirunner.invoke(cmd_check, ["--project-dir", str(tmpdir)]) result_with_flag = clirunner.invoke( - cmd_check, ["--project-dir", str(tmpdir), "--fail-on-defect"] + cmd_check, ["--project-dir", str(tmpdir), "--fail-on-defect=high"] ) assert default_result.exit_code == 0 assert result_with_flag.exit_code != 0 + + +def test_check_fails_on_defects_only_on_specified_level(clirunner, tmpdir): + config = DEFAULT_CONFIG + "\ncheck_tool = cppcheck, clangtidy" + tmpdir.join("platformio.ini").write(config) + tmpdir.mkdir("src").join("main.c").write( + """ +#include + +void unused_function(){ + int unusedVar = 0; + int* iP = &unusedVar; + *iP++; +} + +int main() { +} +""" + ) + + high_result = clirunner.invoke( + cmd_check, ["--project-dir", str(tmpdir), "--fail-on-defect=high"] + ) + + low_result = clirunner.invoke( + cmd_check, ["--project-dir", str(tmpdir), "--fail-on-defect=low"] + ) + + assert high_result.exit_code == 0 + assert low_result.exit_code != 0