test/cli: refactorings

This commit is contained in:
Daniel Marjamäki 2019-04-15 10:01:27 +02:00
parent 1a8a40ea35
commit 65d18b3da9
3 changed files with 46 additions and 37 deletions

View File

@ -244,7 +244,7 @@ script:
- cd ../../
# Testing cli
- cd test/cli
- python -m pytest test-helloworld.py
- python -m pytest test-*.py
- cd ../..
notifications:

View File

@ -23,6 +23,20 @@ def cppcheck(args):
stderr = comm[1].decode(encoding='utf-8', errors='ignore').replace('\r\n', '\n')
return p.returncode, stdout, stderr
# 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')
# Get Visual Studio configurations checking a file
# Checking {file} {config}...
def getVsConfigs(stdout, filename):
@ -48,16 +62,13 @@ def test_relative_path():
def test_local_path():
cwd = os.getcwd()
os.chdir('1-helloworld')
ret, stdout, stderr = cppcheck('.')
os.chdir(cwd)
ret, stdout, stderr = cppcheck_local('.')
assert ret == 0
assert stdout == 'Checking main.c ...\n'
assert stderr == '[main.c:5]: (error) Division by zero.\n'
def test_absolute_path():
prjpath = os.path.join(os.getcwd(), '1-helloworld')
prjpath = getAbsoluteProjectPath()
ret, stdout, stderr = cppcheck(prjpath)
filename = os.path.join(prjpath, 'main.c')
assert ret == 0
@ -65,17 +76,14 @@ def test_absolute_path():
assert stderr == '[%s:5]: (error) Division by zero.\n' % (filename)
def test_addon_local_path():
cwd = os.getcwd()
os.chdir('1-helloworld')
ret, stdout, stderr = cppcheck('--addon=misra .')
os.chdir(cwd)
ret, stdout, stderr = cppcheck_local('--addon=misra .')
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():
prjpath = os.path.join(os.getcwd(), '1-helloworld')
prjpath = getAbsoluteProjectPath()
ret, stdout, stderr = cppcheck('--addon=misra %s' % (prjpath))
filename = os.path.join(prjpath, 'main.c')
assert ret == 0
@ -84,7 +92,7 @@ def test_addon_absolute_path():
'[%s:1]: (style) misra violation (use --rule-texts=<file> to get proper output)\n' % (filename, filename))
def test_addon_relative_path():
prjpath = '1-helloworld'
prjpath = getRelativeProjectPath()
ret, stdout, stderr = cppcheck('--addon=misra %s' % (prjpath))
filename = os.path.join(prjpath, 'main.c')
assert ret == 0
@ -93,7 +101,7 @@ def test_addon_relative_path():
'[%s:1]: (style) misra violation (use --rule-texts=<file> to get proper output)\n' % (filename, filename))
def test_basepath_relative_path():
prjpath = '1-helloworld'
prjpath = getRelativeProjectPath()
ret, stdout, stderr = cppcheck('%s -rp=%s' % (prjpath, prjpath))
filename = os.path.join(prjpath, 'main.c')
assert ret == 0
@ -101,7 +109,7 @@ def test_basepath_relative_path():
assert stderr == '[main.c:5]: (error) Division by zero.\n'
def test_basepath_absolute_path():
prjpath = os.path.join(os.getcwd(), '1-helloworld')
prjpath = getAbsoluteProjectPath()
ret, stdout, stderr = cppcheck('%s -rp=%s' % (prjpath, prjpath))
filename = os.path.join(prjpath, 'main.c')
assert ret == 0
@ -109,16 +117,13 @@ def test_basepath_absolute_path():
assert stderr == '[main.c:5]: (error) Division by zero.\n'
def test_vs_project_local_path():
cwd = os.getcwd()
os.chdir('1-helloworld')
ret, stdout, stderr = cppcheck('--project=helloworld.vcxproj')
os.chdir(cwd)
ret, stdout, stderr = cppcheck_local('--project=helloworld.vcxproj')
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'
def test_vs_project_relative_path():
prjpath = '1-helloworld'
prjpath = getRelativeProjectPath()
ret, stdout, stderr = cppcheck('--project=%s' % (os.path.join(prjpath, 'helloworld.vcxproj')))
filename = os.path.join(prjpath, 'main.c')
assert ret == 0
@ -126,7 +131,7 @@ def test_vs_project_relative_path():
assert stderr == '[%s:5]: (error) Division by zero.\n' % (filename)
def test_vs_project_absolute_path():
prjpath = os.path.join(os.getcwd(), '1-helloworld')
prjpath = getAbsoluteProjectPath()
ret, stdout, stderr = cppcheck('--project=%s' % (os.path.join(prjpath, 'helloworld.vcxproj')))
filename = os.path.join(prjpath, 'main.c')
assert ret == 0
@ -134,16 +139,13 @@ def test_vs_project_absolute_path():
assert stderr == '[%s:5]: (error) Division by zero.\n' % (filename)
def test_cppcheck_project_local_path():
cwd = os.getcwd()
os.chdir('1-helloworld')
ret, stdout, stderr = cppcheck('--platform=win64 --project=helloworld.cppcheck')
os.chdir(cwd)
ret, stdout, stderr = cppcheck_local('--platform=win64 --project=helloworld.cppcheck')
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():
prjpath = '1-helloworld'
prjpath = getRelativeProjectPath()
ret, stdout, stderr = cppcheck('--platform=win64 --project=%s' % (os.path.join(prjpath, 'helloworld.cppcheck')))
filename = os.path.join(prjpath, 'main.c')
assert ret == 0
@ -151,11 +153,10 @@ def test_cppcheck_project_relative_path():
assert stderr == '[%s:5]: (error) Division by zero.\n' % (filename)
def test_cppcheck_project_absolute_path():
prjpath = os.path.join(os.getcwd(), '1-helloworld')
prjpath = getAbsoluteProjectPath()
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)

