Allow specifying defect level that will cause failure

This commit is contained in:
valeros
2019-10-30 13:38:46 +02:00
parent c4e7674585
commit d815daed29
2 changed files with 43 additions and 8 deletions

View File

@ -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)

View File

@ -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 <stdlib.h>
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 <stdlib.h>
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