2019-04-14 10:45:20 +02:00
# python -m pytest test-helloworld.py
import os
2019-04-14 16:48:59 +02:00
import re
2022-06-12 11:06:15 +02:00
import tempfile
2022-09-06 23:11:39 +02:00
import pytest
2023-02-13 20:54:21 +01:00
import glob
2022-09-06 23:11:39 +02:00
2019-04-15 16:57:16 +02:00
from testutils import create_gui_project_file , cppcheck
2019-04-14 10:45:20 +02:00
2019-04-15 10:01:27 +02:00
# Run Cppcheck from project path
def cppcheck_local ( args ) :
cwd = os . getcwd ( )
2019-05-03 20:22:35 +02:00
os . chdir ( ' helloworld ' )
2019-04-15 10:01:27 +02:00
ret , stdout , stderr = cppcheck ( args )
os . chdir ( cwd )
2021-01-31 14:27:11 +01:00
return ret , stdout , stderr
2019-04-15 10:01:27 +02:00
def getRelativeProjectPath ( ) :
2019-05-03 20:22:35 +02:00
return ' helloworld '
2019-04-15 10:01:27 +02:00
def getAbsoluteProjectPath ( ) :
2019-05-03 20:22:35 +02:00
return os . path . join ( os . getcwd ( ) , ' helloworld ' )
2019-04-15 10:01:27 +02:00
2019-04-14 16:48:59 +02:00
# Get Visual Studio configurations checking a file
# Checking {file} {config}...
def getVsConfigs ( stdout , filename ) :
ret = [ ]
for line in stdout . split ( ' \n ' ) :
2021-01-31 14:27:11 +01:00
if not line . startswith ( ' Checking %s ' % filename ) :
2019-04-14 16:48:59 +02:00
continue
if not line . endswith ( ' ... ' ) :
continue
res = re . match ( r ' .* ([A-Za-z0-9|]+)... ' , line )
if res :
ret . append ( res . group ( 1 ) )
ret . sort ( )
return ' ' . join ( ret )
2019-04-14 10:45:20 +02:00
def test_relative_path ( ) :
2019-08-18 12:51:32 +02:00
ret , stdout , stderr = cppcheck ( [ ' --template=cppcheck1 ' , ' helloworld ' ] )
2019-05-03 20:22:35 +02:00
filename = os . path . join ( ' helloworld ' , ' main.c ' )
2021-04-03 21:22:39 +02:00
assert ret == 0 , stdout
2021-01-31 14:27:11 +01:00
assert stderr == ' [ %s :5]: (error) Division by zero. \n ' % filename
2019-04-14 10:45:20 +02:00
2019-04-14 16:48:59 +02:00
2019-04-14 10:45:20 +02:00
def test_local_path ( ) :
2019-08-18 12:51:32 +02:00
ret , stdout , stderr = cppcheck_local ( [ ' --template=cppcheck1 ' , ' . ' ] )
2021-04-03 21:22:39 +02:00
assert ret == 0 , stdout
2019-04-14 10:45:20 +02:00
assert stderr == ' [main.c:5]: (error) Division by zero. \n '
def test_absolute_path ( ) :
2019-04-15 10:01:27 +02:00
prjpath = getAbsoluteProjectPath ( )
2019-08-18 12:51:32 +02:00
ret , stdout , stderr = cppcheck ( [ ' --template=cppcheck1 ' , prjpath ] )
2019-04-14 15:53:32 +02:00
filename = os . path . join ( prjpath , ' main.c ' )
2021-04-03 21:22:39 +02:00
assert ret == 0 , stdout
2021-01-31 14:27:11 +01:00
assert stderr == ' [ %s :5]: (error) Division by zero. \n ' % filename
2019-04-14 10:45:20 +02:00
2019-04-14 15:00:03 +02:00
def test_addon_local_path ( ) :
2023-05-27 10:24:00 +02:00
ret , stdout , stderr = cppcheck_local ( [ ' --addon=misra ' , ' --enable=style ' , ' --template=cppcheck1 ' , ' . ' ] )
2021-04-03 21:22:39 +02:00
assert ret == 0 , stdout
2019-04-14 15:00:03 +02:00
assert stderr == ( ' [main.c:5]: (error) Division by zero. \n '
' [main.c:1]: (style) misra violation (use --rule-texts=<file> to get proper output) \n ' )
2023-05-27 10:24:00 +02:00
def test_addon_local_path_not_enable ( ) :
ret , stdout , stderr = cppcheck_local ( [ ' --addon=misra ' , ' --template=cppcheck1 ' , ' . ' ] )
assert ret == 0 , stdout
assert stderr == ' [main.c:5]: (error) Division by zero. \n '
2019-04-14 15:00:03 +02:00
def test_addon_absolute_path ( ) :
2019-04-15 10:01:27 +02:00
prjpath = getAbsoluteProjectPath ( )
2023-05-27 10:24:00 +02:00
ret , stdout , stderr = cppcheck ( [ ' --addon=misra ' , ' --enable=style ' , ' --template=cppcheck1 ' , prjpath ] )
2019-04-14 15:53:32 +02:00
filename = os . path . join ( prjpath , ' main.c ' )
2021-04-03 21:22:39 +02:00
assert ret == 0 , stdout
2019-04-14 15:53:32 +02:00
assert stderr == ( ' [ %s :5]: (error) Division by zero. \n '
' [ %s :1]: (style) misra violation (use --rule-texts=<file> to get proper output) \n ' % ( filename , filename ) )
2019-04-14 15:00:03 +02:00
def test_addon_relative_path ( ) :
2019-04-15 10:01:27 +02:00
prjpath = getRelativeProjectPath ( )
2023-09-11 11:08:23 +02:00
ret , stdout , stderr = cppcheck ( [ ' --addon=misra ' , ' --enable=style ' , ' --template=cppcheck1 ' , prjpath ] )
2019-04-14 15:53:32 +02:00
filename = os . path . join ( prjpath , ' main.c ' )
2021-04-03 21:22:39 +02:00
assert ret == 0 , stdout
2021-01-31 14:27:11 +01:00
assert stdout == ( ' Checking %s ... \n '
' Checking %s : SOME_CONFIG... \n ' % ( filename , filename ) )
2019-04-14 15:53:32 +02:00
assert stderr == ( ' [ %s :5]: (error) Division by zero. \n '
' [ %s :1]: (style) misra violation (use --rule-texts=<file> to get proper output) \n ' % ( filename , filename ) )
2019-04-14 15:00:03 +02:00
2021-01-31 14:27:11 +01:00
def test_addon_with_gui_project ( ) :
2019-05-03 20:22:35 +02:00
project_file = ' helloworld/test.cppcheck '
2019-04-15 20:02:17 +02:00
create_gui_project_file ( project_file , paths = [ ' . ' ] , addon = ' misra ' )
2023-09-11 11:08:23 +02:00
ret , stdout , stderr = cppcheck ( [ ' --template=cppcheck1 ' , ' --enable=style ' , ' --project= ' + project_file ] )
2019-05-03 20:22:35 +02:00
filename = os . path . join ( ' helloworld ' , ' main.c ' )
2021-04-03 21:22:39 +02:00
assert ret == 0 , stdout
2021-01-31 14:27:11 +01:00
assert stdout == ' Checking %s ... \n ' % filename
2019-04-15 20:02:17 +02:00
assert stderr == ( ' [ %s :5]: (error) Division by zero. \n '
' [ %s :1]: (style) misra violation (use --rule-texts=<file> to get proper output) \n ' % ( filename , filename ) )
2019-04-14 10:45:20 +02:00
def test_basepath_relative_path ( ) :
2019-04-15 10:01:27 +02:00
prjpath = getRelativeProjectPath ( )
2019-08-18 12:51:32 +02:00
ret , stdout , stderr = cppcheck ( [ prjpath , ' --template=cppcheck1 ' , ' -rp= ' + prjpath ] )
2021-04-03 21:22:39 +02:00
assert ret == 0 , stdout
2019-04-14 10:45:20 +02:00
assert stderr == ' [main.c:5]: (error) Division by zero. \n '
def test_basepath_absolute_path ( ) :
2019-04-15 10:01:27 +02:00
prjpath = getAbsoluteProjectPath ( )
2019-08-18 12:51:32 +02:00
ret , stdout , stderr = cppcheck ( [ ' --template=cppcheck1 ' , prjpath , ' -rp= ' + prjpath ] )
2021-04-03 21:22:39 +02:00
assert ret == 0 , stdout
2019-04-14 10:45:20 +02:00
assert stderr == ' [main.c:5]: (error) Division by zero. \n '
2019-04-14 18:21:27 +02:00
def test_vs_project_local_path ( ) :
2019-08-18 12:51:32 +02:00
ret , stdout , stderr = cppcheck_local ( [ ' --template=cppcheck1 ' , ' --project=helloworld.vcxproj ' ] )
2021-04-03 21:22:39 +02:00
assert ret == 0 , stdout
2019-04-14 16:48:59 +02:00
assert getVsConfigs ( stdout , ' main.c ' ) == ' Debug|Win32 Debug|x64 Release|Win32 Release|x64 '
assert stderr == ' [main.c:5]: (error) Division by zero. \n '
2019-04-14 18:21:27 +02:00
def test_vs_project_relative_path ( ) :
2019-04-15 10:01:27 +02:00
prjpath = getRelativeProjectPath ( )
2019-08-18 12:51:32 +02:00
ret , stdout , stderr = cppcheck ( [ ' --template=cppcheck1 ' , ' --project= ' + os . path . join ( prjpath , ' helloworld.vcxproj ' ) ] )
2019-04-14 16:48:59 +02:00
filename = os . path . join ( prjpath , ' main.c ' )
2021-04-03 21:22:39 +02:00
assert ret == 0 , stdout
2019-04-14 16:48:59 +02:00
assert getVsConfigs ( stdout , filename ) == ' Debug|Win32 Debug|x64 Release|Win32 Release|x64 '
2021-01-31 14:27:11 +01:00
assert stderr == ' [ %s :5]: (error) Division by zero. \n ' % filename
2019-04-14 16:48:59 +02:00
2019-04-14 18:21:27 +02:00
def test_vs_project_absolute_path ( ) :
2019-04-15 10:01:27 +02:00
prjpath = getAbsoluteProjectPath ( )
2019-08-18 12:51:32 +02:00
ret , stdout , stderr = cppcheck ( [ ' --template=cppcheck1 ' , ' --project= ' + os . path . join ( prjpath , ' helloworld.vcxproj ' ) ] )
2019-04-14 18:09:35 +02:00
filename = os . path . join ( prjpath , ' main.c ' )
2021-04-03 21:22:39 +02:00
assert ret == 0 , stdout
2019-04-14 18:09:35 +02:00
assert getVsConfigs ( stdout , filename ) == ' Debug|Win32 Debug|x64 Release|Win32 Release|x64 '
2021-01-31 14:27:11 +01:00
assert stderr == ' [ %s :5]: (error) Division by zero. \n ' % filename
2019-04-14 18:09:35 +02:00
2019-04-14 18:21:27 +02:00
def test_cppcheck_project_local_path ( ) :
2019-08-18 12:51:32 +02:00
ret , stdout , stderr = cppcheck_local ( [ ' --template=cppcheck1 ' , ' --platform=win64 ' , ' --project=helloworld.cppcheck ' ] )
2021-04-03 21:22:39 +02:00
assert ret == 0 , stdout
2019-04-14 18:21:27 +02:00
assert getVsConfigs ( stdout , ' main.c ' ) == ' Debug|x64 '
assert stderr == ' [main.c:5]: (error) Division by zero. \n '
def test_cppcheck_project_relative_path ( ) :
2019-04-15 10:01:27 +02:00
prjpath = getRelativeProjectPath ( )
2019-08-18 12:51:32 +02:00
ret , stdout , stderr = cppcheck ( [ ' --template=cppcheck1 ' , ' --platform=win64 ' , ' --project= ' + os . path . join ( prjpath , ' helloworld.cppcheck ' ) ] )
2019-04-14 18:21:27 +02:00
filename = os . path . join ( prjpath , ' main.c ' )
2021-04-03 21:22:39 +02:00
assert ret == 0 , stdout
2019-04-14 18:21:27 +02:00
assert getVsConfigs ( stdout , filename ) == ' Debug|x64 '
2021-01-31 14:27:11 +01:00
assert stderr == ' [ %s :5]: (error) Division by zero. \n ' % filename
2019-04-14 18:21:27 +02:00
def test_cppcheck_project_absolute_path ( ) :
2019-04-15 10:01:27 +02:00
prjpath = getAbsoluteProjectPath ( )
2019-08-18 12:51:32 +02:00
ret , stdout , stderr = cppcheck ( [ ' --template=cppcheck1 ' , ' --platform=win64 ' , ' --project= ' + os . path . join ( prjpath , ' helloworld.cppcheck ' ) ] )
2019-04-14 18:21:27 +02:00
filename = os . path . join ( prjpath , ' main.c ' )
2021-04-03 21:22:39 +02:00
assert ret == 0 , stdout
2019-04-14 18:21:27 +02:00
assert getVsConfigs ( stdout , filename ) == ' Debug|x64 '
2021-01-31 14:27:11 +01:00
assert stderr == ' [ %s :5]: (error) Division by zero. \n ' % filename
2019-04-14 18:09:35 +02:00
2019-04-15 11:11:33 +02:00
def test_suppress_command_line ( ) :
prjpath = getRelativeProjectPath ( )
2019-06-22 19:20:15 +02:00
ret , stdout , stderr = cppcheck ( [ ' --suppress=zerodiv: ' + os . path . join ( prjpath , ' main.c ' ) , prjpath ] )
2021-04-03 21:22:39 +02:00
assert ret == 0 , stdout
2019-04-15 11:11:33 +02:00
assert stderr == ' '
prjpath = getAbsoluteProjectPath ( )
2019-06-22 19:20:15 +02:00
ret , stdout , stderr = cppcheck ( [ ' --suppress=zerodiv: ' + os . path . join ( prjpath , ' main.c ' ) , prjpath ] )
2021-04-03 21:22:39 +02:00
assert ret == 0 , stdout
2019-04-15 11:11:33 +02:00
assert stderr == ' '
def test_suppress_project ( ) :
2019-05-03 20:22:35 +02:00
project_file = os . path . join ( ' helloworld ' , ' test.cppcheck ' )
2019-04-15 15:03:06 +02:00
create_gui_project_file ( project_file ,
paths = [ ' . ' ] ,
suppressions = [ { ' fileName ' : ' main.c ' , ' id ' : ' zerodiv ' } ] )
2019-04-15 11:11:33 +02:00
# Relative path
2019-06-22 19:20:15 +02:00
ret , stdout , stderr = cppcheck ( [ ' --project= ' + project_file ] )
2021-04-03 21:22:39 +02:00
assert ret == 0 , stdout
2019-04-15 11:11:33 +02:00
assert stderr == ' '
2019-04-15 19:00:57 +02:00
# Absolute path
2019-06-22 19:20:15 +02:00
ret , stdout , stderr = cppcheck ( [ ' --project= ' + os . path . join ( os . getcwd ( ) , ' helloworld ' , ' test.cppcheck ' ) ] )
2021-04-03 21:22:39 +02:00
assert ret == 0 , stdout
2019-04-15 11:11:33 +02:00
assert stderr == ' '
2019-09-12 09:32:24 +02:00
def test_exclude ( ) :
prjpath = getRelativeProjectPath ( )
2021-04-03 21:22:39 +02:00
ret , stdout , _ = cppcheck ( [ ' -i ' + prjpath , ' --platform=win64 ' , ' --project= ' + os . path . join ( prjpath , ' helloworld.cppcheck ' ) ] )
assert ret == 1
2021-10-30 13:30:48 +02:00
assert stdout == ' cppcheck: error: no C or C++ source files found. \n '
2019-09-12 09:32:24 +02:00
2022-06-12 11:06:15 +02:00
def test_build_dir_dump_output ( ) :
with tempfile . TemporaryDirectory ( ) as tempdir :
args = f ' --cppcheck-build-dir= { tempdir } --addon=misra helloworld '
cppcheck ( args . split ( ) )
cppcheck ( args . split ( ) )
2023-02-13 20:54:21 +01:00
filename = f ' { tempdir } /main.a1.*.dump '
filelist = glob . glob ( filename )
2023-04-07 19:02:10 +02:00
assert ( len ( filelist ) == 0 )
2023-02-13 20:54:21 +01:00
2022-06-12 11:06:15 +02:00
2023-08-29 12:00:52 +02:00
def test_checkers_report ( ) :
with tempfile . TemporaryDirectory ( ) as tempdir :
filename = os . path . join ( tempdir , ' 1.txt ' )
args = f ' --checkers-report= { filename } helloworld '
cppcheck ( args . split ( ) )
with open ( filename , ' rt ' ) as f :
data = f . read ( )
assert ' No CheckAutoVariables::assignFunctionArg ' in data
assert ' Yes CheckAutoVariables::autoVariables ' in data
args = ' --enable=style ' + args
cppcheck ( args . split ( ) )
with open ( filename , ' rt ' ) as f :
data = f . read ( )
# checker has been activated by --enable=style
assert ' Yes CheckAutoVariables::assignFunctionArg ' in data
2022-09-06 23:11:39 +02:00
def __test_missing_include_system ( use_j ) :
2023-03-04 09:02:35 +01:00
args = [ ' --enable=missingInclude ' , ' --suppress=zerodiv ' , ' --template= {file} : {line} : {column} : {severity} : { inconclusive:inconclusive:} {message} [ {id} ] ' , ' helloworld ' ]
2022-09-06 23:11:39 +02:00
if use_j :
2023-03-04 09:02:35 +01:00
args . insert ( 0 , ' -j2 ' )
2022-06-12 11:06:15 +02:00
2023-03-04 09:02:35 +01:00
_ , _ , stderr = cppcheck ( args )
assert stderr . replace ( ' \\ ' , ' / ' ) == ' helloworld/main.c:1:0: information: Include file: <stdio.h> not found. Please note: Cppcheck does not need standard library headers to get proper results. [missingIncludeSystem] \n '
2022-06-12 11:06:15 +02:00
2022-09-06 23:11:39 +02:00
def test_missing_include_system ( ) :
__test_missing_include_system ( False )
2022-06-12 11:06:15 +02:00
2022-09-06 23:11:39 +02:00
def test_missing_include_system_j ( ) : #11283
__test_missing_include_system ( True )