From a03455c505ea87d43ac7b72101133f7a37f1643f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 15 Apr 2019 15:03:06 +0200 Subject: [PATCH] test/cli: test function that creates gui project file --- test/cli/test-helloworld.py | 47 +++++++++++++++++++------- test/cli/test-proj2.py | 66 +++++++++++++++++++++++++++++-------- 2 files changed, 87 insertions(+), 26 deletions(-) diff --git a/test/cli/test-helloworld.py b/test/cli/test-helloworld.py index a9981caa5..0a8b74c23 100644 --- a/test/cli/test-helloworld.py +++ b/test/cli/test-helloworld.py @@ -52,6 +52,38 @@ def getVsConfigs(stdout, filename): ret.sort() return ' '.join(ret) +# Create Cppcheck project file +def create_gui_project_file(project_file, root_path=None, import_project=None, paths=None, exclude_paths=None, suppressions=None): + cppcheck_xml = ('\n' + '\n') + if root_path: + cppcheck_xml += ' \n' + if import_project: + cppcheck_xml += ' ' + import_project + '\n' + if paths: + cppcheck_xml += ' \n' + for path in paths: + cppcheck_xml += ' \n' + cppcheck_xml += ' \n' + if exclude_paths: + cppcheck_xml += ' \n' + for path in exclude_paths: + cppcheck_xml += ' \n' + cppcheck_xml += ' \n' + if suppressions: + cppcheck_xml += ' \n' + for suppression in suppressions: + cppcheck_xml += ' \n' + cppcheck_xml += ' \n' + cppcheck_xml += '\n' + + f = open(project_file, 'wt') + f.write(cppcheck_xml) + f.close() + def test_relative_path(): ret, stdout, stderr = cppcheck('1-helloworld') @@ -172,19 +204,10 @@ def test_suppress_command_line(): assert stderr == '' def test_suppress_project(): - cppcheck_xml = ('' - '' - ' ' - ' ' - ' ' - ' ' - ' zerodiv' - ' ' - '') project_file = os.path.join('1-helloworld', 'test.cppcheck') - f = open(project_file, 'wt') - f.write(cppcheck_xml) - f.close() + create_gui_project_file(project_file, + paths=['.'], + suppressions=[{'fileName':'main.c', 'id':'zerodiv'}]) # Relative path ret, stdout, stderr = cppcheck('--project=%s' % (project_file)) diff --git a/test/cli/test-proj2.py b/test/cli/test-proj2.py index 1cdf80658..7dab0a36d 100644 --- a/test/cli/test-proj2.py +++ b/test/cli/test-proj2.py @@ -15,6 +15,40 @@ def create_compile_commands(): f = open(COMPILE_COMMANDS_JSON, 'wt') f.write(json.dumps(j)) + +# Create Cppcheck project file +def create_gui_project_file(project_file, root_path=None, import_project=None, paths=None, exclude_paths=None, suppressions=None): + cppcheck_xml = ('\n' + '\n') + if root_path: + cppcheck_xml += ' \n' + if import_project: + cppcheck_xml += ' ' + import_project + '\n' + if paths: + cppcheck_xml += ' \n' + for path in paths: + cppcheck_xml += ' \n' + cppcheck_xml += ' \n' + if exclude_paths: + cppcheck_xml += ' \n' + for path in exclude_paths: + cppcheck_xml += ' \n' + cppcheck_xml += ' \n' + if suppressions: + cppcheck_xml += ' \n' + for suppression in suppressions: + cppcheck_xml += ' \n' + cppcheck_xml += ' \n' + cppcheck_xml += '\n' + + f = open(project_file, 'wt') + f.write(cppcheck_xml) + f.close() + + # Run Cppcheck with args def cppcheck(args): if os.path.isfile('../../bin/debug/cppcheck.exe'): @@ -83,20 +117,24 @@ def test_gui_project_loads_compile_commands_1(): def test_gui_project_loads_compile_commands_2(): create_compile_commands() - - # create cppcheck gui project file - cppcheck_xml = ('' - '' - ' ' - ' compile_commands.json' - ' ' - ' ' - ' ' - '') - f = open('proj2/test.cppcheck', 'wt') - f.write(cppcheck_xml) - f.close() - + exclude_path_1 = os.path.join(os.getcwd(), 'proj2', 'b').replace('\\', '/') + create_gui_project_file('proj2/test.cppcheck', + import_project='compile_commands.json', + exclude_paths=[exclude_path_1]) + ret, stdout, stderr = cppcheck('--project=proj2/test.cppcheck') + cwd = os.getcwd() + file1 = os.path.join(cwd, 'proj2', 'a', 'a.c') + file2 = os.path.join(cwd, 'proj2', 'b', 'b.c') # Excluded by test.cppcheck + assert ret == 0 + assert stdout.find('Checking %s ...' % (file1)) >= 0 + assert stdout.find('Checking %s ...' % (file2)) < 0 + +def test_gui_project_loads_compile_commands_2(): + create_compile_commands() + exclude_path_1 = os.path.join(os.getcwd(), 'proj2', 'b').replace('\\', '/') + create_gui_project_file('proj2/test.cppcheck', + import_project='compile_commands.json', + exclude_paths=[exclude_path_1]) ret, stdout, stderr = cppcheck('--project=proj2/test.cppcheck') cwd = os.getcwd() file1 = os.path.join(cwd, 'proj2', 'a', 'a.c')