View File

@ -8,9 +8,10 @@ import subprocess
COMPILE_COMMANDS_JSON = os.path.join('proj2', 'compile_commands.json')
def create_compile_commands(path):
j = [{'directory': path+'/a', 'command': 'gcc -c a.c', 'file': 'a.c'},
{'directory': path+'/b', 'command': 'gcc -c b.c', 'file': 'b.c'}]
def create_compile_commands():
prjpath = os.path.join(os.getcwd(), 'proj2')
j = [{'directory': os.path.join(prjpath,'a'), 'command': 'gcc -c a.c', 'file': 'a.c'},
{'directory': os.path.join(prjpath,'b'), 'command': 'gcc -c b.c', 'file': 'b.c'}]
f = open(COMPILE_COMMANDS_JSON, 'wt')
f.write(json.dumps(j))
@ -31,12 +32,19 @@ def cppcheck(args):
stderr = comm[1].decode(encoding='utf-8', errors='ignore').replace('\r\n', '\n')
return p.returncode, stdout, stderr
def test_local_path():
create_compile_commands(os.path.join(os.getcwd(), 'proj2'))
# Run Cppcheck from project path
def cppcheck_local(args):
cwd = os.getcwd()
os.chdir('proj2')
ret, stdout, stderr = cppcheck('--project=compile_commands.json')
ret, stdout, stderr = cppcheck(args)
os.chdir(cwd)
return (ret, stdout, stderr)
def test_local_path():
create_compile_commands()
ret, stdout, stderr = cppcheck_local('--project=compile_commands.json')
cwd = os.getcwd()
file1 = os.path.join(cwd, 'proj2', 'a', 'a.c')
file2 = os.path.join(cwd, 'proj2', 'b', 'b.c')
assert ret == 0
@ -44,7 +52,7 @@ def test_local_path():
assert stdout.find('Checking %s ...' % (file2)) >= 0
def test_relative_path():
create_compile_commands(os.path.join(os.getcwd(), 'proj2'))
create_compile_commands()
ret, stdout, stderr = cppcheck('--project=' + COMPILE_COMMANDS_JSON)
cwd = os.getcwd()
file1 = os.path.join(cwd, 'proj2', 'a', 'a.c')
@ -54,7 +62,7 @@ def test_relative_path():
assert stdout.find('Checking %s ...' % (file2)) >= 0
def test_absolute_path():
create_compile_commands(os.path.join(os.getcwd(), 'proj2'))
create_compile_commands()
cwd = os.getcwd()
ret, stdout, stderr = cppcheck('--project=' + os.path.join(cwd,COMPILE_COMMANDS_JSON))
file1 = os.path.join(cwd, 'proj2', 'a', 'a.c')
@ -64,7 +72,7 @@ def test_absolute_path():
assert stdout.find('Checking %s ...' % (file2)) >= 0
def test_gui_project_loads_compile_commands_1():
create_compile_commands(os.path.join(os.getcwd(), 'proj2'))
create_compile_commands()
ret, stdout, stderr = cppcheck('--project=proj2/proj2.cppcheck')
cwd = os.getcwd()
file1 = os.path.join(cwd, 'proj2', 'a', 'a.c')
@ -74,7 +82,7 @@ def test_gui_project_loads_compile_commands_1():
assert stdout.find('Checking %s ...' % (file2)) >= 0
def test_gui_project_loads_compile_commands_2():
create_compile_commands(os.path.join(os.getcwd(), 'proj2'))
create_compile_commands()
# create cppcheck gui project file
cppcheck_xml = ('<?xml version="1.0" encoding="UTF-8"?>'