2019-04-14 10:45:20 +02:00
|
|
|
|
|
|
|
# python -m pytest test-helloworld.py
|
|
|
|
|
|
|
|
import logging
|
|
|
|
import os
|
2019-04-14 16:48:59 +02:00
|
|
|
import re
|
2019-04-14 10:45:20 +02:00
|
|
|
import subprocess
|
|
|
|
|
|
|
|
# Run Cppcheck with args
|
|
|
|
def cppcheck(args):
|
2019-04-14 15:53:32 +02:00
|
|
|
if os.path.isfile('../../bin/debug/cppcheck.exe'):
|
|
|
|
cmd = '../../bin/debug/cppcheck.exe ' + args
|
|
|
|
elif os.path.isfile('../../../bin/debug/cppcheck.exe'):
|
|
|
|
cmd = '../../../bin/debug/cppcheck.exe ' + args
|
|
|
|
elif os.path.isfile('../../cppcheck'):
|
|
|
|
cmd = '../../cppcheck ' + args
|
|
|
|
else:
|
|
|
|
cmd = '../../../cppcheck ' + args
|
2019-04-14 10:45:20 +02:00
|
|
|
logging.info(cmd)
|
|
|
|
p = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
|
comm = p.communicate()
|
2019-04-14 15:53:32 +02:00
|
|
|
stdout = comm[0].decode(encoding='utf-8', errors='ignore').replace('\r\n', '\n')
|
|
|
|
stderr = comm[1].decode(encoding='utf-8', errors='ignore').replace('\r\n', '\n')
|
2019-04-14 10:45:20 +02:00
|
|
|
return p.returncode, stdout, stderr
|
|
|
|
|
2019-04-15 10:01:27 +02:00
|
|
|
# Run Cppcheck from project path
|
|
|
|
def cppcheck_local(args):
|
|
|
|
cwd = os.getcwd()
|
|
|
|
os.chdir('1-helloworld')
|
|
|
|
ret, stdout, stderr = cppcheck(args)
|
|
|
|
os.chdir(cwd)
|
|
|
|
return (ret, stdout, stderr)
|
|
|
|
|
|
|
|
def getRelativeProjectPath():
|
|
|
|
return '1-helloworld'
|
|
|
|
|
|
|
|
def getAbsoluteProjectPath():
|
|
|
|
return os.path.join(os.getcwd(), '1-helloworld')
|
|
|
|
|
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'):
|
|
|
|
if not line.startswith('Checking %s ' % (filename)):
|
|
|
|
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():
|
|
|
|
ret, stdout, stderr = cppcheck('1-helloworld')
|
2019-04-14 15:53:32 +02:00
|
|
|
filename = os.path.join('1-helloworld', 'main.c')
|
2019-04-14 10:45:20 +02:00
|
|
|
assert ret == 0
|
2019-04-14 15:53:32 +02:00
|
|
|
assert stdout == 'Checking %s ...\n' % (filename)
|
|
|
|
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-04-15 10:01:27 +02:00
|
|
|
ret, stdout, stderr = cppcheck_local('.')
|
2019-04-14 10:45:20 +02:00
|
|
|
assert ret == 0
|
|
|
|
assert stdout == 'Checking main.c ...\n'
|
|
|
|
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-04-14 10:45:20 +02:00
|
|
|
ret, stdout, stderr = cppcheck(prjpath)
|
2019-04-14 15:53:32 +02:00
|
|
|
filename = os.path.join(prjpath, 'main.c')
|
2019-04-14 10:45:20 +02:00
|
|
|
assert ret == 0
|
2019-04-14 15:53:32 +02:00
|
|
|
assert stdout == 'Checking %s ...\n' % (filename)
|
|
|
|
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():
|
2019-04-15 10:01:27 +02:00
|
|
|
ret, stdout, stderr = cppcheck_local('--addon=misra .')
|
2019-04-14 15:00:03 +02:00
|
|
|
assert ret == 0
|
|
|
|
assert stdout == 'Checking main.c ...\n'
|
|
|
|
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')
|
|
|
|
|
|
|
|
def test_addon_absolute_path():
|
2019-04-15 10:01:27 +02:00
|
|
|
prjpath = getAbsoluteProjectPath()
|
2019-04-14 15:00:03 +02:00
|
|
|
ret, stdout, stderr = cppcheck('--addon=misra %s' % (prjpath))
|
2019-04-14 15:53:32 +02:00
|
|
|
filename = os.path.join(prjpath, 'main.c')
|
2019-04-14 15:00:03 +02:00
|
|
|
assert ret == 0
|
2019-04-14 15:53:32 +02:00
|
|
|
assert stdout == 'Checking %s ...\n' % (filename)
|
|
|
|
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()
|
2019-04-14 15:00:03 +02:00
|
|
|
ret, stdout, stderr = cppcheck('--addon=misra %s' % (prjpath))
|
2019-04-14 15:53:32 +02:00
|
|
|
filename = os.path.join(prjpath, 'main.c')
|
2019-04-14 15:00:03 +02:00
|
|
|
assert ret == 0
|
2019-04-14 15:53:32 +02:00
|
|
|
assert stdout == 'Checking %s ...\n' % (filename)
|
|
|
|
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
|
|
|
|
2019-04-14 10:45:20 +02:00
|
|
|
def test_basepath_relative_path():
|
2019-04-15 10:01:27 +02:00
|
|
|
prjpath = getRelativeProjectPath()
|
2019-04-14 10:45:20 +02:00
|
|
|
ret, stdout, stderr = cppcheck('%s -rp=%s' % (prjpath, prjpath))
|
2019-04-14 15:53:32 +02:00
|
|
|
filename = os.path.join(prjpath, 'main.c')
|
2019-04-14 10:45:20 +02:00
|
|
|
assert ret == 0
|
2019-04-14 15:53:32 +02:00
|
|
|
assert stdout == 'Checking %s ...\n' % (filename)
|
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-04-14 10:45:20 +02:00
|
|
|
ret, stdout, stderr = cppcheck('%s -rp=%s' % (prjpath, prjpath))
|
2019-04-14 15:53:32 +02:00
|
|
|
filename = os.path.join(prjpath, 'main.c')
|
2019-04-14 10:45:20 +02:00
|
|
|
assert ret == 0
|
2019-04-14 15:53:32 +02:00
|
|
|
assert stdout == 'Checking %s ...\n' % (filename)
|
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-04-15 10:01:27 +02:00
|
|
|
ret, stdout, stderr = cppcheck_local('--project=helloworld.vcxproj')
|
2019-04-14 16:48:59 +02:00
|
|
|
assert ret == 0
|
|
|
|
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-04-14 16:48:59 +02:00
|
|
|
ret, stdout, stderr = cppcheck('--project=%s' % (os.path.join(prjpath, 'helloworld.vcxproj')))
|
|
|
|
filename = os.path.join(prjpath, 'main.c')
|
|
|
|
assert ret == 0
|
|
|
|
assert getVsConfigs(stdout, filename) == 'Debug|Win32 Debug|x64 Release|Win32 Release|x64'
|
|
|
|
assert stderr == '[%s:5]: (error) Division by zero.\n' % (filename)
|
|
|
|
|
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-04-14 18:09:35 +02:00
|
|
|
ret, stdout, stderr = cppcheck('--project=%s' % (os.path.join(prjpath, 'helloworld.vcxproj')))
|
|
|
|
filename = os.path.join(prjpath, 'main.c')
|
|
|
|
assert ret == 0
|
|
|
|
assert getVsConfigs(stdout, filename) == 'Debug|Win32 Debug|x64 Release|Win32 Release|x64'
|
|
|
|
assert stderr == '[%s:5]: (error) Division by zero.\n' % (filename)
|
|
|
|
|
2019-04-14 18:21:27 +02:00
|
|
|
def test_cppcheck_project_local_path():
|
2019-04-15 10:01:27 +02:00
|
|
|
ret, stdout, stderr = cppcheck_local('--platform=win64 --project=helloworld.cppcheck')
|
2019-04-14 18:21:27 +02:00
|
|
|
assert ret == 0
|
|
|
|
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-04-14 18:21:27 +02:00
|
|
|
ret, stdout, stderr = cppcheck('--platform=win64 --project=%s' % (os.path.join(prjpath, 'helloworld.cppcheck')))
|
|
|
|
filename = os.path.join(prjpath, 'main.c')
|
|
|
|
assert ret == 0
|
|
|
|
assert getVsConfigs(stdout, filename) == 'Debug|x64'
|
|
|
|
assert stderr == '[%s:5]: (error) Division by zero.\n' % (filename)
|
|
|
|
|
|
|
|
def test_cppcheck_project_absolute_path():
|
2019-04-15 10:01:27 +02:00
|
|
|
prjpath = getAbsoluteProjectPath()
|
2019-04-14 18:21:27 +02:00
|
|
|
ret, stdout, stderr = cppcheck('--platform=win64 --project=%s' % (os.path.join(prjpath, 'helloworld.cppcheck')))
|
|
|
|
filename = os.path.join(prjpath, 'main.c')
|
|
|
|
assert ret == 0
|
|
|
|
assert getVsConfigs(stdout, filename) == 'Debug|x64'
|
|
|
|
assert stderr == '[%s:5]: (error) Division by zero.\n' % (filename)
|
2019-04-14 18:09:35 +02:00
|
|
|
|