cppcheck/test/cli/test-proj2.py

156 lines
6.4 KiB
Python
Raw Normal View History

2019-04-14 21:02:53 +02:00
2019-04-30 08:54:41 +02:00
# python -m pytest test-proj2.py
2019-04-14 21:02:53 +02:00
import json
import os
2019-04-15 16:57:16 +02:00
from testutils import create_gui_project_file, cppcheck
2019-04-14 21:02:53 +02:00
2021-01-30 10:42:48 +01:00
COMPILE_COMMANDS_JSON = 'compile_commands.json'
2019-04-14 21:02:53 +02:00
2021-01-30 10:42:48 +01:00
ERR_A = ('%s:1:7: error: Division by zero. [zerodiv]\n' +
2019-08-18 12:51:32 +02:00
'x = 3 / 0;\n' +
2021-01-30 10:42:48 +01:00
' ^\n') % os.path.join('a', 'a.c')
ERR_B = ('%s:1:7: error: Division by zero. [zerodiv]\n' +
2019-08-18 12:51:32 +02:00
'x = 3 / 0;\n' +
2021-01-30 10:42:48 +01:00
' ^\n') % os.path.join('b', 'b.c')
def realpath(s):
return os.path.realpath(s).replace('\\', '/')
2019-08-18 12:51:32 +02:00
2019-04-15 10:01:27 +02:00
def create_compile_commands():
2021-01-30 10:42:48 +01:00
j = [{'directory': realpath('proj2/a'), 'command': 'gcc -c a.c', 'file': 'a.c'},
{'directory': realpath('proj2'), 'command': 'gcc -c b/b.c', 'file': 'b/b.c'}]
2021-06-12 12:45:31 +02:00
with open('proj2/' + COMPILE_COMMANDS_JSON, 'wt') as f:
f.write(json.dumps(j))
2019-04-14 21:02:53 +02:00
2019-04-15 10:01:27 +02:00
# Run Cppcheck from project path
def cppcheck_local(args):
2019-04-14 21:02:53 +02:00
cwd = os.getcwd()
os.chdir('proj2')
2019-04-15 10:01:27 +02:00
ret, stdout, stderr = cppcheck(args)
2019-04-14 21:02:53 +02:00
os.chdir(cwd)
return ret, stdout, stderr
2019-04-15 10:01:27 +02:00
def test_file_filter():
ret, stdout, stderr = cppcheck(['proj2/','--file-filter=proj2/a/*'])
file1 = os.path.join('proj2', 'a', 'a.c')
file2 = os.path.join('proj2', 'b', 'b.c')
assert ret == 0
2021-01-30 10:42:48 +01:00
assert stdout.find('Checking %s ...' % file1) >= 0
ret, stdout, stderr = cppcheck(['proj2/','--file-filter=proj2/b*'])
assert ret == 0, stdout
2021-01-30 10:42:48 +01:00
assert stdout.find('Checking %s ...' % file2) >= 0
2019-04-15 10:01:27 +02:00
def test_local_path():
create_compile_commands()
2019-06-22 19:20:15 +02:00
ret, stdout, stderr = cppcheck_local(['--project=compile_commands.json'])
2021-06-12 12:45:31 +02:00
file1 = os.path.join('a', 'a.c')
file2 = os.path.join('b', 'b.c')
assert ret == 0, stdout
2021-01-30 10:42:48 +01:00
assert stdout.find('Checking %s ...' % file1) >= 0
assert stdout.find('Checking %s ...' % file2) >= 0
2019-04-14 21:02:53 +02:00
def test_local_path_force():
create_compile_commands()
ret, stdout, stderr = cppcheck_local(['--project=compile_commands.json', '--force'])
assert ret == 0, stdout
assert stdout.find('AAA') >= 0
def test_local_path_maxconfigs():
create_compile_commands()
ret, stdout, stderr = cppcheck_local(['--project=compile_commands.json', '--max-configs=2'])
assert ret == 0, stdout
assert stdout.find('AAA') >= 0
2019-04-14 21:02:53 +02:00
def test_relative_path():
2019-04-15 10:01:27 +02:00
create_compile_commands()
2021-01-30 10:42:48 +01:00
ret, stdout, stderr = cppcheck(['--project=proj2/' + COMPILE_COMMANDS_JSON])
2021-06-12 12:45:31 +02:00
file1 = os.path.join('proj2', 'a', 'a.c')
file2 = os.path.join('proj2', 'b', 'b.c')
assert ret == 0, stdout
2021-01-30 10:42:48 +01:00
assert stdout.find('Checking %s ...' % file1) >= 0
assert stdout.find('Checking %s ...' % file2) >= 0
2019-04-14 21:02:53 +02:00
def test_absolute_path():
2019-04-15 10:01:27 +02:00
create_compile_commands()
2021-01-30 10:42:48 +01:00
ret, stdout, stderr = cppcheck(['--project=' + os.path.realpath('proj2/' + COMPILE_COMMANDS_JSON)])
file1 = os.path.realpath('proj2/a/a.c')
file2 = os.path.realpath('proj2/b/b.c')
assert ret == 0, stdout
2021-01-30 10:42:48 +01:00
assert stdout.find('Checking %s ...' % file1) >= 0
assert stdout.find('Checking %s ...' % file2) >= 0
2019-04-14 21:02:53 +02:00
2019-04-15 08:53:23 +02:00
def test_gui_project_loads_compile_commands_1():
2019-04-15 10:01:27 +02:00
create_compile_commands()
2019-06-22 19:20:15 +02:00
ret, stdout, stderr = cppcheck(['--project=proj2/proj2.cppcheck'])
2021-06-12 12:45:31 +02:00
file1 = os.path.join('proj2', 'a', 'a.c')
file2 = os.path.join('proj2', 'b', 'b.c')
assert ret == 0, stdout
2021-01-30 10:42:48 +01:00
assert stdout.find('Checking %s ...' % file1) >= 0
assert stdout.find('Checking %s ...' % file2) >= 0
2019-04-14 21:02:53 +02:00
2019-04-15 08:53:23 +02:00
def test_gui_project_loads_compile_commands_2():
2019-04-15 10:01:27 +02:00
create_compile_commands()
exclude_path_1 = 'proj2/b'
create_gui_project_file('proj2/test.cppcheck',
import_project='compile_commands.json',
exclude_paths=[exclude_path_1])
2019-06-22 19:20:15 +02:00
ret, stdout, stderr = cppcheck(['--project=proj2/test.cppcheck'])
2021-06-12 12:45:31 +02:00
file1 = os.path.join('proj2', 'a', 'a.c')
file2 = os.path.join('proj2', 'b', 'b.c') # Excluded by test.cppcheck
assert ret == 0, stdout
2021-01-30 10:42:48 +01:00
assert stdout.find('Checking %s ...' % file1) >= 0
assert stdout.find('Checking %s ...' % file2) < 0
def test_gui_project_loads_relative_vs_solution():
create_gui_project_file('test.cppcheck', import_project='proj2/proj2.sln')
2019-06-22 19:20:15 +02:00
ret, stdout, stderr = cppcheck(['--project=test.cppcheck'])
file1 = os.path.join('proj2', 'a', 'a.c')
file2 = os.path.join('proj2', 'b', 'b.c')
assert ret == 0, stdout
2021-01-30 10:42:48 +01:00
assert stdout.find('Checking %s Debug|Win32...' % file1) >= 0
assert stdout.find('Checking %s Debug|x64...' % file1) >= 0
assert stdout.find('Checking %s Release|Win32...' % file1) >= 0
assert stdout.find('Checking %s Release|x64...' % file1) >= 0
assert stdout.find('Checking %s Debug|Win32...' % file2) >= 0
assert stdout.find('Checking %s Debug|x64...' % file2) >= 0
assert stdout.find('Checking %s Release|Win32...' % file2) >= 0
assert stdout.find('Checking %s Release|x64...' % file2) >= 0
def test_gui_project_loads_absolute_vs_solution():
2021-01-30 10:42:48 +01:00
create_gui_project_file('test.cppcheck', import_project=realpath('proj2/proj2.sln'))
2019-06-22 19:20:15 +02:00
ret, stdout, stderr = cppcheck(['--project=test.cppcheck'])
2021-01-30 10:42:48 +01:00
file1 = os.path.realpath('proj2/a/a.c')
file2 = os.path.realpath('proj2/b/b.c')
print(stdout)
assert ret == 0, stdout
2021-01-30 10:42:48 +01:00
assert stdout.find('Checking %s Debug|Win32...' % file1) >= 0
assert stdout.find('Checking %s Debug|x64...' % file1) >= 0
assert stdout.find('Checking %s Release|Win32...' % file1) >= 0
assert stdout.find('Checking %s Release|x64...' % file1) >= 0
assert stdout.find('Checking %s Debug|Win32...' % file2) >= 0
assert stdout.find('Checking %s Debug|x64...' % file2) >= 0
assert stdout.find('Checking %s Release|Win32...' % file2) >= 0
assert stdout.find('Checking %s Release|x64...' % file2) >= 0
def test_gui_project_loads_relative_vs_solution_2():
create_gui_project_file('test.cppcheck', root_path='proj2', import_project='proj2/proj2.sln')
2019-06-22 19:20:15 +02:00
ret, stdout, stderr = cppcheck(['--project=test.cppcheck'])
assert ret == 0, stdout
2019-08-18 12:51:32 +02:00
assert stderr == ERR_A + ERR_B
2019-04-14 21:02:53 +02:00
def test_gui_project_loads_relative_vs_solution_with_exclude():
create_gui_project_file('test.cppcheck', root_path='proj2', import_project='proj2/proj2.sln', exclude_paths=['b'])
2019-06-22 19:20:15 +02:00
ret, stdout, stderr = cppcheck(['--project=test.cppcheck'])
assert ret == 0, stdout
2019-08-18 12:51:32 +02:00
assert stderr == ERR_A
def test_gui_project_loads_absolute_vs_solution_2():
create_gui_project_file('test.cppcheck',
2021-01-30 10:42:48 +01:00
root_path=realpath('proj2'),
import_project=realpath('proj2/proj2.sln'))
2019-06-22 19:20:15 +02:00
ret, stdout, stderr = cppcheck(['--project=test.cppcheck'])
assert ret == 0, stdout
2019-08-18 12:51:32 +02:00
assert stderr == ERR_A + ERR_B