Skip to content

Commit

Permalink
refs #12274 - made test/cli/inline-suppress_test.py location-indepe…
Browse files Browse the repository at this point in the history
…ndent (danmar#5876)
  • Loading branch information
firewave authored Jan 17, 2024
1 parent 3d8e2ec commit 9c1b643
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 47 deletions.
102 changes: 57 additions & 45 deletions test/cli/inline-suppress_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,98 +3,110 @@

import json
import os
import tempfile
from testutils import cppcheck

def create_unused_function_compile_commands():
prjpath = os.path.realpath('proj-inline-suppress-unusedFunction')
compile_commands = os.path.join(prjpath, 'compile_commands.json')
__script_dir = os.path.dirname(os.path.abspath(__file__))


def create_unused_function_compile_commands(tmpdir):
prjpath = os.path.realpath(os.path.join(__script_dir, 'proj-inline-suppress-unusedFunction'))
j = [{'directory': prjpath,
'command': '/usr/bin/c++ -I"' + prjpath + '" -o "' + os.path.join(prjpath, 'B.cpp.o') + '" -c "' + os.path.join(prjpath, 'B.cpp') + '"',
'file': os.path.join(prjpath, 'B.cpp')},
{'directory': prjpath,
'command': '/usr/bin/c++ -I"' + prjpath + '" -o "' + os.path.join(prjpath, 'A.cpp.o') + '" -c "' + os.path.join(prjpath, 'A.cpp') + '"',
'file': os.path.join(prjpath, 'A.cpp')}]
compdb_path = os.path.join(tmpdir, 'proj-inline-suppress-unusedFunction')
os.makedirs(compdb_path)
compile_commands = os.path.join(compdb_path, 'compile_commands.json')
with open(compile_commands, 'wt') as f:
f.write(json.dumps(j, indent=4))
return compile_commands


def test1():
ret, stdout, stderr = cppcheck(['--inline-suppr', 'proj-inline-suppress'])
assert ret == 0, stdout
ret, stdout, stderr = cppcheck(['--inline-suppr', 'proj-inline-suppress'], cwd=__script_dir)
assert stderr == ''
assert ret == 0, stdout


def test2():
ret, stdout, stderr = cppcheck(['proj-inline-suppress'])
ret, stdout, stderr = cppcheck(['proj-inline-suppress'], cwd=__script_dir)
assert len(stderr) > 0, stderr
assert ret == 0, stdout
assert len(stderr) > 0


def test_unmatched_suppression():
ret, stdout, stderr = cppcheck(['--inline-suppr', '--enable=information', '--error-exitcode=1', 'proj-inline-suppress/2.c'])
assert ret == 1
ret, stdout, stderr = cppcheck(['--inline-suppr', '--enable=information', '--error-exitcode=1', 'proj-inline-suppress/2.c'], cwd=__script_dir)
assert 'Unmatched suppression: some_warning_id' in stderr
assert ret == 1, stdout

def test_unmatched_suppression_path_with_extra_stuf():
ret, stdout, stderr = cppcheck(['--inline-suppr', '--enable=information', '--error-exitcode=1', './proj-inline-suppress/2.c'])
assert ret == 1

def test_unmatched_suppression_path_with_extra_stuff():
ret, stdout, stderr = cppcheck(['--inline-suppr', '--enable=information', '--error-exitcode=1', './proj-inline-suppress/2.c'], cwd=__script_dir)
assert 'Unmatched suppression: some_warning_id' in stderr
assert ret == 1, stdout


def test_backwards_compatibility():
ret, stdout, stderr = cppcheck(['proj-inline-suppress/3.cpp'])
ret, stdout, stderr = cppcheck(['proj-inline-suppress/3.cpp'], cwd=__script_dir)
assert '[zerodiv]' in stderr

ret, stdout, stderr = cppcheck(['--inline-suppr', 'proj-inline-suppress/3.cpp'])
assert ret == 0, stdout

ret, stdout, stderr = cppcheck(['--inline-suppr', 'proj-inline-suppress/3.cpp'], cwd=__script_dir)
assert stderr == ''
assert ret == 0, stdout


def test_compile_commands_unused_function():
create_unused_function_compile_commands()
ret, stdout, stderr = cppcheck(['--enable=all', '--error-exitcode=1', '--project=./proj-inline-suppress-unusedFunction/compile_commands.json'])
assert ret == 1
def test_compile_commands_unused_function(tmpdir):
compdb_file = create_unused_function_compile_commands(tmpdir)
ret, stdout, stderr = cppcheck(['--enable=all', '--error-exitcode=1', '--project={}'.format(compdb_file)])
assert 'unusedFunction' in stderr
assert ret == 1, stdout

def test_compile_commands_unused_function_suppression():
create_unused_function_compile_commands()
ret, stdout, stderr = cppcheck(['--enable=all', '--inline-suppr', '--error-exitcode=1', '--project=./proj-inline-suppress-unusedFunction/compile_commands.json'])
assert ret == 0, stdout

def test_compile_commands_unused_function_suppression(tmpdir):
compdb_file = create_unused_function_compile_commands(tmpdir)
ret, stdout, stderr = cppcheck(['--enable=all', '--inline-suppr', '--error-exitcode=1', '--project={}'.format(compdb_file)])
assert 'unusedFunction' not in stderr
assert ret == 0, stdout


def test_unmatched_suppression_ifdef():
ret, stdout, stderr = cppcheck(['--enable=all', '--suppress=missingIncludeSystem', '--inline-suppr', '-DNO_ZERO_DIV', 'trac5704/trac5704a.c'])
assert ret == 0, stdout
ret, stdout, stderr = cppcheck(['--enable=all', '--suppress=missingIncludeSystem', '--inline-suppr', '-DNO_ZERO_DIV', 'trac5704/trac5704a.c'], cwd=__script_dir)
assert 'unmatchedSuppression' not in stderr
assert ret == 0, stdout


def test_unmatched_suppression_ifdef_0():
ret, stdout, stderr = cppcheck(['--enable=all', '--suppress=missingIncludeSystem', '--inline-suppr', 'trac5704/trac5704b.c'])
assert ret == 0, stdout
ret, stdout, stderr = cppcheck(['--enable=all', '--suppress=missingIncludeSystem', '--inline-suppr', 'trac5704/trac5704b.c'], cwd=__script_dir)
assert 'unmatchedSuppression' not in stderr
assert ret == 0, stdout


def test_build_dir():
with tempfile.TemporaryDirectory() as tempdir:
args = f'--cppcheck-build-dir={tempdir} --enable=all --inline-suppr proj-inline-suppress/4.c'.split()
def test_build_dir(tmpdir):
args = f'--cppcheck-build-dir={tmpdir} --enable=all --inline-suppr proj-inline-suppress/4.c'.split()

ret, stdout, stderr = cppcheck(args)
assert ret == 0, stdout
assert len(stderr) == 0
ret, stdout, stderr = cppcheck(args, cwd=__script_dir)
assert len(stderr) == 0, stderr
assert ret == 0, stdout

ret, stdout, stderr = cppcheck(args, cwd=__script_dir)
assert len(stderr) == 0, stderr
assert ret == 0, stdout

ret, stdout, stderr = cppcheck(args)
assert ret == 0, stdout
assert len(stderr) == 0

def test_build_dir_unused_template():
with tempfile.TemporaryDirectory() as tempdir:
args = f'--cppcheck-build-dir={tempdir} --enable=all --inline-suppr proj-inline-suppress/template.cpp'.split()
def test_build_dir_unused_template(tmpdir):
args = f'--cppcheck-build-dir={tmpdir} --enable=all --inline-suppr proj-inline-suppress/template.cpp'.split()

ret, stdout, stderr = cppcheck(args)
assert ret == 0, stdout
assert len(stderr) == 0
ret, stdout, stderr = cppcheck(args, cwd=__script_dir)
assert len(stderr) == 0, stderr
assert ret == 0, stdout


def test_suppress_unmatched_inline_suppression(): # 11172
ret, stdout, stderr = cppcheck(['--enable=all', '--suppress=unmatchedSuppression', '--inline-suppr', 'proj-inline-suppress/2.c'])
assert ret == 0, stdout
ret, stdout, stderr = cppcheck(['--enable=all', '--suppress=unmatchedSuppression', '--inline-suppr', 'proj-inline-suppress/2.c'], cwd=__script_dir)
assert 'unmatchedSuppression' not in stderr
assert ret == 0, stdout


5 changes: 3 additions & 2 deletions test/cli/testutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,18 @@ def __lookup_cppcheck_exe():
break

if exe_path:
exe_path = os.path.abspath(exe_path)
print("using '{}'".format(exe_path))
return exe_path


# Run Cppcheck with args
def cppcheck(args, env=None, remove_checkers_report=True):
def cppcheck(args, env=None, remove_checkers_report=True, cwd=None):
exe = __lookup_cppcheck_exe()
assert exe is not None, 'no cppcheck binary found'

logging.info(exe + ' ' + ' '.join(args))
p = subprocess.Popen([exe] + args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env)
p = subprocess.Popen([exe] + args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env, cwd=cwd)
comm = p.communicate()
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')
Expand Down

0 comments on commit 9c1b643

Please sign in to comment.